1 // Copyright 2014 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_service_factory.h"
7 #include "base/command_line.h"
8 #include "base/memory/singleton.h"
9 #include "chrome/browser/profiles/incognito_helpers.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/easy_unlock_app_manager.h"
12 #include "chrome/browser/signin/easy_unlock_service.h"
13 #include "chrome/browser/signin/easy_unlock_service_regular.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "components/keyed_service/content/browser_context_dependency_manager.h"
16 #include "extensions/browser/extension_system.h"
17 #include "extensions/browser/extension_system_provider.h"
18 #include "extensions/browser/extensions_browser_client.h"
19 #include "grit/browser_resources.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager_factory.h"
23 #include "chrome/browser/chromeos/profiles/profile_helper.h"
24 #include "chrome/browser/signin/easy_unlock_service_signin_chromeos.h"
29 // Gets the file path from which easy unlock app should be loaded.
30 base::FilePath
GetEasyUnlockAppPath() {
31 #if defined(GOOGLE_CHROME_BUILD)
33 // Only allow app path override switch for debug build.
34 const base::CommandLine
* command_line
=
35 base::CommandLine::ForCurrentProcess();
36 if (command_line
->HasSwitch(switches::kEasyUnlockAppPath
))
37 return command_line
->GetSwitchValuePath(switches::kEasyUnlockAppPath
);
38 #endif // !defined(NDEBUG)
40 #if defined(OS_CHROMEOS)
41 return base::FilePath("/usr/share/chromeos-assets/easy_unlock");
42 #endif // defined(OS_CHROMEOS)
43 #endif // defined(GOOGLE_CHROME_BUILD)
45 return base::FilePath();
51 EasyUnlockServiceFactory
* EasyUnlockServiceFactory::GetInstance() {
52 return Singleton
<EasyUnlockServiceFactory
>::get();
56 EasyUnlockService
* EasyUnlockServiceFactory::GetForBrowserContext(
57 content::BrowserContext
* browser_context
) {
58 return static_cast<EasyUnlockService
*>(
59 EasyUnlockServiceFactory::GetInstance()->GetServiceForBrowserContext(
60 browser_context
, true));
63 EasyUnlockServiceFactory::EasyUnlockServiceFactory()
64 : BrowserContextKeyedServiceFactory(
66 BrowserContextDependencyManager::GetInstance()) {
68 extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
69 #if defined(OS_CHROMEOS)
70 DependsOn(EasyUnlockTpmKeyManagerFactory::GetInstance());
74 EasyUnlockServiceFactory::~EasyUnlockServiceFactory() {
77 KeyedService
* EasyUnlockServiceFactory::BuildServiceInstanceFor(
78 content::BrowserContext
* context
) const {
79 EasyUnlockService
* service
= NULL
;
82 #if defined(OS_CHROMEOS)
83 if (chromeos::ProfileHelper::IsSigninProfile(
84 Profile::FromBrowserContext(context
))) {
85 if (!EasyUnlockService::IsSignInEnabled() || !context
->IsOffTheRecord())
88 service
= new EasyUnlockServiceSignin(Profile::FromBrowserContext(context
));
89 manifest_id
= IDR_EASY_UNLOCK_MANIFEST_SIGNIN
;
95 new EasyUnlockServiceRegular(Profile::FromBrowserContext(context
));
96 manifest_id
= IDR_EASY_UNLOCK_MANIFEST
;
99 const base::FilePath app_path
= app_path_for_testing_
.empty()
100 ? GetEasyUnlockAppPath()
101 : app_path_for_testing_
;
103 service
->Initialize(EasyUnlockAppManager::Create(
104 extensions::ExtensionSystem::Get(context
), manifest_id
, app_path
));
108 content::BrowserContext
* EasyUnlockServiceFactory::GetBrowserContextToUse(
109 content::BrowserContext
* context
) const {
110 #if defined(OS_CHROMEOS)
111 if (chromeos::ProfileHelper::IsSigninProfile(
112 Profile::FromBrowserContext(context
))) {
113 return chrome::GetBrowserContextOwnInstanceInIncognito(context
);
116 return chrome::GetBrowserContextRedirectedInIncognito(context
);
119 bool EasyUnlockServiceFactory::ServiceIsCreatedWithBrowserContext() const {
123 bool EasyUnlockServiceFactory::ServiceIsNULLWhileTesting() const {
124 // Don't create the service for TestingProfile used in unit_tests because
125 // EasyUnlockService uses ExtensionSystem::ready().Post, which expects
126 // a MessageLoop that does not exit in many unit_tests cases.