OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager .h" | |
6 | |
7 #include "content/public/browser/browser_context.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "extensions/browser/declarative_user_script_manager.h" | |
10 #include "extensions/browser/declarative_user_script_master.h" | |
11 #include "extensions/browser/extension_system.h" | |
12 #include "extensions/browser/guest_view/web_view/web_view_constants.h" | |
13 | |
14 namespace { | |
15 using GuestMapKey = std::pair<int, int>; | |
16 using ContentScriptMap = std::map<std::string, extensions::UserScript>; | |
17 using GuestContentScriptMap = std::map<GuestMapKey, ContentScriptMap>; | |
Fady Samuel
2015/03/31 23:40:11
member variables instead so information doesn't le
Xi Han
2015/04/01 22:14:40
Has updated since last patch:)
| |
18 | |
19 using content::BrowserThread; | |
20 | |
21 GuestContentScriptMap guest_content_script_map; | |
22 } | |
23 | |
24 namespace extensions { | |
25 | |
26 WebViewContentScriptManager::WebViewContentScriptManager( | |
27 content::BrowserContext* browser_context) | |
28 : browser_context_(browser_context) { | |
29 } | |
30 | |
31 WebViewContentScriptManager::~WebViewContentScriptManager() { | |
32 } | |
33 | |
34 WebViewContentScriptManager* WebViewContentScriptManager::Get( | |
35 content::BrowserContext* browser_context) { | |
36 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
37 | |
38 WebViewContentScriptManager* manager = | |
39 static_cast<WebViewContentScriptManager*>(browser_context->GetUserData( | |
40 webview::kWebViewContentScriptManagerKeyName)); | |
41 if (!manager) { | |
42 manager = new WebViewContentScriptManager(browser_context); | |
43 browser_context->SetUserData(webview::kWebViewContentScriptManagerKeyName, | |
44 manager); | |
45 } | |
46 return manager; | |
47 } | |
48 | |
49 void WebViewContentScriptManager::AddContentScripts( | |
50 int embedder_process_id, | |
51 int guest_view_instance_id, | |
52 const HostID& host_id, | |
53 const std::map<std::string, UserScript>& scripts) { | |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
55 | |
56 DeclarativeUserScriptMaster* master = | |
57 ExtensionSystem::Get(browser_context_) | |
58 ->declarative_user_script_manager() | |
59 ->GetDeclarativeUserScriptMasterByID(host_id); | |
60 CHECK(master); | |
61 | |
62 GuestMapKey key = | |
63 std::pair<int, int>(embedder_process_id, guest_view_instance_id); | |
64 GuestContentScriptMap::iterator iter = guest_content_script_map.find(key); | |
65 | |
66 // If there isn't any content script added for the given guest yet, insert the | |
67 // |key, scripts| to the |guest_content_script_map_| directly; otherwise, | |
68 // insert the |scripts| to the existing content script map of the guest. | |
69 if (iter == guest_content_script_map.end()) { | |
70 guest_content_script_map.insert( | |
71 std::pair<GuestMapKey, ContentScriptMap>(key, scripts)); | |
72 } else { | |
73 ContentScriptMap& map = iter->second; | |
74 for (const std::pair<std::string, UserScript>& pair : scripts) { | |
75 auto map_iter = map.find(pair.first); | |
76 // If a content script has the same name as the new one, removes the old | |
77 // script first, and insert the new one. | |
78 if (map_iter != map.end()) | |
79 master->RemoveScript(pair.second); | |
80 map.insert(pair); | |
81 } | |
82 } | |
83 | |
84 for (const std::pair<std::string, UserScript>& pair : scripts) | |
85 master->AddScript(pair.second); | |
86 } | |
87 | |
88 void WebViewContentScriptManager::RemoveContentScripts( | |
89 int embedder_process_id, | |
90 int guest_view_instance_id, | |
91 const HostID& host_id, | |
92 std::vector<std::string>* script_name_list) { | |
93 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
94 | |
95 GuestMapKey key = | |
96 std::pair<int, int>(embedder_process_id, guest_view_instance_id); | |
97 GuestContentScriptMap::iterator script_map_iter = | |
98 guest_content_script_map.find(key); | |
99 if (script_map_iter == guest_content_script_map.end()) | |
100 return; | |
101 | |
102 DeclarativeUserScriptMaster* master = | |
103 ExtensionSystem::Get(browser_context_) | |
104 ->declarative_user_script_manager() | |
105 ->GetDeclarativeUserScriptMasterByID(host_id); | |
106 CHECK(master); | |
107 | |
108 std::vector<std::string> names_to_delete; | |
109 std::map<std::string, UserScript>& map = script_map_iter->second; | |
110 // If the |script_name_list| is empty, all the content scripts added by the | |
111 // guest will be removed; otherwise, removes the scripts in the | |
112 // |script_name_list|. | |
113 if (!script_name_list || script_name_list->empty()) { | |
114 for (const std::pair<std::string, UserScript>& iter : map) | |
115 names_to_delete.push_back(iter.first); | |
116 } else { | |
117 names_to_delete = *script_name_list; | |
118 } | |
119 | |
120 for (const std::string& name : names_to_delete) { | |
121 ContentScriptMap::iterator iter = map.find(name); | |
122 if (iter == map.end()) | |
123 continue; | |
124 const UserScript& script = iter->second; | |
125 master->RemoveScript(script); | |
126 map.erase(iter); | |
127 } | |
128 } | |
129 | |
130 bool WebViewContentScriptManager::OwnsUserScript(int embedder_process_id, | |
131 int guest_view_instance_id, | |
132 int script_id) { | |
133 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
134 | |
135 GuestMapKey key = | |
136 std::pair<int, int>(embedder_process_id, guest_view_instance_id); | |
137 GuestContentScriptMap::const_iterator iter = | |
138 guest_content_script_map.find(key); | |
139 if (iter == guest_content_script_map.end()) | |
140 return false; | |
141 const ContentScriptMap& map = iter->second; | |
142 for (const auto& pair : map) { | |
143 if (pair.second.id() == script_id) | |
144 return true; | |
145 } | |
146 return false; | |
147 } | |
148 | |
149 } // namespace extensions | |
OLD | NEW |