Fix Up SingletonHwnd Observer Lifetime Issues
[chromium-blink-merge.git] / components / proximity_auth / screenlock_bridge.cc
blob2af83781dc0dfe986a4ffb3114cf1781361ab2ce
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"
9 #include "components/proximity_auth/proximity_auth_client.h"
11 #if defined(OS_CHROMEOS)
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/session_manager_client.h"
14 #endif
16 namespace proximity_auth {
17 namespace {
19 // Ids for the icons that are supported by lock screen and signin screen
20 // account picker as user pod custom icons.
21 // The id's should be kept in sync with values used by user_pod_row.js.
22 const char kLockedUserPodCustomIconId[] = "locked";
23 const char kLockedToBeActivatedUserPodCustomIconId[] = "locked-to-be-activated";
24 const char kLockedWithProximityHintUserPodCustomIconId[] =
25 "locked-with-proximity-hint";
26 const char kUnlockedUserPodCustomIconId[] = "unlocked";
27 const char kHardlockedUserPodCustomIconId[] = "hardlocked";
28 const char kSpinnerUserPodCustomIconId[] = "spinner";
30 // Given the user pod icon, returns its id as used by the user pod UI code.
31 std::string GetIdForIcon(ScreenlockBridge::UserPodCustomIcon icon) {
32 switch (icon) {
33 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED:
34 return kLockedUserPodCustomIconId;
35 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_TO_BE_ACTIVATED:
36 return kLockedToBeActivatedUserPodCustomIconId;
37 case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_WITH_PROXIMITY_HINT:
38 return kLockedWithProximityHintUserPodCustomIconId;
39 case ScreenlockBridge::USER_POD_CUSTOM_ICON_UNLOCKED:
40 return kUnlockedUserPodCustomIconId;
41 case ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED:
42 return kHardlockedUserPodCustomIconId;
43 case ScreenlockBridge::USER_POD_CUSTOM_ICON_SPINNER:
44 return kSpinnerUserPodCustomIconId;
45 default:
46 return "";
50 } // namespace
52 ScreenlockBridge::ScreenlockBridge(ProximityAuthClient* client)
53 : client_(client), lock_handler_(nullptr) {
54 DCHECK(client_);
57 ScreenlockBridge::~ScreenlockBridge() {
60 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
61 : autoshow_tooltip_(false),
62 hardlock_on_click_(false),
63 is_trial_run_(false) {
66 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);
88 if (is_trial_run_)
89 result->SetBoolean("isTrialRun", true);
91 return result.Pass();
94 void ScreenlockBridge::UserPodCustomIconOptions::SetIcon(
95 ScreenlockBridge::UserPodCustomIcon icon) {
96 icon_ = icon;
99 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
100 const base::string16& tooltip,
101 bool autoshow) {
102 tooltip_ = 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;
119 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
120 DCHECK(lock_handler_ == nullptr || lock_handler == nullptr);
122 // Don't notify observers if there is no change -- i.e. if the screen was
123 // already unlocked, and is remaining unlocked.
124 if (lock_handler == lock_handler_)
125 return;
127 // TODO(isherman): If |lock_handler| is null, then |lock_handler_| might have
128 // been freed. Cache the screen type rather than querying it below.
129 LockHandler::ScreenType screen_type;
130 if (lock_handler_)
131 screen_type = lock_handler_->GetScreenType();
132 else
133 screen_type = lock_handler->GetScreenType();
135 lock_handler_ = lock_handler;
136 if (lock_handler_)
137 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock(screen_type));
138 else
139 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock(screen_type));
142 void ScreenlockBridge::SetFocusedUser(const std::string& user_id) {
143 if (user_id == focused_user_id_)
144 return;
145 focused_user_id_ = user_id;
146 FOR_EACH_OBSERVER(Observer, observers_, OnFocusedUserChanged(user_id));
149 bool ScreenlockBridge::IsLocked() const {
150 return lock_handler_ != nullptr;
153 void ScreenlockBridge::Lock(content::BrowserContext* browser_context) {
154 #if defined(OS_CHROMEOS)
155 chromeos::SessionManagerClient* session_manager =
156 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
157 session_manager->RequestLockScreen();
158 #else
159 client_->Lock(browser_context);
160 #endif
163 void ScreenlockBridge::Unlock(content::BrowserContext* browser_context) {
164 if (lock_handler_)
165 lock_handler_->Unlock(client_->GetAuthenticatedUsername(browser_context));
168 void ScreenlockBridge::AddObserver(Observer* observer) {
169 observers_.AddObserver(observer);
172 void ScreenlockBridge::RemoveObserver(Observer* observer) {
173 observers_.RemoveObserver(observer);
176 } // namespace proximity_auth