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/api/screenlock_private/screenlock_private_api.h"
10 #include "chrome/browser/extensions/component_loader.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/extensions/application_launch.h"
14 #include "chrome/common/extensions/api/easy_unlock_private.h"
15 #include "chrome/common/extensions/extension_constants.h"
16 #include "components/proximity_auth/switches.h"
17 #include "extensions/browser/event_router.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/common/one_shot_event.h"
21 #if defined(OS_CHROMEOS)
22 #include "base/sys_info.h"
27 class EasyUnlockAppManagerImpl
: public EasyUnlockAppManager
{
29 EasyUnlockAppManagerImpl(extensions::ExtensionSystem
* extension_system
,
31 const base::FilePath
& app_path
);
32 ~EasyUnlockAppManagerImpl() override
;
34 // EasyUnlockAppManager overrides.
35 void EnsureReady(const base::Closure
& ready_callback
) override
;
36 void LaunchSetup() override
;
37 void LoadApp() override
;
38 void DisableAppIfLoaded() override
;
39 void ReloadApp() override
;
40 bool SendUserUpdatedEvent(const std::string
& user_id
,
42 bool data_ready
) override
;
43 bool SendAuthAttemptEvent() override
;
46 extensions::ExtensionSystem
* extension_system_
;
48 // The Easy Unlock app id.
52 // The path from which Easy Unlock app should be loaded.
53 base::FilePath app_path_
;
55 DISALLOW_COPY_AND_ASSIGN(EasyUnlockAppManagerImpl
);
58 EasyUnlockAppManagerImpl::EasyUnlockAppManagerImpl(
59 extensions::ExtensionSystem
* extension_system
,
61 const base::FilePath
& app_path
)
62 : extension_system_(extension_system
),
63 app_id_(extension_misc::kEasyUnlockAppId
),
64 manifest_id_(manifest_id
),
68 EasyUnlockAppManagerImpl::~EasyUnlockAppManagerImpl() {
71 void EasyUnlockAppManagerImpl::EnsureReady(
72 const base::Closure
& ready_callback
) {
73 extension_system_
->ready().Post(FROM_HERE
, ready_callback
);
76 void EasyUnlockAppManagerImpl::LaunchSetup() {
77 ExtensionService
* extension_service
= extension_system_
->extension_service();
78 if (!extension_service
)
81 const extensions::Extension
* extension
=
82 extension_service
->GetExtensionById(app_id_
, false);
86 OpenApplication(AppLaunchParams(extension_service
->profile(), extension
,
87 extensions::LAUNCH_CONTAINER_WINDOW
,
89 extensions::SOURCE_CHROME_INTERNAL
));
92 void EasyUnlockAppManagerImpl::LoadApp() {
93 ExtensionService
* extension_service
= extension_system_
->extension_service();
94 if (!extension_service
)
97 #if defined(OS_CHROMEOS)
98 // TODO(xiyuan): Remove this when the app is bundled with chrome.
99 if (!base::SysInfo::IsRunningOnChromeOS() &&
100 !base::CommandLine::ForCurrentProcess()->HasSwitch(
101 proximity_auth::switches::kForceLoadEasyUnlockAppInTests
)) {
106 if (app_path_
.empty())
109 extensions::ComponentLoader
* loader
= extension_service
->component_loader();
110 if (!loader
->Exists(app_id_
))
111 app_id_
= loader
->Add(manifest_id_
, app_path_
);
113 extension_service
->EnableExtension(app_id_
);
116 void EasyUnlockAppManagerImpl::DisableAppIfLoaded() {
117 ExtensionService
* extension_service
= extension_system_
->extension_service();
118 if (!extension_service
)
121 if (!extension_service
->component_loader()->Exists(app_id_
))
124 extension_service
->DisableExtension(app_id_
,
125 extensions::Extension::DISABLE_RELOAD
);
128 void EasyUnlockAppManagerImpl::ReloadApp() {
129 ExtensionService
* extension_service
= extension_system_
->extension_service();
130 if (!extension_service
)
133 if (!extension_service
->component_loader()->Exists(app_id_
))
136 extension_service
->ReloadExtension(app_id_
);
139 bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string
& user_id
,
142 ExtensionService
* extension_service
= extension_system_
->extension_service();
143 if (!extension_service
)
146 extensions::EventRouter
* event_router
=
147 extensions::EventRouter::Get(extension_service
->GetBrowserContext());
151 extensions::events::HistogramValue histogram_value
=
152 extensions::events::EASY_UNLOCK_PRIVATE_ON_USER_INFO_UPDATED
;
153 std::string event_name
=
154 extensions::api::easy_unlock_private::OnUserInfoUpdated::kEventName
;
156 if (!event_router
->ExtensionHasEventListener(app_id_
, event_name
))
159 extensions::api::easy_unlock_private::UserInfo info
;
160 info
.user_id
= user_id
;
161 info
.logged_in
= is_logged_in
;
162 info
.data_ready
= data_ready
;
164 scoped_ptr
<base::ListValue
> args(new base::ListValue());
165 args
->Append(info
.ToValue().release());
167 scoped_ptr
<extensions::Event
> event(
168 new extensions::Event(histogram_value
, event_name
, args
.Pass()));
170 event_router
->DispatchEventToExtension(app_id_
, event
.Pass());
174 bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() {
175 ExtensionService
* extension_service
= extension_system_
->extension_service();
176 if (!extension_service
)
179 // TODO(tbarzic): Restrict this to EasyUnlock app.
180 extensions::ScreenlockPrivateEventRouter
* screenlock_router
=
181 extensions::ScreenlockPrivateEventRouter::GetFactoryInstance()->Get(
182 extension_service
->profile());
183 return screenlock_router
->OnAuthAttempted(
184 proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK
, std::string());
189 EasyUnlockAppManager::~EasyUnlockAppManager() {
193 scoped_ptr
<EasyUnlockAppManager
> EasyUnlockAppManager::Create(
194 extensions::ExtensionSystem
* extension_system
,
196 const base::FilePath
& app_path
) {
197 return scoped_ptr
<EasyUnlockAppManager
>(
198 new EasyUnlockAppManagerImpl(extension_system
, manifest_id
, app_path
));