#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;
ScriptResourceIds;
enum class ReadScriptContentSource { … };
inline constexpr char kId[] = …;
std::tuple<std::optional<std::string>, ReadScriptContentSource>
ReadScriptContent(UserScript::Content* script_file,
const std::optional<int>& script_resource_id,
size_t& remaining_length) { … }
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) { … }
void RecordTotalContentScriptLengthForLoad(size_t manifest_scripts_length,
size_t dynamic_scripts_length) { … }
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) { … }
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) { … }
std::optional<api::scripts_internal::SerializedUserScript>
ContentScriptDictToSerializedUserScript(const base::Value::Dict& dict) { … }
UserScriptList ConvertValueToScripts(const Extension& extension,
bool allowed_in_incognito,
const base::Value::List& list) { … }
UserScriptList GetManifestScriptsMetadata(
content::BrowserContext* browser_context,
const Extension& extension) { … }
std::unique_ptr<UserScript> CopyDynamicScriptInfo(const UserScript& script) { … }
}
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 { … }
}