Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_mode_idle_app_name_notification.cc
blobe016bb5a818f0a63e96c1c97bf0ef2f03eeb68a4
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"
7 #include "ash/shell.h"
8 #include "base/bind.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"
22 namespace chromeos {
24 namespace {
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;
38 } // namespace
40 // static
41 void KioskModeIdleAppNameNotification::Initialize() {
42 DCHECK(!g_kiosk_mode_idle_app_message);
43 g_kiosk_mode_idle_app_message = new KioskModeIdleAppNameNotification();
46 // static
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.
58 Setup();
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());
75 Start();
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();
87 notification_.reset(
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;
96 ResetTimer();
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
102 // message.
103 timer_.Stop();
104 OnTimeout();
107 void KioskModeIdleAppNameNotification::Start() {
108 if (!ui::UserActivityDetector::Get()->HasObserver(this)) {
109 ui::UserActivityDetector::Get()->AddObserver(this);
110 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
111 this);
113 ResetTimer();
116 void KioskModeIdleAppNameNotification::ResetTimer() {
117 if (timer_.IsRunning()) {
118 timer_.Reset();
119 } else {
120 // OneShotTimer destroys the posted task after running it, so Reset()
121 // isn't safe to call on a timer that's already fired.
122 timer_.Start(
123 FROM_HERE,
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