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
13 #include "base/callback.h"
14 #include "base/supports_user_data.h"
15 #include "extensions/browser/user_script_loader.h"
25 namespace extensions
{
28 // WebViewContentScriptManager manages the content scripts that each webview
29 // guest adds and removes programmatically.
30 // TODO(hanxi): crbug.com/476938. Introduce a new class to manage the lifetime
31 // of <webview> and clean up WebViewContentScriptManager.
32 class WebViewContentScriptManager
: public base::SupportsUserData::Data
,
33 public UserScriptLoader::Observer
{
35 explicit WebViewContentScriptManager(
36 content::BrowserContext
* browser_context
);
37 ~WebViewContentScriptManager() override
;
39 static WebViewContentScriptManager
* Get(
40 content::BrowserContext
* browser_context
);
42 // Adds content scripts for the WebView specified by
43 // |embedder_process_id| and |view_instance_id|.
44 void AddContentScripts(int embedder_process_id
,
45 content::RenderViewHost
* render_view_host
,
47 const HostID
& host_id
,
48 const std::set
<UserScript
>& user_scripts
);
50 // Removes all content scripts for the WebView identified by
51 // |embedder_process_id| and |view_instance_id|.
52 void RemoveAllContentScriptsForWebView(int embedder_process_id
,
53 int view_instance_id
);
55 // Removes contents scipts whose names are in the |script_name_list| for the
56 // WebView specified by |embedder_process_id| and |view_instance_id|.
57 // If the |script_name_list| is empty, removes all the content scripts added
59 void RemoveContentScripts(int embedder_process_id
,
61 const HostID
& host_id
,
62 const std::vector
<std::string
>& script_name_list
);
64 // Returns the content script IDs added by the WebView specified by
65 // |embedder_process_id| and |view_instance_id|.
66 std::set
<int> GetContentScriptIDSet(int embedder_process_id
,
67 int view_instance_id
);
69 // Checks if there is any pending content scripts to load.
70 // If no, run |callback| immediately; otherwise caches the |callback|, and
71 // the |callback| will be called after all the pending content scripts are
73 void SignalOnScriptsLoaded(const base::Closure
& callback
);
76 using GuestMapKey
= std::pair
<int, int>;
77 using ContentScriptMap
= std::map
<std::string
, extensions::UserScript
>;
78 using GuestContentScriptMap
= std::map
<GuestMapKey
, ContentScriptMap
>;
80 // UserScriptLoader::Observer implementation:
81 void OnScriptsLoaded(UserScriptLoader
* loader
) override
;
82 void OnUserScriptLoaderDestroyed(UserScriptLoader
* loader
) override
;
84 // If |user_script_loader_observer_| doesn't observe any source, we will run
85 // all the remaining callbacks in |pending_scripts_loading_callbacks_|.
86 void RunCallbacksIfReady();
88 // A map from embedder process ID and view instance ID (uniquely identifying
89 // one webview) to that webview's host ID. All webviews that have content
90 // scripts registered through this WebViewContentScriptManager will have an
92 std::map
<GuestMapKey
, HostID
> webview_host_id_map_
;
94 GuestContentScriptMap guest_content_script_map_
;
96 // WebViewContentScriptManager observes UserScriptLoader to wait for scripts
98 ScopedObserver
<UserScriptLoader
, UserScriptLoader::Observer
>
99 user_script_loader_observer_
;
101 // Caches callbacks and resumes them when all the scripts are loaded.
102 std::vector
<base::Closure
> pending_scripts_loading_callbacks_
;
104 content::BrowserContext
* browser_context_
;
106 DISALLOW_COPY_AND_ASSIGN(WebViewContentScriptManager
);
109 } // namespace extensions
111 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_CONTENT_SCRIPT_MANAGER_H