Skip to content

Commit

Permalink
Remove ExtensionPrefFactory's dependency on Profile.
Browse files Browse the repository at this point in the history
Move the code that checks whether the command-line flag or
pref to disable extensions is set into
ExtensionsBrowserClient, and make ExtensionPrefFactory work
with a BrowserContext instead of requiring a Profile.

BUG=313284

Review URL: https://codereview.chromium.org/89253002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238243 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
derat@chromium.org committed Dec 3, 2013
1 parent 55ca88f commit 367d9b1
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 31 deletions.
1 change: 1 addition & 0 deletions apps/app_restore_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension_set.h"
#include "extensions/common/extension.h"

Expand Down
1 change: 1 addition & 0 deletions apps/app_restore_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "chrome/browser/extensions/api/file_system/file_system_api.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/extension.h"
Expand Down
1 change: 1 addition & 0 deletions apps/saved_files_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/notification_service.h"
#include "extensions/common/permissions/api_permission.h"
#include "extensions/common/permissions/permission_set.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/file_system/file_system_api.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_service.h"
Expand Down
13 changes: 13 additions & 0 deletions chrome/browser/extensions/chrome_extensions_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ bool ChromeExtensionsBrowserClient::IsShuttingDown() {
return g_browser_process->IsShuttingDown();
}

bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
const CommandLine& command_line,
content::BrowserContext* context) {
Profile* profile = static_cast<Profile*>(context);
return command_line.HasSwitch(switches::kDisableExtensions) ||
profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions);
}

bool ChromeExtensionsBrowserClient::IsValidContext(
content::BrowserContext* context) {
Profile* profile = static_cast<Profile*>(context);
Expand Down Expand Up @@ -62,6 +70,11 @@ content::BrowserContext* ChromeExtensionsBrowserClient::GetOriginalContext(
return static_cast<Profile*>(context)->GetOriginalProfile();
}

PrefService* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
content::BrowserContext* context) {
return static_cast<Profile*>(context)->GetPrefs();
}

bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
content::BrowserContext* context) const {
Profile* profile = static_cast<Profile*>(context);
Expand Down
6 changes: 6 additions & 0 deletions chrome/browser/extensions/chrome_extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "chrome/browser/extensions/chrome_notification_observer.h"
#include "extensions/browser/extensions_browser_client.h"

class CommandLine;

namespace content {
class BrowserContext;
}
Expand All @@ -31,6 +33,8 @@ class ChromeExtensionsBrowserClient : public ExtensionsBrowserClient {

// BrowserClient overrides:
virtual bool IsShuttingDown() OVERRIDE;
virtual bool AreExtensionsDisabled(const CommandLine& command_line,
content::BrowserContext* context) OVERRIDE;
virtual bool IsValidContext(content::BrowserContext* context) OVERRIDE;
virtual bool IsSameContext(content::BrowserContext* first,
content::BrowserContext* second) OVERRIDE;
Expand All @@ -40,6 +44,8 @@ class ChromeExtensionsBrowserClient : public ExtensionsBrowserClient {
content::BrowserContext* context) OVERRIDE;
virtual content::BrowserContext* GetOriginalContext(
content::BrowserContext* context) OVERRIDE;
virtual PrefService* GetPrefServiceForContext(
content::BrowserContext* context) OVERRIDE;
virtual bool DeferLoadingBackgroundHosts(
content::BrowserContext* context) const OVERRIDE;
virtual bool DidVersionUpdate(content::BrowserContext* context) OVERRIDE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "chrome/browser/extensions/extension_pref_value_map_factory.h"

#include "chrome/browser/extensions/extension_pref_value_map.h"
#include "chrome/browser/profiles/profile.h"
#include "components/browser_context_keyed_service/browser_context_dependency_manager.h"

ExtensionPrefValueMapFactory::ExtensionPrefValueMapFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"

class ExtensionPrefValueMap;
class Profile;

// The usual factory boilerplate for ExtensionPrefValueMap.
class ExtensionPrefValueMapFactory : public BrowserContextKeyedServiceFactory {
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/extensions/extension_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ ExtensionPrefs::~ExtensionPrefs() {
}

// static
ExtensionPrefs* ExtensionPrefs::Get(Profile* profile) {
return ExtensionPrefsFactory::GetInstance()->GetForProfile(profile);
ExtensionPrefs* ExtensionPrefs::Get(content::BrowserContext* context) {
return ExtensionPrefsFactory::GetInstance()->GetForBrowserContext(context);
}

static base::FilePath::StringType MakePathRelative(const base::FilePath& parent,
Expand Down
9 changes: 6 additions & 3 deletions chrome/browser/extensions/extension_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

class ExtensionPrefValueMap;
class PrefService;
class Profile;

namespace content {
class BrowserContext;
}

namespace user_prefs {
class PrefRegistrySyncable;
Expand Down Expand Up @@ -158,8 +161,8 @@ class ExtensionPrefs : public ExtensionScopedPrefs,

virtual ~ExtensionPrefs();

// Convenience function to get the ExtensionPrefs for a Profile.
static ExtensionPrefs* Get(Profile* profile);
// Convenience function to get the ExtensionPrefs for a BrowserContext.
static ExtensionPrefs* Get(content::BrowserContext* context);

// Returns all installed extensions from extension preferences provided by
// |pref_service|. This is exposed for ProtectedPrefsWatcher because it needs
Expand Down
27 changes: 12 additions & 15 deletions chrome/browser/extensions/extension_prefs_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
#include "chrome/browser/extensions/extension_prefs_factory.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/constants.h"

namespace extensions {

// static
ExtensionPrefs* ExtensionPrefsFactory::GetForProfile(Profile* profile) {
ExtensionPrefs* ExtensionPrefsFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<ExtensionPrefs*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
GetInstance()->GetServiceForBrowserContext(context, true));
}

// static
Expand All @@ -46,21 +45,19 @@ ExtensionPrefsFactory::~ExtensionPrefsFactory() {

BrowserContextKeyedService* ExtensionPrefsFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
bool extensions_disabled =
profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions) ||
CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableExtensions);
ExtensionsBrowserClient* client = ExtensionsBrowserClient::Get();
return ExtensionPrefs::Create(
profile->GetPrefs(),
profile->GetPath().AppendASCII(extensions::kInstallDirectoryName),
ExtensionPrefValueMapFactory::GetForBrowserContext(profile),
ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
extensions_disabled);
client->GetPrefServiceForContext(context),
context->GetPath().AppendASCII(extensions::kInstallDirectoryName),
ExtensionPrefValueMapFactory::GetForBrowserContext(context),
client->CreateAppSorting().Pass(),
client->AreExtensionsDisabled(
*CommandLine::ForCurrentProcess(), context));
}

content::BrowserContext* ExtensionPrefsFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
}

} // namespace extensions
4 changes: 2 additions & 2 deletions chrome/browser/extensions/extension_prefs_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ExtensionPrefs;

class ExtensionPrefsFactory : public BrowserContextKeyedServiceFactory {
public:
static ExtensionPrefs* GetForProfile(Profile* profile);
static ExtensionPrefs* GetForBrowserContext(content::BrowserContext* context);

static ExtensionPrefsFactory* GetInstance();

Expand All @@ -29,7 +29,7 @@ class ExtensionPrefsFactory : public BrowserContextKeyedServiceFactory {
virtual ~ExtensionPrefsFactory();

virtual BrowserContextKeyedService* BuildServiceInstanceFor(
content::BrowserContext* profile) const OVERRIDE;
content::BrowserContext* context) const OVERRIDE;
virtual content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const OVERRIDE;
};
Expand Down
5 changes: 2 additions & 3 deletions chrome/browser/extensions/extension_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,9 @@ ExtensionService::ExtensionService(Profile* profile,
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

// Figure out if extension installation should be enabled.
if (command_line->HasSwitch(switches::kDisableExtensions) ||
profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
*command_line, profile))
extensions_enabled_ = false;
}

registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
content::NotificationService::AllBrowserContextsAndSources());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ BrowserContextKeyedService*
Profile* profile = Profile::FromBrowserContext(context);
return new ExtensionSyncService(
profile,
extensions::ExtensionPrefsFactory::GetForProfile(profile),
extensions::ExtensionPrefsFactory::GetForBrowserContext(profile),
extensions::ExtensionSystemFactory::GetForProfile(profile)->
extension_service());
}
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/extensions/extension_toolbar_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class Browser;
class ExtensionService;
class PrefService;
class Profile;

// Model for the browser actions toolbar.
class ExtensionToolbarModel : public content::NotificationObserver,
Expand Down
5 changes: 2 additions & 3 deletions chrome/browser/extensions/extension_toolbar_model_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ ExtensionToolbarModelFactory::~ExtensionToolbarModelFactory() {}
BrowserContextKeyedService*
ExtensionToolbarModelFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
return new ExtensionToolbarModel(
profile,
extensions::ExtensionPrefsFactory::GetForProfile(profile));
Profile::FromBrowserContext(context),
extensions::ExtensionPrefsFactory::GetForBrowserContext(context));
}

content::BrowserContext* ExtensionToolbarModelFactory::GetBrowserContextToUse(
Expand Down
12 changes: 12 additions & 0 deletions extensions/browser/extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "base/memory/scoped_ptr.h"

class CommandLine;
class PrefService;

namespace content {
class BrowserContext;
}
Expand All @@ -28,6 +31,11 @@ class ExtensionsBrowserClient {
// Returns true if the embedder has started shutting down.
virtual bool IsShuttingDown() = 0;

// Returns true if extensions have been disabled (e.g. via a command-line flag
// or preference).
virtual bool AreExtensionsDisabled(const CommandLine& command_line,
content::BrowserContext* context) = 0;

// Returns true if the |context| is known to the embedder.
virtual bool IsValidContext(content::BrowserContext* context) = 0;

Expand All @@ -51,6 +59,10 @@ class ExtensionsBrowserClient {
virtual content::BrowserContext* GetOriginalContext(
content::BrowserContext* context) = 0;

// Returns the PrefService associated with |context|.
virtual PrefService* GetPrefServiceForContext(
content::BrowserContext* context) = 0;

// Returns true if loading background pages should be deferred.
virtual bool DeferLoadingBackgroundHosts(
content::BrowserContext* context) const = 0;
Expand Down

0 comments on commit 367d9b1

Please sign in to comment.