Change RulesRegistryService to use ProfileKeyedAPI.

BUG=179951

Review URL: https://chromiumcodereview.appspot.com/13825014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@202751 0039d316-1c4b-4281-b951-d872f2087c98
parent d413efc3
......@@ -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.
......
......@@ -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);
......
......@@ -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"
......@@ -35,7 +36,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();
}
}
......@@ -70,6 +72,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());
......
......@@ -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.
......
......@@ -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"
......@@ -288,8 +287,6 @@ ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
}
ExtensionSystemImpl::~ExtensionSystemImpl() {
if (rules_registry_service_)
rules_registry_service_->Shutdown();
}
void ExtensionSystemImpl::Shutdown() {
......@@ -313,23 +310,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();
}
......@@ -366,10 +349,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();
......
......@@ -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);
};
......
......@@ -85,10 +85,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.
......@@ -194,9 +190,9 @@ 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_)->content_rules_registry()->
DidNavigateMainFrame(web_contents(), details, params);
}
#endif // defined(ENABLE_EXTENSIONS)
......@@ -345,8 +341,8 @@ 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_)->content_rules_registry()->Apply(
web_contents(), css_selectors);
}
#endif // defined(ENABLE_EXTENSIONS)
......
......@@ -254,8 +254,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.
......
......@@ -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;
......
......@@ -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;
......
......@@ -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()));
......
......@@ -146,7 +146,7 @@ TEST_F(OffTheRecordProfileImplTest, GetHostZoomMap) {
child_profile->InitHostZoomMap();
BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
child_profile.get(), false);
child_profile.get(), true);
// Prepare child host zoom map.
HostZoomMap* child_zoom_map =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment