Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / screenlock_bridge.cc
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.
4
5 #include "chrome/browser/signin/screenlock_bridge.h"
6
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"
15
16 #if defined(OS_CHROMEOS)
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/session_manager_client.h"
19 #endif
20
21 namespace {
22
23 base::LazyInstance<ScreenlockBridge> g_screenlock_bridge_bridge_instance =
24     LAZY_INSTANCE_INITIALIZER;
25
26 }  // namespace
27
28 // static
29 ScreenlockBridge* ScreenlockBridge::Get() {
30   return g_screenlock_bridge_bridge_instance.Pointer();
31 }
32
33 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
34     : width_(0u),
35       height_(0u),
36       animation_set_(false),
37       animation_resource_width_(0u),
38       animation_frame_length_ms_(0u),
39       opacity_(100u),
40       autoshow_tooltip_(false),
41       hardlock_on_click_(false) {
42 }
43
44 ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {}
45
46 scoped_ptr<base::DictionaryValue>
47 ScreenlockBridge::UserPodCustomIconOptions::ToDictionaryValue() const {
48   scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
49   if (!icon_image_ && icon_resource_url_.empty())
50     return result.Pass();
51
52   if (icon_image_) {
53     gfx::ImageSkia icon_skia = icon_image_->AsImageSkia();
54     base::DictionaryValue* icon_representations = new base::DictionaryValue();
55     icon_representations->SetString(
56         "scale1x",
57         webui::GetBitmapDataUrl(
58             icon_skia.GetRepresentation(1.0f).sk_bitmap()));
59     icon_representations->SetString(
60         "scale2x",
61         webui::GetBitmapDataUrl(
62             icon_skia.GetRepresentation(2.0f).sk_bitmap()));
63     result->Set("data", icon_representations);
64   } else {
65     result->SetString("resourceUrl", icon_resource_url_);
66   }
67
68   if (!tooltip_.empty()) {
69     base::DictionaryValue* tooltip_options = new base::DictionaryValue();
70     tooltip_options->SetString("text", tooltip_);
71     tooltip_options->SetBoolean("autoshow", autoshow_tooltip_);
72     result->Set("tooltip", tooltip_options);
73   }
74
75   base::DictionaryValue* size = new base::DictionaryValue();
76   size->SetInteger("height", height_);
77   size->SetInteger("width", width_);
78   result->Set("size", size);
79
80   result->SetInteger("opacity", opacity_);
81
82   if (animation_set_) {
83     base::DictionaryValue* animation = new base::DictionaryValue();
84     animation->SetInteger("resourceWidth",
85                           animation_resource_width_);
86     animation->SetInteger("frameLengthMs",
87                           animation_frame_length_ms_);
88     result->Set("animation", animation);
89   }
90
91   if (hardlock_on_click_)
92     result->SetBoolean("hardlockOnClick", true);
93
94   return result.Pass();
95 }
96
97 void ScreenlockBridge::UserPodCustomIconOptions::SetIconAsResourceURL(
98     const std::string& url) {
99   DCHECK(!icon_image_);
100
101   icon_resource_url_ = url;
102 }
103
104 void ScreenlockBridge::UserPodCustomIconOptions::SetIconAsImage(
105     const gfx::Image& image) {
106   DCHECK(icon_resource_url_.empty());
107
108   icon_image_.reset(new gfx::Image(image));
109   SetSize(image.Width(), image.Height());
110 }
111
112 void ScreenlockBridge::UserPodCustomIconOptions::SetSize(size_t icon_width,
113                                                          size_t icon_height) {
114   width_ = icon_width;
115   height_ = icon_height;
116 }
117
118 void ScreenlockBridge::UserPodCustomIconOptions::SetAnimation(
119     size_t resource_width,
120     size_t frame_length_ms) {
121   animation_set_ = true;
122   animation_resource_width_ = resource_width;
123   animation_frame_length_ms_ = frame_length_ms;
124 }
125
126 void ScreenlockBridge::UserPodCustomIconOptions::SetOpacity(size_t opacity) {
127   DCHECK_LE(opacity, 100u);
128   opacity_ = opacity;
129 }
130
131 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
132     const base::string16& tooltip,
133     bool autoshow) {
134   tooltip_ = tooltip;
135   autoshow_tooltip_ = autoshow;
136 }
137
138 void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
139   hardlock_on_click_ = true;
140 }
141
142 // static
143 std::string ScreenlockBridge::GetAuthenticatedUserEmail(Profile* profile) {
144   // |profile| has to be a signed-in profile with SigninManager already
145   // created. Otherwise, just crash to collect stack.
146   SigninManagerBase* signin_manager =
147       SigninManagerFactory::GetForProfileIfExists(profile);
148   return signin_manager->GetAuthenticatedUsername();
149 }
150
151 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL) {
152 }
153
154 ScreenlockBridge::~ScreenlockBridge() {
155 }
156
157 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
158   DCHECK(lock_handler_ == NULL || lock_handler == NULL);
159   lock_handler_ = lock_handler;
160   if (lock_handler_)
161     FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock());
162   else
163     FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock());
164 }
165
166 bool ScreenlockBridge::IsLocked() const {
167   return lock_handler_ != NULL;
168 }
169
170 void ScreenlockBridge::Lock(Profile* profile) {
171 #if defined(OS_CHROMEOS)
172   chromeos::SessionManagerClient* session_manager =
173       chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
174   session_manager->RequestLockScreen();
175 #else
176   profiles::LockProfile(profile);
177 #endif
178 }
179
180 void ScreenlockBridge::Unlock(Profile* profile) {
181   if (lock_handler_)
182     lock_handler_->Unlock(GetAuthenticatedUserEmail(profile));
183 }
184
185 void ScreenlockBridge::AddObserver(Observer* observer) {
186   observers_.AddObserver(observer);
187 }
188
189 void ScreenlockBridge::RemoveObserver(Observer* observer) {
190   observers_.RemoveObserver(observer);
191 }