chromium/extensions/browser/extension_user_script_loader.cc

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/extension_user_script_loader.h"

#include <stddef.h>

#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/one_shot_event.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/component_extension_resource_manager.h"
#include "extensions/browser/content_verifier/content_verifier.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/l10n_file_util.h"
#include "extensions/browser/scripting_constants.h"
#include "extensions/browser/scripting_utils.h"
#include "extensions/browser/state_store.h"
#include "extensions/browser/user_script_loader.h"
#include "extensions/common/api/content_scripts.h"
#include "extensions/common/api/scripts_internal.h"
#include "extensions/common/api/scripts_internal/script_serialization.h"
#include "extensions/common/manifest_handlers/content_scripts_handler.h"
#include "extensions/common/manifest_handlers/default_locale_handler.h"
#include "extensions/common/message_bundle.h"
#include "extensions/common/permissions/api_permission.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/url_pattern_set.h"
#include "extensions/common/user_script.h"
#include "extensions/common/utils/content_script_utils.h"
#include "extensions/common/utils/extension_types_utils.h"
#include "ui/base/resource/resource_bundle.h"

BrowserContext;

namespace extensions {

namespace {

SubstitutionMap;

// Each map entry associates a UserScript::Content object with the ID of the
// resource holding the content of the script.
ScriptResourceIds;

// The source of script file from where it's read.
enum class ReadScriptContentSource {};

// The key for storing a dynamic content script's id.
inline constexpr char kId[] =;

// Reads and returns {content, source} of a |script_file|.
//   - content contains the std::string content, or nullopt if the script file
// couldn't be read.
std::tuple<std::optional<std::string>, ReadScriptContentSource>
ReadScriptContent(UserScript::Content* script_file,
                  const std::optional<int>& script_resource_id,
                  size_t& remaining_length) {}

// Verifies file contents as they are read.
void VerifyContent(ContentVerifier* verifier,
                   const ExtensionId& extension_id,
                   const base::FilePath& extension_root,
                   const base::FilePath& relative_path,
                   const std::optional<std::string>& content) {}

void RecordContentScriptLength(const std::string& script_content) {}

// Records the total size in kb of all manifest and dynamic scripts that were
// loaded in a single load.
void RecordTotalContentScriptLengthForLoad(size_t manifest_scripts_length,
                                           size_t dynamic_scripts_length) {}

// Loads user scripts from the extension who owns these scripts.
void LoadScriptContent(const mojom::HostID& host_id,
                       UserScript::Content* script_file,
                       const std::optional<int>& script_resource_id,
                       const SubstitutionMap* localization_messages,
                       const scoped_refptr<ContentVerifier>& verifier,
                       size_t& remaining_length) {}

void FillScriptFileResourceIds(const UserScript::ContentList& script_files,
                               ScriptResourceIds& script_resource_ids) {}

// Returns the total length of scripts that were previously loaded (i.e. not
// present in `added_script_ids`).
size_t GetTotalLoadedScriptsLength(
    UserScriptList* user_scripts,
    const std::set<std::string>& added_script_ids) {}

void LoadUserScripts(
    UserScriptList* user_scripts,
    ScriptResourceIds script_resource_ids,
    const ExtensionUserScriptLoader::PathAndLocaleInfo& host_info,
    const std::set<std::string>& added_script_ids,
    const scoped_refptr<ContentVerifier>& verifier) {}

void LoadScriptsOnFileTaskRunner(
    UserScriptList user_scripts,
    ScriptResourceIds script_resource_ids,
    const ExtensionUserScriptLoader::PathAndLocaleInfo& host_info,
    const std::set<std::string>& added_script_ids,
    const scoped_refptr<ContentVerifier>& verifier,
    UserScriptLoader::LoadScriptsCallback callback) {}

// Attempts to coerce a `dict` from an `api::content_scripts::ContentScript` to
// an `api::scripts_internal::SerializedUserScript`, returning std::nullopt on
// failure.
// TODO(crbug.com/40286091): Remove this when migration is complete.
std::optional<api::scripts_internal::SerializedUserScript>
ContentScriptDictToSerializedUserScript(const base::Value::Dict& dict) {}

// Converts the list of values in `list` to a UserScriptList.
UserScriptList ConvertValueToScripts(const Extension& extension,
                                     bool allowed_in_incognito,
                                     const base::Value::List& list) {}

// Gets an extension's manifest scripts' metadata; i.e., gets a list of
// UserScript objects that contains script info, but not the contents of the
// scripts.
UserScriptList GetManifestScriptsMetadata(
    content::BrowserContext* browser_context,
    const Extension& extension) {}

// Returns a copy of the dynamic `script` info, which includes the script
// content when its source is inline code.
std::unique_ptr<UserScript> CopyDynamicScriptInfo(const UserScript& script) {}

}  // namespace

ExtensionUserScriptLoader::ExtensionUserScriptLoader(
    BrowserContext* browser_context,
    const Extension& extension,
    StateStore* state_store,
    bool listen_for_extension_system_loaded)
    :{}

ExtensionUserScriptLoader::ExtensionUserScriptLoader(
    BrowserContext* browser_context,
    const Extension& extension,
    StateStore* state_store,
    bool listen_for_extension_system_loaded,
    scoped_refptr<ContentVerifier> content_verifier)
    :{}

ExtensionUserScriptLoader::~ExtensionUserScriptLoader() = default;

void ExtensionUserScriptLoader::AddPendingDynamicScriptIDs(
    std::set<std::string> script_ids) {}

void ExtensionUserScriptLoader::RemovePendingDynamicScriptIDs(
    const std::set<std::string>& script_ids) {}

bool ExtensionUserScriptLoader::AddScriptsForExtensionLoad(
    const Extension& extension,
    UserScriptLoader::ScriptsLoadedCallback callback) {}

void ExtensionUserScriptLoader::AddDynamicScripts(
    UserScriptList scripts,
    std::set<std::string> persistent_script_ids,
    DynamicScriptsModifiedCallback callback) {}

void ExtensionUserScriptLoader::RemoveDynamicScripts(
    const std::set<std::string>& ids_to_remove,
    DynamicScriptsModifiedCallback callback) {}

void ExtensionUserScriptLoader::ClearDynamicScripts(
    UserScript::Source source,
    DynamicScriptsModifiedCallback callback) {}

void ExtensionUserScriptLoader::SetSourceEnabled(UserScript::Source source,
                                                 bool enabled) {}

void ExtensionUserScriptLoader::UpdateDynamicScripts(
    UserScriptList scripts,
    std::set<std::string> script_ids,
    std::set<std::string> persistent_script_ids,
    ExtensionUserScriptLoader::DynamicScriptsModifiedCallback add_callback) {}

std::set<std::string> ExtensionUserScriptLoader::GetDynamicScriptIDs(
    UserScript::Source source) const {}

const UserScriptList& ExtensionUserScriptLoader::GetLoadedDynamicScripts()
    const {}

std::set<std::string> ExtensionUserScriptLoader::GetPersistentDynamicScriptIDs()
    const {}

UserScriptList ExtensionUserScriptLoader::LoadScriptsForTest(
    UserScriptList user_scripts) {}

ExtensionUserScriptLoader::DynamicScriptsStorageHelper::
    DynamicScriptsStorageHelper(BrowserContext* browser_context,
                                const ExtensionId& extension_id,
                                StateStore* state_store)
    :{}

ExtensionUserScriptLoader::DynamicScriptsStorageHelper::
    ~DynamicScriptsStorageHelper() = default;

void ExtensionUserScriptLoader::DynamicScriptsStorageHelper::GetDynamicScripts(
    DynamicScriptsReadCallback callback) {}

void ExtensionUserScriptLoader::DynamicScriptsStorageHelper::SetDynamicScripts(
    const UserScriptList& scripts,
    const std::set<std::string>& persistent_dynamic_script_ids) {}

void ExtensionUserScriptLoader::DynamicScriptsStorageHelper::
    OnDynamicScriptsReadFromStorage(DynamicScriptsReadCallback callback,
                                    std::optional<base::Value> value) {}

void ExtensionUserScriptLoader::LoadScripts(
    UserScriptList user_scripts,
    const std::set<std::string>& added_script_ids,
    LoadScriptsCallback callback) {}

void ExtensionUserScriptLoader::OnExtensionSystemReady() {}

void ExtensionUserScriptLoader::OnInitialDynamicScriptsReadFromStateStore(
    UserScriptList manifest_scripts,
    UserScriptLoader::ScriptsLoadedCallback callback,
    UserScriptList initial_dynamic_scripts) {}

void ExtensionUserScriptLoader::OnInitialExtensionScriptsLoaded(
    UserScriptList initial_dynamic_scripts,
    UserScriptLoader::ScriptsLoadedCallback callback,
    UserScriptLoader* loader,
    const std::optional<std::string>& error) {}

void ExtensionUserScriptLoader::OnDynamicScriptsAdded(
    UserScriptList added_scripts,
    std::set<std::string> new_persistent_script_ids,
    DynamicScriptsModifiedCallback callback,
    UserScriptLoader* loader,
    const std::optional<std::string>& error) {}

void ExtensionUserScriptLoader::OnDynamicScriptsRemoved(
    const std::set<std::string>& removed_script_ids,
    DynamicScriptsModifiedCallback callback,
    UserScriptLoader* loader,
    const std::optional<std::string>& error) {}

bool ExtensionUserScriptLoader::HasInitialDynamicScripts(
    const Extension& extension) const {}

}  // namespace extensions