Upstream version 10.39.225.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
13 #if defined(OS_CHROMEOS)
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/session_manager_client.h"
16 #endif
17
18 namespace {
19
20 base::LazyInstance<ScreenlockBridge> g_screenlock_bridge_bridge_instance =
21     LAZY_INSTANCE_INITIALIZER;
22
23 }  // namespace
24
25 // static
26 ScreenlockBridge* ScreenlockBridge::Get() {
27   return g_screenlock_bridge_bridge_instance.Pointer();
28 }
29
30 ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
31     : width_(0u),
32       height_(0u),
33       animation_set_(false),
34       animation_resource_width_(0u),
35       animation_frame_length_ms_(0u),
36       opacity_(100u),
37       autoshow_tooltip_(false),
38       hardlock_on_click_(false) {
39 }
40
41 ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {}
42
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())
47     return result.Pass();
48
49   result->SetString("resourceUrl", icon_resource_url_);
50
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);
56   }
57
58   base::DictionaryValue* size = new base::DictionaryValue();
59   size->SetInteger("height", height_);
60   size->SetInteger("width", width_);
61   result->Set("size", size);
62
63   result->SetInteger("opacity", opacity_);
64
65   if (animation_set_) {
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);
72   }
73
74   if (hardlock_on_click_)
75     result->SetBoolean("hardlockOnClick", true);
76
77   return result.Pass();
78 }
79
80 void ScreenlockBridge::UserPodCustomIconOptions::SetIconAsResourceURL(
81     const std::string& url) {
82   icon_resource_url_ = url;
83 }
84
85
86 void ScreenlockBridge::UserPodCustomIconOptions::SetSize(size_t icon_width,
87                                                          size_t icon_height) {
88   width_ = icon_width;
89   height_ = icon_height;
90 }
91
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;
98 }
99
100 void ScreenlockBridge::UserPodCustomIconOptions::SetOpacity(size_t opacity) {
101   DCHECK_LE(opacity, 100u);
102   opacity_ = opacity;
103 }
104
105 void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
106     const base::string16& tooltip,
107     bool autoshow) {
108   tooltip_ = tooltip;
109   autoshow_tooltip_ = autoshow;
110 }
111
112 void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
113   hardlock_on_click_ = true;
114 }
115
116 // static
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();
123 }
124
125 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL) {
126 }
127
128 ScreenlockBridge::~ScreenlockBridge() {
129 }
130
131 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
132   DCHECK(lock_handler_ == NULL || lock_handler == NULL);
133   lock_handler_ = lock_handler;
134   if (lock_handler_)
135     FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock());
136   else
137     FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock());
138 }
139
140 void ScreenlockBridge::SetFocusedUser(const std::string& user_id) {
141   if (user_id == focused_user_id_)
142     return;
143   focused_user_id_ = user_id;
144   FOR_EACH_OBSERVER(Observer, observers_, OnFocusedUserChanged(user_id));
145 }
146
147 bool ScreenlockBridge::IsLocked() const {
148   return lock_handler_ != NULL;
149 }
150
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();
156 #else
157   profiles::LockProfile(profile);
158 #endif
159 }
160
161 void ScreenlockBridge::Unlock(Profile* profile) {
162   if (lock_handler_)
163     lock_handler_->Unlock(GetAuthenticatedUserEmail(profile));
164 }
165
166 void ScreenlockBridge::AddObserver(Observer* observer) {
167   observers_.AddObserver(observer);
168 }
169
170 void ScreenlockBridge::RemoveObserver(Observer* observer) {
171   observers_.RemoveObserver(observer);
172 }