| OLD | NEW |
| 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 "extensions/browser/api/declarative/rules_registry_service.h" | 5 #include "extensions/browser/api/declarative/rules_registry_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "extensions/browser/api/declarative/rules_cache_delegate.h" | 15 #include "extensions/browser/api/declarative/rules_cache_delegate.h" |
| 16 #include "extensions/browser/api/declarative_content/content_rules_registry.h" | 16 #include "extensions/browser/api/declarative_content/content_rules_registry.h" |
| 17 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 17 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
| 18 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr
y.h" | 18 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr
y.h" |
| 19 #include "extensions/browser/api/extensions_api_client.h" | 19 #include "extensions/browser/api/extensions_api_client.h" |
| 20 #include "extensions/browser/api/web_request/web_request_api.h" | 20 #include "extensions/browser/api/web_request/web_request_api.h" |
| 21 #include "extensions/browser/extension_registry.h" | 21 #include "extensions/browser/extension_registry.h" |
| 22 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 23 #include "extensions/common/features/feature_channel.h" |
| 24 #include "extensions/common/features/feature_provider.h" |
| 23 | 25 |
| 24 namespace extensions { | 26 namespace extensions { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 // Registers |web_request_rules_registry| on the IO thread. | 30 // Registers |web_request_rules_registry| on the IO thread. |
| 29 void RegisterToExtensionWebRequestEventRouterOnIO( | 31 void RegisterToExtensionWebRequestEventRouterOnIO( |
| 30 content::BrowserContext* browser_context, | 32 content::BrowserContext* browser_context, |
| 31 int rules_registry_id, | 33 int rules_registry_id, |
| 32 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { | 34 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int rules_registry_id) { | 69 int rules_registry_id) { |
| 68 if (!browser_context_) | 70 if (!browser_context_) |
| 69 return; | 71 return; |
| 70 RulesRegistryKey key(declarative_webrequest_constants::kOnRequest, | 72 RulesRegistryKey key(declarative_webrequest_constants::kOnRequest, |
| 71 rules_registry_id); | 73 rules_registry_id); |
| 72 // If we can find the key in the |rule_registries_| then we have already | 74 // If we can find the key in the |rule_registries_| then we have already |
| 73 // installed the default registries. | 75 // installed the default registries. |
| 74 if (ContainsKey(rule_registries_, key)) | 76 if (ContainsKey(rule_registries_, key)) |
| 75 return; | 77 return; |
| 76 | 78 |
| 77 // Only cache rules for regular pages. | 79 // Create a web request rules registry if declarative web request is enabled |
| 78 RulesCacheDelegate* web_request_cache_delegate = NULL; | 80 // on the current channel. |
| 79 if (rules_registry_id == kDefaultRulesRegistryID) { | 81 const Feature* declarative_web_request = |
| 80 // Create a RulesCacheDelegate. | 82 FeatureProvider::GetAPIFeature("declarativeWebRequest"); |
| 81 web_request_cache_delegate = | 83 if (declarative_web_request->IsAvailableToChannel(GetCurrentChannel()) |
| 82 new RulesCacheDelegate(true /*log_storage_init_delay*/); | 84 .is_available()) { |
| 83 cache_delegates_.push_back(base::WrapUnique(web_request_cache_delegate)); | 85 // Only cache rules for regular pages. |
| 86 RulesCacheDelegate* web_request_cache_delegate = nullptr; |
| 87 if (rules_registry_id == kDefaultRulesRegistryID) { |
| 88 // Create a RulesCacheDelegate. |
| 89 web_request_cache_delegate = |
| 90 new RulesCacheDelegate(true /*log_storage_init_delay*/); |
| 91 cache_delegates_.push_back(base::WrapUnique(web_request_cache_delegate)); |
| 92 } |
| 93 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry( |
| 94 new WebRequestRulesRegistry( |
| 95 browser_context_, web_request_cache_delegate, rules_registry_id)); |
| 96 |
| 97 RegisterRulesRegistry(web_request_rules_registry); |
| 98 content::BrowserThread::PostTask( |
| 99 content::BrowserThread::IO, FROM_HERE, |
| 100 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, |
| 101 browser_context_, rules_registry_id, |
| 102 web_request_rules_registry)); |
| 84 } | 103 } |
| 85 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry( | |
| 86 new WebRequestRulesRegistry(browser_context_, web_request_cache_delegate, | |
| 87 rules_registry_id)); | |
| 88 | |
| 89 RegisterRulesRegistry(web_request_rules_registry); | |
| 90 content::BrowserThread::PostTask( | |
| 91 content::BrowserThread::IO, FROM_HERE, | |
| 92 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, | |
| 93 browser_context_, rules_registry_id, | |
| 94 web_request_rules_registry)); | |
| 95 | 104 |
| 96 // Only create a ContentRulesRegistry for regular pages. | 105 // Only create a ContentRulesRegistry for regular pages. |
| 97 if (rules_registry_id == kDefaultRulesRegistryID) { | 106 if (rules_registry_id == kDefaultRulesRegistryID) { |
| 98 RulesCacheDelegate* content_rules_cache_delegate = | 107 RulesCacheDelegate* content_rules_cache_delegate = |
| 99 new RulesCacheDelegate(false /*log_storage_init_delay*/); | 108 new RulesCacheDelegate(false /*log_storage_init_delay*/); |
| 100 cache_delegates_.push_back(base::WrapUnique(content_rules_cache_delegate)); | 109 cache_delegates_.push_back(base::WrapUnique(content_rules_cache_delegate)); |
| 101 scoped_refptr<ContentRulesRegistry> content_rules_registry = | 110 scoped_refptr<ContentRulesRegistry> content_rules_registry = |
| 102 ExtensionsAPIClient::Get()->CreateContentRulesRegistry( | 111 ExtensionsAPIClient::Get()->CreateContentRulesRegistry( |
| 103 browser_context_, content_rules_cache_delegate); | 112 browser_context_, content_rules_cache_delegate); |
| 104 if (content_rules_registry.get() != nullptr) { | 113 if (content_rules_registry.get() != nullptr) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 229 } |
| 221 | 230 |
| 222 void RulesRegistryService::OnExtensionUninstalled( | 231 void RulesRegistryService::OnExtensionUninstalled( |
| 223 content::BrowserContext* browser_context, | 232 content::BrowserContext* browser_context, |
| 224 const Extension* extension, | 233 const Extension* extension, |
| 225 extensions::UninstallReason reason) { | 234 extensions::UninstallReason reason) { |
| 226 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension); | 235 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension); |
| 227 } | 236 } |
| 228 | 237 |
| 229 } // namespace extensions | 238 } // namespace extensions |
| OLD | NEW |