Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / signin / screenlock_bridge.cc
bloba78d2613a24a3678d35844b720d93abde52179b1
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/signin/screenlock_bridge.h"
7 #include "base/logging.h"
8 #include "base/strings/string16.h"
9 #include "chrome/browser/profiles/profile_window.h"
10 #include "chrome/browser/signin/signin_manager_factory.h"
11 #include "components/signin/core/browser/signin_manager.h"
13 #if defined(OS_CHROMEOS)
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/session_manager_client.h"
16 #endif
18 namespace {
20 base::LazyInstance<ScreenlockBridge> g_screenlock_bridge_bridge_instance =
21 LAZY_INSTANCE_INITIALIZER;
23 // Ids for the icons that are supported by lock screen and signin screen
24 // account picker as user pod custom icons.
25 // The id's should be kept in sync with values used by user_pod_row.js.
26 const char kLockedUserPodCustomIconId[] = "locked";
27 const char kUnlockedUserPodCustomIconId[] = "unlocked";
28 const char kHardlockedUserPodCustomIconId[] = "hardlocked";
29 const char kSpinnerUserPodCustomIconId[] = "spinner";
31 // Given the user pod icon, returns its id as used by the user pod UI code.
32 std::string GetIdForIcon(ScreenlockBridge::UserPodCustomIcon icon) {
33 switch (icon) {
34 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED:
35 return kLockedUserPodCustomIconId;
36 case ScreenlockBridge::USER_POD_CUSTOM_ICON_UNLOCKED:
37 return kUnlockedUserPodCustomIconId;
38 case ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED:
39 return kHardlockedUserPodCustomIconId;
40 case ScreenlockBridge::USER_POD_CUSTOM_ICON_SPINNER:
41 return kSpinnerUserPodCustomIconId;
42 default:
43 return "";
47 } // namespace
49 // static
50 ScreenlockBridge* ScreenlockBridge::Get() {
51 return g_screenlock_bridge_bridge_instance.Pointer();
54 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
55 : autoshow_tooltip_(false),
56 hardlock_on_click_(false) {
59 ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {}
61 scoped_ptr<base::DictionaryValue>
62 ScreenlockBridge::UserPodCustomIconOptions::ToDictionaryValue() const {
63 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
64 std::string icon_id = GetIdForIcon(icon_);
65 if (icon_id.empty())
66 return result.Pass();
68 result->SetString("id", icon_id);
70 if (!tooltip_.empty()) {
71 base::DictionaryValue* tooltip_options = new base::DictionaryValue();
72 tooltip_options->SetString("text", tooltip_);
73 tooltip_options->SetBoolean("autoshow", autoshow_tooltip_);
74 result->Set("tooltip", tooltip_options);
77 if (hardlock_on_click_)
78 result->SetBoolean("hardlockOnClick", true);
80 return result.Pass();
83 void ScreenlockBridge::UserPodCustomIconOptions::SetIcon(
84 ScreenlockBridge::UserPodCustomIcon icon) {
85 icon_ = icon;
88 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
89 const base::string16& tooltip,
90 bool autoshow) {
91 tooltip_ = tooltip;
92 autoshow_tooltip_ = autoshow;
95 void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
96 hardlock_on_click_ = true;
99 // static
100 std::string ScreenlockBridge::GetAuthenticatedUserEmail(Profile* profile) {
101 // |profile| has to be a signed-in profile with SigninManager already
102 // created. Otherwise, just crash to collect stack.
103 SigninManagerBase* signin_manager =
104 SigninManagerFactory::GetForProfileIfExists(profile);
105 return signin_manager->GetAuthenticatedUsername();
108 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL) {
111 ScreenlockBridge::~ScreenlockBridge() {
114 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
115 DCHECK(lock_handler_ == NULL || lock_handler == NULL);
116 lock_handler_ = lock_handler;
117 if (lock_handler_)
118 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock());
119 else
120 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock());
123 void ScreenlockBridge::SetFocusedUser(const std::string& user_id) {
124 if (user_id == focused_user_id_)
125 return;
126 focused_user_id_ = user_id;
127 FOR_EACH_OBSERVER(Observer, observers_, OnFocusedUserChanged(user_id));
130 bool ScreenlockBridge::IsLocked() const {
131 return lock_handler_ != NULL;
134 void ScreenlockBridge::Lock(Profile* profile) {
135 #if defined(OS_CHROMEOS)
136 chromeos::SessionManagerClient* session_manager =
137 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
138 session_manager->RequestLockScreen();
139 #else
140 profiles::LockProfile(profile);
141 #endif
144 void ScreenlockBridge::Unlock(Profile* profile) {
145 if (lock_handler_)
146 lock_handler_->Unlock(GetAuthenticatedUserEmail(profile));
149 void ScreenlockBridge::AddObserver(Observer* observer) {
150 observers_.AddObserver(observer);
153 void ScreenlockBridge::RemoveObserver(Observer* observer) {
154 observers_.RemoveObserver(observer);