Skip to content

Commit

Permalink
Cleanup many APIs to use ProfileKeyedAPI.
Browse files Browse the repository at this point in the history
This allows extra boilerplate BrowserContextKeyedServiceFactory code to be deleted.

In this CL: ActivityLog, ExtensionSyncEventObserver, RuntimeAPI, DeveloperPrivateAPI, BluetoothAPI, AutotestPrivateAPI.

BUG=179951
TBR=erg@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@253928 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
yoz@chromium.org committed Feb 27, 2014
1 parent f8e422d commit 3242279
Show file tree
Hide file tree
Showing 31 changed files with 230 additions and 609 deletions.
49 changes: 17 additions & 32 deletions chrome/browser/extensions/activity_log/activity_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/command_line.h"
#include "base/json/json_string_value_serializer.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
Expand All @@ -29,7 +30,6 @@
#include "chrome/common/chrome_constants.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_thread.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_system.h"
Expand Down Expand Up @@ -336,48 +336,27 @@ void ExtractUrls(scoped_refptr<Action> action, Profile* profile) {

namespace extensions {

// ActivityLogFactory
// SET THINGS UP. --------------------------------------------------------------

ActivityLogFactory* ActivityLogFactory::GetInstance() {
return Singleton<ActivityLogFactory>::get();
}
static base::LazyInstance<ProfileKeyedAPIFactory<ActivityLog> > g_factory =
LAZY_INSTANCE_INITIALIZER;

BrowserContextKeyedService* ActivityLogFactory::BuildServiceInstanceFor(
content::BrowserContext* profile) const {
return new ActivityLog(static_cast<Profile*>(profile));
}

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

ActivityLogFactory::ActivityLogFactory()
: BrowserContextKeyedServiceFactory(
"ActivityLog",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
DependsOn(InstallTrackerFactory::GetInstance());
ProfileKeyedAPIFactory<ActivityLog>* ActivityLog::GetFactoryInstance() {
return g_factory.Pointer();
}

// static
ActivityLog* ActivityLog::GetInstance(content::BrowserContext* context) {
return ActivityLogFactory::GetForBrowserContext(context);
return ActivityLog::GetFactoryInstance()->GetForProfile(
Profile::FromBrowserContext(context));
}

ActivityLogFactory::~ActivityLogFactory() {
}

// ActivityLog

// SET THINGS UP. --------------------------------------------------------------

// Use GetInstance instead of directly creating an ActivityLog.
ActivityLog::ActivityLog(Profile* profile)
ActivityLog::ActivityLog(content::BrowserContext* context)
: database_policy_(NULL),
database_policy_type_(ActivityLogPolicy::POLICY_INVALID),
uma_policy_(NULL),
profile_(profile),
profile_(Profile::FromBrowserContext(context)),
db_enabled_(false),
testing_mode_(false),
has_threads_(true),
Expand Down Expand Up @@ -419,7 +398,7 @@ ActivityLog::ActivityLog(Profile* profile)
// checks. However, UmaPolicy can't even compile on Android because it uses
// BrowserList and related classes that aren't compiled for Android.
#if !defined(OS_ANDROID)
if (!profile->IsOffTheRecord())
if (!profile_->IsOffTheRecord())
uma_policy_ = new UmaPolicy(profile_);
#endif

Expand Down Expand Up @@ -716,4 +695,10 @@ void ActivityLog::DeleteDatabase() {
database_policy_->DeleteDatabase();
}

template <>
void ProfileKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies() {
DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
DependsOn(InstallTrackerFactory::GetInstance());
}

} // namespace extensions
50 changes: 17 additions & 33 deletions chrome/browser/extensions/activity_log/activity_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
#include <vector>

#include "base/callback.h"
#include "base/memory/singleton.h"
#include "base/observer_list_threadsafe.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread.h"
#include "chrome/browser/extensions/activity_log/activity_actions.h"
#include "chrome/browser/extensions/activity_log/activity_log_policy.h"
#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
#include "chrome/browser/extensions/install_observer.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/common/extensions/dom_action_types.h"
#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
#include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
#include "extensions/browser/api_activity_monitor.h"

class Profile;
Expand All @@ -39,7 +37,9 @@ class InstallTracker;

// A utility for tracing interesting activity for each extension.
// It writes to an ActivityDatabase on a separate thread to record the activity.
class ActivityLog : public BrowserContextKeyedService,
// Each profile has different extensions, so we keep a different database for
// each profile.
class ActivityLog : public ProfileKeyedAPI,
public ApiActivityMonitor,
public TabHelper::ScriptExecutionObserver,
public InstallObserver {
Expand All @@ -51,8 +51,10 @@ class ActivityLog : public BrowserContextKeyedService,
virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0;
};

// ActivityLog is a singleton, so don't instantiate it with the constructor;
// use GetInstance instead.
static ProfileKeyedAPIFactory<ActivityLog>* GetFactoryInstance();

// ActivityLog is a BrowserContextKeyedService, so don't instantiate it with
// the constructor; use GetInstance instead.
static ActivityLog* GetInstance(content::BrowserContext* context);

// Add/remove observer: the activityLogPrivate API only listens when the
Expand Down Expand Up @@ -116,11 +118,11 @@ class ActivityLog : public BrowserContextKeyedService,
void DeleteDatabase();

private:
friend class ActivityLogFactory;
friend class ActivityLogTest;
friend class ProfileKeyedAPIFactory<ActivityLog>;
friend class RenderViewActivityLogTest;

explicit ActivityLog(Profile* profile);
explicit ActivityLog(content::BrowserContext* context);
virtual ~ActivityLog();

// Specifies if the Watchdog app is active (installed & enabled).
Expand Down Expand Up @@ -154,6 +156,11 @@ class ActivityLog : public BrowserContextKeyedService,
void ChooseDatabasePolicy();
void SetDatabasePolicy(ActivityLogPolicy::PolicyType policy_type);

// ProfileKeyedAPI implementation.
static const char* service_name() { return "ActivityLog"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsCreatedWithBrowserContext = false;

typedef ObserverListThreadSafe<Observer> ObserverList;
scoped_refptr<ObserverList> observers_;

Expand Down Expand Up @@ -204,31 +211,8 @@ class ActivityLog : public BrowserContextKeyedService,
DISALLOW_COPY_AND_ASSIGN(ActivityLog);
};

// Each profile has different extensions, so we keep a different database for
// each profile.
class ActivityLogFactory : public BrowserContextKeyedServiceFactory {
public:
static ActivityLog* GetForBrowserContext(content::BrowserContext* context) {
return static_cast<ActivityLog*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

static ActivityLogFactory* GetInstance();

private:
friend struct DefaultSingletonTraits<ActivityLogFactory>;
ActivityLogFactory();
virtual ~ActivityLogFactory();

virtual BrowserContextKeyedService* BuildServiceInstanceFor(
content::BrowserContext* profile) const OVERRIDE;

virtual content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const OVERRIDE;

DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory);
};

template <>
void ProfileKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies();

} // namespace extensions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ProfileKeyedAPIFactory<ActivityLogAPI>* ActivityLogAPI::GetFactoryInstance() {
template<>
void ProfileKeyedAPIFactory<ActivityLogAPI>::DeclareFactoryDependencies() {
DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
DependsOn(ActivityLogFactory::GetInstance());
DependsOn(ActivityLog::GetFactoryInstance());
}

ActivityLogAPI::ActivityLogAPI(content::BrowserContext* context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "chrome/browser/extensions/api/autotest_private/autotest_private_api.h"

#include "base/lazy_instance.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/extensions/api/autotest_private/autotest_private_api_factory.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_function_registry.h"
#include "chrome/browser/extensions/extension_service.h"
Expand Down Expand Up @@ -60,18 +60,24 @@ base::ListValue* GetAPIPermissions(const Extension* ext) {
return permissions;
}

bool IsTestMode(Profile* profile) {
return AutotestPrivateAPI::GetFactoryInstance()
->GetForProfile(profile)
->test_mode();
}

} // namespace

bool AutotestPrivateLogoutFunction::RunImpl() {
DVLOG(1) << "AutotestPrivateLogoutFunction";
if (!AutotestPrivateAPIFactory::GetForProfile(GetProfile())->test_mode())
if (!IsTestMode(GetProfile()))
chrome::AttemptUserExit();
return true;
}

bool AutotestPrivateRestartFunction::RunImpl() {
DVLOG(1) << "AutotestPrivateRestartFunction";
if (!AutotestPrivateAPIFactory::GetForProfile(GetProfile())->test_mode())
if (!IsTestMode(GetProfile()))
chrome::AttemptRestart();
return true;
}
Expand All @@ -83,7 +89,7 @@ bool AutotestPrivateShutdownFunction::RunImpl() {

DVLOG(1) << "AutotestPrivateShutdownFunction " << params->force;

if (!AutotestPrivateAPIFactory::GetForProfile(GetProfile())->test_mode())
if (!IsTestMode(GetProfile()))
chrome::AttemptExit();
return true;
}
Expand Down Expand Up @@ -210,7 +216,7 @@ static int AccessArray(const volatile int arr[], const volatile int *index) {

bool AutotestPrivateSimulateAsanMemoryBugFunction::RunImpl() {
DVLOG(1) << "AutotestPrivateSimulateAsanMemoryBugFunction";
if (!AutotestPrivateAPIFactory::GetForProfile(GetProfile())->test_mode()) {
if (!IsTestMode(GetProfile())) {
// This array is volatile not to let compiler optimize us out.
volatile int testarray[3] = {0, 0, 0};

Expand All @@ -221,6 +227,21 @@ bool AutotestPrivateSimulateAsanMemoryBugFunction::RunImpl() {
return true;
}

static base::LazyInstance<ProfileKeyedAPIFactory<AutotestPrivateAPI> >
g_factory = LAZY_INSTANCE_INITIALIZER;

// static
ProfileKeyedAPIFactory<AutotestPrivateAPI>*
AutotestPrivateAPI::GetFactoryInstance() {
return g_factory.Pointer();
}

template <>
BrowserContextKeyedService*
ProfileKeyedAPIFactory<AutotestPrivateAPI>::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new AutotestPrivateAPI();
}

AutotestPrivateAPI::AutotestPrivateAPI() : test_mode_(false) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <string>

#include "base/compiler_specific.h"
#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "components/browser_context_keyed_service/browser_context_keyed_service.h"

namespace extensions {

Expand Down Expand Up @@ -87,20 +87,33 @@ class AutotestPrivateSimulateAsanMemoryBugFunction
void SetAutotestPrivateTest();

// The profile-keyed service that manages the autotestPrivate extension API.
class AutotestPrivateAPI : public BrowserContextKeyedService {
class AutotestPrivateAPI : public ProfileKeyedAPI {
public:
AutotestPrivateAPI();
static ProfileKeyedAPIFactory<AutotestPrivateAPI>* GetFactoryInstance();

// TODO(achuith): Replace these with a mock object for system calls.
bool test_mode() const { return test_mode_; }
void set_test_mode(bool test_mode) { test_mode_ = test_mode; }

private:
friend class ProfileKeyedAPIFactory<AutotestPrivateAPI>;

AutotestPrivateAPI();
virtual ~AutotestPrivateAPI();

// ProfileKeyedAPI implementation.
static const char* service_name() { return "AutotestPrivateAPI"; }
static const bool kServiceIsNULLWhileTesting = true;
static const bool kServiceRedirectedInIncognito = true;

bool test_mode_; // true for ExtensionApiTest.AutotestPrivate browser test.
};

template <>
BrowserContextKeyedService*
ProfileKeyedAPIFactory<AutotestPrivateAPI>::BuildServiceInstanceFor(
content::BrowserContext* context) const;

} // namespace extensions

#endif // CHROME_BROWSER_EXTENSIONS_API_AUTOTEST_PRIVATE_AUTOTEST_PRIVATE_API_H_

This file was deleted.

Loading

0 comments on commit 3242279

Please sign in to comment.