// Copyright 2021 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef EXTENSIONS_BROWSER_SCRIPTING_UTILS_H_ #define EXTENSIONS_BROWSER_SCRIPTING_UTILS_H_ #include #include "base/containers/contains.h" #include "base/functional/callback_forward.h" #include "base/strings/utf_string_conversions.h" #include "extensions/browser/extension_user_script_loader.h" #include "extensions/browser/script_executor.h" #include "extensions/common/api/scripts_internal.h" #include "extensions/common/error_utils.h" #include "extensions/common/extension_id.h" #include "extensions/common/extension_resource.h" #include "extensions/common/url_pattern_set.h" #include "extensions/common/user_script.h" namespace content { class BrowserContext; } // namespace content namespace extensions::scripting { // Details specifying the target into which to inject the script. struct InjectionTarget { InjectionTarget(); InjectionTarget(InjectionTarget&& other); ~InjectionTarget(); // Whether the script should inject into all frames within the tab. std::optional all_frames; // The IDs of specific documentIds to inject into. std::optional> document_ids; // The IDs of specific frames to inject into. std::optional> frame_ids; // The ID of the tab into which to inject. int tab_id; }; // Details specifying the read file (either CSS or JS) for the script to be // injected. struct InjectedFileSource { InjectedFileSource(std::string file_name, std::unique_ptr data); InjectedFileSource(InjectedFileSource&&); ~InjectedFileSource(); std::string file_name; std::unique_ptr data; }; using ResourcesLoadedCallback = base::OnceCallback, std::optional)>; // Appends the prefix corresponding to the dynamic script `source` to // `script_id`. std::string AddPrefixToDynamicScriptId(const std::string& script_id, UserScript::Source source); // Returns whether the extension provided `script_id` (without an internal // prefix) is valid. Populates `error` if invalid. bool IsScriptIdValid(const std::string& script_id, std::string* error); // Returns whether new scripts added for the extension with the given // `extension_id` should be allowed in incognito contexts. bool ScriptsShouldBeAllowedInIncognito( const ExtensionId& extension_id, content::BrowserContext* browser_context); // Returns a set of unique dynamic script IDs (with an added prefix // corresponding to `source`) for all given `scripts`. If the script is invalid // or duplicated in `existing_script_ids` or the new ids, populates error and // returns an empty set. template std::set CreateDynamicScriptIds( std::vector