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 "ash/wm/user_activity_detector.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "chrome/browser/chromeos/login/user_manager.h"
13 #include "chrome/browser/chromeos/ui/idle_app_name_notification_view.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "extensions/browser/extension_system.h"
24 // The timeout in ms before the message shows up.
25 const int kIdleAppNameNotificationTimeoutMs = 2 * 60 * 1000;
27 // The duration of visibility for the message.
28 const int kMessageVisibilityTimeMs = 3000;
30 // The anomation time to show / hide the message.
31 const int kMessageAnimationTimeMs = 200;
33 // Our global instance of the Kiosk mode message.
34 KioskModeIdleAppNameNotification* g_kiosk_mode_idle_app_message = NULL;
39 void KioskModeIdleAppNameNotification::Initialize() {
40 DCHECK(!g_kiosk_mode_idle_app_message);
41 g_kiosk_mode_idle_app_message = new KioskModeIdleAppNameNotification();
45 void KioskModeIdleAppNameNotification::Shutdown() {
46 if (g_kiosk_mode_idle_app_message) {
47 delete g_kiosk_mode_idle_app_message;
48 g_kiosk_mode_idle_app_message = NULL;
52 KioskModeIdleAppNameNotification::KioskModeIdleAppNameNotification()
53 : show_notification_upon_next_user_activity_(false) {
54 // Note: The timeout is currently fixed. If that changes we need to check if
55 // the KioskModeSettings were already initialized.
59 KioskModeIdleAppNameNotification::~KioskModeIdleAppNameNotification() {
60 if (ash::Shell::HasInstance() &&
61 ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this)) {
62 ash::Shell::GetInstance()->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(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::SystemResumed(
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 (!ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this)) {
102 ash::Shell::GetInstance()->user_activity_detector()->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