Index: extensions/browser/guest_view/web_view/web_view_content_script_manager.cc |
diff --git a/extensions/browser/guest_view/web_view/web_view_content_script_manager.cc b/extensions/browser/guest_view/web_view/web_view_content_script_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..40de75c79944c7716a843347b4f63e59fac5b1d4 |
--- /dev/null |
+++ b/extensions/browser/guest_view/web_view/web_view_content_script_manager.cc |
@@ -0,0 +1,163 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "extensions/browser/guest_view/web_view/web_view_content_script_manager.h" |
+ |
+#include "content/public/browser/browser_context.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "extensions/browser/declarative_user_script_manager.h" |
+#include "extensions/browser/declarative_user_script_master.h" |
+#include "extensions/browser/extension_system.h" |
+#include "extensions/browser/guest_view/web_view/web_view_constants.h" |
+#include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace extensions { |
+ |
+WebViewContentScriptManager::WebViewContentScriptManager( |
+ content::BrowserContext* browser_context) |
+ : browser_context_(browser_context) { |
+} |
+ |
+WebViewContentScriptManager::~WebViewContentScriptManager() { |
+} |
+ |
+WebViewContentScriptManager* WebViewContentScriptManager::Get( |
+ content::BrowserContext* browser_context) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ WebViewContentScriptManager* manager = |
+ static_cast<WebViewContentScriptManager*>(browser_context->GetUserData( |
+ webview::kWebViewContentScriptManagerKeyName)); |
+ if (!manager) { |
+ manager = new WebViewContentScriptManager(browser_context); |
+ browser_context->SetUserData(webview::kWebViewContentScriptManagerKeyName, |
+ manager); |
+ } |
+ return manager; |
+} |
+ |
+void WebViewContentScriptManager::AddContentScripts( |
+ int embedder_process_id, |
+ int view_instance_id, |
+ const HostID& host_id, |
+ const std::map<std::string, UserScript>& scripts) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ DeclarativeUserScriptMaster* master = |
+ ExtensionSystem::Get(browser_context_) |
+ ->declarative_user_script_manager() |
+ ->GetDeclarativeUserScriptMasterByID(host_id); |
+ DCHECK(master); |
+ |
+ // We need to update WebViewRenderState in the IO thread if the guest exists. |
+ std::set<int> ids_to_add; |
+ |
+ GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); |
+ GuestContentScriptMap::iterator iter = guest_content_script_map_.find(key); |
+ |
+ // If there isn't any content script added for the given guest yet, insert an |
+ // empty map first. |
+ if (iter == guest_content_script_map_.end()) { |
+ iter = guest_content_script_map_.insert( |
+ iter, std::pair<GuestMapKey, ContentScriptMap>( |
+ key, std::map<std::string, UserScript>())); |
+ } |
+ |
+ ContentScriptMap& map = iter->second; |
+ for (const std::pair<std::string, UserScript>& pair : scripts) { |
+ auto map_iter = map.find(pair.first); |
+ // If a content script has the same name as the new one, removes the old |
+ // script first, and insert the new one. |
+ if (map_iter != map.end()) { |
+ master->RemoveScript(map_iter->second); |
+ map.erase(map_iter); |
+ } |
+ map.insert(pair); |
+ ids_to_add.insert(pair.second.id()); |
+ master->AddScript(pair.second); |
+ } |
+ |
+ // Update WebViewRenderState in the IO thread. |
+ if (!ids_to_add.empty()) { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&WebViewRendererState::AddContentScriptIDs, |
+ base::Unretained(WebViewRendererState::GetInstance()), |
+ embedder_process_id, view_instance_id, ids_to_add)); |
+ } |
+} |
+ |
+void WebViewContentScriptManager::RemoveContentScripts( |
+ int embedder_process_id, |
+ int view_instance_id, |
+ const HostID& host_id, |
+ const std::vector<std::string>& script_name_list) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); |
+ GuestContentScriptMap::iterator script_map_iter = |
+ guest_content_script_map_.find(key); |
+ if (script_map_iter == guest_content_script_map_.end()) |
+ return; |
+ |
+ DeclarativeUserScriptMaster* master = |
+ ExtensionSystem::Get(browser_context_) |
+ ->declarative_user_script_manager() |
+ ->GetDeclarativeUserScriptMasterByID(host_id); |
+ CHECK(master); |
+ |
+ // We need to update WebViewRenderState in the IO thread if the guest exists. |
+ std::set<int> ids_to_delete; |
+ |
+ std::vector<std::string> names_to_delete; |
+ std::map<std::string, UserScript>& map = script_map_iter->second; |
+ // If the |script_name_list| is empty, all the content scripts added by the |
+ // guest will be removed; otherwise, removes the scripts in the |
+ // |script_name_list|. |
+ if (script_name_list.empty()) { |
+ for (const std::pair<std::string, UserScript>& iter : map) |
+ names_to_delete.push_back(iter.first); |
+ } else { |
+ names_to_delete = script_name_list; |
+ } |
+ |
+ for (const std::string& name : names_to_delete) { |
+ ContentScriptMap::iterator iter = map.find(name); |
+ if (iter == map.end()) |
+ continue; |
+ const UserScript& script = iter->second; |
+ ids_to_delete.insert(script.id()); |
+ master->RemoveScript(script); |
+ map.erase(iter); |
+ } |
+ |
+ // Update WebViewRenderState in the IO thread. |
+ if (!ids_to_delete.empty()) { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&WebViewRendererState::RemoveContentScriptIDs, |
+ base::Unretained(WebViewRendererState::GetInstance()), |
+ embedder_process_id, view_instance_id, ids_to_delete)); |
+ } |
+} |
+ |
+std::set<int> WebViewContentScriptManager::GetContentScriptIDSet( |
+ int embedder_process_id, |
+ int view_instance_id) { |
+ std::set<int> ids; |
+ |
+ GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); |
+ GuestContentScriptMap::const_iterator iter = |
+ guest_content_script_map_.find(key); |
+ if (iter == guest_content_script_map_.end()) |
+ return ids; |
+ const ContentScriptMap& map = iter->second; |
+ for (const auto& pair : map) |
+ ids.insert(pair.second.id()); |
+ |
+ return ids; |
+} |
+ |
+} // namespace extensions |