summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorseanparent@google.com <seanparent@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-19 21:59:01 +0000
committerseanparent@google.com <seanparent@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-19 21:59:01 +0000
commit0fecda62ce784048c032bbe31c7aeaeb8d5cdeab (patch)
tree304f230ebe78ffbcea1a217fa4fd88f23b854bc3 /chrome
parent5fde9812dbd02a294af8eb5966548fae2884bd98 (diff)
downloadchromium_src-0fecda62ce784048c032bbe31c7aeaeb8d5cdeab.zip
chromium_src-0fecda62ce784048c032bbe31c7aeaeb8d5cdeab.tar.gz
chromium_src-0fecda62ce784048c032bbe31c7aeaeb8d5cdeab.tar.bz2
Notification for battery with <= 15 minutes remaining.
BUG=521 TEST=none Review URL: http://codereview.chromium.org/1079007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42153 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/app/generated_resources.grd7
-rw-r--r--chrome/browser/browser_init.cc11
-rw-r--r--chrome/browser/chromeos/low_battery_observer.cc71
-rw-r--r--chrome/browser/chromeos/low_battery_observer.h40
-rw-r--r--chrome/browser/chromeos/notifications/system_notification.cc50
-rw-r--r--chrome/browser/chromeos/notifications/system_notification.h69
-rw-r--r--chrome/browser/chromeos/notifications/system_notification_factory.h6
-rwxr-xr-xchrome/chrome_browser.gypi4
8 files changed, 255 insertions, 3 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd
index 0b1d193..02486fd 100644
--- a/chrome/app/generated_resources.grd
+++ b/chrome/app/generated_resources.grd
@@ -6891,6 +6891,13 @@ Keep your key file in a safe place. You will need it to create new versions of y
<!-- Chrome OS Strings -->
<if expr="pp_ifdef('chromeos')">
+ <message name="IDS_LOW_BATTERY_TITLE" desc="Title for low battery notification">
+ Battery Low
+ </message>
+ <message name="IDS_LOW_BATTERY_MESSAGE" desc="Message for low battery notification">
+ Less than <ph name="MINUTES">$1<ex>15 mins left</ex></ph>.
+ Please connect charger.
+ </message>
<message name="IDS_INSTALLING_UPDATE" desc="Label shown on the updates installation screen">
Please wait while <ph name="PRODUCT_NAME">$1<ex>Chrome OS</ex></ph> installs an important update.\nYour computer will restart after this is complete.
</message>
diff --git a/chrome/browser/browser_init.cc b/chrome/browser/browser_init.cc
index 72a134b..141e993 100644
--- a/chrome/browser/browser_init.cc
+++ b/chrome/browser/browser_init.cc
@@ -21,6 +21,7 @@
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/first_run.h"
#include "chrome/browser/net/dns_global.h"
+#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_process_host.h"
@@ -64,7 +65,9 @@
#include "chrome/browser/chromeos/browser_notification_observers.h"
#include "chrome/browser/dom_ui/mediaplayer_ui.h"
#include "chrome/browser/chromeos/cros/mount_library.h"
+#include "chrome/browser/chromeos/cros/power_library.h"
#include "chrome/browser/chromeos/gview_request_interceptor.h"
+#include "chrome/browser/chromeos/low_battery_observer.h"
#include "chrome/browser/chromeos/usb_mount_observer.h"
#include "chrome/browser/chromeos/wm_message_listener.h"
#endif
@@ -402,6 +405,14 @@ bool BrowserInit::LaunchBrowser(
player->set_profile(profile);
observe->set_profile(profile);
lib->AddObserver(observe);
+
+ // Connect the chromeos notifications
+
+ // This observer is a singleton. It is never deleted but the pointer is kept
+ // in a global so that it isn't reported as a leak.
+ static chromeos::LowBatteryObserver* observer =
+ new chromeos::LowBatteryObserver(profile);
+ chromeos::PowerLibrary::Get()->AddObserver(observer);
}
#endif
#if defined(OS_MACOSX)
diff --git a/chrome/browser/chromeos/low_battery_observer.cc b/chrome/browser/chromeos/low_battery_observer.cc
new file mode 100644
index 0000000..400c024
--- /dev/null
+++ b/chrome/browser/chromeos/low_battery_observer.cc
@@ -0,0 +1,71 @@
+// Copyright (c) 2010 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/chromeos/low_battery_observer.h"
+
+#include "app/l10n_util.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/common/time_format.h"
+#include "grit/generated_resources.h"
+
+namespace chromeos {
+
+LowBatteryObserver::LowBatteryObserver(Profile* profile)
+ : notification_(profile, "battery.chromeos",
+ l10n_util::GetStringUTF16(IDS_LOW_BATTERY_TITLE)),
+ remaining_(0) {}
+
+LowBatteryObserver::~LowBatteryObserver() {
+ Hide();
+}
+
+void LowBatteryObserver::PowerChanged(PowerLibrary* object) {
+ const int limit = 15; // Notification will show when remaining number
+ // of minutes is <= limit
+
+ base::TimeDelta remaining = object->battery_time_to_empty();
+ bool line_power = object->line_power_on();
+
+ // remaining time of zero means still calculating, this is denoted by
+ // TimeDelta().
+
+ // This is a simple state machine with two states and three edges:
+ // States: visible_, !visible_
+ // Edges: hide: is visible_ to !visible_ triggered if we transition
+ // to line_power, we're calculating our time remaining,
+ // or our time remaining has climbed higher than
+ // limit (either by reduced useor an undected transition to/from
+ // line_power).
+ // update: is visible_ to _visible triggered when we didn't hide
+ // and the minutes remaining changed from what's shown.
+ // show: is !visible_ to visible_ triggered when we're on battery,
+ // we know the remaining time, and that time is less than limit.
+
+ if (notification_.visible()) {
+ if (line_power || remaining == base::TimeDelta()
+ || remaining.InMinutes() > limit) {
+ Hide();
+ } else if (remaining.InMinutes() != remaining_) {
+ Show(remaining);
+ }
+ } else {
+ if (!line_power && remaining != base::TimeDelta()
+ && remaining.InMinutes() <= limit) {
+ Show(remaining);
+ }
+ }
+}
+
+void LowBatteryObserver::Show(base::TimeDelta remaining) {
+ notification_.Show(l10n_util::GetStringFUTF16(IDS_LOW_BATTERY_MESSAGE,
+ WideToUTF16(TimeFormat::TimeRemaining(remaining))));
+ remaining_ = remaining.InMinutes();
+}
+
+void LowBatteryObserver::Hide() {
+ notification_.Hide();
+}
+
+} // namespace chromeos
+
diff --git a/chrome/browser/chromeos/low_battery_observer.h b/chrome/browser/chromeos/low_battery_observer.h
new file mode 100644
index 0000000..59e2af8
--- /dev/null
+++ b/chrome/browser/chromeos/low_battery_observer.h
@@ -0,0 +1,40 @@
+// Copyright (c) 2010 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 CHROME_BROWSER_CHROMEOS_LOW_BATTERY_OBSERVER_H_
+#define CHROME_BROWSER_CHROMEOS_LOW_BATTERY_OBSERVER_H_
+
+#include "base/basictypes.h"
+#include "base/time.h"
+#include "chrome/browser/chromeos/cros/power_library.h"
+#include "chrome/browser/chromeos/notifications/system_notification.h"
+
+class Profile;
+
+namespace chromeos {
+
+// The low battery observer displays a system notification when the battery
+// is low.
+
+class LowBatteryObserver : public PowerLibrary::Observer {
+ public:
+ explicit LowBatteryObserver(Profile* profile);
+ virtual ~LowBatteryObserver();
+
+ private:
+ virtual void PowerChanged(PowerLibrary* object);
+
+ void Show(base::TimeDelta remaining);
+ void Hide();
+
+ SystemNotification notification_;
+ int remaining_; // Last displayed remaining time in minutes
+
+ DISALLOW_COPY_AND_ASSIGN(LowBatteryObserver);
+};
+
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_LOW_BATTERY_OBSERVER_H_
+
diff --git a/chrome/browser/chromeos/notifications/system_notification.cc b/chrome/browser/chromeos/notifications/system_notification.cc
new file mode 100644
index 0000000..5697205
--- /dev/null
+++ b/chrome/browser/chromeos/notifications/system_notification.cc
@@ -0,0 +1,50 @@
+// Copyright (c) 2010 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/chromeos/notifications/system_notification.h"
+
+#include "base/move.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/chromeos/notifications/system_notification_factory.h"
+#include "chrome/browser/notifications/notification.h"
+#include "chrome/browser/notifications/notification_ui_manager.h"
+
+namespace chromeos {
+
+SystemNotification::SystemNotification(Profile* profile, std::string id,
+ string16 title)
+ : profile_(profile),
+ collection_(static_cast<BalloonCollectionImpl*>(
+ g_browser_process->notification_ui_manager()->balloon_collection())),
+ delegate_(new Delegate(base::move(id))),
+ title_(move(title)),
+ visible_(false) {}
+
+SystemNotification::~SystemNotification() {
+ Hide();
+}
+
+void SystemNotification::Show(const string16& message) {
+ Notification notify = SystemNotificationFactory::Create(GURL(),
+ title_, message, delegate_.get());
+ if (visible_) {
+ collection_->UpdateNotification(notify);
+ } else {
+ collection_->AddSystemNotification(notify, profile_, true /* sticky */,
+ false /* no controls */);
+ visible_ = true;
+ }
+}
+
+void SystemNotification::Hide() {
+ if (visible_) {
+ collection_->Remove(Notification(GURL(), GURL(), std::wstring(),
+ delegate_.get()));
+
+ visible_ = false;
+ }
+}
+
+} // namespace chromeos
+
diff --git a/chrome/browser/chromeos/notifications/system_notification.h b/chrome/browser/chromeos/notifications/system_notification.h
new file mode 100644
index 0000000..d78fec0
--- /dev/null
+++ b/chrome/browser/chromeos/notifications/system_notification.h
@@ -0,0 +1,69 @@
+// Copyright (c) 2010 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 CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_H_
+#define CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/move.h"
+#include "base/ref_counted.h"
+#include "base/string16.h"
+#include "chrome/browser/chromeos/notifications/balloon_collection_impl.h"
+#include "chrome/browser/notifications/notification_delegate.h"
+
+class Profile;
+
+namespace chromeos {
+
+// The system notification object handles the display of a system notification
+
+class SystemNotification {
+ public:
+ // The profile is the current user profile. The id is any string used
+ // to uniquely identify this notification. The title is the title of
+ // the message to be displayed. On creation, the message is hidden.
+ SystemNotification(Profile* profile, std::string id, string16 title);
+
+ ~SystemNotification();
+
+ // Show will show or update the message for this notification
+ void Show(const string16& message);
+
+ // Hide will dismiss the notification, if the notification is already
+ // hidden it does nothing
+ void Hide();
+
+ // Current visibility state for this notification
+ bool visible() const { return visible_; }
+
+ private:
+ class Delegate : public NotificationDelegate {
+ public:
+ explicit Delegate(std::string id) : id_(base::move(id)) {}
+ void Display() {}
+ void Error() {}
+ void Close(bool by_user) {}
+ std::string id() const { return id_; }
+
+ private:
+ std::string id_;
+
+ DISALLOW_COPY_AND_ASSIGN(Delegate);
+ };
+
+ Profile* profile_;
+ BalloonCollectionImpl* collection_;
+ scoped_refptr<Delegate> delegate_;
+ string16 title_;
+ bool visible_;
+
+ DISALLOW_COPY_AND_ASSIGN(SystemNotification);
+};
+
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_H_
+
diff --git a/chrome/browser/chromeos/notifications/system_notification_factory.h b/chrome/browser/chromeos/notifications/system_notification_factory.h
index 8dc8d93..6acd53a 100644
--- a/chrome/browser/chromeos/notifications/system_notification_factory.h
+++ b/chrome/browser/chromeos/notifications/system_notification_factory.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_H_
-#define CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_H_
+#ifndef CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_FACTORY_H_
+#define CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_FACTORY_H_
#include "base/basictypes.h"
#include "chrome/browser/notifications/notification.h"
@@ -26,4 +26,4 @@ class SystemNotificationFactory {
} // namespace chromeos
-#endif // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_H_
+#endif // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_SYSTEM_NOTIFICATION_FACTORY_H_
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index 492c4311..a2dba11 100755
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -310,6 +310,8 @@
'browser/chromeos/notifications/balloon_view.cc',
'browser/chromeos/notifications/notification_panel.h',
'browser/chromeos/notifications/notification_panel.cc',
+ 'browser/chromeos/notifications/system_notification.h',
+ 'browser/chromeos/notifications/system_notification.cc',
'browser/chromeos/notifications/system_notification_factory.h',
'browser/chromeos/notifications/system_notification_factory.cc',
'browser/chromeos/browser_notification_observers.cc',
@@ -393,6 +395,8 @@
'browser/chromeos/login/wizard_controller.cc',
'browser/chromeos/login/wizard_controller.h',
'browser/chromeos/login/wizard_screen.h',
+ 'browser/chromeos/low_battery_observer.cc',
+ 'browser/chromeos/low_battery_observer.h',
'browser/chromeos/network_list.cc',
'browser/chromeos/network_list.h',
'browser/chromeos/options/internet_page_view.cc',