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 "base/location.h"
9 #include "chrome/browser/extensions/component_loader.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/ui/extensions/application_launch.h"
12 #include "chrome/common/extensions/api/easy_unlock_private.h"
13 #include "chrome/common/extensions/api/screenlock_private.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "components/proximity_auth/switches.h"
16 #include "extensions/browser/event_router.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/one_shot_event.h"
20 #if defined(OS_CHROMEOS)
21 #include "base/sys_info.h"
26 class EasyUnlockAppManagerImpl
: public EasyUnlockAppManager
{
28 EasyUnlockAppManagerImpl(extensions::ExtensionSystem
* extension_system
,
30 const base::FilePath
& app_path
);
31 ~EasyUnlockAppManagerImpl() override
;
33 // EasyUnlockAppManager overrides.
34 void EnsureReady(const base::Closure
& ready_callback
) override
;
35 void LaunchSetup() override
;
36 void LoadApp() override
;
37 void DisableAppIfLoaded() override
;
38 void ReloadApp() override
;
39 bool SendUserUpdatedEvent(const std::string
& user_id
,
41 bool data_ready
) override
;
42 bool SendAuthAttemptEvent() override
;
45 extensions::ExtensionSystem
* extension_system_
;
47 // The Easy Unlock app id.
51 // The path from which Easy Unlock app should be loaded.
52 base::FilePath app_path_
;
54 DISALLOW_COPY_AND_ASSIGN(EasyUnlockAppManagerImpl
);
57 EasyUnlockAppManagerImpl::EasyUnlockAppManagerImpl(
58 extensions::ExtensionSystem
* extension_system
,
60 const base::FilePath
& app_path
)
61 : extension_system_(extension_system
),
62 app_id_(extension_misc::kEasyUnlockAppId
),
63 manifest_id_(manifest_id
),
67 EasyUnlockAppManagerImpl::~EasyUnlockAppManagerImpl() {
70 void EasyUnlockAppManagerImpl::EnsureReady(
71 const base::Closure
& ready_callback
) {
72 extension_system_
->ready().Post(FROM_HERE
, ready_callback
);
75 void EasyUnlockAppManagerImpl::LaunchSetup() {
76 ExtensionService
* extension_service
= extension_system_
->extension_service();
77 if (!extension_service
)
80 const extensions::Extension
* extension
=
81 extension_service
->GetExtensionById(app_id_
, false);
85 OpenApplication(AppLaunchParams(extension_service
->profile(), extension
,
86 extensions::LAUNCH_CONTAINER_WINDOW
,
88 extensions::SOURCE_CHROME_INTERNAL
));
91 void EasyUnlockAppManagerImpl::LoadApp() {
92 ExtensionService
* extension_service
= extension_system_
->extension_service();
93 if (!extension_service
)
96 #if defined(OS_CHROMEOS)
97 // TODO(xiyuan): Remove this when the app is bundled with chrome.
98 if (!base::SysInfo::IsRunningOnChromeOS() &&
99 !base::CommandLine::ForCurrentProcess()->HasSwitch(
100 proximity_auth::switches::kForceLoadEasyUnlockAppInTests
)) {
105 if (app_path_
.empty())
108 extensions::ComponentLoader
* loader
= extension_service
->component_loader();
109 if (!loader
->Exists(app_id_
))
110 app_id_
= loader
->Add(manifest_id_
, app_path_
);
112 extension_service
->EnableExtension(app_id_
);
115 void EasyUnlockAppManagerImpl::DisableAppIfLoaded() {
116 ExtensionService
* extension_service
= extension_system_
->extension_service();
117 if (!extension_service
)
120 if (!extension_service
->component_loader()->Exists(app_id_
))
123 extension_service
->DisableExtension(app_id_
,
124 extensions::Extension::DISABLE_RELOAD
);
127 void EasyUnlockAppManagerImpl::ReloadApp() {
128 ExtensionService
* extension_service
= extension_system_
->extension_service();
129 if (!extension_service
)
132 if (!extension_service
->component_loader()->Exists(app_id_
))
135 extension_service
->ReloadExtension(app_id_
);
138 bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string
& user_id
,
141 ExtensionService
* extension_service
= extension_system_
->extension_service();
142 if (!extension_service
)
145 extensions::EventRouter
* event_router
=
146 extensions::EventRouter::Get(extension_service
->GetBrowserContext());
150 std::string event_name
=
151 extensions::api::easy_unlock_private::OnUserInfoUpdated::kEventName
;
153 if (!event_router
->ExtensionHasEventListener(app_id_
, event_name
))
156 extensions::api::easy_unlock_private::UserInfo info
;
157 info
.user_id
= user_id
;
158 info
.logged_in
= is_logged_in
;
159 info
.data_ready
= data_ready
;
161 scoped_ptr
<base::ListValue
> args(new base::ListValue());
162 args
->Append(info
.ToValue().release());
164 scoped_ptr
<extensions::Event
> event(
165 new extensions::Event(event_name
, args
.Pass()));
167 event_router
->DispatchEventToExtension(app_id_
, event
.Pass());
171 bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() {
172 ExtensionService
* extension_service
= extension_system_
->extension_service();
173 if (!extension_service
)
176 extensions::EventRouter
* event_router
=
177 extensions::EventRouter::Get(extension_service
->GetBrowserContext());
181 std::string event_name
=
182 extensions::api::screenlock_private::OnAuthAttempted::kEventName
;
184 if (!event_router
->HasEventListener(event_name
))
187 scoped_ptr
<base::ListValue
> args(new base::ListValue());
188 args
->AppendString(extensions::api::screenlock_private::ToString(
189 extensions::api::screenlock_private::AUTH_TYPE_USERCLICK
));
190 args
->AppendString(std::string());
192 scoped_ptr
<extensions::Event
> event(
193 new extensions::Event(event_name
, args
.Pass()));
195 // TODO(tbarzic): Restrict this to EasyUnlock app.
196 event_router
->BroadcastEvent(event
.Pass());
202 EasyUnlockAppManager::~EasyUnlockAppManager() {
206 scoped_ptr
<EasyUnlockAppManager
> EasyUnlockAppManager::Create(
207 extensions::ExtensionSystem
* extension_system
,
209 const base::FilePath
& app_path
) {
210 return scoped_ptr
<EasyUnlockAppManager
>(
211 new EasyUnlockAppManagerImpl(extension_system
, manifest_id
, app_path
));