| OLD | NEW |
| 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 Loading... |
| 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 UserScriptList& scripts) { | 49 std::unique_ptr<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<UserScriptIDPair> to_delete; | 74 std::set<UserScriptIDPair> to_delete; |
| 75 for (const UserScript& script : scripts) { | 75 for (const std::unique_ptr<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 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, UserScriptIDPair>( | 83 map.insert(std::pair<std::string, UserScriptIDPair>( |
| 84 script.name(), UserScriptIDPair(script.id(), script.host_id()))); | 84 script->name(), UserScriptIDPair(script->id(), script->host_id()))); |
| 85 ids_to_add.insert(script.id()); | 85 ids_to_add.insert(script->id()); |
| 86 } | 86 } |
| 87 |
| 87 if (!to_delete.empty()) | 88 if (!to_delete.empty()) |
| 88 master->RemoveScripts(to_delete); | 89 master->RemoveScripts(to_delete); |
| 89 | 90 |
| 90 // Step 3: makes WebViewContentScriptManager become an observer of the | 91 // Step 3: makes WebViewContentScriptManager become an observer of the |
| 91 // |loader| for scripts loaded event. | 92 // |loader| for scripts loaded event. |
| 92 UserScriptLoader* loader = master->loader(); | 93 UserScriptLoader* loader = master->loader(); |
| 93 DCHECK(loader); | 94 DCHECK(loader); |
| 94 if (!user_script_loader_observer_.IsObserving(loader)) | 95 if (!user_script_loader_observer_.IsObserving(loader)) |
| 95 user_script_loader_observer_.Add(loader); | 96 user_script_loader_observer_.Add(loader); |
| 96 | 97 |
| 97 // Step 4: adds new scripts to the master. | 98 // Step 4: adds new scripts to the master. |
| 98 master->AddScripts(scripts, embedder_process_id, | 99 master->AddScripts(std::move(scripts), embedder_process_id, |
| 99 render_frame_host->GetRoutingID()); | 100 render_frame_host->GetRoutingID()); |
| 100 | 101 |
| 101 // Step 5: creates an entry in |webview_host_id_map_| for the given | 102 // Step 5: creates an entry in |webview_host_id_map_| for the given |
| 102 // |embedder_process_id| and |view_instance_id| if it doesn't exist. | 103 // |embedder_process_id| and |view_instance_id| if it doesn't exist. |
| 103 auto host_it = webview_host_id_map_.find(key); | 104 auto host_it = webview_host_id_map_.find(key); |
| 104 if (host_it == webview_host_id_map_.end()) | 105 if (host_it == webview_host_id_map_.end()) |
| 105 webview_host_id_map_.insert(std::make_pair(key, host_id)); | 106 webview_host_id_map_.insert(std::make_pair(key, host_id)); |
| 106 | 107 |
| 107 // Step 6: updates WebViewRenderState in the IO thread. | 108 // Step 6: updates WebViewRenderState in the IO thread. |
| 108 // It is safe to use base::Unretained(WebViewRendererState::GetInstance()) | 109 // It is safe to use base::Unretained(WebViewRendererState::GetInstance()) |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 242 |
| 242 void WebViewContentScriptManager::RunCallbacksIfReady() { | 243 void WebViewContentScriptManager::RunCallbacksIfReady() { |
| 243 if (user_script_loader_observer_.IsObservingSources()) | 244 if (user_script_loader_observer_.IsObservingSources()) |
| 244 return; | 245 return; |
| 245 for (auto& callback : pending_scripts_loading_callbacks_) | 246 for (auto& callback : pending_scripts_loading_callbacks_) |
| 246 callback.Run(); | 247 callback.Run(); |
| 247 pending_scripts_loading_callbacks_.clear(); | 248 pending_scripts_loading_callbacks_.clear(); |
| 248 } | 249 } |
| 249 | 250 |
| 250 } // namespace extensions | 251 } // namespace extensions |
| OLD | NEW |