Roll src/third_party/WebKit e0ce2a6:675d715 (svn 202297:202300)
[chromium-blink-merge.git] / components / proximity_auth / screenlock_bridge.cc
blob029e0b781e734f607f6fce85f2c9bf2338537c84
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 "components/proximity_auth/screenlock_bridge.h"
7 #include "base/logging.h"
8 #include "base/strings/string16.h"
10 #if defined(OS_CHROMEOS)
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/session_manager_client.h"
13 #endif
15 namespace proximity_auth {
16 namespace {
18 base::LazyInstance<ScreenlockBridge> g_screenlock_bridge_instance =
19 LAZY_INSTANCE_INITIALIZER;
21 // Ids for the icons that are supported by lock screen and signin screen
22 // account picker as user pod custom icons.
23 // The id's should be kept in sync with values used by user_pod_row.js.
24 const char kLockedUserPodCustomIconId[] = "locked";
25 const char kLockedToBeActivatedUserPodCustomIconId[] = "locked-to-be-activated";
26 const char kLockedWithProximityHintUserPodCustomIconId[] =
27 "locked-with-proximity-hint";
28 const char kUnlockedUserPodCustomIconId[] = "unlocked";
29 const char kHardlockedUserPodCustomIconId[] = "hardlocked";
30 const char kSpinnerUserPodCustomIconId[] = "spinner";
32 // Given the user pod icon, returns its id as used by the user pod UI code.
33 std::string GetIdForIcon(ScreenlockBridge::UserPodCustomIcon icon) {
34 switch (icon) {
35 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED:
36 return kLockedUserPodCustomIconId;
37 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_TO_BE_ACTIVATED:
38 return kLockedToBeActivatedUserPodCustomIconId;
39 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_WITH_PROXIMITY_HINT:
40 return kLockedWithProximityHintUserPodCustomIconId;
41 case ScreenlockBridge::USER_POD_CUSTOM_ICON_UNLOCKED:
42 return kUnlockedUserPodCustomIconId;
43 case ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED:
44 return kHardlockedUserPodCustomIconId;
45 case ScreenlockBridge::USER_POD_CUSTOM_ICON_SPINNER:
46 return kSpinnerUserPodCustomIconId;
47 default:
48 return "";
52 } // namespace
54 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
55 : autoshow_tooltip_(false),
56 hardlock_on_click_(false),
57 is_trial_run_(false) {
60 ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {
63 scoped_ptr<base::DictionaryValue>
64 ScreenlockBridge::UserPodCustomIconOptions::ToDictionaryValue() const {
65 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
66 std::string icon_id = GetIdForIcon(icon_);
67 result->SetString("id", icon_id);
69 if (!tooltip_.empty()) {
70 base::DictionaryValue* tooltip_options = new base::DictionaryValue();
71 tooltip_options->SetString("text", tooltip_);
72 tooltip_options->SetBoolean("autoshow", autoshow_tooltip_);
73 result->Set("tooltip", tooltip_options);
76 if (!aria_label_.empty())
77 result->SetString("ariaLabel", aria_label_);
79 if (hardlock_on_click_)
80 result->SetBoolean("hardlockOnClick", true);
82 if (is_trial_run_)
83 result->SetBoolean("isTrialRun", true);
85 return result.Pass();
88 void ScreenlockBridge::UserPodCustomIconOptions::SetIcon(
89 ScreenlockBridge::UserPodCustomIcon icon) {
90 icon_ = icon;
93 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
94 const base::string16& tooltip,
95 bool autoshow) {
96 tooltip_ = tooltip;
97 autoshow_tooltip_ = autoshow;
100 void ScreenlockBridge::UserPodCustomIconOptions::SetAriaLabel(
101 const base::string16& aria_label) {
102 aria_label_ = aria_label;
105 void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
106 hardlock_on_click_ = true;
109 void ScreenlockBridge::UserPodCustomIconOptions::SetTrialRun() {
110 is_trial_run_ = true;
113 // static
114 ScreenlockBridge* ScreenlockBridge::Get() {
115 return g_screenlock_bridge_instance.Pointer();
118 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
119 DCHECK(lock_handler_ == nullptr || lock_handler == nullptr);
121 // Don't notify observers if there is no change -- i.e. if the screen was
122 // already unlocked, and is remaining unlocked.
123 if (lock_handler == lock_handler_)
124 return;
126 // TODO(isherman): If |lock_handler| is null, then |lock_handler_| might have
127 // been freed. Cache the screen type rather than querying it below.
128 LockHandler::ScreenType screen_type;
129 if (lock_handler_)
130 screen_type = lock_handler_->GetScreenType();
131 else
132 screen_type = lock_handler->GetScreenType();
134 lock_handler_ = lock_handler;
135 if (lock_handler_)
136 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock(screen_type));
137 else
138 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock(screen_type));
141 void ScreenlockBridge::SetFocusedUser(const std::string& user_id) {
142 if (user_id == focused_user_id_)
143 return;
144 focused_user_id_ = user_id;
145 FOR_EACH_OBSERVER(Observer, observers_, OnFocusedUserChanged(user_id));
148 bool ScreenlockBridge::IsLocked() const {
149 return lock_handler_ != nullptr;
152 void ScreenlockBridge::Lock() {
153 #if defined(OS_CHROMEOS)
154 chromeos::SessionManagerClient* session_manager =
155 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
156 session_manager->RequestLockScreen();
157 #else
158 NOTIMPLEMENTED();
159 #endif
162 void ScreenlockBridge::Unlock(const std::string& user_email) {
163 if (lock_handler_)
164 lock_handler_->Unlock(user_email);
167 void ScreenlockBridge::AddObserver(Observer* observer) {
168 observers_.AddObserver(observer);
171 void ScreenlockBridge::RemoveObserver(Observer* observer) {
172 observers_.RemoveObserver(observer);
175 ScreenlockBridge::ScreenlockBridge() : lock_handler_(nullptr) {
178 ScreenlockBridge::~ScreenlockBridge() {
181 } // namespace proximity_auth