/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_ProcessIsolation_h
#define mozilla_dom_ProcessIsolation_h

#include <stdint.h>

#include "mozilla/Logging.h"
#include "mozilla/dom/RemoteType.h"
#include "mozilla/dom/SessionHistoryEntry.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "nsIPrincipal.h"
#include "nsIURI.h"
#include "nsString.h"

namespace mozilla::dom {

class CanonicalBrowsingContext;
class LoadedOriginSet;
class WindowGlobalParent;

extern mozilla::LazyLogModule gProcessIsolationLog;

constexpr nsLiteralCString kHighValueCOOPPermission = "highValueCOOP"_ns;
constexpr nsLiteralCString kHighValueHasSavedLoginPermission =
    "highValueHasSavedLogin"_ns;
constexpr nsLiteralCString kHighValueIsLoggedInPermission =
    "highValueIsLoggedIn"_ns;

// NavigationIsolationOptions is passed through the methods to store the state
// of the possible process and/or browsing context change.
struct NavigationIsolationOptions {
  RemoteType mRemoteType;
  bool mReplaceBrowsingContext = false;
  uint64_t mSpecificGroupId = 0;
  bool mShouldCrossOriginIsolate = false;
  bool mTryUseBFCache = false;
  RefPtr<SessionHistoryEntry> mActiveSessionHistoryEntry;
};

/**
 * Given a specific channel, determines which process the navigation should
 * complete in, and whether or not to perform a BrowsingContext-replace load
 * or enter the BFCache.
 *
 * This method will always return a `NavigationIsolationOptions` even if the
 * current remote type is compatible. Compatibility with the current process
 * should be checked at the call-site. An error should only be returned in
 * exceptional circumstances, and should lead to the load being cancelled.
 *
 * This method is only intended for use with document navigations.
 */
Result<NavigationIsolationOptions, nsresult> IsolationOptionsForNavigation(
    CanonicalBrowsingContext* aTopBC, WindowGlobalParent* aParentWindow,
    nsIURI* aChannelCreationURI, nsIChannel* aChannel,
    const RemoteType& aCurrentRemoteType, bool aHasCOOPMismatch,
    bool aForNewTab, uint32_t aLoadStateLoadType,
    const Maybe<uint64_t>& aChannelId,
    const Maybe<RemoteType>& aRemoteTypeOverride);

// WorkerIsolationOptions is passed back to the RemoteWorkerManager to store the
// destination process information for remote worker loads.
struct WorkerIsolationOptions {
  RemoteType mRemoteType;
};

/**
 * Given a specific worker principal and kind, determines which process the
 * remote worker load should complete in.
 *
 * This method is only intended for use with remote workers.
 */
Result<WorkerIsolationOptions, nsresult> IsolationOptionsForWorker(
    nsIPrincipal* aPrincipal, WorkerKind aWorkerKind,
    const RemoteType& aCurrentRemoteType, bool aUseRemoteSubframes);

/**
 * Given a URI being loaded, and some relevant context, predict what remote type
 * the load will complete in. The preferred remote type will be used in cases
 * where the process to load in would otherwise be ambiguous, such as when
 * loading documents like `about:blank`.
 *
 * This should generally only be used for opening new tabs as requested by
 * frontend JS, and should not be used as part of navigation. The remote types
 * selected by this method are not used to enforce security invariants.
 */
Result<RemoteType, nsresult> PredictRemoteTypeForURI(
    nsIURI* aURI, const OriginAttributes& aOriginAttributes,
    const RemoteType& aPreferredRemoteType, bool aUseRemoteSubframes);

/**
 * If `aURI` is an `about:reader` URI whose "url" query parameter names a
 * document about:reader will actually load, return that URL, otherwise return
 * `nullptr`.
 *
 * Safe to call with any URI.
 */
already_AddRefed<nsIURI> GetAboutReaderURL(nsIURI* aURI);

/**
 * Adds a `highValue` permission to the permissions database, and make loads of
 * that origin isolated.
 *
 * The 'aPermissionType' parameter indicates why the site is treated as a high
 * value site. The possible values are:
 *
 * kHighValueCOOPPermission
 *     Called when a document request responds with a
 * `Cross-Origin-Opener-Policy` header.
 *
 * kHighValueHasSavedLoginPermission
 *     Called for sites that have an associated login saved in the password
 * manager.
 *
 * kHighValueIsLoggedInPermission
 *     Called when we detect a form with a password is submitted.
 */
void AddHighValuePermission(nsIPrincipal* aResultPrincipal,
                            const nsACString& aPermissionType);

void AddHighValuePermission(const nsACString& aOrigin,
                            const nsACString& aPermissionType);

/**
 * Returns true when fission is enabled and the
 * `fission.webContentIsolationStrategy` pref is set to `IsolateHighValue`.
 */
bool IsIsolateHighValueSiteEnabled();

/**
 * Options for ValidatePrincipal methods. By default:
 *
 * 1. The principal must not be `nullptr`.
 * 2. The principal must be a null or content principal
 * 3. The principal must be in the target process' LoadedOriginSet.
 *   - There are some exceptions to this (e.g. extension, null principals).
 *   - Principals are added to the set by `ContentParent::AboutToLoadOrigin`.
 * 4. The principal must be "generally loadable" by the target remote type.
 *   - See the implementation of ValidatePrincipalCouldPotentiallyBeLoadedBy.
 *
 * These options allow disabling parts of these checks.
 */
enum class ValidatePrincipalOptions {
  // Allow the principal to be `nullptr` (i.e. not present)
  AllowNullPtr,

  // Allow the principal to be an expanded principal.
  // Sub-principals will each be validated.
  AllowExpanded,

  // Allow the system principal if it is in the process' LoadedOriginSet.
  //
  // This should only occur in the inference process, or when a legacy system
  // principal chrome:// document has been forcibly loaded within this process.
  AllowSystemIfLoaded,

  // Don't check the LoadedOriginSet during this ValidatePrincipal call.
  // This is used for checks before `ContentParent::AboutToLoadOrigin`.
  AllowNotLoadedOrigin,

  // Allow the system principal unconditionally, ignoring the LoadedOriginSet.
  AlwaysAllowSystem,

  // Internal flag used while validating null principal precursors.
  Internal_ValidatingPrecursor,
};

/**
 * Perform a lax check that a process with the given RemoteType could
 * potentially load a Document or run script with the given principal.
 *
 * WARNING: This is intentionally a lax check, to avoid false positives in
 * assertions, and should NOT be used for process isolation decisions.
 */
bool ValidatePrincipalCouldPotentiallyBeLoadedBy(
    nsIPrincipal* aPrincipal, const RemoteType& aRemoteType,
    const EnumSet<ValidatePrincipalOptions>& aOptions,
    LoadedOriginSet* aLoadedOriginSet = nullptr);

}  // namespace mozilla::dom

#endif
