Support SizeClassIdiom on iOS7.
[chromium-blink-merge.git] / chrome / browser / signin / easy_unlock_app_manager.cc
blob62bdf66948174e1af899c59f64c9f7e712176aa3
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"
23 #endif
25 namespace {
27 class EasyUnlockAppManagerImpl : public EasyUnlockAppManager {
28 public:
29 EasyUnlockAppManagerImpl(extensions::ExtensionSystem* extension_system,
30 int manifest_id,
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,
41 bool is_logged_in,
42 bool data_ready) override;
43 bool SendAuthAttemptEvent() override;
45 private:
46 extensions::ExtensionSystem* extension_system_;
48 // The Easy Unlock app id.
49 std::string app_id_;
50 int manifest_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,
60 int manifest_id,
61 const base::FilePath& app_path)
62 : extension_system_(extension_system),
63 app_id_(extension_misc::kEasyUnlockAppId),
64 manifest_id_(manifest_id),
65 app_path_(app_path) {
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)
79 return;
81 const extensions::Extension* extension =
82 extension_service->GetExtensionById(app_id_, false);
83 if (!extension)
84 return;
86 OpenApplication(AppLaunchParams(extension_service->profile(), extension,
87 extensions::LAUNCH_CONTAINER_WINDOW,
88 NEW_WINDOW,
89 extensions::SOURCE_CHROME_INTERNAL));
92 void EasyUnlockAppManagerImpl::LoadApp() {
93 ExtensionService* extension_service = extension_system_->extension_service();
94 if (!extension_service)
95 return;
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)) {
102 return;
104 #endif
106 if (app_path_.empty())
107 return;
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)
119 return;
121 if (!extension_service->component_loader()->Exists(app_id_))
122 return;
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)
131 return;
133 if (!extension_service->component_loader()->Exists(app_id_))
134 return;
136 extension_service->ReloadExtension(app_id_);
139 bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string& user_id,
140 bool is_logged_in,
141 bool data_ready) {
142 ExtensionService* extension_service = extension_system_->extension_service();
143 if (!extension_service)
144 return false;
146 extensions::EventRouter* event_router =
147 extensions::EventRouter::Get(extension_service->GetBrowserContext());
148 if (!event_router)
149 return false;
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))
157 return false;
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());
171 return true;
174 bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() {
175 ExtensionService* extension_service = extension_system_->extension_service();
176 if (!extension_service)
177 return false;
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());
187 } // namespace
189 EasyUnlockAppManager::~EasyUnlockAppManager() {
192 // static
193 scoped_ptr<EasyUnlockAppManager> EasyUnlockAppManager::Create(
194 extensions::ExtensionSystem* extension_system,
195 int manifest_id,
196 const base::FilePath& app_path) {
197 return scoped_ptr<EasyUnlockAppManager>(
198 new EasyUnlockAppManagerImpl(extension_system, manifest_id, app_path));