Chromium Code Reviews| 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 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" | |
| 14 | |
| 15 using content::BrowserThread; | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 WebViewContentScriptManager::WebViewContentScriptManager( | |
| 20 content::BrowserContext* browser_context) | |
| 21 : browser_context_(browser_context) { | |
| 22 } | |
| 23 | |
| 24 WebViewContentScriptManager::~WebViewContentScriptManager() { | |
| 25 } | |
| 26 | |
| 27 WebViewContentScriptManager* WebViewContentScriptManager::Get( | |
| 28 content::BrowserContext* browser_context) { | |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 30 WebViewContentScriptManager* manager = | |
| 31 static_cast<WebViewContentScriptManager*>(browser_context->GetUserData( | |
| 32 webview::kWebViewContentScriptManagerKeyName)); | |
| 33 if (!manager) { | |
| 34 manager = new WebViewContentScriptManager(browser_context); | |
| 35 browser_context->SetUserData(webview::kWebViewContentScriptManagerKeyName, | |
| 36 manager); | |
| 37 } | |
| 38 return manager; | |
| 39 } | |
| 40 | |
| 41 void WebViewContentScriptManager::AddContentScripts( | |
| 42 int embedder_process_id, | |
| 43 int guest_view_instance_id, | |
|
Fady Samuel
2015/03/31 23:40:12
nit: view_instance_id
Xi Han
2015/04/01 22:14:41
Done.
| |
| 44 const HostID& host_id, | |
| 45 const std::map<std::string, UserScript>& scripts) { | |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 47 | |
| 48 DeclarativeUserScriptMaster* master = | |
| 49 ExtensionSystem::Get(browser_context_) | |
| 50 ->declarative_user_script_manager() | |
| 51 ->GetDeclarativeUserScriptMasterByID(host_id); | |
| 52 CHECK(master); | |
|
Fady Samuel
2015/03/31 23:40:12
Why is this a CHECK? Maybe DCHECK?
Xi Han
2015/04/01 22:14:41
Done.
| |
| 53 | |
| 54 // We need to update WebViewRenderState in the IO thread if the guest exists. | |
| 55 std::set<int> ids_to_add; | |
| 56 | |
| 57 GuestMapKey key = | |
| 58 std::pair<int, int>(embedder_process_id, guest_view_instance_id); | |
|
Fady Samuel
2015/03/31 23:40:12
Call this the view_instance_id to match usage else
Xi Han
2015/04/01 22:14:41
Done.
| |
| 59 GuestContentScriptMap::iterator iter = guest_content_script_map_.find(key); | |
| 60 | |
| 61 // If there isn't any content script added for the given guest yet, insert the | |
| 62 // |key, scripts| to the |guest_content_script_map_| directly; otherwise, | |
| 63 // insert the |scripts| to the existing content script map of the guest. | |
| 64 if (iter == guest_content_script_map_.end()) { | |
|
Fady Samuel
2015/03/31 23:40:12
This code is a bit difficult to read. How about cr
Xi Han
2015/04/01 22:14:41
Some changes are made to simply the for loop here,
| |
| 65 guest_content_script_map_.insert( | |
| 66 std::pair<GuestMapKey, ContentScriptMap>(key, scripts)); | |
| 67 } else { | |
| 68 ContentScriptMap& map = iter->second; | |
| 69 for (const std::pair<std::string, UserScript>& pair : scripts) { | |
| 70 auto map_iter = map.find(pair.first); | |
| 71 // If a content script has the same name as the new one, removes the old | |
| 72 // script first, and insert the new one. | |
| 73 if (map_iter != map.end()) { | |
| 74 master->RemoveScript(map_iter->second); | |
| 75 map.erase(map_iter); | |
| 76 } | |
| 77 map.insert(pair); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 for (const std::pair<std::string, UserScript>& pair : scripts) { | |
| 82 ids_to_add.insert(pair.second.id()); | |
| 83 master->AddScript(pair.second); | |
| 84 } | |
| 85 | |
| 86 // Update WebViewRenderState in the IO thread. | |
| 87 if (!ids_to_add.empty()) { | |
| 88 content::BrowserThread::PostTask( | |
| 89 content::BrowserThread::IO, FROM_HERE, | |
| 90 base::Bind(&WebViewRendererState::AddContentScriptIDs, | |
| 91 base::Unretained(WebViewRendererState::GetInstance()), | |
| 92 embedder_process_id, guest_view_instance_id, ids_to_add)); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void WebViewContentScriptManager::RemoveContentScripts( | |
| 97 int embedder_process_id, | |
| 98 int guest_view_instance_id, | |
| 99 const HostID& host_id, | |
| 100 std::vector<std::string>* script_name_list) { | |
| 101 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 102 | |
| 103 GuestMapKey key = | |
| 104 std::pair<int, int>(embedder_process_id, guest_view_instance_id); | |
| 105 GuestContentScriptMap::iterator script_map_iter = | |
| 106 guest_content_script_map_.find(key); | |
| 107 if (script_map_iter == guest_content_script_map_.end()) | |
| 108 return; | |
| 109 | |
| 110 DeclarativeUserScriptMaster* master = | |
| 111 ExtensionSystem::Get(browser_context_) | |
| 112 ->declarative_user_script_manager() | |
| 113 ->GetDeclarativeUserScriptMasterByID(host_id); | |
| 114 CHECK(master); | |
| 115 | |
| 116 // We need to update WebViewRenderState in the IO thread if the guest exists. | |
| 117 std::set<int> ids_to_delete; | |
| 118 | |
| 119 std::vector<std::string> names_to_delete; | |
| 120 std::map<std::string, UserScript>& map = script_map_iter->second; | |
| 121 // If the |script_name_list| is empty, all the content scripts added by the | |
| 122 // guest will be removed; otherwise, removes the scripts in the | |
| 123 // |script_name_list|. | |
| 124 if (!script_name_list || script_name_list->empty()) { | |
| 125 for (const std::pair<std::string, UserScript>& iter : map) | |
| 126 names_to_delete.push_back(iter.first); | |
| 127 } else { | |
| 128 names_to_delete = *script_name_list; | |
| 129 } | |
| 130 | |
| 131 for (const std::string& name : names_to_delete) { | |
| 132 ContentScriptMap::iterator iter = map.find(name); | |
| 133 if (iter == map.end()) | |
| 134 continue; | |
| 135 const UserScript& script = iter->second; | |
| 136 ids_to_delete.insert(script.id()); | |
| 137 master->RemoveScript(script); | |
| 138 map.erase(iter); | |
| 139 } | |
| 140 | |
| 141 // Update WebViewRenderState in the IO thread. | |
| 142 if (!ids_to_delete.empty()) { | |
| 143 content::BrowserThread::PostTask( | |
| 144 content::BrowserThread::IO, FROM_HERE, | |
| 145 base::Bind(&WebViewRendererState::RemoveContentScriptIDs, | |
| 146 base::Unretained(WebViewRendererState::GetInstance()), | |
| 147 embedder_process_id, guest_view_instance_id, ids_to_delete)); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 std::set<int> WebViewContentScriptManager::GetContentScriptIdSet( | |
|
Fady Samuel
2015/03/31 23:40:12
nit: GetContentScriptIDSet
Xi Han
2015/04/01 22:14:41
Done.
| |
| 152 int embedder_process_id, | |
| 153 int guest_view_instance_id) { | |
| 154 std::set<int> ids; | |
| 155 | |
| 156 GuestMapKey key = | |
| 157 std::pair<int, int>(embedder_process_id, guest_view_instance_id); | |
| 158 GuestContentScriptMap::const_iterator iter = | |
| 159 guest_content_script_map_.find(key); | |
| 160 if (iter == guest_content_script_map_.end()) | |
| 161 return ids; | |
| 162 const ContentScriptMap& map = iter->second; | |
| 163 for (const auto& pair : map) | |
| 164 ids.insert(pair.second.id()); | |
| 165 | |
| 166 return ids; | |
| 167 } | |
| 168 | |
| 169 } // namespace extensions | |
| OLD | NEW |