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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 "extensions/browser/declarative_user_script_manager.h"
8 #include "extensions/browser/declarative_user_script_master.h"
9 #include "extensions/browser/extension_system.h"
10
11 namespace {
12 using GuestMapKey = std::pair<int, int>;
13 using ContentScriptMap = std::map<std::string, extensions::UserScript>;
14 using GuestContentScriptMap = std::map<GuestMapKey, ContentScriptMap>;
15
16 GuestContentScriptMap guest_content_script_map;
17 }
18
19 namespace extensions {
20
21 WebViewContentScriptManager::WebViewContentScriptManager(
22 content::BrowserContext* browser_context)
23 : browser_context_(browser_context) {
24 }
25
26 WebViewContentScriptManager::~WebViewContentScriptManager() {
27 }
28
29 WebViewContentScriptManager* WebViewContentScriptManager::Get(
30 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.
31 return ExtensionSystem::Get(browser_context)
32 ->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!
33 }
34
35 void WebViewContentScriptManager::AddContentScripts(
36 int embedder_process_id,
37 int guest_view_instance_id,
38 const HostID& host_id,
39 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.
40 GuestMapKey key =
41 std::pair<int, int>(embedder_process_id, guest_view_instance_id);
42 GuestContentScriptMap::iterator iter = guest_content_script_map.find(key);
43 if (iter == guest_content_script_map.end()) {
44 guest_content_script_map.insert(
45 std::pair<GuestMapKey, ContentScriptMap>(key, scripts));
46 } else {
47 ContentScriptMap& map = iter->second;
48 for (const std::pair<std::string, UserScript>& pair : scripts)
49 map.insert(pair);
50 }
51
52 DeclarativeUserScriptMaster* master =
53 ExtensionSystem::Get(browser_context_)
54 ->declarative_user_script_manager()
55 ->GetDeclarativeUserScriptMasterByID(host_id);
56
57 CHECK(master);
58 for (const std::pair<std::string, UserScript>& pair : scripts)
59 master->AddScript(pair.second);
60 }
61
62 void WebViewContentScriptManager::RemoveContentScripts(
63 int embedder_process_id,
64 int guest_view_instance_id,
65 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.
66 GuestMapKey key =
67 std::pair<int, int>(embedder_process_id, guest_view_instance_id);
68 GuestContentScriptMap::iterator script_map_iter =
69 guest_content_script_map.find(key);
70 if (script_map_iter == guest_content_script_map.end())
71 return;
72
73 DeclarativeUserScriptMaster* master = nullptr;
74 std::vector<std::string> names;
75 std::map<std::string, UserScript>& map = script_map_iter->second;
76
77 if (!script_name_list || script_name_list->empty()) {
78 for (const std::pair<std::string, UserScript>& iter : map)
79 names.push_back(iter.first);
80 } else {
81 names = *script_name_list;
82 }
83
84 for (const std::string& name : names) {
85 ContentScriptMap::iterator iter = map.find(name);
86 if (iter == map.end())
87 continue;
88 const UserScript& script = iter->second;
89 if (!master) {
90 master = ExtensionSystem::Get(browser_context_)
91 ->declarative_user_script_manager()
92 ->GetDeclarativeUserScriptMasterByID(script.host_id());
93 }
94 master->RemoveScript(script);
95 map.erase(iter);
96 }
97 }
98
99 bool WebViewContentScriptManager::OwnsUserScript(int embedder_process_id,
100 int guest_view_instance_id,
101 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.
102 GuestMapKey key =
103 std::pair<int, int>(embedder_process_id, guest_view_instance_id);
104 GuestContentScriptMap::const_iterator iter =
105 guest_content_script_map.find(key);
106 if (iter == guest_content_script_map.end())
107 return false;
108 const ContentScriptMap& map = iter->second;
109 for (const auto& pair : map) {
110 if (pair.second.id() == script_id)
111 return true;
112 }
113 return false;
114 }
115
116 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698