Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_mode_idle_app_name_notification.cc
blob52cba04e06ab53cd686073186f1574ce31116a17
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/wm/core/user_activity_detector.h"
20 namespace chromeos {
22 namespace {
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;
36 } // namespace
38 // static
39 void KioskModeIdleAppNameNotification::Initialize() {
40 DCHECK(!g_kiosk_mode_idle_app_message);
41 g_kiosk_mode_idle_app_message = new KioskModeIdleAppNameNotification();
44 // static
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.
56 Setup();
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(user_manager::UserManager::Get()->IsUserLoggedIn());
72 Start();
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();
81 notification_.reset(
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;
89 ResetTimer();
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
95 // message.
96 timer_.Stop();
97 OnTimeout();
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(
104 this);
106 ResetTimer();
109 void KioskModeIdleAppNameNotification::ResetTimer() {
110 if (timer_.IsRunning()) {
111 timer_.Reset();
112 } else {
113 // OneShotTimer destroys the posted task after running it, so Reset()
114 // isn't safe to call on a timer that's already fired.
115 timer_.Start(
116 FROM_HERE,
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