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"
12 #include "ui/base/webui/web_ui_util.h"
13 #include "ui/gfx/image/image.h"
14 #include "ui/gfx/image/image_skia.h"
16 #if defined(OS_CHROMEOS)
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/session_manager_client.h"
23 base::LazyInstance<ScreenlockBridge> g_screenlock_bridge_bridge_instance =
24 LAZY_INSTANCE_INITIALIZER;
29 ScreenlockBridge* ScreenlockBridge::Get() {
30 return g_screenlock_bridge_bridge_instance.Pointer();
33 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
36 animation_set_(false),
37 animation_resource_width_(0u),
38 animation_frame_length_ms_(0u),
40 autoshow_tooltip_(false) {
43 ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {}
45 scoped_ptr<base::DictionaryValue>
46 ScreenlockBridge::UserPodCustomIconOptions::ToDictionaryValue() const {
47 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
48 if (!icon_image_ && icon_resource_url_.empty())
52 gfx::ImageSkia icon_skia = icon_image_->AsImageSkia();
53 base::DictionaryValue* icon_representations = new base::DictionaryValue();
54 icon_representations->SetString(
56 webui::GetBitmapDataUrl(
57 icon_skia.GetRepresentation(1.0f).sk_bitmap()));
58 icon_representations->SetString(
60 webui::GetBitmapDataUrl(
61 icon_skia.GetRepresentation(2.0f).sk_bitmap()));
62 result->Set("data", icon_representations);
64 result->SetString("resourceUrl", icon_resource_url_);
67 if (!tooltip_.empty()) {
68 base::DictionaryValue* tooltip_options = new base::DictionaryValue();
69 tooltip_options->SetString("text", tooltip_);
70 tooltip_options->SetBoolean("autoshow", autoshow_tooltip_);
71 result->Set("tooltip", tooltip_options);
74 base::DictionaryValue* size = new base::DictionaryValue();
75 size->SetInteger("height", height_);
76 size->SetInteger("width", width_);
77 result->Set("size", size);
79 result->SetInteger("opacity", opacity_);
82 base::DictionaryValue* animation = new base::DictionaryValue();
83 animation->SetInteger("resourceWidth",
84 animation_resource_width_);
85 animation->SetInteger("frameLengthMs",
86 animation_frame_length_ms_);
87 result->Set("animation", animation);
92 void ScreenlockBridge::UserPodCustomIconOptions::SetIconAsResourceURL(
93 const std::string& url) {
96 icon_resource_url_ = url;
99 void ScreenlockBridge::UserPodCustomIconOptions::SetIconAsImage(
100 const gfx::Image& image) {
101 DCHECK(icon_resource_url_.empty());
103 icon_image_.reset(new gfx::Image(image));
104 SetSize(image.Width(), image.Height());
107 void ScreenlockBridge::UserPodCustomIconOptions::SetSize(size_t icon_width,
108 size_t icon_height) {
110 height_ = icon_height;
113 void ScreenlockBridge::UserPodCustomIconOptions::SetAnimation(
114 size_t resource_width,
115 size_t frame_length_ms) {
116 animation_set_ = true;
117 animation_resource_width_ = resource_width;
118 animation_frame_length_ms_ = frame_length_ms;
121 void ScreenlockBridge::UserPodCustomIconOptions::SetOpacity(size_t opacity) {
122 DCHECK_LE(opacity, 100u);
126 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
127 const base::string16& tooltip,
130 autoshow_tooltip_ = autoshow;
134 std::string ScreenlockBridge::GetAuthenticatedUserEmail(Profile* profile) {
135 // |profile| has to be a signed-in profile with SigninManager already
136 // created. Otherwise, just crash to collect stack.
137 SigninManagerBase* signin_manager =
138 SigninManagerFactory::GetForProfileIfExists(profile);
139 return signin_manager->GetAuthenticatedUsername();
142 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL) {
145 ScreenlockBridge::~ScreenlockBridge() {
148 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
149 DCHECK(lock_handler_ == NULL || lock_handler == NULL);
150 lock_handler_ = lock_handler;
152 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock());
154 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock());
157 bool ScreenlockBridge::IsLocked() const {
158 return lock_handler_ != NULL;
161 void ScreenlockBridge::Lock(Profile* profile) {
162 #if defined(OS_CHROMEOS)
163 chromeos::SessionManagerClient* session_manager =
164 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
165 session_manager->RequestLockScreen();
167 profiles::LockProfile(profile);
171 void ScreenlockBridge::Unlock(Profile* profile) {
173 lock_handler_->Unlock(GetAuthenticatedUserEmail(profile));
176 void ScreenlockBridge::AddObserver(Observer* observer) {
177 observers_.AddObserver(observer);
180 void ScreenlockBridge::RemoveObserver(Observer* observer) {
181 observers_.RemoveObserver(observer);