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 kLockedToBeActivatedUserPodCustomIconId
[] = "locked-to-be-activated";
28 const char kLockedWithProximityHintUserPodCustomIconId
[] =
29 "locked-with-proximity-hint";
30 const char kUnlockedUserPodCustomIconId
[] = "unlocked";
31 const char kHardlockedUserPodCustomIconId
[] = "hardlocked";
32 const char kSpinnerUserPodCustomIconId
[] = "spinner";
34 // Given the user pod icon, returns its id as used by the user pod UI code.
35 std::string
GetIdForIcon(ScreenlockBridge::UserPodCustomIcon icon
) {
37 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED
:
38 return kLockedUserPodCustomIconId
;
39 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_TO_BE_ACTIVATED
:
40 return kLockedToBeActivatedUserPodCustomIconId
;
41 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_WITH_PROXIMITY_HINT
:
42 return kLockedWithProximityHintUserPodCustomIconId
;
43 case ScreenlockBridge::USER_POD_CUSTOM_ICON_UNLOCKED
:
44 return kUnlockedUserPodCustomIconId
;
45 case ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED
:
46 return kHardlockedUserPodCustomIconId
;
47 case ScreenlockBridge::USER_POD_CUSTOM_ICON_SPINNER
:
48 return kSpinnerUserPodCustomIconId
;
57 ScreenlockBridge
* ScreenlockBridge::Get() {
58 return g_screenlock_bridge_bridge_instance
.Pointer();
61 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
62 : autoshow_tooltip_(false),
63 hardlock_on_click_(false),
64 is_trial_run_(false) {
67 ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {}
69 scoped_ptr
<base::DictionaryValue
>
70 ScreenlockBridge::UserPodCustomIconOptions::ToDictionaryValue() const {
71 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue());
72 std::string icon_id
= GetIdForIcon(icon_
);
73 result
->SetString("id", icon_id
);
75 if (!tooltip_
.empty()) {
76 base::DictionaryValue
* tooltip_options
= new base::DictionaryValue();
77 tooltip_options
->SetString("text", tooltip_
);
78 tooltip_options
->SetBoolean("autoshow", autoshow_tooltip_
);
79 result
->Set("tooltip", tooltip_options
);
82 if (!aria_label_
.empty())
83 result
->SetString("ariaLabel", aria_label_
);
85 if (hardlock_on_click_
)
86 result
->SetBoolean("hardlockOnClick", true);
89 result
->SetBoolean("isTrialRun", true);
94 void ScreenlockBridge::UserPodCustomIconOptions::SetIcon(
95 ScreenlockBridge::UserPodCustomIcon icon
) {
99 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
100 const base::string16
& tooltip
,
103 autoshow_tooltip_
= autoshow
;
106 void ScreenlockBridge::UserPodCustomIconOptions::SetAriaLabel(
107 const base::string16
& aria_label
) {
108 aria_label_
= aria_label
;
111 void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
112 hardlock_on_click_
= true;
115 void ScreenlockBridge::UserPodCustomIconOptions::SetTrialRun() {
116 is_trial_run_
= true;
120 std::string
ScreenlockBridge::GetAuthenticatedUserEmail(
121 const Profile
* profile
) {
122 // |profile| has to be a signed-in profile with SigninManager already
123 // created. Otherwise, just crash to collect stack.
124 const SigninManagerBase
* signin_manager
=
125 SigninManagerFactory::GetForProfileIfExists(profile
);
126 return signin_manager
->GetAuthenticatedUsername();
129 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL
) {
132 ScreenlockBridge::~ScreenlockBridge() {
135 void ScreenlockBridge::SetLockHandler(LockHandler
* lock_handler
) {
136 DCHECK(lock_handler_
== NULL
|| lock_handler
== NULL
);
138 // Don't notify observers if there is no change -- i.e. if the screen was
139 // already unlocked, and is remaining unlocked.
140 if (lock_handler
== lock_handler_
)
143 // TODO(isherman): If |lock_handler| is null, then |lock_handler_| might have
144 // been freed. Cache the screen type rather than querying it below.
145 LockHandler::ScreenType screen_type
;
147 screen_type
= lock_handler_
->GetScreenType();
149 screen_type
= lock_handler
->GetScreenType();
151 lock_handler_
= lock_handler
;
153 FOR_EACH_OBSERVER(Observer
, observers_
, OnScreenDidLock(screen_type
));
155 FOR_EACH_OBSERVER(Observer
, observers_
, OnScreenDidUnlock(screen_type
));
158 void ScreenlockBridge::SetFocusedUser(const std::string
& user_id
) {
159 if (user_id
== focused_user_id_
)
161 focused_user_id_
= user_id
;
162 FOR_EACH_OBSERVER(Observer
, observers_
, OnFocusedUserChanged(user_id
));
165 bool ScreenlockBridge::IsLocked() const {
166 return lock_handler_
!= NULL
;
169 void ScreenlockBridge::Lock(Profile
* profile
) {
170 #if defined(OS_CHROMEOS)
171 chromeos::SessionManagerClient
* session_manager
=
172 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
173 session_manager
->RequestLockScreen();
175 profiles::LockProfile(profile
);
179 void ScreenlockBridge::Unlock(Profile
* profile
) {
181 lock_handler_
->Unlock(GetAuthenticatedUserEmail(profile
));
184 void ScreenlockBridge::AddObserver(Observer
* observer
) {
185 observers_
.AddObserver(observer
);
188 void ScreenlockBridge::RemoveObserver(Observer
* observer
) {
189 observers_
.RemoveObserver(observer
);