Commit a51ad4b0 authored by derat's avatar derat Committed by Commit bot

Update idle extension API to use BrowserContext.

As a first step toward moving the chrome.idle API to
//extensions, update it to use content::BrowserContext
rather than Profile.

BUG=446320

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

Cr-Commit-Position: refs/heads/master@{#310122}
parent ca798484
......@@ -35,7 +35,7 @@ bool IdleQueryStateFunction::RunAsync() {
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold));
threshold = ClampThreshold(threshold);
IdleManagerFactory::GetForProfile(GetProfile())->QueryState(
IdleManagerFactory::GetForBrowserContext(context_)->QueryState(
threshold, base::Bind(&IdleQueryStateFunction::IdleStateCallback, this));
// Don't send the response, it'll be sent by our callback
......@@ -52,7 +52,7 @@ bool IdleSetDetectionIntervalFunction::RunSync() {
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold));
threshold = ClampThreshold(threshold);
IdleManagerFactory::GetForProfile(GetProfile())
IdleManagerFactory::GetForBrowserContext(context_)
->SetThreshold(extension_id(), threshold);
return true;
......
......@@ -5,13 +5,13 @@
#ifndef CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_API_H_
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/idle.h"
#include "extensions/browser/extension_function.h"
namespace extensions {
// Implementation of the chrome.idle.queryState API.
class IdleQueryStateFunction : public ChromeAsyncExtensionFunction {
class IdleQueryStateFunction : public AsyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("idle.queryState", IDLE_QUERYSTATE)
......@@ -26,7 +26,7 @@ class IdleQueryStateFunction : public ChromeAsyncExtensionFunction {
};
// Implementation of the chrome.idle.setDetectionInterval API.
class IdleSetDetectionIntervalFunction : public ChromeSyncExtensionFunction {
class IdleSetDetectionIntervalFunction : public SyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("idle.setDetectionInterval",
IDLE_SETDETECTIONINTERVAL)
......
......@@ -117,12 +117,16 @@ ScopedListen::~ScopedListen() {
idle_manager_->OnListenerRemoved(details);
}
KeyedService* IdleManagerTestFactory(content::BrowserContext* profile) {
return new IdleManager(static_cast<Profile*>(profile));
KeyedService* IdleManagerTestFactory(content::BrowserContext* context) {
return new IdleManager(context);
}
} // namespace
// TODO(derat): Make this instead derive from extensions::ApiUnitTest after
// moving it out of the unit_tests target. Its base class can't be changed
// before then since doing so results in crashes due to multiple
// content::NotificationService instances being created.
class IdleTest : public ExtensionApiUnittest {
public:
void SetUp() override;
......@@ -138,7 +142,8 @@ void IdleTest::SetUp() {
IdleManagerFactory::GetInstance()->SetTestingFactory(browser()->profile(),
&IdleManagerTestFactory);
idle_manager_ = IdleManagerFactory::GetForProfile(browser()->profile());
idle_manager_ = IdleManagerFactory::GetForBrowserContext(
browser()->profile());
idle_provider_ = new TestIdleProvider();
idle_manager_->SetIdleTimeProviderForTest(
......
......@@ -8,9 +8,9 @@
#include "base/stl_util.h"
#include "chrome/browser/extensions/api/idle/idle_api_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/idle.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
......@@ -27,7 +27,7 @@ const int kPollInterval = 1;
class DefaultEventDelegate : public IdleManager::EventDelegate {
public:
explicit DefaultEventDelegate(Profile* profile);
explicit DefaultEventDelegate(content::BrowserContext* context);
~DefaultEventDelegate() override;
void OnStateChanged(const std::string& extension_id,
......@@ -36,11 +36,11 @@ class DefaultEventDelegate : public IdleManager::EventDelegate {
void UnregisterObserver(EventRouter::Observer* observer) override;
private:
Profile* profile_;
content::BrowserContext* const context_;
};
DefaultEventDelegate::DefaultEventDelegate(Profile* profile)
: profile_(profile) {
DefaultEventDelegate::DefaultEventDelegate(content::BrowserContext* context)
: context_(context) {
}
DefaultEventDelegate::~DefaultEventDelegate() {
......@@ -52,19 +52,19 @@ void DefaultEventDelegate::OnStateChanged(const std::string& extension_id,
args->Append(IdleManager::CreateIdleValue(new_state));
scoped_ptr<Event> event(new Event(idle::OnStateChanged::kEventName,
args.Pass()));
event->restrict_to_browser_context = profile_;
EventRouter::Get(profile_)
event->restrict_to_browser_context = context_;
EventRouter::Get(context_)
->DispatchEventToExtension(extension_id, event.Pass());
}
void DefaultEventDelegate::RegisterObserver(
EventRouter::Observer* observer) {
EventRouter::Get(profile_)
EventRouter::Get(context_)
->RegisterObserver(observer, idle::OnStateChanged::kEventName);
}
void DefaultEventDelegate::UnregisterObserver(EventRouter::Observer* observer) {
EventRouter::Get(profile_)->UnregisterObserver(observer);
EventRouter::Get(context_)->UnregisterObserver(observer);
}
class DefaultIdleProvider : public IdleManager::IdleTimeProvider {
......@@ -117,11 +117,11 @@ IdleMonitor::IdleMonitor(IdleState initial_state)
threshold(kDefaultIdleThreshold) {
}
IdleManager::IdleManager(Profile* profile)
: profile_(profile),
IdleManager::IdleManager(content::BrowserContext* context)
: context_(context),
last_state_(IDLE_STATE_ACTIVE),
idle_time_provider_(new DefaultIdleProvider()),
event_delegate_(new DefaultEventDelegate(profile)),
event_delegate_(new DefaultEventDelegate(context)),
extension_registry_observer_(this),
weak_factory_(this) {
}
......@@ -130,7 +130,7 @@ IdleManager::~IdleManager() {
}
void IdleManager::Init() {
extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
extension_registry_observer_.Add(ExtensionRegistry::Get(context_));
event_delegate_->RegisterObserver(this);
}
......
......@@ -23,7 +23,9 @@ namespace base {
class StringValue;
} // namespace base
class Profile;
namespace content {
class BrowserContext;
} // namespace content
namespace extensions {
class ExtensionRegistry;
......@@ -68,7 +70,7 @@ class IdleManager : public ExtensionRegistryObserver,
DISALLOW_COPY_AND_ASSIGN(EventDelegate);
};
explicit IdleManager(Profile* profile);
explicit IdleManager(content::BrowserContext* context);
~IdleManager() override;
void Init();
......@@ -119,7 +121,7 @@ class IdleManager : public ExtensionRegistryObserver,
void UpdateIdleState();
void UpdateIdleStateCallback(int idle_time);
Profile* profile_;
content::BrowserContext* const context_;
IdleState last_state_;
MonitorMap monitors_;
......
......@@ -5,7 +5,6 @@
#include "chrome/browser/extensions/api/idle/idle_manager_factory.h"
#include "chrome/browser/extensions/api/idle/idle_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "extensions/browser/extension_system_provider.h"
#include "extensions/browser/extensions_browser_client.h"
......@@ -13,10 +12,10 @@
namespace extensions {
// static
IdleManager* IdleManagerFactory::GetForProfile(
Profile* profile) {
IdleManager* IdleManagerFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<IdleManager*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
GetInstance()->GetServiceForBrowserContext(context, true));
}
// static
......@@ -35,8 +34,8 @@ IdleManagerFactory::~IdleManagerFactory() {
}
KeyedService* IdleManagerFactory::BuildServiceInstanceFor(
content::BrowserContext* profile) const {
IdleManager* idle_manager = new IdleManager(static_cast<Profile*>(profile));
content::BrowserContext* context) const {
IdleManager* idle_manager = new IdleManager(context);
idle_manager->Init();
return idle_manager;
}
......
......@@ -8,14 +8,16 @@
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class Profile;
namespace content {
class BrowserContext;
} // namespace content
namespace extensions {
class IdleManager;
class IdleManagerFactory : public BrowserContextKeyedServiceFactory {
public:
static IdleManager* GetForProfile(Profile* profile);
static IdleManager* GetForBrowserContext(content::BrowserContext* context);
static IdleManagerFactory* GetInstance();
......@@ -27,7 +29,7 @@ class IdleManagerFactory : public BrowserContextKeyedServiceFactory {
// BrowserContextKeyedBaseFactory implementation.
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* profile) const override;
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
bool ServiceIsCreatedWithBrowserContext() const override;
......
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