Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1135)

Unified Diff: extensions/browser/guest_view/web_view/web_view_content_script_manager.cc

Issue 959413003: Implement <webview>.addContentScript/removeContentScript API [1] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Delvin's comments. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..d1965b6d1af3456df9fcf59598089d9fccceb684
--- /dev/null
+++ b/extensions/browser/guest_view/web_view/web_view_content_script_manager.cc
@@ -0,0 +1,264 @@
+// 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 extensions {
+
+// This observer ensures that the content scripts added by the guest are removed
+// when its embedder goes away.
+// The OwnerWebContentsObserver object will be destroyed when the embedder web
+// contents it observed is gone.
+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();
+ }
+
+ void add_view_instance_id(int view_instance_id) {
+ view_instance_ids_.push_back(view_instance_id);
+ }
+
+ private:
+ void RemoveContentScripts() {
+ DCHECK(web_view_content_script_manager_);
+
+ // Step 1: removes content scripts of all the guests embedded.
+ for (int view_instance_id : view_instance_ids_) {
+ web_view_content_script_manager_->RemoveContentScripts(
+ web_contents(), view_instance_id, host_id_,
+ std::vector<std::string>());
+ }
+ // Step 2: removes this observer.
+ // This object can be deleted after this line.
+ web_view_content_script_manager_->RemoveObserver(web_contents());
+ }
+
+ HostID host_id_;
+ std::vector<int> view_instance_ids_;
Devlin 2015/04/14 23:04:57 This should be a set.
Xi Han 2015/04/15 19:43:55 Done.
+ 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::set<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, ContentScriptMap()));
+ }
+
+ // Step 2: updates the guest_content_script_map_.
+ ContentScriptMap& map = iter->second;
+ for (const UserScript& script : scripts) {
+ auto map_iter = map.find(script.name());
+ // If a content script has the same name as the new one, removes the old
Devlin 2015/04/14 23:04:57 s/removes/remove
Xi Han 2015/04/15 19:43:55 Done.
+ // script first, and insert the new one.
+ if (map_iter != map.end()) {
+ master->RemoveScript(map_iter->second);
+ map.erase(map_iter);
+ }
+ map.insert(std::pair<std::string, UserScript>(script.name(), script));
+ ids_to_add.insert(script.id());
+ }
+
+ // Step 3: adds new scripts to the master.
+ master->AddScripts(scripts);
+
+ // Step 4: creates owner web contents observer for the given
+ // |embedder_web_contents| if it doesn't exist.
+ if (owner_web_contents_observer_map_.find(embedder_web_contents) ==
+ owner_web_contents_observer_map_.end()) {
+ linked_ptr<OwnerWebContentsObserver> observer(
+ new OwnerWebContentsObserver(embedder_web_contents, host_id, this));
+ observer->add_view_instance_id(view_instance_id);
Devlin 2015/04/14 23:04:57 Your if guard on line 158 means each observer will
Xi Han 2015/04/15 19:43:55 Great catch, it is a bug. Fixed.
+ owner_web_contents_observer_map_[embedder_web_contents] = observer;
+ }
+
+ // Step 5: updates WebViewRenderState in the IO thread.
+ // It is safe to use base::Unretained(WebViewRendererState::GetInstance())
+ // since WebViewRendererState::GetInstance() always returns a Singleton of
+ // WebViewRendererState.
+ 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: removes 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;
+ }
+
+ std::set<UserScript> scripts_to_delete;
+ 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());
+ scripts_to_delete.insert(script);
+ map.erase(iter);
+ }
+
+ // Step 2: removes content scripts from master.
+ master->RemoveScripts(scripts_to_delete);
+
+ // Step 3: updates 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::RemoveObserver(
+ content::WebContents* 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

Powered by Google App Engine
This is Rietveld 408576698