Commit dc74c6d5 authored by zhuoyu.qian's avatar zhuoyu.qian Committed by Commit bot

[CleanUp] Move PowerApiManager to power_api and Rename

As the TODO in power_api_manager.h by derat@,
move PowerApiManager to power_api and rename to PowerAPI.

BUG=
R=derat@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#318816}
parent 16fda611
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#include "content/public/browser/power_save_blocker.h" #include "content/public/browser/power_save_blocker.h"
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "extensions/browser/api/power/power_api_manager.h" #include "extensions/browser/api/power/power_api.h"
#include "extensions/browser/app_sorting.h" #include "extensions/browser/app_sorting.h"
#include "extensions/browser/event_router.h" #include "extensions/browser/event_router.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
...@@ -1028,9 +1028,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralAppBrowserTest, ...@@ -1028,9 +1028,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralAppBrowserTest,
// chrome.power.releaseKeepAwake() themselves. // chrome.power.releaseKeepAwake() themselves.
IN_PROC_BROWSER_TEST_F(EphemeralAppBrowserTest, ReleasePowerKeepAwake) { IN_PROC_BROWSER_TEST_F(EphemeralAppBrowserTest, ReleasePowerKeepAwake) {
PowerSettingsMock power_settings; PowerSettingsMock power_settings;
extensions::PowerApiManager* power_manager = extensions::PowerAPI::Get(profile())->SetCreateBlockerFunctionForTesting(
extensions::PowerApiManager::Get(profile());
power_manager->SetCreateBlockerFunctionForTesting(
base::Bind(&PowerSaveBlockerStub::Create, &power_settings)); base::Bind(&PowerSaveBlockerStub::Create, &power_settings));
const Extension* app = InstallAndLaunchEphemeralApp(kPowerTestApp); const Extension* app = InstallAndLaunchEphemeralApp(kPowerTestApp);
......
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h" #include "content/public/browser/notification_source.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "extensions/browser/api/power/power_api_manager.h" #include "extensions/browser/api/power/power_api.h"
#include "extensions/common/api/power.h" #include "extensions/common/api/power.h"
#include "policy/proto/device_management_backend.pb.h" #include "policy/proto/device_management_backend.pb.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -482,8 +482,8 @@ IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest, AllowScreenWakeLocks) { ...@@ -482,8 +482,8 @@ IN_PROC_BROWSER_TEST_F(PowerPolicyInSessionBrowserTest, AllowScreenWakeLocks) {
// Pretend an extension grabs a screen wake lock. // Pretend an extension grabs a screen wake lock.
const char kExtensionId[] = "abcdefghijklmnopabcdefghijlkmnop"; const char kExtensionId[] = "abcdefghijklmnopabcdefghijlkmnop";
extensions::PowerApiManager::Get(browser()->profile())->AddRequest( extensions::PowerAPI::Get(browser()->profile())
kExtensionId, extensions::core_api::power::LEVEL_DISPLAY); ->AddRequest(kExtensionId, extensions::core_api::power::LEVEL_DISPLAY);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Check that the lock is in effect (ignoring ac_idle_action, // Check that the lock is in effect (ignoring ac_idle_action,
......
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
#include "extensions/browser/api/bluetooth/bluetooth_private_api.h" #include "extensions/browser/api/bluetooth/bluetooth_private_api.h"
#include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.h" #include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.h"
#include "extensions/browser/api/bluetooth_socket/bluetooth_socket_event_dispatcher.h" #include "extensions/browser/api/bluetooth_socket/bluetooth_socket_event_dispatcher.h"
#include "extensions/browser/api/power/power_api_manager.h" #include "extensions/browser/api/power/power_api.h"
#include "extensions/browser/api/usb/usb_device_resource.h" #include "extensions/browser/api/usb/usb_device_resource.h"
#include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_private_api.h" #include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_private_api.h"
#include "extensions/browser/api/web_request/web_request_api.h" #include "extensions/browser/api/web_request/web_request_api.h"
...@@ -121,7 +121,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() { ...@@ -121,7 +121,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
#if defined(ENABLE_PLUGINS) #if defined(ENABLE_PLUGINS)
extensions::PluginManager::GetFactoryInstance(); extensions::PluginManager::GetFactoryInstance();
#endif // defined(ENABLE_PLUGINS) #endif // defined(ENABLE_PLUGINS)
extensions::PowerApiManager::GetFactoryInstance(); extensions::PowerAPI::GetFactoryInstance();
extensions::PreferenceAPI::GetFactoryInstance(); extensions::PreferenceAPI::GetFactoryInstance();
extensions::ProcessesAPI::GetFactoryInstance(); extensions::ProcessesAPI::GetFactoryInstance();
extensions::PushMessagingAPI::GetFactoryInstance(); extensions::PushMessagingAPI::GetFactoryInstance();
......
...@@ -4,24 +4,128 @@ ...@@ -4,24 +4,128 @@
#include "extensions/browser/api/power/power_api.h" #include "extensions/browser/api/power/power_api.h"
#include "extensions/browser/api/power/power_api_manager.h" #include "base/bind.h"
#include "base/lazy_instance.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/api/power.h" #include "extensions/common/api/power.h"
#include "extensions/common/extension.h"
namespace extensions { namespace extensions {
namespace {
const char kPowerSaveBlockerDescription[] = "extension";
content::PowerSaveBlocker::PowerSaveBlockerType LevelToPowerSaveBlockerType(
core_api::power::Level level) {
switch (level) {
case core_api::power::LEVEL_SYSTEM:
return content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension;
case core_api::power::LEVEL_DISPLAY: // fallthrough
case core_api::power::LEVEL_NONE:
return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
}
NOTREACHED() << "Unhandled level " << level;
return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
}
base::LazyInstance<BrowserContextKeyedAPIFactory<PowerAPI>> g_factory =
LAZY_INSTANCE_INITIALIZER;
} // namespace
bool PowerRequestKeepAwakeFunction::RunSync() { bool PowerRequestKeepAwakeFunction::RunSync() {
scoped_ptr<core_api::power::RequestKeepAwake::Params> params( scoped_ptr<core_api::power::RequestKeepAwake::Params> params(
core_api::power::RequestKeepAwake::Params::Create(*args_)); core_api::power::RequestKeepAwake::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params); EXTENSION_FUNCTION_VALIDATE(params);
EXTENSION_FUNCTION_VALIDATE(params->level != core_api::power::LEVEL_NONE); EXTENSION_FUNCTION_VALIDATE(params->level != core_api::power::LEVEL_NONE);
PowerApiManager::Get(browser_context())->AddRequest( PowerAPI::Get(browser_context())->AddRequest(extension_id(), params->level);
extension_id(), params->level);
return true; return true;
} }
bool PowerReleaseKeepAwakeFunction::RunSync() { bool PowerReleaseKeepAwakeFunction::RunSync() {
PowerApiManager::Get(browser_context())->RemoveRequest(extension_id()); PowerAPI::Get(browser_context())->RemoveRequest(extension_id());
return true; return true;
} }
// static
PowerAPI* PowerAPI::Get(content::BrowserContext* context) {
return BrowserContextKeyedAPIFactory<PowerAPI>::Get(context);
}
// static
BrowserContextKeyedAPIFactory<PowerAPI>* PowerAPI::GetFactoryInstance() {
return g_factory.Pointer();
}
void PowerAPI::AddRequest(const std::string& extension_id,
core_api::power::Level level) {
extension_levels_[extension_id] = level;
UpdatePowerSaveBlocker();
}
void PowerAPI::RemoveRequest(const std::string& extension_id) {
extension_levels_.erase(extension_id);
UpdatePowerSaveBlocker();
}
void PowerAPI::SetCreateBlockerFunctionForTesting(
CreateBlockerFunction function) {
create_blocker_function_ =
!function.is_null() ? function
: base::Bind(&content::PowerSaveBlocker::Create);
}
void PowerAPI::OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) {
RemoveRequest(extension->id());
UpdatePowerSaveBlocker();
}
PowerAPI::PowerAPI(content::BrowserContext* context)
: browser_context_(context),
create_blocker_function_(base::Bind(&content::PowerSaveBlocker::Create)),
current_level_(core_api::power::LEVEL_SYSTEM) {
ExtensionRegistry::Get(browser_context_)->AddObserver(this);
}
PowerAPI::~PowerAPI() {
}
void PowerAPI::UpdatePowerSaveBlocker() {
if (extension_levels_.empty()) {
power_save_blocker_.reset();
return;
}
core_api::power::Level new_level = core_api::power::LEVEL_SYSTEM;
for (ExtensionLevelMap::const_iterator it = extension_levels_.begin();
it != extension_levels_.end(); ++it) {
if (it->second == core_api::power::LEVEL_DISPLAY)
new_level = it->second;
}
// If the level changed and we need to create a new blocker, do a swap
// to ensure that there isn't a brief period where power management is
// unblocked.
if (!power_save_blocker_ || new_level != current_level_) {
content::PowerSaveBlocker::PowerSaveBlockerType type =
LevelToPowerSaveBlockerType(new_level);
scoped_ptr<content::PowerSaveBlocker> new_blocker(
create_blocker_function_.Run(type,
content::PowerSaveBlocker::kReasonOther,
kPowerSaveBlockerDescription));
power_save_blocker_.swap(new_blocker);
current_level_ = new_level;
}
}
void PowerAPI::Shutdown() {
// Unregister here rather than in the d'tor; otherwise this call will recreate
// the already-deleted ExtensionRegistry.
ExtensionRegistry::Get(browser_context_)->RemoveObserver(this);
power_save_blocker_.reset();
}
} // namespace extensions } // namespace extensions
...@@ -5,7 +5,20 @@ ...@@ -5,7 +5,20 @@
#ifndef EXTENSIONS_BROWSER_API_POWER_POWER_API_H_ #ifndef EXTENSIONS_BROWSER_API_POWER_POWER_API_H_
#define EXTENSIONS_BROWSER_API_POWER_POWER_API_H_ #define EXTENSIONS_BROWSER_API_POWER_POWER_API_H_
#include <map>
#include <string>
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/power_save_blocker.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/extension_function.h" #include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/api/power.h"
namespace content {
class BrowserContext;
}
namespace extensions { namespace extensions {
...@@ -33,6 +46,77 @@ class PowerReleaseKeepAwakeFunction : public SyncExtensionFunction { ...@@ -33,6 +46,77 @@ class PowerReleaseKeepAwakeFunction : public SyncExtensionFunction {
bool RunSync() override; bool RunSync() override;
}; };
// Handles calls made via the chrome.power API. There is a separate instance of
// this class for each profile, as requests are tracked by extension ID, but a
// regular and incognito profile will share the same instance.
class PowerAPI : public BrowserContextKeyedAPI,
public extensions::ExtensionRegistryObserver {
public:
typedef base::Callback<scoped_ptr<content::PowerSaveBlocker>(
content::PowerSaveBlocker::PowerSaveBlockerType,
content::PowerSaveBlocker::Reason,
const std::string&)> CreateBlockerFunction;
static PowerAPI* Get(content::BrowserContext* context);
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<PowerAPI>* GetFactoryInstance();
// Adds an extension lock at |level| for |extension_id|, replacing the
// extension's existing lock, if any.
void AddRequest(const std::string& extension_id,
core_api::power::Level level);
// Removes an extension lock for an extension. Calling this for an
// extension id without a lock will do nothing.
void RemoveRequest(const std::string& extension_id);
// Replaces the function that will be called to create PowerSaveBlocker
// objects. Passing an empty callback will revert to the default.
void SetCreateBlockerFunctionForTesting(CreateBlockerFunction function);
// Overridden from extensions::ExtensionRegistryObserver.
void OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) override;
private:
friend class BrowserContextKeyedAPIFactory<PowerAPI>;
explicit PowerAPI(content::BrowserContext* context);
~PowerAPI() override;
// Updates |power_save_blocker_| and |current_level_| after iterating
// over |extension_levels_|.
void UpdatePowerSaveBlocker();
// BrowserContextKeyedAPI implementation.
static const char* service_name() { return "PowerAPI"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsCreatedWithBrowserContext = false;
void Shutdown() override;
content::BrowserContext* browser_context_;
// Function that should be called to create PowerSaveBlocker objects.
// Tests can change this to record what would've been done instead of
// actually changing the system power-saving settings.
CreateBlockerFunction create_blocker_function_;
scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;
// Current level used by |power_save_blocker_|. Meaningless if
// |power_save_blocker_| is NULL.
core_api::power::Level current_level_;
// Map from extension ID to the corresponding level for each extension
// that has an outstanding request.
typedef std::map<std::string, core_api::power::Level> ExtensionLevelMap;
ExtensionLevelMap extension_levels_;
DISALLOW_COPY_AND_ASSIGN(PowerAPI);
};
} // namespace extensions } // namespace extensions
#endif // EXTENSIONS_BROWSER_API_POWER_POWER_API_H_ #endif // EXTENSIONS_BROWSER_API_POWER_POWER_API_H_
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/power/power_api_manager.h"
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
namespace extensions {
namespace {
const char kPowerSaveBlockerDescription[] = "extension";
content::PowerSaveBlocker::PowerSaveBlockerType
LevelToPowerSaveBlockerType(core_api::power::Level level) {
switch (level) {
case core_api::power::LEVEL_SYSTEM:
return content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension;
case core_api::power::LEVEL_DISPLAY: // fallthrough
case core_api::power::LEVEL_NONE:
return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
}
NOTREACHED() << "Unhandled level " << level;
return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep;
}
base::LazyInstance<BrowserContextKeyedAPIFactory<PowerApiManager> > g_factory =
LAZY_INSTANCE_INITIALIZER;
} // namespace
// static
PowerApiManager* PowerApiManager::Get(content::BrowserContext* context) {
return BrowserContextKeyedAPIFactory<PowerApiManager>::Get(context);
}
// static
BrowserContextKeyedAPIFactory<PowerApiManager>*
PowerApiManager::GetFactoryInstance() {
return g_factory.Pointer();
}
void PowerApiManager::AddRequest(const std::string& extension_id,
core_api::power::Level level) {
extension_levels_[extension_id] = level;
UpdatePowerSaveBlocker();
}
void PowerApiManager::RemoveRequest(const std::string& extension_id) {
extension_levels_.erase(extension_id);
UpdatePowerSaveBlocker();
}
void PowerApiManager::SetCreateBlockerFunctionForTesting(
CreateBlockerFunction function) {
create_blocker_function_ = !function.is_null() ? function :
base::Bind(&content::PowerSaveBlocker::Create);
}
void PowerApiManager::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) {
RemoveRequest(extension->id());
UpdatePowerSaveBlocker();
}
PowerApiManager::PowerApiManager(content::BrowserContext* context)
: browser_context_(context),
create_blocker_function_(base::Bind(&content::PowerSaveBlocker::Create)),
current_level_(core_api::power::LEVEL_SYSTEM) {
ExtensionRegistry::Get(browser_context_)->AddObserver(this);
}
PowerApiManager::~PowerApiManager() {}
void PowerApiManager::UpdatePowerSaveBlocker() {
if (extension_levels_.empty()) {
power_save_blocker_.reset();
return;
}
core_api::power::Level new_level = core_api::power::LEVEL_SYSTEM;
for (ExtensionLevelMap::const_iterator it = extension_levels_.begin();
it != extension_levels_.end(); ++it) {
if (it->second == core_api::power::LEVEL_DISPLAY)
new_level = it->second;
}
// If the level changed and we need to create a new blocker, do a swap
// to ensure that there isn't a brief period where power management is
// unblocked.
if (!power_save_blocker_ || new_level != current_level_) {
content::PowerSaveBlocker::PowerSaveBlockerType type =
LevelToPowerSaveBlockerType(new_level);
scoped_ptr<content::PowerSaveBlocker> new_blocker(
create_blocker_function_.Run(type,
content::PowerSaveBlocker::kReasonOther,
kPowerSaveBlockerDescription));
power_save_blocker_.swap(new_blocker);
current_level_ = new_level;
}
}
void PowerApiManager::Shutdown() {
// Unregister here rather than in the d'tor; otherwise this call will recreate
// the already-deleted ExtensionRegistry.
ExtensionRegistry::Get(browser_context_)->RemoveObserver(this);
power_save_blocker_.reset();
}
} // namespace extensions
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_
#define EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_
#include <map>
#include <string>
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/power_save_blocker.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/api/power.h"
namespace content {
class BrowserContext;
}
namespace extensions {
// Handles calls made via the chrome.power API. There is a separate instance of
// this class for each profile, as requests are tracked by extension ID, but a
// regular and incognito profile will share the same instance.
// TODO(derat): Move this to power_api.h and rename it to PowerApi.
class PowerApiManager : public BrowserContextKeyedAPI,
public extensions::ExtensionRegistryObserver {
public:
typedef base::Callback<scoped_ptr<content::PowerSaveBlocker>(
content::PowerSaveBlocker::PowerSaveBlockerType,
content::PowerSaveBlocker::Reason,
const std::string&)> CreateBlockerFunction;
static PowerApiManager* Get(content::BrowserContext* context);
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<PowerApiManager>* GetFactoryInstance();
// Adds an extension lock at |level| for |extension_id|, replacing the
// extension's existing lock, if any.
void AddRequest(const std::string& extension_id,
core_api::power::Level level);
// Removes an extension lock for an extension. Calling this for an
// extension id without a lock will do nothing.
void RemoveRequest(const std::string& extension_id);
// Replaces the function that will be called to create PowerSaveBlocker
// objects. Passing an empty callback will revert to the default.
void SetCreateBlockerFunctionForTesting(CreateBlockerFunction function);
// Overridden from extensions::ExtensionRegistryObserver.
void OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) override;
private:
friend class BrowserContextKeyedAPIFactory<PowerApiManager>;
explicit PowerApiManager(content::BrowserContext* context);
~PowerApiManager() override;
// Updates |power_save_blocker_| and |current_level_| after iterating
// over |extension_levels_|.
void UpdatePowerSaveBlocker();
// BrowserContextKeyedAPI implementation.
static const char* service_name() { return "PowerApiManager"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsCreatedWithBrowserContext = false;
void Shutdown() override;
content::BrowserContext* browser_context_;
// Function that should be called to create PowerSaveBlocker objects.
// Tests can change this to record what would've been done instead of
// actually changing the system power-saving settings.
CreateBlockerFunction create_blocker_function_;
scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;
// Current level used by |power_save_blocker_|. Meaningless if
// |power_save_blocker_| is NULL.
core_api::power::Level current_level_;
// Map from extension ID to the corresponding level for each extension
// that has an outstanding request.
typedef std::map<std::string, core_api::power::Level> ExtensionLevelMap;
ExtensionLevelMap extension_levels_;
DISALLOW_COPY_AND_ASSIGN(PowerApiManager);
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "content/public/browser/power_save_blocker.h" #include "content/public/browser/power_save_blocker.h"
#include "extensions/browser/api/power/power_api_manager.h"
#include "extensions/browser/api_test_utils.h" #include "extensions/browser/api_test_utils.h"
#include "extensions/browser/api_unittest.h" #include "extensions/browser/api_unittest.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
...@@ -56,7 +55,7 @@ class PowerSaveBlockerStub : public content::PowerSaveBlocker { ...@@ -56,7 +55,7 @@ class PowerSaveBlockerStub : public content::PowerSaveBlocker {
}; };
// Manages PowerSaveBlockerStub objects. Tests can instantiate this class // Manages PowerSaveBlockerStub objects. Tests can instantiate this class
// to make PowerApiManager's calls to create PowerSaveBlockers record the // to make PowerAPI's calls to create PowerSaveBlockers record the
// actions that would've been performed instead of actually blocking and // actions that would've been performed instead of actually blocking and
// unblocking power management. // unblocking power management.
class PowerSaveBlockerStubManager { class PowerSaveBlockerStubManager {
...@@ -66,14 +65,14 @@ class PowerSaveBlockerStubManager { ...@@ -66,14 +65,14 @@ class PowerSaveBlockerStubManager {
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
// Use base::Unretained since callbacks with return values can't use // Use base::Unretained since callbacks with return values can't use
// weak pointers. // weak pointers.
PowerApiManager::Get(browser_context_)->SetCreateBlockerFunctionForTesting( PowerAPI::Get(browser_context_)
base::Bind(&PowerSaveBlockerStubManager::CreateStub, ->SetCreateBlockerFunctionForTesting(base::Bind(
base::Unretained(this))); &PowerSaveBlockerStubManager::CreateStub, base::Unretained(this)));
} }
~PowerSaveBlockerStubManager() { ~PowerSaveBlockerStubManager() {
PowerApiManager::Get(browser_context_)->SetCreateBlockerFunctionForTesting( PowerAPI::Get(browser_context_)
PowerApiManager::CreateBlockerFunction()); ->SetCreateBlockerFunctionForTesting(PowerAPI::CreateBlockerFunction());
} }
// Removes and returns the first item from |requests_|. Returns NONE if // Removes and returns the first item from |requests_|. Returns NONE if
...@@ -127,7 +126,7 @@ class PowerSaveBlockerStubManager { ...@@ -127,7 +126,7 @@ class PowerSaveBlockerStubManager {
} // namespace } // namespace
class PowerApiTest : public ApiUnitTest { class PowerAPITest : public ApiUnitTest {
public: public:
void SetUp() override { void SetUp() override {
ApiUnitTest::SetUp(); ApiUnitTest::SetUp();
...@@ -162,17 +161,18 @@ class PowerApiTest : public ApiUnitTest { ...@@ -162,17 +161,18 @@ class PowerApiTest : public ApiUnitTest {
return api_test_utils::RunFunction(function.get(), args, browser_context()); return api_test_utils::RunFunction(function.get(), args, browser_context());
} }
// Send a notification to PowerApiManager saying that |extension| has // Send a notification to PowerAPI saying that |extension| has
// been unloaded. // been unloaded.
void UnloadExtension(const extensions::Extension* extension) { void UnloadExtension(const extensions::Extension* extension) {
PowerApiManager::Get(browser_context())->OnExtensionUnloaded( PowerAPI::Get(browser_context())
browser_context(), extension, UnloadedExtensionInfo::REASON_UNINSTALL); ->OnExtensionUnloaded(browser_context(), extension,
UnloadedExtensionInfo::REASON_UNINSTALL);
} }
scoped_ptr<PowerSaveBlockerStubManager> manager_; scoped_ptr<PowerSaveBlockerStubManager> manager_;
}; };
TEST_F(PowerApiTest, RequestAndRelease) { TEST_F(PowerAPITest, RequestAndRelease) {
// Simulate an extension making and releasing a "display" request and a // Simulate an extension making and releasing a "display" request and a
// "system" request. // "system" request.
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
...@@ -190,7 +190,7 @@ TEST_F(PowerApiTest, RequestAndRelease) { ...@@ -190,7 +190,7 @@ TEST_F(PowerApiTest, RequestAndRelease) {
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
TEST_F(PowerApiTest, RequestWithoutRelease) { TEST_F(PowerAPITest, RequestWithoutRelease) {
// Simulate an extension calling requestKeepAwake() without calling // Simulate an extension calling requestKeepAwake() without calling
// releaseKeepAwake(). The override should be automatically removed when // releaseKeepAwake(). The override should be automatically removed when
// the extension is unloaded. // the extension is unloaded.
...@@ -203,14 +203,14 @@ TEST_F(PowerApiTest, RequestWithoutRelease) { ...@@ -203,14 +203,14 @@ TEST_F(PowerApiTest, RequestWithoutRelease) {
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
TEST_F(PowerApiTest, ReleaseWithoutRequest) { TEST_F(PowerAPITest, ReleaseWithoutRequest) {
// Simulate an extension calling releaseKeepAwake() without having // Simulate an extension calling releaseKeepAwake() without having
// calling requestKeepAwake() earlier. The call should be ignored. // calling requestKeepAwake() earlier. The call should be ignored.
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
TEST_F(PowerApiTest, UpgradeRequest) { TEST_F(PowerAPITest, UpgradeRequest) {
// Simulate an extension calling requestKeepAwake("system") and then // Simulate an extension calling requestKeepAwake("system") and then
// requestKeepAwake("display"). When the second call is made, a // requestKeepAwake("display"). When the second call is made, a
// display-sleep-blocking request should be made before the initial // display-sleep-blocking request should be made before the initial
...@@ -229,7 +229,7 @@ TEST_F(PowerApiTest, UpgradeRequest) { ...@@ -229,7 +229,7 @@ TEST_F(PowerApiTest, UpgradeRequest) {
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
TEST_F(PowerApiTest, DowngradeRequest) { TEST_F(PowerAPITest, DowngradeRequest) {
// Simulate an extension calling requestKeepAwake("display") and then // Simulate an extension calling requestKeepAwake("display") and then
// requestKeepAwake("system"). When the second call is made, an // requestKeepAwake("system"). When the second call is made, an
// app-suspension-blocking request should be made before the initial // app-suspension-blocking request should be made before the initial
...@@ -248,7 +248,7 @@ TEST_F(PowerApiTest, DowngradeRequest) { ...@@ -248,7 +248,7 @@ TEST_F(PowerApiTest, DowngradeRequest) {
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
TEST_F(PowerApiTest, MultipleExtensions) { TEST_F(PowerAPITest, MultipleExtensions) {
// Simulate an extension blocking the display from sleeping. // Simulate an extension blocking the display from sleeping.
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
......
...@@ -383,8 +383,6 @@ ...@@ -383,8 +383,6 @@
'browser/api/networking_private/networking_private_event_router_factory.h', 'browser/api/networking_private/networking_private_event_router_factory.h',
'browser/api/power/power_api.cc', 'browser/api/power/power_api.cc',
'browser/api/power/power_api.h', 'browser/api/power/power_api.h',
'browser/api/power/power_api_manager.cc',
'browser/api/power/power_api_manager.h',
'browser/api/printer_provider/printer_provider_api.cc', 'browser/api/printer_provider/printer_provider_api.cc',
'browser/api/printer_provider/printer_provider_api.h', 'browser/api/printer_provider/printer_provider_api.h',
'browser/api/printer_provider/printer_provider_api_factory.cc', 'browser/api/printer_provider/printer_provider_api_factory.cc',
......
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