Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef EXTENSIONS_BROWSER_WEB_UI_USER_SCRIPT_LOADER_H_ |
| 6 | #define EXTENSIONS_BROWSER_WEB_UI_USER_SCRIPT_LOADER_H_ |
| 7 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 10 | #include <memory> |
limasdf | 6ca2e3c | 2015-11-13 22:16:04 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 13 | #include "base/callback.h" |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 14 | #include "extensions/browser/user_script_loader.h" |
Julie Jeongeun Kim | 30f6463 | 2021-03-10 01:10:02 | [diff] [blame] | 15 | #include "extensions/common/mojom/host_id.mojom-forward.h" |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 16 | |
Kelvin Jiang | f33eec3d6 | 2021-02-13 01:01:41 | [diff] [blame] | 17 | class GURL; |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 18 | class WebUIURLFetcher; |
| 19 | |
| 20 | namespace content { |
| 21 | class BrowserContext; |
| 22 | } |
| 23 | |
| 24 | // UserScriptLoader for WebUI. |
| 25 | class WebUIUserScriptLoader : public extensions::UserScriptLoader { |
| 26 | public: |
| 27 | WebUIUserScriptLoader(content::BrowserContext* browser_context, |
Kelvin Jiang | f33eec3d6 | 2021-02-13 01:01:41 | [diff] [blame] | 28 | const GURL& url); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 29 | |
| 30 | WebUIUserScriptLoader(const WebUIUserScriptLoader&) = delete; |
| 31 | WebUIUserScriptLoader& operator=(const WebUIUserScriptLoader&) = delete; |
| 32 | |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 33 | ~WebUIUserScriptLoader() override; |
| 34 | |
| 35 | private: |
| 36 | struct UserScriptRenderInfo; |
Kelvin Jiang | 6f9941c0 | 2020-12-15 22:11:57 | [diff] [blame] | 37 | using UserScriptRenderInfoMap = std::map<std::string, UserScriptRenderInfo>; |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 38 | |
| 39 | // UserScriptLoader: |
lazyboy | 12c77d7 | 2016-08-19 20:06:09 | [diff] [blame] | 40 | void AddScripts(std::unique_ptr<extensions::UserScriptList> scripts, |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 41 | int render_process_id, |
Kelvin Jiang | 7afb442 | 2021-03-04 08:27:20 | [diff] [blame] | 42 | int render_frame_id, |
| 43 | ScriptsLoadedCallback callback) override; |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 44 | void LoadScripts(std::unique_ptr<extensions::UserScriptList> user_scripts, |
Kelvin Jiang | 6f9941c0 | 2020-12-15 22:11:57 | [diff] [blame] | 45 | const std::set<std::string>& added_script_ids, |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 46 | LoadScriptsCallback callback) override; |
| 47 | |
| 48 | // Called at the end of each fetch, tracking whether all fetches are done. |
| 49 | void OnSingleWebUIURLFetchComplete(extensions::UserScript::File* script_file, |
| 50 | bool success, |
lazyboy | b81e69a | 2016-08-18 22:35:04 | [diff] [blame] | 51 | std::unique_ptr<std::string> data); |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 52 | |
| 53 | // Called when the loads of the user scripts are done. |
| 54 | void OnWebUIURLFetchComplete(); |
| 55 | |
| 56 | // Creates WebUiURLFetchers for the given |script_files|. |
lazyboy | 12c77d7 | 2016-08-19 20:06:09 | [diff] [blame] | 57 | void CreateWebUIURLFetchers( |
| 58 | const extensions::UserScript::FileList& script_files, |
lazyboy | 12c77d7 | 2016-08-19 20:06:09 | [diff] [blame] | 59 | int render_process_id, |
| 60 | int render_frame_id); |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 61 | |
| 62 | // Caches the render info of script from WebUI when AddScripts is called. |
| 63 | // When starting to load the script, we look up this map to retrieve the |
| 64 | // render info. It is used for the script from WebUI only, since the fetch |
| 65 | // of script content requires the info of associated render. |
| 66 | UserScriptRenderInfoMap script_render_info_map_; |
| 67 | |
| 68 | // The number of complete fetchs. |
| 69 | size_t complete_fetchers_; |
| 70 | |
| 71 | // Caches |user_scripts_| from UserScriptLoader when loading. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 72 | std::unique_ptr<extensions::UserScriptList> user_scripts_cache_; |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 73 | |
| 74 | LoadScriptsCallback scripts_loaded_callback_; |
| 75 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 76 | std::vector<std::unique_ptr<WebUIURLFetcher>> fetchers_; |
hanxi | feb6a64f | 2015-04-24 19:21:40 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | #endif // EXTENSIONS_BROWSER_WEB_UI_USER_SCRIPT_LOADER_H_ |