diff options
-rw-r--r-- | chrome/browser/chromeos/cros/mock_power_library.h | 2 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/power_library.cc | 24 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/power_library.h | 8 | ||||
-rw-r--r-- | chrome/browser/chromeos/notifications/desktop_notifications_unittest.h | 4 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_idle_api.cc | 5 | ||||
-rw-r--r-- | chrome/browser/idle_chromeos.cc | 30 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 |
7 files changed, 72 insertions, 3 deletions
diff --git a/chrome/browser/chromeos/cros/mock_power_library.h b/chrome/browser/chromeos/cros/mock_power_library.h index 044614b..762fa00 100644 --- a/chrome/browser/chromeos/cros/mock_power_library.h +++ b/chrome/browser/chromeos/cros/mock_power_library.h @@ -29,6 +29,8 @@ class MockPowerLibrary : public PowerLibrary { MOCK_CONST_METHOD0(battery_time_to_empty, base::TimeDelta(void)); MOCK_CONST_METHOD0(battery_time_to_full, base::TimeDelta(void)); + MOCK_METHOD1(CalculateIdleTime, void(CalculateIdleTimeCallback*)); + MOCK_METHOD1(EnableScreenLock, void(bool)); MOCK_METHOD0(RequestRestart, void(void)); MOCK_METHOD0(RequestShutdown, void(void)); diff --git a/chrome/browser/chromeos/cros/power_library.cc b/chrome/browser/chromeos/cros/power_library.cc index 721202b..070d377 100644 --- a/chrome/browser/chromeos/cros/power_library.cc +++ b/chrome/browser/chromeos/cros/power_library.cc @@ -78,6 +78,12 @@ class PowerLibraryImpl : public PowerLibrary { return base::TimeDelta::FromSeconds(status_.battery_time_to_full); } + virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { + // TODO(sidor): Examine if it's really a good idea to use void* as a second + // argument. + chromeos::GetIdleTime(&GetIdleTimeCallback, callback); + } + virtual void EnableScreenLock(bool enable) OVERRIDE { if (!CrosLibrary::Get()->EnsureLoaded()) return; @@ -106,6 +112,21 @@ class PowerLibraryImpl : public PowerLibrary { // End PowerLibrary implementation. private: + static void GetIdleTimeCallback(void* object, + int64_t time_idle_ms, + bool success) { + DCHECK(object); + CalculateIdleTimeCallback* notify = + static_cast<CalculateIdleTimeCallback*>(object); + if (success) { + notify->Run(time_idle_ms/1000); + } else { + LOG(ERROR) << "Power manager failed to calculate idle time."; + notify->Run(-1); + } + delete notify; + } + static void PowerStatusChangedHandler(void* object, const chromeos::PowerStatus& status) { PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); @@ -220,6 +241,9 @@ class PowerLibraryStubImpl : public PowerLibrary { return base::TimeDelta::FromHours(3) - battery_time_to_empty(); } + virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { + callback->Run(0); + } virtual void EnableScreenLock(bool enable) OVERRIDE {} virtual void RequestRestart() OVERRIDE {} virtual void RequestShutdown() OVERRIDE {} diff --git a/chrome/browser/chromeos/cros/power_library.h b/chrome/browser/chromeos/cros/power_library.h index a32f8fc..5efa124 100644 --- a/chrome/browser/chromeos/cros/power_library.h +++ b/chrome/browser/chromeos/cros/power_library.h @@ -6,12 +6,16 @@ #define CHROME_BROWSER_CHROMEOS_CROS_POWER_LIBRARY_H_ #pragma once +#include "base/callback.h" + namespace base { class TimeDelta; } namespace chromeos { +typedef base::Callback<void(int64_t)> CalculateIdleTimeCallback; + // This interface defines interaction with the ChromeOS power library APIs. // Classes can add themselves as observers. Users can get an instance of this // library class like this: chromeos::CrosLibrary::Get()->GetPowerLibrary() @@ -51,6 +55,10 @@ class PowerLibrary { // The amount of time until battery is full. virtual base::TimeDelta battery_time_to_full() const = 0; + // Calculates idle time asynchronously. If it encounters some error, + // it returns -1. + virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) = 0; + // Enable/disable screen lock for current session. virtual void EnableScreenLock(bool enable) = 0; diff --git a/chrome/browser/chromeos/notifications/desktop_notifications_unittest.h b/chrome/browser/chromeos/notifications/desktop_notifications_unittest.h index 18137b9..ab8cc1d 100644 --- a/chrome/browser/chromeos/notifications/desktop_notifications_unittest.h +++ b/chrome/browser/chromeos/notifications/desktop_notifications_unittest.h @@ -11,6 +11,7 @@ #include "base/message_loop.h" #include "base/string_util.h" +#include "chrome/browser/chromeos/cros/cros_library.h" #include "chrome/browser/chromeos/notifications/balloon_collection_impl.h" #include "chrome/browser/notifications/balloon.h" #include "chrome/browser/notifications/desktop_notification_service.h" @@ -109,6 +110,9 @@ class DesktopNotificationsTest : public testing::Test { // Contains the cumulative output of the unit test. static std::string log_output_; + + // Initializes / shuts down a stub CrosLibrary. + chromeos::ScopedStubCrosEnabler stub_cros_enabler_; }; } // namespace chromeos diff --git a/chrome/browser/extensions/extension_idle_api.cc b/chrome/browser/extensions/extension_idle_api.cc index f52f4b7..a3c45a5 100644 --- a/chrome/browser/extensions/extension_idle_api.cc +++ b/chrome/browser/extensions/extension_idle_api.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -169,8 +169,7 @@ bool ExtensionIdleQueryStateFunction::RunImpl() { polling_data.state = IDLE_STATE_UNKNOWN; CalculateIdleState(threshold, base::Bind(&ExtensionIdleQueryStateFunction::IdleStateCallback, - base::Unretained(this), threshold)); - + this, threshold)); // Don't send the response, it'll be sent by our callback return true; } diff --git a/chrome/browser/idle_chromeos.cc b/chrome/browser/idle_chromeos.cc new file mode 100644 index 0000000..2971fb9 --- /dev/null +++ b/chrome/browser/idle_chromeos.cc @@ -0,0 +1,30 @@ +// Copyright (c) 2011 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 "chrome/browser/idle.h" + +#include "base/bind.h" +#include "base/callback.h" +#include "chrome/browser/chromeos/cros/cros_library.h" +#include "chrome/browser/chromeos/cros/power_library.h" + +void CalculateIdleStateNotifier(unsigned int idle_treshold, + IdleCallback notify, + int64_t idle_time_s) { + if (idle_time_s >= (int64_t)idle_treshold) { + notify.Run(IDLE_STATE_IDLE); + } else if (idle_time_s < 0) { + notify.Run(IDLE_STATE_UNKNOWN); + } else { + notify.Run(IDLE_STATE_ACTIVE); + } +} + +void CalculateIdleState(unsigned int idle_threshold, IdleCallback notify) { + chromeos::CalculateIdleTimeCallback* callback = + new base::Callback<void(int64_t)>(base::Bind(&CalculateIdleStateNotifier, + idle_threshold, + notify)); + chromeos::CrosLibrary::Get()->GetPowerLibrary()->CalculateIdleTime(callback); +} diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 29f494f..7f628e9 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1311,6 +1311,7 @@ 'browser/icon_manager_mac.mm', 'browser/icon_manager_win.cc', 'browser/idle.cc', + 'browser/idle_chromeos.cc', 'browser/idle_linux.cc', 'browser/idle_mac.mm', 'browser/idle_query_linux.cc', @@ -3906,6 +3907,7 @@ 'browser/first_run/upgrade_util.cc', 'browser/first_run/upgrade_util.h', 'browser/first_run/upgrade_util_linux.cc', + 'browser/idle_linux.cc', 'browser/password_manager/native_backend_gnome_x.cc', 'browser/password_manager/native_backend_gnome_x.h', 'browser/password_manager/native_backend_kwallet_x.cc', |