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;
26 ScreenlockBridge* ScreenlockBridge::Get() {
27 return g_screenlock_bridge_bridge_instance.Pointer();
30 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
33 animation_set_(false),
34 animation_resource_width_(0u),
35 animation_frame_length_ms_(0u),
37 autoshow_tooltip_(false),
38 hardlock_on_click_(false) {
41 ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {}
43 scoped_ptr<base::DictionaryValue>
44 ScreenlockBridge::UserPodCustomIconOptions::ToDictionaryValue() const {
45 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
46 if (icon_resource_url_.empty())
49 result->SetString("resourceUrl", icon_resource_url_);
51 if (!tooltip_.empty()) {
52 base::DictionaryValue* tooltip_options = new base::DictionaryValue();
53 tooltip_options->SetString("text", tooltip_);
54 tooltip_options->SetBoolean("autoshow", autoshow_tooltip_);
55 result->Set("tooltip", tooltip_options);
58 base::DictionaryValue* size = new base::DictionaryValue();
59 size->SetInteger("height", height_);
60 size->SetInteger("width", width_);
61 result->Set("size", size);
63 result->SetInteger("opacity", opacity_);
66 base::DictionaryValue* animation = new base::DictionaryValue();
67 animation->SetInteger("resourceWidth",
68 animation_resource_width_);
69 animation->SetInteger("frameLengthMs",
70 animation_frame_length_ms_);
71 result->Set("animation", animation);
74 if (hardlock_on_click_)
75 result->SetBoolean("hardlockOnClick", true);
80 void ScreenlockBridge::UserPodCustomIconOptions::SetIconAsResourceURL(
81 const std::string& url) {
82 icon_resource_url_ = url;
86 void ScreenlockBridge::UserPodCustomIconOptions::SetSize(size_t icon_width,
89 height_ = icon_height;
92 void ScreenlockBridge::UserPodCustomIconOptions::SetAnimation(
93 size_t resource_width,
94 size_t frame_length_ms) {
95 animation_set_ = true;
96 animation_resource_width_ = resource_width;
97 animation_frame_length_ms_ = frame_length_ms;
100 void ScreenlockBridge::UserPodCustomIconOptions::SetOpacity(size_t opacity) {
101 DCHECK_LE(opacity, 100u);
105 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
106 const base::string16& tooltip,
109 autoshow_tooltip_ = autoshow;
112 void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
113 hardlock_on_click_ = true;
117 std::string ScreenlockBridge::GetAuthenticatedUserEmail(Profile* profile) {
118 // |profile| has to be a signed-in profile with SigninManager already
119 // created. Otherwise, just crash to collect stack.
120 SigninManagerBase* signin_manager =
121 SigninManagerFactory::GetForProfileIfExists(profile);
122 return signin_manager->GetAuthenticatedUsername();
125 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL) {
128 ScreenlockBridge::~ScreenlockBridge() {
131 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
132 DCHECK(lock_handler_ == NULL || lock_handler == NULL);
133 lock_handler_ = lock_handler;
135 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock());
137 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock());
140 void ScreenlockBridge::SetFocusedUser(const std::string& user_id) {
141 if (user_id == focused_user_id_)
143 focused_user_id_ = user_id;
144 FOR_EACH_OBSERVER(Observer, observers_, OnFocusedUserChanged(user_id));
147 bool ScreenlockBridge::IsLocked() const {
148 return lock_handler_ != NULL;
151 void ScreenlockBridge::Lock(Profile* profile) {
152 #if defined(OS_CHROMEOS)
153 chromeos::SessionManagerClient* session_manager =
154 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
155 session_manager->RequestLockScreen();
157 profiles::LockProfile(profile);
161 void ScreenlockBridge::Unlock(Profile* profile) {
163 lock_handler_->Unlock(GetAuthenticatedUserEmail(profile));
166 void ScreenlockBridge::AddObserver(Observer* observer) {
167 observers_.AddObserver(observer);
170 void ScreenlockBridge::RemoveObserver(Observer* observer) {
171 observers_.RemoveObserver(observer);