1 // Copyright 2013 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.
4 
5 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
6 
7 #include <set>
8 
9 #include "build/build_config.h"
10 #include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
11 #include "chrome/browser/chromeos/profiles/profile_helper.h"
12 #include "chrome/browser/profiles/incognito_helpers.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
15 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs_factory.h"
16 #include "components/keyed_service/content/browser_context_dependency_manager.h"
17 #include "components/prefs/pref_service.h"
18 #include "extensions/browser/extension_system_provider.h"
19 #include "extensions/browser/extensions_browser_client.h"
20 
21 namespace app_list {
22 
23 namespace {
24 bool use_in_testing = false;
25 }
26 
27 // static
GetForProfile(Profile * profile)28 AppListSyncableService* AppListSyncableServiceFactory::GetForProfile(
29     Profile* profile) {
30   return static_cast<AppListSyncableService*>(
31       GetInstance()->GetServiceForBrowserContext(profile, true));
32 }
33 
34 // static
GetInstance()35 AppListSyncableServiceFactory* AppListSyncableServiceFactory::GetInstance() {
36   return base::Singleton<AppListSyncableServiceFactory>::get();
37 }
38 
39 // static
BuildInstanceFor(content::BrowserContext * browser_context)40 std::unique_ptr<KeyedService> AppListSyncableServiceFactory::BuildInstanceFor(
41     content::BrowserContext* browser_context) {
42   Profile* profile = static_cast<Profile*>(browser_context);
43   if (chromeos::ProfileHelper::IsSigninProfile(profile) ||
44       chromeos::ProfileHelper::IsLockScreenAppProfile(profile)) {
45     return nullptr;
46   }
47   VLOG(1) << "BuildInstanceFor: " << profile->GetDebugName()
48           << " (" << profile << ")";
49   return std::make_unique<AppListSyncableService>(profile);
50 }
51 
52 // static
SetUseInTesting(bool use)53 void AppListSyncableServiceFactory::SetUseInTesting(bool use) {
54   use_in_testing = use;
55 }
56 
AppListSyncableServiceFactory()57 AppListSyncableServiceFactory::AppListSyncableServiceFactory()
58     : BrowserContextKeyedServiceFactory(
59         "AppListSyncableService",
60         BrowserContextDependencyManager::GetInstance()) {
61   VLOG(1) << "AppListSyncableServiceFactory()";
62   typedef std::set<BrowserContextKeyedServiceFactory*> FactorySet;
63   FactorySet dependent_factories;
64   dependent_factories.insert(
65       extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
66   dependent_factories.insert(ArcAppListPrefsFactory::GetInstance());
67   dependent_factories.insert(apps::AppServiceProxyFactory::GetInstance());
68   for (FactorySet::iterator it = dependent_factories.begin();
69        it != dependent_factories.end();
70        ++it) {
71     DependsOn(*it);
72   }
73 }
74 
~AppListSyncableServiceFactory()75 AppListSyncableServiceFactory::~AppListSyncableServiceFactory() {
76 }
77 
BuildServiceInstanceFor(content::BrowserContext * browser_context) const78 KeyedService* AppListSyncableServiceFactory::BuildServiceInstanceFor(
79     content::BrowserContext* browser_context) const {
80   return BuildInstanceFor(static_cast<Profile*>(browser_context)).release();
81 }
82 
RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)83 void AppListSyncableServiceFactory::RegisterProfilePrefs(
84     user_prefs::PrefRegistrySyncable* registry) {
85 }
86 
GetBrowserContextToUse(content::BrowserContext * context) const87 content::BrowserContext* AppListSyncableServiceFactory::GetBrowserContextToUse(
88     content::BrowserContext* context) const {
89   Profile* const profile = Profile::FromBrowserContext(context);
90   // No service if |context| is not a profile.
91   if (!profile)
92     return nullptr;
93 
94   // No service for system profile.
95   if (profile->IsSystemProfile())
96     return nullptr;
97 
98   // No service for sign in profile.
99   if (chromeos::ProfileHelper::IsSigninProfile(profile))
100     return nullptr;
101 
102   // Use profile as-is for guest session.
103   if (profile->IsGuestSession())
104     return chrome::GetBrowserContextOwnInstanceInIncognito(context);
105 
106   // This matches the logic in ExtensionSyncServiceFactory, which uses the
107   // orginal browser context.
108   return chrome::GetBrowserContextRedirectedInIncognito(context);
109 }
110 
ServiceIsCreatedWithBrowserContext() const111 bool AppListSyncableServiceFactory::ServiceIsCreatedWithBrowserContext() const {
112   // Start AppListSyncableService early so that the app list positions are
113   // available before the app list is opened.
114   return true;
115 }
116 
ServiceIsNULLWhileTesting() const117 bool AppListSyncableServiceFactory::ServiceIsNULLWhileTesting() const {
118   return !use_in_testing;
119 }
120 
121 }  // namespace app_list
122