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, | |
|
Fady Samuel
2015/04/08 22:40:04
Take in an embedder_web_contents here. Keep a map
| |
| 43 int view_instance_id, | |
| 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 DCHECK(master); | |
| 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 = std::pair<int, int>(embedder_process_id, view_instance_id); | |
| 58 GuestContentScriptMap::iterator iter = guest_content_script_map_.find(key); | |
| 59 | |
| 60 // If there isn't any content script added for the given guest yet, insert an | |
| 61 // empty map first. | |
| 62 if (iter == guest_content_script_map_.end()) { | |
| 63 iter = guest_content_script_map_.insert( | |
| 64 iter, std::pair<GuestMapKey, ContentScriptMap>( | |
| 65 key, std::map<std::string, UserScript>())); | |
| 66 } | |
| 67 | |
| 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 | |
|
Fady Samuel
2015/04/08 22:40:04
nit: remove
| |
| 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 ids_to_add.insert(pair.second.id()); | |
| 79 master->AddScript(pair.second); | |
| 80 } | |
| 81 | |
| 82 // Update WebViewRenderState in the IO thread. | |
| 83 if (!ids_to_add.empty()) { | |
| 84 content::BrowserThread::PostTask( | |
| 85 content::BrowserThread::IO, FROM_HERE, | |
| 86 base::Bind(&WebViewRendererState::AddContentScriptIDs, | |
| 87 base::Unretained(WebViewRendererState::GetInstance()), | |
| 88 embedder_process_id, view_instance_id, ids_to_add)); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void WebViewContentScriptManager::RemoveContentScripts( | |
| 93 int embedder_process_id, | |
| 94 int view_instance_id, | |
| 95 const HostID& host_id, | |
| 96 const std::vector<std::string>& script_name_list) { | |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 98 | |
| 99 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); | |
| 100 GuestContentScriptMap::iterator script_map_iter = | |
| 101 guest_content_script_map_.find(key); | |
| 102 if (script_map_iter == guest_content_script_map_.end()) | |
| 103 return; | |
| 104 | |
| 105 DeclarativeUserScriptMaster* master = | |
| 106 ExtensionSystem::Get(browser_context_) | |
| 107 ->declarative_user_script_manager() | |
| 108 ->GetDeclarativeUserScriptMasterByID(host_id); | |
| 109 CHECK(master); | |
| 110 | |
| 111 // We need to update WebViewRenderState in the IO thread if the guest exists. | |
| 112 std::set<int> ids_to_delete; | |
| 113 | |
| 114 std::vector<std::string> names_to_delete; | |
| 115 std::map<std::string, UserScript>& map = script_map_iter->second; | |
| 116 // If the |script_name_list| is empty, all the content scripts added by the | |
| 117 // guest will be removed; otherwise, removes the scripts in the | |
| 118 // |script_name_list|. | |
| 119 if (script_name_list.empty()) { | |
| 120 for (const std::pair<std::string, UserScript>& iter : map) | |
| 121 names_to_delete.push_back(iter.first); | |
| 122 } else { | |
| 123 names_to_delete = script_name_list; | |
| 124 } | |
| 125 | |
| 126 for (const std::string& name : names_to_delete) { | |
| 127 ContentScriptMap::iterator iter = map.find(name); | |
| 128 if (iter == map.end()) | |
| 129 continue; | |
| 130 const UserScript& script = iter->second; | |
| 131 ids_to_delete.insert(script.id()); | |
| 132 master->RemoveScript(script); | |
| 133 map.erase(iter); | |
| 134 } | |
| 135 | |
| 136 // Update WebViewRenderState in the IO thread. | |
| 137 if (!ids_to_delete.empty()) { | |
| 138 content::BrowserThread::PostTask( | |
| 139 content::BrowserThread::IO, FROM_HERE, | |
| 140 base::Bind(&WebViewRendererState::RemoveContentScriptIDs, | |
| 141 base::Unretained(WebViewRendererState::GetInstance()), | |
| 142 embedder_process_id, view_instance_id, ids_to_delete)); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 std::set<int> WebViewContentScriptManager::GetContentScriptIDSet( | |
| 147 int embedder_process_id, | |
| 148 int view_instance_id) { | |
| 149 std::set<int> ids; | |
| 150 | |
| 151 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); | |
| 152 GuestContentScriptMap::const_iterator iter = | |
| 153 guest_content_script_map_.find(key); | |
| 154 if (iter == guest_content_script_map_.end()) | |
| 155 return ids; | |
| 156 const ContentScriptMap& map = iter->second; | |
| 157 for (const auto& pair : map) | |
| 158 ids.insert(pair.second.id()); | |
| 159 | |
| 160 return ids; | |
| 161 } | |
| 162 | |
| 163 } // namespace extensions | |
| OLD | NEW |