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

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: Make the API work before the first navigation of guest. Created 5 years, 9 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..4363b441bfc9f7921b09d5ab63406e8d225b1148
--- /dev/null
+++ b/extensions/browser/guest_view/web_view/web_view_content_script_manager.cc
@@ -0,0 +1,116 @@
+// 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 "extensions/browser/declarative_user_script_manager.h"
+#include "extensions/browser/declarative_user_script_master.h"
+#include "extensions/browser/extension_system.h"
+
+namespace {
+using GuestMapKey = std::pair<int, int>;
+using ContentScriptMap = std::map<std::string, extensions::UserScript>;
+using GuestContentScriptMap = std::map<GuestMapKey, ContentScriptMap>;
+
+GuestContentScriptMap guest_content_script_map;
+}
+
+namespace extensions {
+
+WebViewContentScriptManager::WebViewContentScriptManager(
+ content::BrowserContext* browser_context)
+ : browser_context_(browser_context) {
+}
+
+WebViewContentScriptManager::~WebViewContentScriptManager() {
+}
+
+WebViewContentScriptManager* WebViewContentScriptManager::Get(
+ content::BrowserContext* browser_context) {
Fady Samuel 2015/03/27 20:25:36 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Xi Han 2015/03/30 17:50:49 Done.
+ return ExtensionSystem::Get(browser_context)
+ ->web_view_content_script_manager();
Fady Samuel 2015/03/27 20:25:36 Hang this off the BrowserContext instead. See http
Xi Han 2015/03/30 17:50:49 Good idea, done!
+}
+
+void WebViewContentScriptManager::AddContentScripts(
+ int embedder_process_id,
+ int guest_view_instance_id,
+ const HostID& host_id,
+ const std::map<std::string, UserScript>& scripts) {
Fady Samuel 2015/03/27 20:25:36 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Xi Han 2015/03/30 17:50:49 Done.
+ GuestMapKey key =
+ std::pair<int, int>(embedder_process_id, guest_view_instance_id);
+ GuestContentScriptMap::iterator iter = guest_content_script_map.find(key);
+ if (iter == guest_content_script_map.end()) {
+ guest_content_script_map.insert(
+ std::pair<GuestMapKey, ContentScriptMap>(key, scripts));
+ } else {
+ ContentScriptMap& map = iter->second;
+ for (const std::pair<std::string, UserScript>& pair : scripts)
+ map.insert(pair);
+ }
+
+ DeclarativeUserScriptMaster* master =
+ ExtensionSystem::Get(browser_context_)
+ ->declarative_user_script_manager()
+ ->GetDeclarativeUserScriptMasterByID(host_id);
+
+ CHECK(master);
+ for (const std::pair<std::string, UserScript>& pair : scripts)
+ master->AddScript(pair.second);
+}
+
+void WebViewContentScriptManager::RemoveContentScripts(
+ int embedder_process_id,
+ int guest_view_instance_id,
+ std::vector<std::string>* script_name_list) {
Fady Samuel 2015/03/27 20:25:36 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Xi Han 2015/03/30 17:50:49 Done.
+ GuestMapKey key =
+ std::pair<int, int>(embedder_process_id, guest_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 = nullptr;
+ std::vector<std::string> names;
+ std::map<std::string, UserScript>& map = script_map_iter->second;
+
+ if (!script_name_list || script_name_list->empty()) {
+ for (const std::pair<std::string, UserScript>& iter : map)
+ names.push_back(iter.first);
+ } else {
+ names = *script_name_list;
+ }
+
+ for (const std::string& name : names) {
+ ContentScriptMap::iterator iter = map.find(name);
+ if (iter == map.end())
+ continue;
+ const UserScript& script = iter->second;
+ if (!master) {
+ master = ExtensionSystem::Get(browser_context_)
+ ->declarative_user_script_manager()
+ ->GetDeclarativeUserScriptMasterByID(script.host_id());
+ }
+ master->RemoveScript(script);
+ map.erase(iter);
+ }
+}
+
+bool WebViewContentScriptManager::OwnsUserScript(int embedder_process_id,
+ int guest_view_instance_id,
+ int script_id) {
Fady Samuel 2015/03/27 20:25:36 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Xi Han 2015/03/30 17:50:49 Done.
+ GuestMapKey key =
+ std::pair<int, int>(embedder_process_id, guest_view_instance_id);
+ GuestContentScriptMap::const_iterator iter =
+ guest_content_script_map.find(key);
+ if (iter == guest_content_script_map.end())
+ return false;
+ const ContentScriptMap& map = iter->second;
+ for (const auto& pair : map) {
+ if (pair.second.id() == script_id)
+ return true;
+ }
+ return false;
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698