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"
24 namespace extensions
{
27 // WebViewContentScriptManager manages the content scripts that each webview
28 // guest adds and removes programmatically.
29 // TODO(hanxi): crbug.com/476938. Introduce a new class to manage the lifetime
30 // of <webview> and clean up WebViewContentScriptManager.
31 class WebViewContentScriptManager
: public base::SupportsUserData::Data
,
32 public UserScriptLoader::Observer
{
34 explicit WebViewContentScriptManager(
35 content::BrowserContext
* browser_context
);
36 ~WebViewContentScriptManager() override
;
38 static WebViewContentScriptManager
* Get(
39 content::BrowserContext
* browser_context
);
41 // Adds content scripts for the guest specified by the |embedder_web_contents,
43 void AddContentScripts(content::WebContents
* embedder_web_contents
,
44 int embedder_routing_id
,
46 const HostID
& host_id
,
47 const std::set
<UserScript
>& user_scripts
);
49 // Removes contents scipts whose names are in the |script_name_list| for the
50 // guest specified by |embedder_web_contents, view_instance_id|.
51 // If the |script_name_list| is empty, removes all the content scripts added
53 void RemoveContentScripts(content::WebContents
* embedder_web_contents
,
55 const HostID
& host_id
,
56 const std::vector
<std::string
>& script_name_list
);
58 // Returns the content script IDs added by the guest specified by
59 // |embedder_process_id, view_instance_id|.
60 std::set
<int> GetContentScriptIDSet(int embedder_process_id
,
61 int view_instance_id
);
63 // Checks if there is any pending content scripts to load.
64 // If no, run |callback| immediately; otherwise caches the |callback|, and
65 // the |callback| will be called after all the pending content scripts are
67 void SignalOnScriptsLoaded(const base::Closure
& callback
);
70 class OwnerWebContentsObserver
;
72 using GuestMapKey
= std::pair
<int, int>;
73 using ContentScriptMap
= std::map
<std::string
, extensions::UserScript
>;
74 using GuestContentScriptMap
= std::map
<GuestMapKey
, ContentScriptMap
>;
76 using OwnerWebContentsObserverMap
=
77 std::map
<content::WebContents
*, linked_ptr
<OwnerWebContentsObserver
>>;
79 // UserScriptLoader::Observer implementation:
80 void OnScriptsLoaded(UserScriptLoader
* loader
) override
;
81 void OnUserScriptLoaderDestroyed(UserScriptLoader
* loader
) override
;
83 // Called by OwnerWebContentsObserver when the observer sees a main frame
84 // navigation or sees the process has went away or the |embedder_web_contents|
85 // is being destroyed.
86 void RemoveObserver(content::WebContents
* embedder_web_contents
);
88 // If |user_script_loader_observer_| doesn't observe any source, we will run
89 // all the remaining callbacks in |pending_scripts_loading_callbacks_|.
90 void RunCallbacksIfReady();
92 OwnerWebContentsObserverMap owner_web_contents_observer_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