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

Side by Side Diff: chrome/browser/extensions/api/declarative/rules_registry_service.cc

Issue 344433003: Prepare declarativeContent API for new script injection feature. Added Javascript types and functio… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Get rid of ContentScript Javascript type; only RequestContentScript is needed Created 6 years, 6 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" 5 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
6 6
7 #include <utility>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" 14 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
15 #include "chrome/browser/extensions/api/declarative_content/content_constants.h"
13 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h" 16 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h"
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" 17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" 18 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h"
16 #include "chrome/browser/extensions/api/web_request/web_request_api.h" 19 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
17 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/notification_details.h" 22 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h" 24 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/render_process_host.h" 25 #include "content/public/browser/render_process_host.h"
23 #include "extensions/browser/extension_registry.h" 26 #include "extensions/browser/extension_registry.h"
24 #include "extensions/common/extension.h" 27 #include "extensions/common/extension.h"
28 #include "extensions/common/watched_pages_recipient.h"
25 29
26 namespace extensions { 30 namespace extensions {
27 31
28 namespace { 32 namespace {
29 33
30 // Registers |web_request_rules_registry| on the IO thread. 34 // Registers |web_request_rules_registry| on the IO thread.
31 void RegisterToExtensionWebRequestEventRouterOnIO( 35 void RegisterToExtensionWebRequestEventRouterOnIO(
32 void* profile, 36 void* profile,
33 const RulesRegistryService::WebViewKey& webview_key, 37 const RulesRegistryService::WebViewKey& webview_key,
34 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { 38 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) {
35 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry( 39 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry(
36 profile, webview_key, web_request_rules_registry); 40 profile, webview_key, web_request_rules_registry);
37 } 41 }
38 42
39 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) { 43 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) {
40 return webview_key.embedder_process_id && webview_key.webview_instance_id; 44 return webview_key.embedder_process_id && webview_key.webview_instance_id;
41 } 45 }
42 46
43 } // namespace 47 } // namespace
44 48
45 RulesRegistryService::RulesRegistryService(content::BrowserContext* context) 49 RulesRegistryService::RulesRegistryService(content::BrowserContext* context)
46 : content_rules_registry_(NULL), 50 : extension_registry_observer_(this),
47 extension_registry_observer_(this),
48 profile_(Profile::FromBrowserContext(context)) { 51 profile_(Profile::FromBrowserContext(context)) {
49 if (profile_) { 52 if (profile_) {
50 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); 53 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
51 registrar_.Add( 54 registrar_.Add(
52 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 55 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
53 content::NotificationService::AllBrowserContextsAndSources()); 56 content::NotificationService::AllBrowserContextsAndSources());
54 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0)); 57 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
55 } 58 }
56 } 59 }
57 60
(...skipping 23 matching lines...) Expand all
81 web_request_cache_delegate, 84 web_request_cache_delegate,
82 webview_key)); 85 webview_key));
83 86
84 RegisterRulesRegistry(web_request_rules_registry); 87 RegisterRulesRegistry(web_request_rules_registry);
85 content::BrowserThread::PostTask( 88 content::BrowserThread::PostTask(
86 content::BrowserThread::IO, FROM_HERE, 89 content::BrowserThread::IO, FROM_HERE,
87 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, 90 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
88 profile_, webview_key, web_request_rules_registry)); 91 profile_, webview_key, web_request_rules_registry));
89 92
90 #if defined(ENABLE_EXTENSIONS) 93 #if defined(ENABLE_EXTENSIONS)
91 // Only create a ContentRulesRegistry for regular pages and not webviews. 94 // Only create content rules registries for regular pages and not webviews.
92 if (!IsWebView(webview_key)) { 95 if (!IsWebView(webview_key)) {
93 RulesCacheDelegate* content_rules_cache_delegate = 96 namespace dcc = declarative_content_constants;
94 new RulesCacheDelegate(false /*log_storage_init_delay*/); 97 typedef std::vector< std::pair<std::string, WatchedPagesRecipient> >
95 cache_delegates_.push_back(content_rules_cache_delegate); 98 ContentRegistryIDList;
96 scoped_refptr<ContentRulesRegistry> content_rules_registry( 99 ContentRegistryIDList content_registry_ids;
97 new ContentRulesRegistry(profile_, content_rules_cache_delegate)); 100
98 RegisterRulesRegistry(content_rules_registry); 101 // onPageChanged supports browser-side ShowPageAction. Browser-side rules
99 content_rules_registry_ = content_rules_registry.get(); 102 // registry must be notified.
103 content_registry_ids.push_back(
104 std::make_pair(dcc::kOnPageChanged, CONTENT_RULES_REGISTRY));
105
106 // onDocument- Start/End/Idle supports renderer-side RequestContentScript.
107 // Renderer-side user script manager must be notified.
108 content_registry_ids.push_back(
109 std::make_pair(dcc::kOnDocumentStart, USER_SCRIPT_MANAGER));
110 content_registry_ids.push_back(
111 std::make_pair(dcc::kOnDocumentEnd, USER_SCRIPT_MANAGER));
112 content_registry_ids.push_back(
113 std::make_pair(dcc::kOnDocumentIdle, USER_SCRIPT_MANAGER));
114
115 for (ContentRegistryIDList::const_iterator it =
116 content_registry_ids.begin();
117 it != content_registry_ids.end(); ++it) {
118 RulesCacheDelegate* content_rules_cache_delegate =
119 new RulesCacheDelegate(false /*log_storage_init_delay*/);
120 cache_delegates_.push_back(content_rules_cache_delegate);
121 scoped_refptr<ContentRulesRegistry> content_rules_registry(
122 new ContentRulesRegistry(profile_,
123 it->first, // event_name.
124 it->second, // watched_pages_recipient.
125 content_rules_cache_delegate));
126 RegisterRulesRegistry(content_rules_registry);
127 content_rules_registry_map_.insert(
128 std::make_pair(it->first, // event_name.
129 content_rules_registry.get()));
130 }
100 } 131 }
101 #endif // defined(ENABLE_EXTENSIONS) 132 #endif // defined(ENABLE_EXTENSIONS)
102 } 133 }
103 134
104 void RulesRegistryService::Shutdown() { 135 void RulesRegistryService::Shutdown() {
105 // Release the references to all registries. This would happen soon during 136 // Release the references to all registries. This would happen soon during
106 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to 137 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to
107 // be the last to reference the WebRequestRulesRegistry objects, so that 138 // be the last to reference the WebRequestRulesRegistry objects, so that
108 // the posted task below causes their destruction on the IO thread, not on UI 139 // the posted task below causes their destruction on the IO thread, not on UI
109 // where the destruction of |*this| takes place. 140 // where the destruction of |*this| takes place.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 const std::string& event_name) { 175 const std::string& event_name) {
145 EnsureDefaultRulesRegistriesRegistered(webview_key); 176 EnsureDefaultRulesRegistriesRegistered(webview_key);
146 177
147 RulesRegistryKey key(event_name, webview_key); 178 RulesRegistryKey key(event_name, webview_key);
148 RulesRegistryMap::const_iterator i = rule_registries_.find(key); 179 RulesRegistryMap::const_iterator i = rule_registries_.find(key);
149 if (i == rule_registries_.end()) 180 if (i == rule_registries_.end())
150 return scoped_refptr<RulesRegistry>(); 181 return scoped_refptr<RulesRegistry>();
151 return i->second; 182 return i->second;
152 } 183 }
153 184
185 ContentRulesRegistry* RulesRegistryService::GetContentRulesRegistry(
186 const std::string& event_name) {
187 ContentRulesRegistryMap::iterator it =
188 content_rules_registry_map_.find(event_name);
189 if (it == content_rules_registry_map_.end())
190 return NULL;
191
192 return content_rules_registry_map_[event_name];
193 }
194
195
154 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) { 196 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) {
155 DCHECK_NE(0, process_id); 197 DCHECK_NE(0, process_id);
156 198
157 std::set<RulesRegistryKey> registries_to_delete; 199 std::set<RulesRegistryKey> registries_to_delete;
158 for (RulesRegistryMap::iterator it = rule_registries_.begin(); 200 for (RulesRegistryMap::iterator it = rule_registries_.begin();
159 it != rule_registries_.end(); ++it) { 201 it != rule_registries_.end(); ++it) {
160 const RulesRegistryKey& key = it->first; 202 const RulesRegistryKey& key = it->first;
161 const WebViewKey& webview_key = key.webview_key; 203 const WebViewKey& webview_key = key.webview_key;
162 int embedder_process_id = webview_key.embedder_process_id; 204 int embedder_process_id = webview_key.embedder_process_id;
163 // |process_id| will always be non-zero. 205 // |process_id| will always be non-zero.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 const content::NotificationSource& source, 267 const content::NotificationSource& source,
226 const content::NotificationDetails& details) { 268 const content::NotificationDetails& details) {
227 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type); 269 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type);
228 270
229 content::RenderProcessHost* process = 271 content::RenderProcessHost* process =
230 content::Source<content::RenderProcessHost>(source).ptr(); 272 content::Source<content::RenderProcessHost>(source).ptr();
231 RemoveWebViewRulesRegistries(process->GetID()); 273 RemoveWebViewRulesRegistries(process->GetID());
232 } 274 }
233 275
234 } // namespace extensions 276 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698