Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / guest_view / web_view / web_view_content_script_manager.h
blob0650ec1497edce73cd5ee7d0ede1038f67524447
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_CONTENT_SCRIPT_MANAGER_H
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_CONTENT_SCRIPT_MANAGER_H
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/supports_user_data.h"
15 #include "extensions/browser/user_script_loader.h"
17 struct HostID;
19 namespace content {
20 class BrowserContext;
21 class RenderViewHost;
22 class WebContents;
25 namespace extensions {
26 class UserScript;
28 // WebViewContentScriptManager manages the content scripts that each webview
29 // guest adds and removes programmatically.
30 class WebViewContentScriptManager : public base::SupportsUserData::Data,
31 public UserScriptLoader::Observer {
32 public:
33 explicit WebViewContentScriptManager(
34 content::BrowserContext* browser_context);
35 ~WebViewContentScriptManager() override;
37 static WebViewContentScriptManager* Get(
38 content::BrowserContext* browser_context);
40 // Adds content scripts for the WebView specified by
41 // |embedder_process_id| and |view_instance_id|.
42 void AddContentScripts(int embedder_process_id,
43 content::RenderViewHost* render_view_host,
44 int view_instance_id,
45 const HostID& host_id,
46 const std::set<UserScript>& user_scripts);
48 // Removes all content scripts for the WebView identified by
49 // |embedder_process_id| and |view_instance_id|.
50 void RemoveAllContentScriptsForWebView(int embedder_process_id,
51 int view_instance_id);
53 // Removes contents scipts whose names are in the |script_name_list| for the
54 // WebView specified by |embedder_process_id| and |view_instance_id|.
55 // If the |script_name_list| is empty, removes all the content scripts added
56 // for this WebView.
57 void RemoveContentScripts(int embedder_process_id,
58 int view_instance_id,
59 const HostID& host_id,
60 const std::vector<std::string>& script_name_list);
62 // Returns the content script IDs added by the WebView specified by
63 // |embedder_process_id| and |view_instance_id|.
64 std::set<int> GetContentScriptIDSet(int embedder_process_id,
65 int view_instance_id);
67 // Checks if there is any pending content scripts to load.
68 // If no, run |callback| immediately; otherwise caches the |callback|, and
69 // the |callback| will be called after all the pending content scripts are
70 // loaded.
71 void SignalOnScriptsLoaded(const base::Closure& callback);
73 private:
74 using GuestMapKey = std::pair<int, int>;
75 using ContentScriptMap = std::map<std::string, extensions::UserScript>;
76 using GuestContentScriptMap = std::map<GuestMapKey, ContentScriptMap>;
78 // UserScriptLoader::Observer implementation:
79 void OnScriptsLoaded(UserScriptLoader* loader) override;
80 void OnUserScriptLoaderDestroyed(UserScriptLoader* loader) override;
82 // If |user_script_loader_observer_| doesn't observe any source, we will run
83 // all the remaining callbacks in |pending_scripts_loading_callbacks_|.
84 void RunCallbacksIfReady();
86 // A map from embedder process ID and view instance ID (uniquely identifying
87 // one webview) to that webview's host ID. All webviews that have content
88 // scripts registered through this WebViewContentScriptManager will have an
89 // entry in this map.
90 std::map<GuestMapKey, HostID> webview_host_id_map_;
92 GuestContentScriptMap guest_content_script_map_;
94 // WebViewContentScriptManager observes UserScriptLoader to wait for scripts
95 // loaded event.
96 ScopedObserver<UserScriptLoader, UserScriptLoader::Observer>
97 user_script_loader_observer_;
99 // Caches callbacks and resumes them when all the scripts are loaded.
100 std::vector<base::Closure> pending_scripts_loading_callbacks_;
102 content::BrowserContext* browser_context_;
104 DISALLOW_COPY_AND_ASSIGN(WebViewContentScriptManager);
107 } // namespace extensions
109 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_CONTENT_SCRIPT_MANAGER_H