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"
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
) {
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
;
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_
);
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);
83 void ScreenlockBridge::UserPodCustomIconOptions::SetIcon(
84 ScreenlockBridge::UserPodCustomIcon icon
) {
88 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
89 const base::string16
& tooltip
,
92 autoshow_tooltip_
= autoshow
;
95 void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
96 hardlock_on_click_
= true;
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
;
118 FOR_EACH_OBSERVER(Observer
, observers_
, OnScreenDidLock());
120 FOR_EACH_OBSERVER(Observer
, observers_
, OnScreenDidUnlock());
123 void ScreenlockBridge::SetFocusedUser(const std::string
& user_id
) {
124 if (user_id
== focused_user_id_
)
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();
140 profiles::LockProfile(profile
);
144 void ScreenlockBridge::Unlock(Profile
* profile
) {
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
);