#include "chrome/browser/extensions/api/scripting/scripting_api.h"
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/json/json_writer.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/types/optional_util.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/common/extensions/api/scripting.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_user_script_loader.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/load_and_localize_file.h"
#include "extensions/browser/script_executor.h"
#include "extensions/browser/scripting_constants.h"
#include "extensions/browser/scripting_utils.h"
#include "extensions/browser/user_script_manager.h"
#include "extensions/common/api/extension_types.h"
#include "extensions/common/api/scripts_internal.h"
#include "extensions/common/api/scripts_internal/script_serialization.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/mojom/css_origin.mojom-shared.h"
#include "extensions/common/mojom/execution_world.mojom-shared.h"
#include "extensions/common/mojom/host_id.mojom.h"
#include "extensions/common/mojom/run_location.mojom-shared.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/script_constants.h"
#include "extensions/common/user_script.h"
#include "extensions/common/utils/content_script_utils.h"
#include "extensions/common/utils/extension_types_utils.h"
namespace extensions {
namespace {
constexpr char kCouldNotLoadFileError[] = …;
constexpr char kDuplicateFileSpecifiedError[] = …;
constexpr char kEmptyMatchesError[] = …;
constexpr char kExactlyOneOfCssAndFilesError[] = …;
constexpr mojom::RunLocation kCSSRunLocation = …;
mojom::CSSOrigin ConvertStyleOriginToCSSOrigin(
api::scripting::StyleOrigin style_origin) { … }
mojom::ExecutionWorld ConvertExecutionWorld(
api::scripting::ExecutionWorld world) { … }
std::string InjectionKeyForCode(const mojom::HostID& host_id,
const std::string& code) { … }
std::string InjectionKeyForFile(const mojom::HostID& host_id,
const GURL& resource_url) { … }
std::vector<InjectedFileSource> ConstructFileSources(
std::vector<std::unique_ptr<std::string>> data,
std::vector<std::string> file_names) { … }
std::vector<mojom::JSSourcePtr> FileSourcesToJSSources(
const Extension& extension,
std::vector<InjectedFileSource> file_sources) { … }
std::vector<mojom::CSSSourcePtr> FileSourcesToCSSSources(
const Extension& extension,
std::vector<InjectedFileSource> file_sources) { … }
bool GetFileResources(const std::vector<std::string>& files,
const Extension& extension,
std::vector<ExtensionResource>* resources_out,
std::string* error_out) { … }
ResourcesLoadedCallback;
void CheckLoadedResources(std::vector<std::string> file_names,
ResourcesLoadedCallback callback,
std::vector<std::unique_ptr<std::string>> file_data,
std::optional<std::string> load_error) { … }
bool CheckAndLoadFiles(std::vector<std::string> files,
const Extension& extension,
bool requires_localization,
ResourcesLoadedCallback callback,
std::string* error) { … }
std::string GetCannotAccessPageErrorMessage(const PermissionsData& permissions,
const GURL& url) { … }
bool HasPermissionToInjectIntoFrame(const PermissionsData& permissions,
int tab_id,
content::RenderFrameHost* frame,
std::string* error) { … }
bool CollectFramesForInjection(const api::scripting::InjectionTarget& target,
content::WebContents* tab,
std::set<int>& frame_ids,
std::set<content::RenderFrameHost*>& frames,
std::string* error_out) { … }
bool CanAccessTarget(const PermissionsData& permissions,
const api::scripting::InjectionTarget& target,
content::BrowserContext* browser_context,
bool include_incognito_information,
ScriptExecutor** script_executor_out,
ScriptExecutor::FrameScope* frame_scope_out,
std::set<int>* frame_ids_out,
std::string* error_out) { … }
api::scripts_internal::SerializedUserScript
ConvertRegisteredContentScriptToSerializedUserScript(
api::scripting::RegisteredContentScript content_script) { … }
std::unique_ptr<UserScript> ParseUserScript(
content::BrowserContext* browser_context,
const Extension& extension,
bool allowed_in_incognito,
api::scripting::RegisteredContentScript content_script,
std::u16string* error) { … }
api::scripting::RegisteredContentScript CreateRegisteredContentScriptInfo(
const UserScript& script) { … }
}
InjectedFileSource::InjectedFileSource(std::string file_name,
std::unique_ptr<std::string> data)
: … { … }
InjectedFileSource::InjectedFileSource(InjectedFileSource&&) = default;
InjectedFileSource::~InjectedFileSource() = default;
ScriptingExecuteScriptFunction::ScriptingExecuteScriptFunction() = default;
ScriptingExecuteScriptFunction::~ScriptingExecuteScriptFunction() = default;
ExtensionFunction::ResponseAction ScriptingExecuteScriptFunction::Run() { … }
void ScriptingExecuteScriptFunction::DidLoadResources(
std::vector<InjectedFileSource> file_sources,
std::optional<std::string> load_error) { … }
bool ScriptingExecuteScriptFunction::Execute(
std::vector<mojom::JSSourcePtr> sources,
std::string* error) { … }
void ScriptingExecuteScriptFunction::OnScriptExecuted(
std::vector<ScriptExecutor::FrameResult> frame_results) { … }
ScriptingInsertCSSFunction::ScriptingInsertCSSFunction() = default;
ScriptingInsertCSSFunction::~ScriptingInsertCSSFunction() = default;
ExtensionFunction::ResponseAction ScriptingInsertCSSFunction::Run() { … }
void ScriptingInsertCSSFunction::DidLoadResources(
std::vector<InjectedFileSource> file_sources,
std::optional<std::string> load_error) { … }
bool ScriptingInsertCSSFunction::Execute(
std::vector<mojom::CSSSourcePtr> sources,
std::string* error) { … }
void ScriptingInsertCSSFunction::OnCSSInserted(
std::vector<ScriptExecutor::FrameResult> results) { … }
ScriptingRemoveCSSFunction::ScriptingRemoveCSSFunction() = default;
ScriptingRemoveCSSFunction::~ScriptingRemoveCSSFunction() = default;
ExtensionFunction::ResponseAction ScriptingRemoveCSSFunction::Run() { … }
void ScriptingRemoveCSSFunction::OnCSSRemoved(
std::vector<ScriptExecutor::FrameResult> results) { … }
ScriptingRegisterContentScriptsFunction::
ScriptingRegisterContentScriptsFunction() = default;
ScriptingRegisterContentScriptsFunction::
~ScriptingRegisterContentScriptsFunction() = default;
ExtensionFunction::ResponseAction
ScriptingRegisterContentScriptsFunction::Run() { … }
void ScriptingRegisterContentScriptsFunction::OnContentScriptFilesValidated(
std::set<std::string> persistent_script_ids,
scripting::ValidateScriptsResult result) { … }
void ScriptingRegisterContentScriptsFunction::OnContentScriptsRegistered(
const std::optional<std::string>& error) { … }
ScriptingGetRegisteredContentScriptsFunction::
ScriptingGetRegisteredContentScriptsFunction() = default;
ScriptingGetRegisteredContentScriptsFunction::
~ScriptingGetRegisteredContentScriptsFunction() = default;
ExtensionFunction::ResponseAction
ScriptingGetRegisteredContentScriptsFunction::Run() { … }
ScriptingUnregisterContentScriptsFunction::
ScriptingUnregisterContentScriptsFunction() = default;
ScriptingUnregisterContentScriptsFunction::
~ScriptingUnregisterContentScriptsFunction() = default;
ExtensionFunction::ResponseAction
ScriptingUnregisterContentScriptsFunction::Run() { … }
void ScriptingUnregisterContentScriptsFunction::OnContentScriptsUnregistered(
const std::optional<std::string>& error) { … }
ScriptingUpdateContentScriptsFunction::ScriptingUpdateContentScriptsFunction() =
default;
ScriptingUpdateContentScriptsFunction::
~ScriptingUpdateContentScriptsFunction() = default;
ExtensionFunction::ResponseAction ScriptingUpdateContentScriptsFunction::Run() { … }
std::unique_ptr<UserScript> ScriptingUpdateContentScriptsFunction::ApplyUpdate(
std::set<std::string>* script_ids_to_persist,
api::scripting::RegisteredContentScript& new_script,
api::scripting::RegisteredContentScript& original_script,
std::u16string* parse_error) { … }
void ScriptingUpdateContentScriptsFunction::OnContentScriptFilesValidated(
std::set<std::string> persistent_script_ids,
scripting::ValidateScriptsResult result) { … }
void ScriptingUpdateContentScriptsFunction::OnContentScriptsUpdated(
const std::optional<std::string>& error) { … }
}