Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: extensions/browser/guest_view/web_view/web_view_content_script_manager.cc

Issue 2228743002: Rework some UserScriptLoader logic in preparation to removing UserScript copy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync @tott Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager .h" 5 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager .h"
6 6
7 #include "content/public/browser/browser_context.h" 7 #include "content/public/browser/browser_context.h"
8 #include "content/public/browser/browser_thread.h" 8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/navigation_details.h" 9 #include "content/public/browser/navigation_details.h"
10 #include "content/public/browser/render_frame_host.h" 10 #include "content/public/browser/render_frame_host.h"
(...skipping 28 matching lines...) Expand all
39 manager); 39 manager);
40 } 40 }
41 return manager; 41 return manager;
42 } 42 }
43 43
44 void WebViewContentScriptManager::AddContentScripts( 44 void WebViewContentScriptManager::AddContentScripts(
45 int embedder_process_id, 45 int embedder_process_id,
46 content::RenderFrameHost* render_frame_host, 46 content::RenderFrameHost* render_frame_host,
47 int view_instance_id, 47 int view_instance_id,
48 const HostID& host_id, 48 const HostID& host_id,
49 const std::set<UserScript>& scripts) { 49 const UserScriptList& scripts) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI); 50 DCHECK_CURRENTLY_ON(BrowserThread::UI);
51 51
52 DeclarativeUserScriptMaster* master = 52 DeclarativeUserScriptMaster* master =
53 DeclarativeUserScriptManager::Get(browser_context_) 53 DeclarativeUserScriptManager::Get(browser_context_)
54 ->GetDeclarativeUserScriptMasterByID(host_id); 54 ->GetDeclarativeUserScriptMasterByID(host_id);
55 DCHECK(master); 55 DCHECK(master);
56 56
57 // We need to update WebViewRenderState in the IO thread if the guest exists. 57 // We need to update WebViewRenderState in the IO thread if the guest exists.
58 std::set<int> ids_to_add; 58 std::set<int> ids_to_add;
59 59
60 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); 60 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id);
61 GuestContentScriptMap::iterator iter = guest_content_script_map_.find(key); 61 GuestContentScriptMap::iterator iter = guest_content_script_map_.find(key);
62 62
63 // Step 1: finds the entry in guest_content_script_map_ by the given |key|. 63 // Step 1: finds the entry in guest_content_script_map_ by the given |key|.
64 // If there isn't any content script added for the given guest yet, insert an 64 // If there isn't any content script added for the given guest yet, insert an
65 // empty map first. 65 // empty map first.
66 if (iter == guest_content_script_map_.end()) { 66 if (iter == guest_content_script_map_.end()) {
67 iter = guest_content_script_map_.insert( 67 iter = guest_content_script_map_.insert(
68 iter, 68 iter,
69 std::pair<GuestMapKey, ContentScriptMap>(key, ContentScriptMap())); 69 std::pair<GuestMapKey, ContentScriptMap>(key, ContentScriptMap()));
70 } 70 }
71 71
72 // Step 2: updates the guest_content_script_map_. 72 // Step 2: updates the guest_content_script_map_.
73 ContentScriptMap& map = iter->second; 73 ContentScriptMap& map = iter->second;
74 std::set<UserScript> scripts_to_delete; 74 std::set<UserScriptIDPair> to_delete;
75 for (const UserScript& script : scripts) { 75 for (const UserScript& script : scripts) {
76 auto map_iter = map.find(script.name()); 76 auto map_iter = map.find(script.name());
77 // If a content script has the same name as the new one, remove the old 77 // If a content script has the same name as the new one, remove the old
78 // script first, and insert the new one. 78 // script first, and insert the new one.
79 if (map_iter != map.end()) { 79 if (map_iter != map.end()) {
80 scripts_to_delete.insert(map_iter->second); 80 to_delete.insert(map_iter->second);
81 map.erase(map_iter); 81 map.erase(map_iter);
82 } 82 }
83 map.insert(std::pair<std::string, UserScript>(script.name(), script)); 83 map.insert(std::pair<std::string, UserScriptIDPair>(
84 script.name(), UserScriptIDPair(script.id(), script.host_id())));
84 ids_to_add.insert(script.id()); 85 ids_to_add.insert(script.id());
85 } 86 }
86 87 if (!to_delete.empty())
87 if (!scripts_to_delete.empty()) { 88 master->RemoveScripts(to_delete);
88 master->RemoveScripts(scripts_to_delete);
89 }
90 89
91 // Step 3: makes WebViewContentScriptManager become an observer of the 90 // Step 3: makes WebViewContentScriptManager become an observer of the
92 // |loader| for scripts loaded event. 91 // |loader| for scripts loaded event.
93 UserScriptLoader* loader = master->loader(); 92 UserScriptLoader* loader = master->loader();
94 DCHECK(loader); 93 DCHECK(loader);
95 if (!user_script_loader_observer_.IsObserving(loader)) 94 if (!user_script_loader_observer_.IsObserving(loader))
96 user_script_loader_observer_.Add(loader); 95 user_script_loader_observer_.Add(loader);
97 96
98 // Step 4: adds new scripts to the master. 97 // Step 4: adds new scripts to the master.
99 master->AddScripts(scripts, embedder_process_id, 98 master->AddScripts(scripts, embedder_process_id,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 if (script_map_iter == guest_content_script_map_.end()) 148 if (script_map_iter == guest_content_script_map_.end())
150 return; 149 return;
151 150
152 DeclarativeUserScriptMaster* master = 151 DeclarativeUserScriptMaster* master =
153 DeclarativeUserScriptManager::Get(browser_context_) 152 DeclarativeUserScriptManager::Get(browser_context_)
154 ->GetDeclarativeUserScriptMasterByID(host_id); 153 ->GetDeclarativeUserScriptMasterByID(host_id);
155 CHECK(master); 154 CHECK(master);
156 155
157 // We need to update WebViewRenderState in the IO thread if the guest exists. 156 // We need to update WebViewRenderState in the IO thread if the guest exists.
158 std::set<int> ids_to_delete; 157 std::set<int> ids_to_delete;
159 std::set<UserScript> scripts_to_delete; 158 std::set<UserScriptIDPair> scripts_to_delete;
160 159
161 // Step 1: removes content scripts from |master| and updates 160 // Step 1: removes content scripts from |master| and updates
162 // |guest_content_script_map_|. 161 // |guest_content_script_map_|.
163 std::map<std::string, UserScript>& map = script_map_iter->second; 162 std::map<std::string, UserScriptIDPair>& map = script_map_iter->second;
164 // If the |script_name_list| is empty, all the content scripts added by the 163 // If the |script_name_list| is empty, all the content scripts added by the
165 // guest will be removed; otherwise, removes the scripts in the 164 // guest will be removed; otherwise, removes the scripts in the
166 // |script_name_list|. 165 // |script_name_list|.
167 if (script_name_list.empty()) { 166 if (script_name_list.empty()) {
168 auto it = map.begin(); 167 auto it = map.begin();
169 while (it != map.end()) { 168 while (it != map.end()) {
170 scripts_to_delete.insert(it->second); 169 scripts_to_delete.insert(it->second);
171 ids_to_delete.insert(it->second.id()); 170 ids_to_delete.insert(it->second.id);
172 map.erase(it++); 171 map.erase(it++);
173 } 172 }
174 } else { 173 } else {
175 for (const std::string& name : script_name_list) { 174 for (const std::string& name : script_name_list) {
176 ContentScriptMap::iterator iter = map.find(name); 175 ContentScriptMap::iterator iter = map.find(name);
177 if (iter == map.end()) 176 if (iter == map.end())
178 continue; 177 continue;
179 const UserScript& script = iter->second; 178 const UserScriptIDPair& id_pair = iter->second;
180 ids_to_delete.insert(script.id()); 179 ids_to_delete.insert(id_pair.id);
181 scripts_to_delete.insert(script); 180 scripts_to_delete.insert(id_pair);
182 map.erase(iter); 181 map.erase(iter);
183 } 182 }
184 } 183 }
185 184
186 // Step 2: makes WebViewContentScriptManager become an observer of the 185 // Step 2: makes WebViewContentScriptManager become an observer of the
187 // |loader| for scripts loaded event. 186 // |loader| for scripts loaded event.
188 UserScriptLoader* loader = master->loader(); 187 UserScriptLoader* loader = master->loader();
189 DCHECK(loader); 188 DCHECK(loader);
190 if (!user_script_loader_observer_.IsObserving(loader)) 189 if (!user_script_loader_observer_.IsObserving(loader))
191 user_script_loader_observer_.Add(loader); 190 user_script_loader_observer_.Add(loader);
(...skipping 15 matching lines...) Expand all
207 int embedder_process_id, 206 int embedder_process_id,
208 int view_instance_id) { 207 int view_instance_id) {
209 std::set<int> ids; 208 std::set<int> ids;
210 209
211 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); 210 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id);
212 GuestContentScriptMap::const_iterator iter = 211 GuestContentScriptMap::const_iterator iter =
213 guest_content_script_map_.find(key); 212 guest_content_script_map_.find(key);
214 if (iter == guest_content_script_map_.end()) 213 if (iter == guest_content_script_map_.end())
215 return ids; 214 return ids;
216 const ContentScriptMap& map = iter->second; 215 const ContentScriptMap& map = iter->second;
217 for (const auto& pair : map) 216 for (const auto& id_pair : map)
218 ids.insert(pair.second.id()); 217 ids.insert(id_pair.second.id);
219 218
220 return ids; 219 return ids;
221 } 220 }
222 221
223 void WebViewContentScriptManager::SignalOnScriptsLoaded( 222 void WebViewContentScriptManager::SignalOnScriptsLoaded(
224 const base::Closure& callback) { 223 const base::Closure& callback) {
225 if (!user_script_loader_observer_.IsObservingSources()) { 224 if (!user_script_loader_observer_.IsObservingSources()) {
226 callback.Run(); 225 callback.Run();
227 return; 226 return;
228 } 227 }
(...skipping 13 matching lines...) Expand all
242 241
243 void WebViewContentScriptManager::RunCallbacksIfReady() { 242 void WebViewContentScriptManager::RunCallbacksIfReady() {
244 if (user_script_loader_observer_.IsObservingSources()) 243 if (user_script_loader_observer_.IsObservingSources())
245 return; 244 return;
246 for (auto& callback : pending_scripts_loading_callbacks_) 245 for (auto& callback : pending_scripts_loading_callbacks_)
247 callback.Run(); 246 callback.Run();
248 pending_scripts_loading_callbacks_.clear(); 247 pending_scripts_loading_callbacks_.clear();
249 } 248 }
250 249
251 } // namespace extensions 250 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/guest_view/web_view/web_view_content_script_manager.h ('k') | extensions/browser/user_script_loader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698