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"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "chrome/browser/chromeos/ui/idle_app_name_notification_view.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "components/user_manager/user_manager.h"
17 #include "extensions/browser/extension_system.h"
18 #include "ui/base/user_activity/user_activity_detector.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/screen.h"
26 // The timeout in ms before the message shows up.
27 const int kIdleAppNameNotificationTimeoutMs
= 20 * 60 * 1000;
29 // The duration of visibility for the message.
30 const int kMessageVisibilityTimeMs
= 3000;
32 // The anomation time to show / hide the message.
33 const int kMessageAnimationTimeMs
= 200;
35 // Our global instance of the Kiosk mode message.
36 KioskModeIdleAppNameNotification
* g_kiosk_mode_idle_app_message
= NULL
;
41 void KioskModeIdleAppNameNotification::Initialize() {
42 DCHECK(!g_kiosk_mode_idle_app_message
);
43 g_kiosk_mode_idle_app_message
= new KioskModeIdleAppNameNotification();
47 void KioskModeIdleAppNameNotification::Shutdown() {
48 if (g_kiosk_mode_idle_app_message
) {
49 delete g_kiosk_mode_idle_app_message
;
50 g_kiosk_mode_idle_app_message
= NULL
;
54 KioskModeIdleAppNameNotification::KioskModeIdleAppNameNotification()
55 : show_notification_upon_next_user_activity_(false) {
56 // Note: The timeout is currently fixed. If that changes we need to check if
57 // the KioskModeSettings were already initialized.
61 KioskModeIdleAppNameNotification::~KioskModeIdleAppNameNotification() {
62 ui::UserActivityDetector
* user_activity_detector
=
63 ui::UserActivityDetector::Get();
64 if (user_activity_detector
&& user_activity_detector
->HasObserver(this)) {
65 user_activity_detector
->RemoveObserver(this);
66 // At this time the DBusThreadManager might already be gone.
67 if (chromeos::DBusThreadManager::IsInitialized())
68 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(
69 )->RemoveObserver(this);
73 void KioskModeIdleAppNameNotification::Setup() {
74 DCHECK(user_manager::UserManager::Get()->IsUserLoggedIn());
78 void KioskModeIdleAppNameNotification::OnUserActivity(const ui::Event
* event
) {
79 if (show_notification_upon_next_user_activity_
) {
80 gfx::Display display
= ash::Shell::GetScreen()->GetPrimaryDisplay();
81 // Display the notification only on internal display.
82 if (display
.IsInternal()) {
83 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
84 const std::string app_id
=
85 command_line
->GetSwitchValueASCII(::switches::kAppId
);
86 Profile
* profile
= ProfileManager::GetActiveUserProfile();
88 new IdleAppNameNotificationView(
89 kMessageVisibilityTimeMs
,
90 kMessageAnimationTimeMs
,
91 extensions::ExtensionSystem::Get(profile
92 )->extension_service()->GetInstalledExtension(app_id
)));
94 show_notification_upon_next_user_activity_
= false;
99 void KioskModeIdleAppNameNotification::SuspendDone(
100 const base::TimeDelta
& sleep_duration
) {
101 // When we come back from a system resume we stop the timer and show the
107 void KioskModeIdleAppNameNotification::Start() {
108 if (!ui::UserActivityDetector::Get()->HasObserver(this)) {
109 ui::UserActivityDetector::Get()->AddObserver(this);
110 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
116 void KioskModeIdleAppNameNotification::ResetTimer() {
117 if (timer_
.IsRunning()) {
120 // OneShotTimer destroys the posted task after running it, so Reset()
121 // isn't safe to call on a timer that's already fired.
124 base::TimeDelta::FromMilliseconds(kIdleAppNameNotificationTimeoutMs
),
125 base::Bind(&KioskModeIdleAppNameNotification::OnTimeout
,
126 base::Unretained(this)));
130 void KioskModeIdleAppNameNotification::OnTimeout() {
131 show_notification_upon_next_user_activity_
= true;
134 } // namespace chromeos