Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / guest_view / web_view / web_view_content_script_manager.h
blobf6a2521e50046128c5c01640bae688284bccb42c
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 WebContents;
24 namespace extensions {
25 class UserScript;
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 {
33 public:
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,
42 // view_instance_id|.
43 void AddContentScripts(content::WebContents* embedder_web_contents,
44 int embedder_routing_id,
45 int view_instance_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
52 // for this guest.
53 void RemoveContentScripts(content::WebContents* embedder_web_contents,
54 int view_instance_id,
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
66 // loaded.
67 void SignalOnScriptsLoaded(const base::Closure& callback);
69 private:
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
97 // loaded event.
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