ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / signin / easy_unlock_app_manager.cc
blobf9469b784a740bcd50a23f13b9548dbfa9c1d9c8
1 // Copyright 2015 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/easy_unlock_app_manager.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/extensions/component_loader.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/ui/extensions/application_launch.h"
11 #include "chrome/common/extensions/api/easy_unlock_private.h"
12 #include "chrome/common/extensions/api/screenlock_private.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "components/proximity_auth/switches.h"
15 #include "extensions/browser/event_router.h"
16 #include "extensions/browser/extension_system.h"
17 #include "extensions/common/one_shot_event.h"
19 #if defined(OS_CHROMEOS)
20 #include "base/sys_info.h"
21 #endif
23 namespace {
25 class EasyUnlockAppManagerImpl : public EasyUnlockAppManager {
26 public:
27 EasyUnlockAppManagerImpl(extensions::ExtensionSystem* extension_system,
28 int manifest_id,
29 const base::FilePath& app_path);
30 ~EasyUnlockAppManagerImpl() override;
32 // EasyUnlockAppManager overrides.
33 void EnsureReady(const base::Closure& ready_callback) override;
34 void LaunchSetup() override;
35 void LoadApp() override;
36 void DisableAppIfLoaded() override;
37 void ReloadApp() override;
38 bool SendUserUpdatedEvent(const std::string& user_id,
39 bool is_logged_in,
40 bool data_ready) override;
41 bool SendAuthAttemptEvent() override;
43 private:
44 extensions::ExtensionSystem* extension_system_;
46 // The Easy Unlock app id.
47 std::string app_id_;
48 int manifest_id_;
50 // The path from which Easy Unlock app should be loaded.
51 base::FilePath app_path_;
53 DISALLOW_COPY_AND_ASSIGN(EasyUnlockAppManagerImpl);
56 EasyUnlockAppManagerImpl::EasyUnlockAppManagerImpl(
57 extensions::ExtensionSystem* extension_system,
58 int manifest_id,
59 const base::FilePath& app_path)
60 : extension_system_(extension_system),
61 app_id_(extension_misc::kEasyUnlockAppId),
62 manifest_id_(manifest_id),
63 app_path_(app_path) {
66 EasyUnlockAppManagerImpl::~EasyUnlockAppManagerImpl() {
69 void EasyUnlockAppManagerImpl::EnsureReady(
70 const base::Closure& ready_callback) {
71 extension_system_->ready().Post(FROM_HERE, ready_callback);
74 void EasyUnlockAppManagerImpl::LaunchSetup() {
75 ExtensionService* extension_service = extension_system_->extension_service();
76 if (!extension_service)
77 return;
79 const extensions::Extension* extension =
80 extension_service->GetExtensionById(app_id_, false);
81 if (!extension)
82 return;
84 OpenApplication(AppLaunchParams(extension_service->profile(), extension,
85 extensions::LAUNCH_CONTAINER_WINDOW,
86 NEW_WINDOW,
87 extensions::SOURCE_CHROME_INTERNAL));
90 void EasyUnlockAppManagerImpl::LoadApp() {
91 ExtensionService* extension_service = extension_system_->extension_service();
92 if (!extension_service)
93 return;
95 #if defined(OS_CHROMEOS)
96 // TODO(xiyuan): Remove this when the app is bundled with chrome.
97 if (!base::SysInfo::IsRunningOnChromeOS() &&
98 !base::CommandLine::ForCurrentProcess()->HasSwitch(
99 proximity_auth::switches::kForceLoadEasyUnlockAppInTests)) {
100 return;
102 #endif
104 if (app_path_.empty())
105 return;
107 extensions::ComponentLoader* loader = extension_service->component_loader();
108 if (!loader->Exists(app_id_))
109 app_id_ = loader->Add(manifest_id_, app_path_);
111 extension_service->EnableExtension(app_id_);
114 void EasyUnlockAppManagerImpl::DisableAppIfLoaded() {
115 ExtensionService* extension_service = extension_system_->extension_service();
116 if (!extension_service)
117 return;
119 if (!extension_service->component_loader()->Exists(app_id_))
120 return;
122 extension_service->DisableExtension(app_id_,
123 extensions::Extension::DISABLE_RELOAD);
126 void EasyUnlockAppManagerImpl::ReloadApp() {
127 ExtensionService* extension_service = extension_system_->extension_service();
128 if (!extension_service)
129 return;
131 if (!extension_service->component_loader()->Exists(app_id_))
132 return;
134 extension_service->ReloadExtension(app_id_);
137 bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string& user_id,
138 bool is_logged_in,
139 bool data_ready) {
140 extensions::EventRouter* event_router = extension_system_->event_router();
141 if (!event_router)
142 return false;
144 std::string event_name =
145 extensions::api::easy_unlock_private::OnUserInfoUpdated::kEventName;
147 if (!event_router->ExtensionHasEventListener(app_id_, event_name))
148 return false;
150 extensions::api::easy_unlock_private::UserInfo info;
151 info.user_id = user_id;
152 info.logged_in = is_logged_in;
153 info.data_ready = data_ready;
155 scoped_ptr<base::ListValue> args(new base::ListValue());
156 args->Append(info.ToValue().release());
158 scoped_ptr<extensions::Event> event(
159 new extensions::Event(event_name, args.Pass()));
161 event_router->DispatchEventToExtension(app_id_, event.Pass());
162 return true;
165 bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() {
166 extensions::EventRouter* event_router = extension_system_->event_router();
167 if (!event_router)
168 return false;
170 std::string event_name =
171 extensions::api::screenlock_private::OnAuthAttempted::kEventName;
173 if (!event_router->HasEventListener(event_name))
174 return false;
176 scoped_ptr<base::ListValue> args(new base::ListValue());
177 args->AppendString(extensions::api::screenlock_private::ToString(
178 extensions::api::screenlock_private::AUTH_TYPE_USERCLICK));
179 args->AppendString(std::string());
181 scoped_ptr<extensions::Event> event(
182 new extensions::Event(event_name, args.Pass()));
184 // TODO(tbarzic): Restrict this to EasyUnlock app.
185 event_router->BroadcastEvent(event.Pass());
186 return true;
189 } // namespace
191 EasyUnlockAppManager::~EasyUnlockAppManager() {
194 // static
195 scoped_ptr<EasyUnlockAppManager> EasyUnlockAppManager::Create(
196 extensions::ExtensionSystem* extension_system,
197 int manifest_id,
198 const base::FilePath& app_path) {
199 return scoped_ptr<EasyUnlockAppManager>(
200 new EasyUnlockAppManagerImpl(extension_system, manifest_id, app_path));