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..2229ae0fe802dbd560af8ae9336c45377e09a42f |
--- /dev/null |
+++ b/extensions/browser/guest_view/web_view/web_view_content_script_manager.cc |
@@ -0,0 +1,273 @@ |
+// 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 "base/lazy_instance.h" |
+#include "base/memory/linked_ptr.h" |
+#include "content/public/browser/browser_context.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/navigation_details.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_observer.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/guest_view_manager.h" |
+#include "extensions/browser/guest_view/web_view/web_view_constants.h" |
+#include "extensions/browser/guest_view/web_view/web_view_guest.h" |
+#include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+bool RemoveContentScriptsForGuest(content::BrowserContext* browser_context, |
+ HostID host_id, |
+ content::WebContents* embedder_web_contents, |
+ content::WebContents* guest_web_contents) { |
+ auto guest = extensions::WebViewGuest::FromWebContents(guest_web_contents); |
+ if (!guest) |
+ return false; |
+ |
+ extensions::WebViewContentScriptManager* manager = |
+ extensions::WebViewContentScriptManager::Get(browser_context); |
+ if (!manager) |
+ return false; |
+ |
+ manager->RemoveContentScripts(embedder_web_contents, |
+ guest->view_instance_id(), host_id, |
+ std::vector<std::string>()); |
+ return true; |
Fady Samuel
2015/04/10 16:16:04
I think you should return false here too to allow
Xi Han
2015/04/10 18:27:07
Done.
|
+} |
+} |
+ |
+namespace extensions { |
+ |
+// This observer ensures that the content scripts added by the guest are removed |
+// when its embedder goes away. |
+class WebViewContentScriptManager::OwnerWebContentsObserver |
+ : public content::WebContentsObserver { |
+ public: |
+ OwnerWebContentsObserver(content::WebContents* embedder_web_contents, |
+ const HostID& host_id, |
+ WebViewContentScriptManager* manager) |
+ : WebContentsObserver(embedder_web_contents), |
+ host_id_(host_id), |
+ web_view_content_script_manager_(manager) {} |
+ ~OwnerWebContentsObserver() override {} |
+ |
+ // WebContentsObserver: |
+ void WebContentsDestroyed() override { |
+ // If the embedder is destroyed then remove all the content scripts of the |
+ // guest. |
+ RemoveContentScripts(); |
+ } |
+ void DidNavigateMainFrame( |
+ const content::LoadCommittedDetails& details, |
+ const content::FrameNavigateParams& params) override { |
+ // If the embedder navigates to a different page then destroy the guest. |
+ if (details.is_navigation_to_different_page()) |
+ RemoveContentScripts(); |
+ } |
+ void RenderProcessGone(base::TerminationStatus status) override { |
+ // If the embedder crashes, then destroy the guest. |
+ RemoveContentScripts(); |
+ } |
+ |
+ private: |
+ void RemoveContentScripts() { |
+ web_view_content_script_manager_->RemoveContentScripts(web_contents(), |
+ host_id_); |
+ } |
+ |
+ HostID host_id_; |
+ WebViewContentScriptManager* web_view_content_script_manager_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(OwnerWebContentsObserver); |
+}; |
+ |
+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( |
+ content::WebContents* embedder_web_contents, |
+ int view_instance_id, |
+ const HostID& host_id, |
+ const std::map<std::string, UserScript>& scripts) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(embedder_web_contents); |
+ |
+ 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; |
+ |
+ int embedder_process_id = |
+ embedder_web_contents->GetRenderProcessHost()->GetID(); |
+ GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); |
+ GuestContentScriptMap::iterator iter = guest_content_script_map_.find(key); |
+ |
+ // Step 1: finds the entry in guest_content_script_map_ by the given |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>())); |
+ } |
+ |
+ // Step 2: updates the guest_content_script_map_, and adds content scripts to |
+ // the master. |
+ 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); |
Devlin
2015/04/10 16:04:32
What happens when a WebUI has two webviews, and ad
Fady Samuel
2015/04/10 16:16:04
This API doesn't allow two webviews to share a con
Xi Han
2015/04/10 18:27:07
We will create two different user scripts for them
|
+ map.erase(map_iter); |
+ } |
+ map.insert(pair); |
+ ids_to_add.insert(pair.second.id()); |
+ master->AddScript(pair.second); |
+ } |
+ |
+ // Step 3: creates owner web contents observer for the given |
+ // |embedder_web_contents| if it doesn't exist. |
+ auto observers_map_iter = |
+ owner_web_contents_observer_map_.find(embedder_web_contents); |
+ if (observers_map_iter == owner_web_contents_observer_map_.end()) { |
+ linked_ptr<OwnerWebContentsObserver> observer( |
+ new OwnerWebContentsObserver(embedder_web_contents, host_id, this)); |
+ owner_web_contents_observer_map_[embedder_web_contents] = observer; |
+ } |
+ |
+ // Step 4: updates 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( |
+ content::WebContents* embedder_web_contents, |
+ int view_instance_id, |
+ const HostID& host_id, |
+ const std::vector<std::string>& script_name_list) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ int embedder_process_id = |
+ embedder_web_contents->GetRenderProcessHost()->GetID(); |
+ 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; |
+ |
+ // Step 1: removed content scripts from |master| and updates |
+ // |guest_content_script_map_| |
+ 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); |
+ } |
+ |
+ // Step 2: 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)); |
+ } |
+} |
+ |
+void WebViewContentScriptManager::RemoveContentScripts( |
+ content::WebContents* embedder_web_contents, |
+ const HostID& host_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ // Step 1: removes content scripts of all the guests embedded. |
+ auto guest_view_manager = |
+ GuestViewManager::FromBrowserContext(browser_context_); |
+ guest_view_manager->ForEachGuest( |
+ embedder_web_contents, |
+ base::Bind(&RemoveContentScriptsForGuest, browser_context_, host_id, |
+ embedder_web_contents)); |
+ |
+ // Step 2: removes the observer of the given |embedder_web_contents|. |
+ owner_web_contents_observer_map_.erase(embedder_web_contents); |
+} |
+ |
+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 |