1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/app_mode/kiosk_mode_idle_app_name_notification.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "chrome/browser/chromeos/ui/idle_app_name_notification_view.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "components/user_manager/user_manager.h"
16 #include "extensions/browser/extension_system.h"
17 #include "ui/wm/core/user_activity_detector.h"
23 // The timeout in ms before the message shows up.
24 const int kIdleAppNameNotificationTimeoutMs = 2 * 60 * 1000;
26 // The duration of visibility for the message.
27 const int kMessageVisibilityTimeMs = 3000;
29 // The anomation time to show / hide the message.
30 const int kMessageAnimationTimeMs = 200;
32 // Our global instance of the Kiosk mode message.
33 KioskModeIdleAppNameNotification* g_kiosk_mode_idle_app_message = NULL;
38 void KioskModeIdleAppNameNotification::Initialize() {
39 DCHECK(!g_kiosk_mode_idle_app_message);
40 g_kiosk_mode_idle_app_message = new KioskModeIdleAppNameNotification();
44 void KioskModeIdleAppNameNotification::Shutdown() {
45 if (g_kiosk_mode_idle_app_message) {
46 delete g_kiosk_mode_idle_app_message;
47 g_kiosk_mode_idle_app_message = NULL;
51 KioskModeIdleAppNameNotification::KioskModeIdleAppNameNotification()
52 : show_notification_upon_next_user_activity_(false) {
53 // Note: The timeout is currently fixed. If that changes we need to check if
54 // the KioskModeSettings were already initialized.
58 KioskModeIdleAppNameNotification::~KioskModeIdleAppNameNotification() {
59 wm::UserActivityDetector* user_activity_detector =
60 wm::UserActivityDetector::Get();
61 if (user_activity_detector && user_activity_detector->HasObserver(this)) {
62 user_activity_detector->RemoveObserver(this);
63 // At this time the DBusThreadManager might already be gone.
64 if (chromeos::DBusThreadManager::IsInitialized())
65 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(
66 )->RemoveObserver(this);
70 void KioskModeIdleAppNameNotification::Setup() {
71 DCHECK(user_manager::UserManager::Get()->IsUserLoggedIn());
75 void KioskModeIdleAppNameNotification::OnUserActivity(const ui::Event* event) {
76 if (show_notification_upon_next_user_activity_) {
77 CommandLine* command_line = CommandLine::ForCurrentProcess();
78 const std::string app_id =
79 command_line->GetSwitchValueASCII(::switches::kAppId);
80 Profile* profile = ProfileManager::GetActiveUserProfile();
82 new IdleAppNameNotificationView(
83 kMessageVisibilityTimeMs,
84 kMessageAnimationTimeMs,
85 extensions::ExtensionSystem::Get(profile
86 )->extension_service()->GetInstalledExtension(app_id)));
87 show_notification_upon_next_user_activity_ = false;
92 void KioskModeIdleAppNameNotification::SuspendDone(
93 const base::TimeDelta& sleep_duration) {
94 // When we come back from a system resume we stop the timer and show the
100 void KioskModeIdleAppNameNotification::Start() {
101 if (!wm::UserActivityDetector::Get()->HasObserver(this)) {
102 wm::UserActivityDetector::Get()->AddObserver(this);
103 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
109 void KioskModeIdleAppNameNotification::ResetTimer() {
110 if (timer_.IsRunning()) {
113 // OneShotTimer destroys the posted task after running it, so Reset()
114 // isn't safe to call on a timer that's already fired.
117 base::TimeDelta::FromMilliseconds(kIdleAppNameNotificationTimeoutMs),
118 base::Bind(&KioskModeIdleAppNameNotification::OnTimeout,
119 base::Unretained(this)));
123 void KioskModeIdleAppNameNotification::OnTimeout() {
124 show_notification_upon_next_user_activity_ = true;
127 } // namespace chromeos