blob: c748e7c8c0f95c85f1fc4b19a2480fd2d0f0bf5d [file] [log] [blame]
Avi Drissman60039d42022-09-13 21:49:051// Copyright 2015 The Chromium Authors
hanxifeb6a64f2015-04-24 19:21:402// 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
avic9cec102015-12-23 00:39:268#include <stddef.h>
9
dchengf5d241082016-04-21 03:43:1110#include <memory>
limasdf6ca2e3c2015-11-13 22:16:0411#include <vector>
12
hanxifeb6a64f2015-04-24 19:21:4013#include "base/callback.h"
hanxifeb6a64f2015-04-24 19:21:4014#include "extensions/browser/user_script_loader.h"
Julie Jeongeun Kim30f64632021-03-10 01:10:0215#include "extensions/common/mojom/host_id.mojom-forward.h"
hanxifeb6a64f2015-04-24 19:21:4016
Kelvin Jiangf33eec3d62021-02-13 01:01:4117class GURL;
hanxifeb6a64f2015-04-24 19:21:4018class WebUIURLFetcher;
19
20namespace content {
21class BrowserContext;
22}
23
24// UserScriptLoader for WebUI.
25class WebUIUserScriptLoader : public extensions::UserScriptLoader {
26 public:
27 WebUIUserScriptLoader(content::BrowserContext* browser_context,
Kelvin Jiangf33eec3d62021-02-13 01:01:4128 const GURL& url);
Peter Boström951cf77e2021-09-22 00:02:5929
30 WebUIUserScriptLoader(const WebUIUserScriptLoader&) = delete;
31 WebUIUserScriptLoader& operator=(const WebUIUserScriptLoader&) = delete;
32
hanxifeb6a64f2015-04-24 19:21:4033 ~WebUIUserScriptLoader() override;
34
35 private:
36 struct UserScriptRenderInfo;
Kelvin Jiang6f9941c02020-12-15 22:11:5737 using UserScriptRenderInfoMap = std::map<std::string, UserScriptRenderInfo>;
hanxifeb6a64f2015-04-24 19:21:4038
39 // UserScriptLoader:
lazyboy12c77d72016-08-19 20:06:0940 void AddScripts(std::unique_ptr<extensions::UserScriptList> scripts,
hanxifeb6a64f2015-04-24 19:21:4041 int render_process_id,
Kelvin Jiang7afb4422021-03-04 08:27:2042 int render_frame_id,
43 ScriptsLoadedCallback callback) override;
dchengf5d241082016-04-21 03:43:1144 void LoadScripts(std::unique_ptr<extensions::UserScriptList> user_scripts,
Kelvin Jiang6f9941c02020-12-15 22:11:5745 const std::set<std::string>& added_script_ids,
hanxifeb6a64f2015-04-24 19:21:4046 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,
lazyboyb81e69a2016-08-18 22:35:0451 std::unique_ptr<std::string> data);
hanxifeb6a64f2015-04-24 19:21:4052
53 // Called when the loads of the user scripts are done.
54 void OnWebUIURLFetchComplete();
55
56 // Creates WebUiURLFetchers for the given |script_files|.
lazyboy12c77d72016-08-19 20:06:0957 void CreateWebUIURLFetchers(
58 const extensions::UserScript::FileList& script_files,
lazyboy12c77d72016-08-19 20:06:0959 int render_process_id,
60 int render_frame_id);
hanxifeb6a64f2015-04-24 19:21:4061
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.
dchengf5d241082016-04-21 03:43:1172 std::unique_ptr<extensions::UserScriptList> user_scripts_cache_;
hanxifeb6a64f2015-04-24 19:21:4073
74 LoadScriptsCallback scripts_loaded_callback_;
75
dchengf5d241082016-04-21 03:43:1176 std::vector<std::unique_ptr<WebUIURLFetcher>> fetchers_;
hanxifeb6a64f2015-04-24 19:21:4077};
78
79#endif // EXTENSIONS_BROWSER_WEB_UI_USER_SCRIPT_LOADER_H_