Componentize HistoryURLProvider/ScoredHistoryMatch.
[chromium-blink-merge.git] / chrome / browser / signin / easy_unlock_app_manager.cc
blob06ee826b085da2344911211df157b9e1a3185f79
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"
22 #endif
24 namespace {
26 class EasyUnlockAppManagerImpl : public EasyUnlockAppManager {
27 public:
28 EasyUnlockAppManagerImpl(extensions::ExtensionSystem* extension_system,
29 int manifest_id,
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,
40 bool is_logged_in,
41 bool data_ready) override;
42 bool SendAuthAttemptEvent() override;
44 private:
45 extensions::ExtensionSystem* extension_system_;
47 // The Easy Unlock app id.
48 std::string app_id_;
49 int manifest_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,
59 int manifest_id,
60 const base::FilePath& app_path)
61 : extension_system_(extension_system),
62 app_id_(extension_misc::kEasyUnlockAppId),
63 manifest_id_(manifest_id),
64 app_path_(app_path) {
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)
78 return;
80 const extensions::Extension* extension =
81 extension_service->GetExtensionById(app_id_, false);
82 if (!extension)
83 return;
85 OpenApplication(AppLaunchParams(extension_service->profile(), extension,
86 extensions::LAUNCH_CONTAINER_WINDOW,
87 NEW_WINDOW,
88 extensions::SOURCE_CHROME_INTERNAL));
91 void EasyUnlockAppManagerImpl::LoadApp() {
92 ExtensionService* extension_service = extension_system_->extension_service();
93 if (!extension_service)
94 return;
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)) {
101 return;
103 #endif
105 if (app_path_.empty())
106 return;
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)
118 return;
120 if (!extension_service->component_loader()->Exists(app_id_))
121 return;
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)
130 return;
132 if (!extension_service->component_loader()->Exists(app_id_))
133 return;
135 extension_service->ReloadExtension(app_id_);
138 bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string& user_id,
139 bool is_logged_in,
140 bool data_ready) {
141 ExtensionService* extension_service = extension_system_->extension_service();
142 if (!extension_service)
143 return false;
145 extensions::EventRouter* event_router =
146 extensions::EventRouter::Get(extension_service->GetBrowserContext());
147 if (!event_router)
148 return false;
150 std::string event_name =
151 extensions::api::easy_unlock_private::OnUserInfoUpdated::kEventName;
153 if (!event_router->ExtensionHasEventListener(app_id_, event_name))
154 return false;
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());
168 return true;
171 bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() {
172 ExtensionService* extension_service = extension_system_->extension_service();
173 if (!extension_service)
174 return false;
176 extensions::EventRouter* event_router =
177 extensions::EventRouter::Get(extension_service->GetBrowserContext());
178 if (!event_router)
179 return false;
181 std::string event_name =
182 extensions::api::screenlock_private::OnAuthAttempted::kEventName;
184 if (!event_router->HasEventListener(event_name))
185 return false;
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());
197 return true;
200 } // namespace
202 EasyUnlockAppManager::~EasyUnlockAppManager() {
205 // static
206 scoped_ptr<EasyUnlockAppManager> EasyUnlockAppManager::Create(
207 extensions::ExtensionSystem* extension_system,
208 int manifest_id,
209 const base::FilePath& app_path) {
210 return scoped_ptr<EasyUnlockAppManager>(
211 new EasyUnlockAppManagerImpl(extension_system, manifest_id, app_path));