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

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: Clean up declarative_content.json and store RequestContentScript data in action object Created 6 years, 5 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 <map>
8 #include <string>
9 #include <utility>
10
7 #include "base/bind.h" 11 #include "base/bind.h"
8 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
9 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" 17 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
18 #include "chrome/browser/extensions/api/declarative_content/content_constants.h"
13 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h" 19 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h"
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" 20 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" 21 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h"
16 #include "chrome/browser/extensions/api/web_request/web_request_api.h" 22 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
17 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/notification_details.h" 25 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h" 27 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/render_process_host.h" 28 #include "content/public/browser/render_process_host.h"
(...skipping 13 matching lines...) Expand all
36 profile, webview_key, web_request_rules_registry); 42 profile, webview_key, web_request_rules_registry);
37 } 43 }
38 44
39 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) { 45 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) {
40 return webview_key.embedder_process_id && webview_key.webview_instance_id; 46 return webview_key.embedder_process_id && webview_key.webview_instance_id;
41 } 47 }
42 48
43 } // namespace 49 } // namespace
44 50
45 RulesRegistryService::RulesRegistryService(content::BrowserContext* context) 51 RulesRegistryService::RulesRegistryService(content::BrowserContext* context)
46 : content_rules_registry_(NULL), 52 : extension_registry_observer_(this),
47 extension_registry_observer_(this),
48 profile_(Profile::FromBrowserContext(context)) { 53 profile_(Profile::FromBrowserContext(context)) {
49 if (profile_) { 54 if (profile_) {
50 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); 55 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
51 registrar_.Add( 56 registrar_.Add(
52 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 57 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
53 content::NotificationService::AllBrowserContextsAndSources()); 58 content::NotificationService::AllBrowserContextsAndSources());
54 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0)); 59 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
55 } 60 }
56 } 61 }
57 62
(...skipping 24 matching lines...) Expand all
82 webview_key)); 87 webview_key));
83 88
84 RegisterRulesRegistry(web_request_rules_registry); 89 RegisterRulesRegistry(web_request_rules_registry);
85 content::BrowserThread::PostTask( 90 content::BrowserThread::PostTask(
86 content::BrowserThread::IO, FROM_HERE, 91 content::BrowserThread::IO, FROM_HERE,
87 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, 92 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
88 profile_, webview_key, web_request_rules_registry)); 93 profile_, webview_key, web_request_rules_registry));
89 94
90 // Only create a ContentRulesRegistry for regular pages and not webviews. 95 // Only create a ContentRulesRegistry for regular pages and not webviews.
91 if (!IsWebView(webview_key)) { 96 if (!IsWebView(webview_key)) {
92 RulesCacheDelegate* content_rules_cache_delegate = 97 namespace dcc = declarative_content_constants;
93 new RulesCacheDelegate(false /*log_storage_init_delay*/); 98 const std::string content_registry_ids[] = {
94 cache_delegates_.push_back(content_rules_cache_delegate); 99 dcc::kOnPageChanged,
95 scoped_refptr<ContentRulesRegistry> content_rules_registry( 100 };
96 new ContentRulesRegistry(profile_, content_rules_cache_delegate)); 101
97 RegisterRulesRegistry(content_rules_registry); 102 for (const std::string* it = content_registry_ids;
98 content_rules_registry_ = content_rules_registry.get(); 103 it < content_registry_ids + arraysize(content_registry_ids); ++it) {
104 RulesCacheDelegate* content_rules_cache_delegate =
105 new RulesCacheDelegate(false /*log_storage_init_delay*/);
106 cache_delegates_.push_back(content_rules_cache_delegate);
107 scoped_refptr<ContentRulesRegistry> content_rules_registry(
108 new ContentRulesRegistry(profile_,
109 *it, // event_name.
110 content_rules_cache_delegate));
111 RegisterRulesRegistry(content_rules_registry);
112 content_rules_registry_map_.insert(
113 std::make_pair(*it, // event_name.
114 content_rules_registry.get()));
115 }
99 } 116 }
100 } 117 }
101 118
102 void RulesRegistryService::Shutdown() { 119 void RulesRegistryService::Shutdown() {
103 // Release the references to all registries. This would happen soon during 120 // Release the references to all registries. This would happen soon during
104 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to 121 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to
105 // be the last to reference the WebRequestRulesRegistry objects, so that 122 // be the last to reference the WebRequestRulesRegistry objects, so that
106 // the posted task below causes their destruction on the IO thread, not on UI 123 // the posted task below causes their destruction on the IO thread, not on UI
107 // where the destruction of |*this| takes place. 124 // where the destruction of |*this| takes place.
108 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed. 125 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 const std::string& event_name) { 159 const std::string& event_name) {
143 EnsureDefaultRulesRegistriesRegistered(webview_key); 160 EnsureDefaultRulesRegistriesRegistered(webview_key);
144 161
145 RulesRegistryKey key(event_name, webview_key); 162 RulesRegistryKey key(event_name, webview_key);
146 RulesRegistryMap::const_iterator i = rule_registries_.find(key); 163 RulesRegistryMap::const_iterator i = rule_registries_.find(key);
147 if (i == rule_registries_.end()) 164 if (i == rule_registries_.end())
148 return scoped_refptr<RulesRegistry>(); 165 return scoped_refptr<RulesRegistry>();
149 return i->second; 166 return i->second;
150 } 167 }
151 168
169 ContentRulesRegistry* RulesRegistryService::GetContentRulesRegistry(
170 const std::string& event_name) {
171 std::map<std::string, ContentRulesRegistry*>::iterator it =
172 content_rules_registry_map_.find(event_name);
173 if (it == content_rules_registry_map_.end())
174 return NULL;
175
176 return content_rules_registry_map_[event_name];
Jeffrey Yasskin 2014/07/15 17:12:59 If you've already run map::find, you can just retu
Mark Dittmer 2014/07/15 18:32:42 Done.
177 }
178
179
152 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) { 180 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) {
153 DCHECK_NE(0, process_id); 181 DCHECK_NE(0, process_id);
154 182
155 std::set<RulesRegistryKey> registries_to_delete; 183 std::set<RulesRegistryKey> registries_to_delete;
156 for (RulesRegistryMap::iterator it = rule_registries_.begin(); 184 for (RulesRegistryMap::iterator it = rule_registries_.begin();
157 it != rule_registries_.end(); ++it) { 185 it != rule_registries_.end(); ++it) {
158 const RulesRegistryKey& key = it->first; 186 const RulesRegistryKey& key = it->first;
159 const WebViewKey& webview_key = key.webview_key; 187 const WebViewKey& webview_key = key.webview_key;
160 int embedder_process_id = webview_key.embedder_process_id; 188 int embedder_process_id = webview_key.embedder_process_id;
161 // |process_id| will always be non-zero. 189 // |process_id| will always be non-zero.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 const content::NotificationSource& source, 251 const content::NotificationSource& source,
224 const content::NotificationDetails& details) { 252 const content::NotificationDetails& details) {
225 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type); 253 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type);
226 254
227 content::RenderProcessHost* process = 255 content::RenderProcessHost* process =
228 content::Source<content::RenderProcessHost>(source).ptr(); 256 content::Source<content::RenderProcessHost>(source).ptr();
229 RemoveWebViewRulesRegistries(process->GetID()); 257 RemoveWebViewRulesRegistries(process->GetID());
230 } 258 }
231 259
232 } // namespace extensions 260 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698