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"
25 class EasyUnlockAppManagerImpl
: public EasyUnlockAppManager
{
27 EasyUnlockAppManagerImpl(extensions::ExtensionSystem
* extension_system
,
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
,
40 bool data_ready
) override
;
41 bool SendAuthAttemptEvent() override
;
44 extensions::ExtensionSystem
* extension_system_
;
46 // The Easy Unlock app 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
,
59 const base::FilePath
& app_path
)
60 : extension_system_(extension_system
),
61 app_id_(extension_misc::kEasyUnlockAppId
),
62 manifest_id_(manifest_id
),
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
)
79 const extensions::Extension
* extension
=
80 extension_service
->GetExtensionById(app_id_
, false);
84 OpenApplication(AppLaunchParams(extension_service
->profile(), extension
,
85 extensions::LAUNCH_CONTAINER_WINDOW
,
87 extensions::SOURCE_CHROME_INTERNAL
));
90 void EasyUnlockAppManagerImpl::LoadApp() {
91 ExtensionService
* extension_service
= extension_system_
->extension_service();
92 if (!extension_service
)
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
)) {
104 if (app_path_
.empty())
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
)
119 if (!extension_service
->component_loader()->Exists(app_id_
))
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
)
131 if (!extension_service
->component_loader()->Exists(app_id_
))
134 extension_service
->ReloadExtension(app_id_
);
137 bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string
& user_id
,
140 extensions::EventRouter
* event_router
= extension_system_
->event_router();
144 std::string event_name
=
145 extensions::api::easy_unlock_private::OnUserInfoUpdated::kEventName
;
147 if (!event_router
->ExtensionHasEventListener(app_id_
, event_name
))
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());
165 bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() {
166 extensions::EventRouter
* event_router
= extension_system_
->event_router();
170 std::string event_name
=
171 extensions::api::screenlock_private::OnAuthAttempted::kEventName
;
173 if (!event_router
->HasEventListener(event_name
))
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());
191 EasyUnlockAppManager::~EasyUnlockAppManager() {
195 scoped_ptr
<EasyUnlockAppManager
> EasyUnlockAppManager::Create(
196 extensions::ExtensionSystem
* extension_system
,
198 const base::FilePath
& app_path
) {
199 return scoped_ptr
<EasyUnlockAppManager
>(
200 new EasyUnlockAppManagerImpl(extension_system
, manifest_id
, app_path
));