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/services/gcm/gcm_profile_service_factory.h"
12 #include "chrome/browser/signin/easy_unlock_app_manager.h"
13 #include "chrome/browser/signin/easy_unlock_service.h"
14 #include "chrome/browser/signin/easy_unlock_service_regular.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "components/keyed_service/content/browser_context_dependency_manager.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/browser/extension_system_provider.h"
21 #include "extensions/browser/extensions_browser_client.h"
22 #include "grit/browser_resources.h"
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager_factory.h"
26 #include "chrome/browser/chromeos/profiles/profile_helper.h"
27 #include "chrome/browser/signin/easy_unlock_service_signin_chromeos.h"
32 // Gets the file path from which easy unlock app should be loaded.
33 base::FilePath
GetEasyUnlockAppPath() {
34 #if defined(GOOGLE_CHROME_BUILD)
36 // Only allow app path override switch for debug build.
37 const base::CommandLine
* command_line
=
38 base::CommandLine::ForCurrentProcess();
39 if (command_line
->HasSwitch(switches::kEasyUnlockAppPath
))
40 return command_line
->GetSwitchValuePath(switches::kEasyUnlockAppPath
);
41 #endif // !defined(NDEBUG)
43 #if defined(OS_CHROMEOS)
44 return base::FilePath("/usr/share/chromeos-assets/easy_unlock");
45 #endif // defined(OS_CHROMEOS)
46 #endif // defined(GOOGLE_CHROME_BUILD)
48 return base::FilePath();
54 EasyUnlockServiceFactory
* EasyUnlockServiceFactory::GetInstance() {
55 return base::Singleton
<EasyUnlockServiceFactory
>::get();
59 EasyUnlockService
* EasyUnlockServiceFactory::GetForBrowserContext(
60 content::BrowserContext
* browser_context
) {
61 return static_cast<EasyUnlockService
*>(
62 EasyUnlockServiceFactory::GetInstance()->GetServiceForBrowserContext(
63 browser_context
, true));
66 EasyUnlockServiceFactory::EasyUnlockServiceFactory()
67 : BrowserContextKeyedServiceFactory(
69 BrowserContextDependencyManager::GetInstance()) {
71 extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
72 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
73 DependsOn(SigninManagerFactory::GetInstance());
74 DependsOn(gcm::GCMProfileServiceFactory::GetInstance());
75 #if defined(OS_CHROMEOS)
76 DependsOn(EasyUnlockTpmKeyManagerFactory::GetInstance());
80 EasyUnlockServiceFactory::~EasyUnlockServiceFactory() {
83 KeyedService
* EasyUnlockServiceFactory::BuildServiceInstanceFor(
84 content::BrowserContext
* context
) const {
85 EasyUnlockService
* service
= NULL
;
88 #if defined(OS_CHROMEOS)
89 if (chromeos::ProfileHelper::IsSigninProfile(
90 Profile::FromBrowserContext(context
))) {
91 if (!context
->IsOffTheRecord())
94 service
= new EasyUnlockServiceSignin(Profile::FromBrowserContext(context
));
95 manifest_id
= IDR_EASY_UNLOCK_MANIFEST_SIGNIN
;
101 new EasyUnlockServiceRegular(Profile::FromBrowserContext(context
));
102 manifest_id
= IDR_EASY_UNLOCK_MANIFEST
;
105 const base::FilePath app_path
= app_path_for_testing_
.empty()
106 ? GetEasyUnlockAppPath()
107 : app_path_for_testing_
;
109 service
->Initialize(EasyUnlockAppManager::Create(
110 extensions::ExtensionSystem::Get(context
), manifest_id
, app_path
));
114 content::BrowserContext
* EasyUnlockServiceFactory::GetBrowserContextToUse(
115 content::BrowserContext
* context
) const {
116 #if defined(OS_CHROMEOS)
117 if (chromeos::ProfileHelper::IsSigninProfile(
118 Profile::FromBrowserContext(context
))) {
119 return chrome::GetBrowserContextOwnInstanceInIncognito(context
);
122 return chrome::GetBrowserContextRedirectedInIncognito(context
);
125 bool EasyUnlockServiceFactory::ServiceIsCreatedWithBrowserContext() const {
129 bool EasyUnlockServiceFactory::ServiceIsNULLWhileTesting() const {
130 // Don't create the service for TestingProfile used in unit_tests because
131 // EasyUnlockService uses ExtensionSystem::ready().Post, which expects
132 // a MessageLoop that does not exit in many unit_tests cases.