| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | |
| 6 | |
| 7 #include "base/feature_list.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/memory/singleton.h" | |
| 11 #include "chrome/browser/browser_process.h" | |
| 12 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | |
| 15 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | |
| 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 18 #include "chrome/common/channel_info.h" | |
| 19 #include "components/image_fetcher/image_decoder.h" | |
| 20 #include "components/image_fetcher/image_fetcher.h" | |
| 21 #include "components/image_fetcher/image_fetcher_impl.h" | |
| 22 #include "components/keyed_service/content/browser_context_dependency_manager.h" | |
| 23 #include "components/ntp_snippets/content_suggestions_service.h" | |
| 24 #include "components/ntp_snippets/ntp_snippets_constants.h" | |
| 25 #include "components/ntp_snippets/ntp_snippets_database.h" | |
| 26 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | |
| 27 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | |
| 28 #include "components/ntp_snippets/ntp_snippets_service.h" | |
| 29 #include "components/ntp_snippets/ntp_snippets_status_service.h" | |
| 30 #include "components/prefs/pref_service.h" | |
| 31 #include "components/safe_json/safe_json_parser.h" | |
| 32 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 33 #include "components/signin/core/browser/signin_manager.h" | |
| 34 #include "components/version_info/version_info.h" | |
| 35 #include "content/public/browser/browser_context.h" | |
| 36 #include "content/public/browser/browser_thread.h" | |
| 37 #include "content/public/browser/storage_partition.h" | |
| 38 #include "net/url_request/url_request_context_getter.h" | |
| 39 | |
| 40 #if defined(OS_ANDROID) | |
| 41 #include "chrome/browser/android/chrome_feature_list.h" | |
| 42 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" | |
| 43 #endif // OS_ANDROID | |
| 44 | |
| 45 using content::BrowserThread; | |
| 46 using image_fetcher::ImageFetcherImpl; | |
| 47 using suggestions::ImageDecoderImpl; | |
| 48 using suggestions::SuggestionsService; | |
| 49 using suggestions::SuggestionsServiceFactory; | |
| 50 using ntp_snippets::ContentSuggestionsService; | |
| 51 | |
| 52 // static | |
| 53 NTPSnippetsServiceFactory* NTPSnippetsServiceFactory::GetInstance() { | |
| 54 return base::Singleton<NTPSnippetsServiceFactory>::get(); | |
| 55 } | |
| 56 | |
| 57 // static | |
| 58 ntp_snippets::NTPSnippetsService* NTPSnippetsServiceFactory::GetForProfile( | |
| 59 Profile* profile) { | |
| 60 DCHECK(!profile->IsOffTheRecord()); | |
| 61 return static_cast<ntp_snippets::NTPSnippetsService*>( | |
| 62 GetInstance()->GetServiceForBrowserContext(profile, true)); | |
| 63 } | |
| 64 | |
| 65 NTPSnippetsServiceFactory::NTPSnippetsServiceFactory() | |
| 66 : BrowserContextKeyedServiceFactory( | |
| 67 "NTPSnippetsService", | |
| 68 BrowserContextDependencyManager::GetInstance()) { | |
| 69 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | |
| 70 DependsOn(SigninManagerFactory::GetInstance()); | |
| 71 DependsOn(SuggestionsServiceFactory::GetInstance()); | |
| 72 DependsOn(ContentSuggestionsServiceFactory::GetInstance()); | |
| 73 } | |
| 74 | |
| 75 NTPSnippetsServiceFactory::~NTPSnippetsServiceFactory() {} | |
| 76 | |
| 77 KeyedService* NTPSnippetsServiceFactory::BuildServiceInstanceFor( | |
| 78 content::BrowserContext* context) const { | |
| 79 Profile* profile = Profile::FromBrowserContext(context); | |
| 80 | |
| 81 ContentSuggestionsService* content_suggestions_service = | |
| 82 ContentSuggestionsServiceFactory::GetForProfile(profile); | |
| 83 // TODO(pke): When the AndroidBridge does not access the NTPSnippetsService | |
| 84 // directly anymore (for retrieving content), the NTPSnippetsService does not | |
| 85 // need to be created if content_suggestions_service->state() == DISABLED, | |
| 86 // just return nullptr then and remove the other "if" below. | |
| 87 | |
| 88 bool enabled = false; | |
| 89 #if defined(OS_ANDROID) | |
| 90 enabled = base::FeatureList::IsEnabled(chrome::android::kNTPSnippetsFeature); | |
| 91 #endif // OS_ANDROID | |
| 92 | |
| 93 SigninManagerBase* signin_manager = | |
| 94 SigninManagerFactory::GetForProfile(profile); | |
| 95 OAuth2TokenService* token_service = | |
| 96 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 97 scoped_refptr<net::URLRequestContextGetter> request_context = | |
| 98 content::BrowserContext::GetDefaultStoragePartition(context)-> | |
| 99 GetURLRequestContext(); | |
| 100 SuggestionsService* suggestions_service = | |
| 101 SuggestionsServiceFactory::GetForProfile(profile); | |
| 102 | |
| 103 ntp_snippets::NTPSnippetsScheduler* scheduler = nullptr; | |
| 104 #if defined(OS_ANDROID) | |
| 105 scheduler = NTPSnippetsLauncher::Get(); | |
| 106 #endif // OS_ANDROID | |
| 107 | |
| 108 base::FilePath database_dir( | |
| 109 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); | |
| 110 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 111 BrowserThread::GetBlockingPool() | |
| 112 ->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 113 base::SequencedWorkerPool::GetSequenceToken(), | |
| 114 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 115 | |
| 116 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 117 new ntp_snippets::NTPSnippetsService( | |
| 118 enabled, profile->GetPrefs(), suggestions_service, | |
| 119 content_suggestions_service->category_factory(), | |
| 120 g_browser_process->GetApplicationLocale(), scheduler, | |
| 121 base::MakeUnique<ntp_snippets::NTPSnippetsFetcher>( | |
| 122 signin_manager, token_service, request_context, | |
| 123 profile->GetPrefs(), | |
| 124 base::Bind(&safe_json::SafeJsonParser::Parse), | |
| 125 chrome::GetChannel() == version_info::Channel::STABLE), | |
| 126 base::MakeUnique<ImageFetcherImpl>( | |
| 127 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), | |
| 128 base::MakeUnique<ImageDecoderImpl>(), | |
| 129 base::MakeUnique<ntp_snippets::NTPSnippetsDatabase>(database_dir, | |
| 130 task_runner), | |
| 131 base::MakeUnique<ntp_snippets::NTPSnippetsStatusService>( | |
| 132 signin_manager, profile->GetPrefs())); | |
| 133 | |
| 134 if (content_suggestions_service->state() == | |
| 135 ContentSuggestionsService::State::ENABLED) { | |
| 136 content_suggestions_service->RegisterProvider(ntp_snippets_service); | |
| 137 } | |
| 138 return ntp_snippets_service; | |
| 139 } | |
| OLD | NEW |