Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / screenlock_private_api.cc
blob431808936abebf2c65194239758fcddce1ef6861
1 // Copyright 2013 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/chromeos/extensions/screenlock_private_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/login/screen_locker.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/extensions/image_loader.h"
12 #include "chrome/common/extensions/api/screenlock_private.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "extensions/browser/event_router.h"
16 namespace screenlock = extensions::api::screenlock_private;
18 namespace extensions {
20 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
22 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
24 bool ScreenlockPrivateGetLockedFunction::RunImpl() {
25 bool locked = false;
26 chromeos::ScreenLocker* locker =
27 chromeos::ScreenLocker::default_screen_locker();
28 if (locker)
29 locked = locker->locked();
30 SetResult(new base::FundamentalValue(locked));
31 SendResponse(error_.empty());
32 return true;
35 ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {}
37 ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {}
39 bool ScreenlockPrivateSetLockedFunction::RunImpl() {
40 scoped_ptr<screenlock::SetLocked::Params> params(
41 screenlock::SetLocked::Params::Create(*args_));
42 EXTENSION_FUNCTION_VALIDATE(params.get());
43 if (params->locked) {
44 chromeos::SessionManagerClient* session_manager =
45 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
46 session_manager->RequestLockScreen();
47 } else {
48 chromeos::ScreenLocker* locker =
49 chromeos::ScreenLocker::default_screen_locker();
50 if (locker)
51 chromeos::ScreenLocker::Hide();
53 SendResponse(error_.empty());
54 return true;
57 ScreenlockPrivateShowMessageFunction::ScreenlockPrivateShowMessageFunction() {}
59 ScreenlockPrivateShowMessageFunction::~ScreenlockPrivateShowMessageFunction() {}
61 bool ScreenlockPrivateShowMessageFunction::RunImpl() {
62 scoped_ptr<screenlock::ShowMessage::Params> params(
63 screenlock::ShowMessage::Params::Create(*args_));
64 EXTENSION_FUNCTION_VALIDATE(params.get());
65 chromeos::ScreenLocker* locker =
66 chromeos::ScreenLocker::default_screen_locker();
67 if (locker)
68 locker->ShowBannerMessage(params->message);
69 SendResponse(error_.empty());
70 return true;
73 static const int kMaxButtonIconSize = 40;
75 ScreenlockPrivateShowButtonFunction::
76 ScreenlockPrivateShowButtonFunction() {}
78 ScreenlockPrivateShowButtonFunction::
79 ~ScreenlockPrivateShowButtonFunction() {}
81 bool ScreenlockPrivateShowButtonFunction::RunImpl() {
82 scoped_ptr<screenlock::ShowButton::Params> params(
83 screenlock::ShowButton::Params::Create(*args_));
84 EXTENSION_FUNCTION_VALIDATE(params.get());
85 chromeos::ScreenLocker* locker =
86 chromeos::ScreenLocker::default_screen_locker();
87 if (!locker) {
88 SendResponse(error_.empty());
89 return true;
91 extensions::ImageLoader* loader = extensions::ImageLoader::Get(GetProfile());
92 loader->LoadImageAsync(
93 GetExtension(), GetExtension()->GetResource(params->icon),
94 gfx::Size(kMaxButtonIconSize, kMaxButtonIconSize),
95 base::Bind(&ScreenlockPrivateShowButtonFunction::OnImageLoaded, this));
96 return true;
99 void ScreenlockPrivateShowButtonFunction::OnImageLoaded(
100 const gfx::Image& image) {
101 chromeos::ScreenLocker* locker =
102 chromeos::ScreenLocker::default_screen_locker();
103 ScreenlockPrivateEventRouter* router =
104 ScreenlockPrivateEventRouter::GetFactoryInstance()->GetForProfile(
105 GetProfile());
106 locker->ShowUserPodButton(
107 GetProfile()->GetProfileName(), image,
108 base::Bind(&ScreenlockPrivateEventRouter::OnButtonClicked,
109 base::Unretained(router)));
110 SendResponse(error_.empty());
113 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(Profile* profile)
114 : profile_(profile) {
115 chromeos::SessionManagerClient* session_manager =
116 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
117 if (!session_manager->HasObserver(this))
118 session_manager->AddObserver(this);
121 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {}
123 void ScreenlockPrivateEventRouter::ScreenIsLocked() {
124 DispatchEvent(screenlock::OnChanged::kEventName,
125 new base::FundamentalValue(true));
128 void ScreenlockPrivateEventRouter::ScreenIsUnlocked() {
129 DispatchEvent(screenlock::OnChanged::kEventName,
130 new base::FundamentalValue(false));
133 void ScreenlockPrivateEventRouter::DispatchEvent(
134 const std::string& event_name,
135 base::Value* arg) {
136 scoped_ptr<base::ListValue> args(new base::ListValue());
137 if (arg)
138 args->Append(arg);
139 scoped_ptr<extensions::Event> event(new extensions::Event(
140 event_name, args.Pass()));
141 extensions::ExtensionSystem::Get(profile_)->event_router()->
142 BroadcastEvent(event.Pass());
145 static base::LazyInstance<extensions::ProfileKeyedAPIFactory<
146 ScreenlockPrivateEventRouter> >
147 g_factory = LAZY_INSTANCE_INITIALIZER;
149 // static
150 extensions::ProfileKeyedAPIFactory<ScreenlockPrivateEventRouter>*
151 ScreenlockPrivateEventRouter::GetFactoryInstance() {
152 return &g_factory.Get();
155 void ScreenlockPrivateEventRouter::Shutdown() {
156 chromeos::SessionManagerClient* session_manager =
157 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
158 if (session_manager->HasObserver(this))
159 session_manager->RemoveObserver(this);
162 void ScreenlockPrivateEventRouter::OnButtonClicked() {
163 DispatchEvent(screenlock::OnButtonClicked::kEventName, NULL);
166 } // namespace extensions