diff options
author | patrickriordan177@gmail.com <patrickriordan177@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-07 08:12:10 +0000 |
---|---|---|
committer | patrickriordan177@gmail.com <patrickriordan177@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-07 08:12:10 +0000 |
commit | b28b3e368a1d48e142f9a33b034f242d0e79debe (patch) | |
tree | 04b7ace260a2b88c52dc2375a2362b78720ebe3d | |
parent | 046a9712362dcf891a2634fee98bd9cf20bbabfb (diff) | |
download | chromium_src-b28b3e368a1d48e142f9a33b034f242d0e79debe.zip chromium_src-b28b3e368a1d48e142f9a33b034f242d0e79debe.tar.gz chromium_src-b28b3e368a1d48e142f9a33b034f242d0e79debe.tar.bz2 |
Resubmit "Change RulesRegistryService to use ProfileKeyedAPI."
Original CL: 13825014
It was reverted because tab_helper didn't check for a NULL
RulesRegistryService.
BUG=179951
Review URL: https://chromiumcodereview.appspot.com/16252003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204748 0039d316-1c4b-4281-b951-d872f2087c98
12 files changed, 47 insertions, 61 deletions
diff --git a/chrome/browser/extensions/api/declarative/declarative_api.cc b/chrome/browser/extensions/api/declarative/declarative_api.cc index f0ec23e..4274b20 100644 --- a/chrome/browser/extensions/api/declarative/declarative_api.cc +++ b/chrome/browser/extensions/api/declarative/declarative_api.cc @@ -37,8 +37,7 @@ bool RulesFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); RulesRegistryService* rules_registry_service = - ExtensionSystemFactory::GetForProfile(profile())-> - rules_registry_service(); + RulesRegistryService::Get(profile()); rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); // Raw access to this function is not available to extensions, therefore // there should never be a request for a nonexisting rules registry. diff --git a/chrome/browser/extensions/api/declarative/declarative_apitest.cc b/chrome/browser/extensions/api/declarative/declarative_apitest.cc index b2ce026..e9ba29b 100644 --- a/chrome/browser/extensions/api/declarative/declarative_apitest.cc +++ b/chrome/browser/extensions/api/declarative/declarative_apitest.cc @@ -45,8 +45,7 @@ IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, DeclarativeApi) { // is therefore processed after the UnloadExtension task has been executed. RulesRegistryService* rules_registry_service = - extensions::ExtensionSystemFactory::GetForProfile(browser()->profile())-> - rules_registry_service(); + extensions::RulesRegistryService::Get(browser()->profile()); scoped_refptr<RulesRegistry> rules_registry = rules_registry_service->GetRulesRegistry( extensions::declarative_webrequest_constants::kOnRequest); diff --git a/chrome/browser/extensions/api/declarative/rules_registry_service.cc b/chrome/browser/extensions/api/declarative/rules_registry_service.cc index 94b7135..67a45c4 100644 --- a/chrome/browser/extensions/api/declarative/rules_registry_service.cc +++ b/chrome/browser/extensions/api/declarative/rules_registry_service.cc @@ -5,6 +5,7 @@ #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" #include "base/bind.h" +#include "base/lazy_instance.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "chrome/browser/extensions/api/declarative/initializing_rules_registry.h" @@ -36,7 +37,8 @@ RulesRegistryService::RulesRegistryService(Profile* profile) profile_(profile) { if (profile) { registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, - content::Source<Profile>(profile->GetOriginalProfile())); + content::Source<Profile>(profile->GetOriginalProfile())); + RegisterDefaultRulesRegistries(); } } @@ -78,6 +80,20 @@ void RulesRegistryService::Shutdown() { profile_, scoped_refptr<WebRequestRulesRegistry>(NULL))); } +static base::LazyInstance<ProfileKeyedAPIFactory<RulesRegistryService> > +g_factory = LAZY_INSTANCE_INITIALIZER; + +// static +ProfileKeyedAPIFactory<RulesRegistryService>* +RulesRegistryService::GetFactoryInstance() { + return &g_factory.Get(); +} + +// static +RulesRegistryService* RulesRegistryService::Get(Profile* profile) { + return ProfileKeyedAPIFactory<RulesRegistryService>::GetForProfile(profile); +} + void RulesRegistryService::RegisterRulesRegistry( scoped_refptr<RulesRegistry> rule_registry) { const std::string event_name(rule_registry->event_name()); diff --git a/chrome/browser/extensions/api/declarative/rules_registry_service.h b/chrome/browser/extensions/api/declarative/rules_registry_service.h index 4c846b8..ee8833e 100644 --- a/chrome/browser/extensions/api/declarative/rules_registry_service.h +++ b/chrome/browser/extensions/api/declarative/rules_registry_service.h @@ -13,6 +13,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_vector.h" #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" +#include "chrome/browser/extensions/api/profile_keyed_api_factory.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" @@ -34,14 +35,21 @@ namespace extensions { // This class owns all RulesRegistries implementations of an ExtensionService. // This class lives on the UI thread. -class RulesRegistryService : public content::NotificationObserver { +class RulesRegistryService : public ProfileKeyedAPI, + public content::NotificationObserver { public: explicit RulesRegistryService(Profile* profile); virtual ~RulesRegistryService(); // Unregisters refptrs to concrete RulesRegistries at other objects that were // created by us so that the RulesRegistries can be released. - void Shutdown(); + virtual void Shutdown() OVERRIDE; + + // ProfileKeyedAPI implementation. + static ProfileKeyedAPIFactory<RulesRegistryService>* GetFactoryInstance(); + + // Convenience method to get the RulesRegistryService for a profile. + static RulesRegistryService* Get(Profile* profile); // Registers the default RulesRegistries used in Chromium. void RegisterDefaultRulesRegistries(); @@ -62,6 +70,8 @@ class RulesRegistryService : public content::NotificationObserver { // For testing. void SimulateExtensionUnloaded(const std::string& extension_id); private: + friend class ProfileKeyedAPIFactory<RulesRegistryService>; + // Maps event names to RuleRegistries that handle these events. typedef std::map<std::string, scoped_refptr<RulesRegistry> > RulesRegistryMap; @@ -75,6 +85,13 @@ class RulesRegistryService : public content::NotificationObserver { const content::NotificationSource& source, const content::NotificationDetails& details) OVERRIDE; + // ProfileKeyedAPI implementation. + static const char* service_name() { + return "RulesRegistryService"; + } + static const bool kServiceHasOwnInstanceInIncognito = true; + static const bool kServiceIsNULLWhileTesting = true; + RulesRegistryMap rule_registries_; // We own the parts of the registries which need to run on the UI thread. diff --git a/chrome/browser/extensions/extension_system.cc b/chrome/browser/extensions/extension_system.cc index ed32eb4..0babc26 100644 --- a/chrome/browser/extensions/extension_system.cc +++ b/chrome/browser/extensions/extension_system.cc @@ -11,7 +11,6 @@ #include "base/strings/string_tokenizer.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/cookie_settings.h" -#include "chrome/browser/extensions/api/declarative/rules_registry_service.h" #include "chrome/browser/extensions/blacklist.h" #include "chrome/browser/extensions/component_loader.h" #include "chrome/browser/extensions/event_router.h" @@ -289,8 +288,6 @@ ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile) } ExtensionSystemImpl::~ExtensionSystemImpl() { - if (rules_registry_service_) - rules_registry_service_->Shutdown(); } void ExtensionSystemImpl::Shutdown() { @@ -314,23 +311,9 @@ void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled) { usb_device_resource_manager_.reset( new ApiResourceManager<UsbDeviceResource>(BrowserThread::IO)); - rules_registry_service_.reset(new RulesRegistryService(profile_)); - rules_registry_service_->RegisterDefaultRulesRegistries(); - shared_->Init(extensions_enabled); } -void ExtensionSystemImpl::InitForOTRProfile() { - // Only initialize the RulesRegistryService of the OTR ExtensionSystem if the - // regular ExtensionSystem has been initialized properly, as we depend on it. - // Some ChromeOS browser tests don't initialize the regular ExtensionSystem - // in login-tests. - if (extension_service()) { - rules_registry_service_.reset(new RulesRegistryService(profile_)); - rules_registry_service_->RegisterDefaultRulesRegistries(); - } -} - ExtensionService* ExtensionSystemImpl::extension_service() { return shared_->extension_service(); } @@ -367,10 +350,6 @@ EventRouter* ExtensionSystemImpl::event_router() { return shared_->event_router(); } -RulesRegistryService* ExtensionSystemImpl::rules_registry_service() { - return rules_registry_service_.get(); -} - ApiResourceManager<SerialConnection>* ExtensionSystemImpl::serial_connection_manager() { return serial_connection_manager_.get(); diff --git a/chrome/browser/extensions/extension_system.h b/chrome/browser/extensions/extension_system.h index 28c7b72..055aa2e 100644 --- a/chrome/browser/extensions/extension_system.h +++ b/chrome/browser/extensions/extension_system.h @@ -25,7 +25,6 @@ class Profile; namespace extensions { // Unfortunately, for the ApiResourceManager<> template classes, we don't seem // to be able to forward-declare because of compilation errors on Windows. -class AlarmManager; class Blacklist; class EventRouter; class Extension; @@ -34,9 +33,7 @@ class ExtensionWarningBadgeService; class ExtensionWarningService; class LazyBackgroundTaskQueue; class ManagementPolicy; -class MessageService; class NavigationObserver; -class RulesRegistryService; class StandardManagementPolicyProvider; class StateStore; class UserScriptMaster; @@ -63,8 +60,6 @@ class ExtensionSystem : public BrowserContextKeyedService { // are controlled by |extensions_enabled|. virtual void InitForRegularProfile(bool extensions_enabled) = 0; - virtual void InitForOTRProfile() = 0; - // The ExtensionService is created at startup. virtual ExtensionService* extension_service() = 0; @@ -94,9 +89,6 @@ class ExtensionSystem : public BrowserContextKeyedService { // The EventRouter is created at startup. virtual EventRouter* event_router() = 0; - // The RulesRegistryService is created at startup. - virtual RulesRegistryService* rules_registry_service() = 0; - // The SerialConnection ResourceManager is created at startup. virtual ApiResourceManager<SerialConnection>* serial_connection_manager() = 0; @@ -148,7 +140,6 @@ class ExtensionSystemImpl : public ExtensionSystem { virtual void Shutdown() OVERRIDE; virtual void InitForRegularProfile(bool extensions_enabled) OVERRIDE; - virtual void InitForOTRProfile() OVERRIDE; virtual ExtensionService* extension_service() OVERRIDE; // shared virtual ManagementPolicy* management_policy() OVERRIDE; // shared @@ -160,8 +151,6 @@ class ExtensionSystemImpl : public ExtensionSystem { OVERRIDE; // shared virtual ExtensionInfoMap* info_map() OVERRIDE; // shared virtual EventRouter* event_router() OVERRIDE; // shared - virtual RulesRegistryService* rules_registry_service() - OVERRIDE; // shared virtual ApiResourceManager<SerialConnection>* serial_connection_manager() OVERRIDE; virtual ApiResourceManager<Socket>* socket_manager() OVERRIDE; @@ -251,7 +240,6 @@ class ExtensionSystemImpl : public ExtensionSystem { scoped_ptr<ApiResourceManager<Socket> > socket_manager_; scoped_ptr<ApiResourceManager< UsbDeviceResource> > usb_device_resource_manager_; - scoped_ptr<RulesRegistryService> rules_registry_service_; DISALLOW_COPY_AND_ASSIGN(ExtensionSystemImpl); }; diff --git a/chrome/browser/extensions/tab_helper.cc b/chrome/browser/extensions/tab_helper.cc index 25a9b2c..5c16906 100644 --- a/chrome/browser/extensions/tab_helper.cc +++ b/chrome/browser/extensions/tab_helper.cc @@ -86,10 +86,6 @@ TabHelper::TabHelper(content::WebContents* web_contents) pending_web_app_action_(NONE), script_executor_(new ScriptExecutor(web_contents, &script_execution_observers_)), - rules_registry_service_( - ExtensionSystem::Get( - Profile::FromBrowserContext(web_contents->GetBrowserContext()))-> - rules_registry_service()), image_loader_ptr_factory_(this) { // The ActiveTabPermissionManager requires a session ID; ensure this // WebContents has one. @@ -195,9 +191,10 @@ void TabHelper::DidNavigateMainFrame( const content::LoadCommittedDetails& details, const content::FrameNavigateParams& params) { #if defined(ENABLE_EXTENSIONS) - if (rules_registry_service_) { - rules_registry_service_->content_rules_registry()->DidNavigateMainFrame( - web_contents(), details, params); + if (ExtensionSystem::Get(profile_)->extension_service() && + RulesRegistryService::Get(profile_)) { + RulesRegistryService::Get(profile_)->content_rules_registry()-> + DidNavigateMainFrame(web_contents(), details, params); } #endif // defined(ENABLE_EXTENSIONS) @@ -346,8 +343,9 @@ void TabHelper::OnContentScriptsExecuting( void TabHelper::OnWatchedPageChange( const std::vector<std::string>& css_selectors) { #if defined(ENABLE_EXTENSIONS) - if (rules_registry_service_) { - rules_registry_service_->content_rules_registry()->Apply( + if (ExtensionSystem::Get(profile_)->extension_service() && + RulesRegistryService::Get(profile_)) { + RulesRegistryService::Get(profile_)->content_rules_registry()->Apply( web_contents(), css_selectors); } #endif // defined(ENABLE_EXTENSIONS) diff --git a/chrome/browser/extensions/tab_helper.h b/chrome/browser/extensions/tab_helper.h index c5b6d23..95facc2 100644 --- a/chrome/browser/extensions/tab_helper.h +++ b/chrome/browser/extensions/tab_helper.h @@ -32,7 +32,6 @@ class Image; namespace extensions { class Extension; class LocationBarController; -class RulesRegistryService; class ScriptBadgeController; class ScriptBubbleController; class ScriptExecutor; @@ -254,8 +253,6 @@ class TabHelper : public content::WebContentsObserver, scoped_ptr<ScriptBubbleController> script_bubble_controller_; - RulesRegistryService* rules_registry_service_; - Profile* profile_; // Vend weak pointers that can be invalidated to stop in-progress loads. diff --git a/chrome/browser/extensions/test_extension_system.cc b/chrome/browser/extensions/test_extension_system.cc index a43dfb1..b3ed09f 100644 --- a/chrome/browser/extensions/test_extension_system.cc +++ b/chrome/browser/extensions/test_extension_system.cc @@ -148,10 +148,6 @@ EventRouter* TestExtensionSystem::event_router() { return NULL; } -RulesRegistryService* TestExtensionSystem::rules_registry_service() { - return NULL; -} - ApiResourceManager<SerialConnection>* TestExtensionSystem::serial_connection_manager() { return NULL; diff --git a/chrome/browser/extensions/test_extension_system.h b/chrome/browser/extensions/test_extension_system.h index 2657090..7186e93 100644 --- a/chrome/browser/extensions/test_extension_system.h +++ b/chrome/browser/extensions/test_extension_system.h @@ -51,7 +51,6 @@ class TestExtensionSystem : public ExtensionSystem { void CreateSocketManager(); virtual void InitForRegularProfile(bool extensions_enabled) OVERRIDE {} - virtual void InitForOTRProfile() OVERRIDE {} void SetExtensionService(ExtensionService* service); virtual ExtensionService* extension_service() OVERRIDE; virtual ManagementPolicy* management_policy() OVERRIDE; @@ -63,7 +62,6 @@ class TestExtensionSystem : public ExtensionSystem { virtual ExtensionInfoMap* info_map() OVERRIDE; virtual LazyBackgroundTaskQueue* lazy_background_task_queue() OVERRIDE; virtual EventRouter* event_router() OVERRIDE; - virtual RulesRegistryService* rules_registry_service() OVERRIDE; virtual ApiResourceManager<SerialConnection>* serial_connection_manager() OVERRIDE; virtual ApiResourceManager<Socket>* socket_manager() OVERRIDE; diff --git a/chrome/browser/profiles/off_the_record_profile_impl.cc b/chrome/browser/profiles/off_the_record_profile_impl.cc index 6fd6e94..f7cf724 100644 --- a/chrome/browser/profiles/off_the_record_profile_impl.cc +++ b/chrome/browser/profiles/off_the_record_profile_impl.cc @@ -97,8 +97,6 @@ void OffTheRecordProfileImpl::Init() { BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices( this, false); - extensions::ExtensionSystem::Get(this)->InitForOTRProfile(); - DCHECK_NE(IncognitoModePrefs::DISABLED, IncognitoModePrefs::GetAvailability(profile_->GetPrefs())); diff --git a/chrome/browser/profiles/off_the_record_profile_impl_unittest.cc b/chrome/browser/profiles/off_the_record_profile_impl_unittest.cc index 81e29f5..0b9a9b9 100644 --- a/chrome/browser/profiles/off_the_record_profile_impl_unittest.cc +++ b/chrome/browser/profiles/off_the_record_profile_impl_unittest.cc @@ -146,7 +146,8 @@ TEST_F(OffTheRecordProfileImplTest, GetHostZoomMap) { child_profile->InitHostZoomMap(); BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices( - child_profile.get(), false); + child_profile.get(), + true); // For testing. // Prepare child host zoom map. HostZoomMap* child_zoom_map = |