| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 5 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | 14 #include "chrome/browser/search/suggestions/image_decoder_impl.h" |
| 14 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 15 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 16 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 17 #include "chrome/common/channel_info.h" | 18 #include "chrome/common/channel_info.h" |
| 18 #include "chrome/common/pref_names.h" | 19 #include "components/bookmarks/browser/bookmark_model.h" |
| 19 #include "components/image_fetcher/image_decoder.h" | 20 #include "components/image_fetcher/image_decoder.h" |
| 20 #include "components/image_fetcher/image_fetcher.h" | 21 #include "components/image_fetcher/image_fetcher.h" |
| 21 #include "components/image_fetcher/image_fetcher_impl.h" | 22 #include "components/image_fetcher/image_fetcher_impl.h" |
| 22 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 23 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 24 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" |
| 23 #include "components/ntp_snippets/content_suggestions_service.h" | 25 #include "components/ntp_snippets/content_suggestions_service.h" |
| 24 #include "components/ntp_snippets/features.h" | 26 #include "components/ntp_snippets/features.h" |
| 25 #include "components/ntp_snippets/ntp_snippets_constants.h" | 27 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 26 #include "components/ntp_snippets/ntp_snippets_database.h" | 28 #include "components/ntp_snippets/ntp_snippets_database.h" |
| 27 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 29 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 28 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 30 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 29 #include "components/ntp_snippets/ntp_snippets_service.h" | 31 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 30 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 32 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 31 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 32 #include "components/safe_json/safe_json_parser.h" | 34 #include "components/safe_json/safe_json_parser.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 43 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" | 45 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" |
| 44 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 46 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 45 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide
r.h" | 47 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide
r.h" |
| 46 #include "components/offline_pages/offline_page_model.h" | 48 #include "components/offline_pages/offline_page_model.h" |
| 47 | 49 |
| 48 using ntp_snippets::OfflinePageSuggestionsProvider; | 50 using ntp_snippets::OfflinePageSuggestionsProvider; |
| 49 using offline_pages::OfflinePageModel; | 51 using offline_pages::OfflinePageModel; |
| 50 using offline_pages::OfflinePageModelFactory; | 52 using offline_pages::OfflinePageModelFactory; |
| 51 #endif // OS_ANDROID | 53 #endif // OS_ANDROID |
| 52 | 54 |
| 55 using bookmarks::BookmarkModel; |
| 53 using content::BrowserThread; | 56 using content::BrowserThread; |
| 54 using image_fetcher::ImageFetcherImpl; | 57 using image_fetcher::ImageFetcherImpl; |
| 58 using ntp_snippets::BookmarkSuggestionsProvider; |
| 55 using ntp_snippets::ContentSuggestionsService; | 59 using ntp_snippets::ContentSuggestionsService; |
| 56 using ntp_snippets::NTPSnippetsDatabase; | 60 using ntp_snippets::NTPSnippetsDatabase; |
| 57 using ntp_snippets::NTPSnippetsFetcher; | 61 using ntp_snippets::NTPSnippetsFetcher; |
| 58 using ntp_snippets::NTPSnippetsService; | 62 using ntp_snippets::NTPSnippetsService; |
| 59 using ntp_snippets::NTPSnippetsScheduler; | 63 using ntp_snippets::NTPSnippetsScheduler; |
| 60 using ntp_snippets::NTPSnippetsStatusService; | 64 using ntp_snippets::NTPSnippetsStatusService; |
| 61 using suggestions::ImageDecoderImpl; | 65 using suggestions::ImageDecoderImpl; |
| 62 using suggestions::SuggestionsService; | 66 using suggestions::SuggestionsService; |
| 63 using suggestions::SuggestionsServiceFactory; | 67 using suggestions::SuggestionsServiceFactory; |
| 64 | 68 |
| 65 // static | 69 // static |
| 66 ContentSuggestionsServiceFactory* | 70 ContentSuggestionsServiceFactory* |
| 67 ContentSuggestionsServiceFactory::GetInstance() { | 71 ContentSuggestionsServiceFactory::GetInstance() { |
| 68 return base::Singleton<ContentSuggestionsServiceFactory>::get(); | 72 return base::Singleton<ContentSuggestionsServiceFactory>::get(); |
| 69 } | 73 } |
| 70 | 74 |
| 71 // static | 75 // static |
| 72 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( | 76 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( |
| 73 Profile* profile) { | 77 Profile* profile) { |
| 74 DCHECK(!profile->IsOffTheRecord()); | 78 DCHECK(!profile->IsOffTheRecord()); |
| 75 return static_cast<ContentSuggestionsService*>( | 79 return static_cast<ContentSuggestionsService*>( |
| 76 GetInstance()->GetServiceForBrowserContext(profile, true)); | 80 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 77 } | 81 } |
| 78 | 82 |
| 79 ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory() | 83 ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory() |
| 80 : BrowserContextKeyedServiceFactory( | 84 : BrowserContextKeyedServiceFactory( |
| 81 "ContentSuggestionsService", | 85 "ContentSuggestionsService", |
| 82 BrowserContextDependencyManager::GetInstance()) { | 86 BrowserContextDependencyManager::GetInstance()) { |
| 87 DependsOn(BookmarkModelFactory::GetInstance()); |
| 83 #if defined(OS_ANDROID) | 88 #if defined(OS_ANDROID) |
| 84 DependsOn(OfflinePageModelFactory::GetInstance()); | 89 DependsOn(OfflinePageModelFactory::GetInstance()); |
| 85 #endif // OS_ANDROID | 90 #endif // OS_ANDROID |
| 86 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 91 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 87 DependsOn(SigninManagerFactory::GetInstance()); | 92 DependsOn(SigninManagerFactory::GetInstance()); |
| 88 DependsOn(SuggestionsServiceFactory::GetInstance()); | 93 DependsOn(SuggestionsServiceFactory::GetInstance()); |
| 89 } | 94 } |
| 90 | 95 |
| 91 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() {} | 96 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() {} |
| 92 | 97 |
| 93 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( | 98 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( |
| 94 content::BrowserContext* context) const { | 99 content::BrowserContext* context) const { |
| 95 using State = ContentSuggestionsService::State; | 100 using State = ContentSuggestionsService::State; |
| 96 Profile* profile = Profile::FromBrowserContext(context); | 101 Profile* profile = Profile::FromBrowserContext(context); |
| 97 | 102 |
| 98 // Create the ContentSuggestionsService. | 103 // Create the ContentSuggestionsService. |
| 99 State state = | 104 State state = |
| 100 base::FeatureList::IsEnabled(ntp_snippets::kContentSuggestionsFeature) | 105 base::FeatureList::IsEnabled(ntp_snippets::kContentSuggestionsFeature) |
| 101 ? State::ENABLED | 106 ? State::ENABLED |
| 102 : State::DISABLED; | 107 : State::DISABLED; |
| 103 ContentSuggestionsService* service = new ContentSuggestionsService(state); | 108 ContentSuggestionsService* service = new ContentSuggestionsService(state); |
| 104 if (state == State::DISABLED) | 109 if (state == State::DISABLED) |
| 105 return service; | 110 return service; |
| 106 | 111 |
| 107 // Create the OfflinePageSuggestionsProvider. | |
| 108 #if defined(OS_ANDROID) | 112 #if defined(OS_ANDROID) |
| 113 // Create the OfflinePageSuggestionsProvider. |
| 109 if (base::FeatureList::IsEnabled( | 114 if (base::FeatureList::IsEnabled( |
| 110 chrome::android::kNTPOfflinePageSuggestionsFeature)) { | 115 chrome::android::kNTPOfflinePageSuggestionsFeature)) { |
| 111 OfflinePageModel* offline_page_model = | 116 OfflinePageModel* offline_page_model = |
| 112 OfflinePageModelFactory::GetForBrowserContext(profile); | 117 OfflinePageModelFactory::GetForBrowserContext(profile); |
| 113 | 118 |
| 114 std::unique_ptr<OfflinePageSuggestionsProvider> | 119 std::unique_ptr<OfflinePageSuggestionsProvider> |
| 115 offline_page_suggestions_provider = | 120 offline_page_suggestions_provider = |
| 116 base::MakeUnique<OfflinePageSuggestionsProvider>( | 121 base::MakeUnique<OfflinePageSuggestionsProvider>( |
| 117 service, service->category_factory(), offline_page_model); | 122 service, service->category_factory(), offline_page_model); |
| 118 service->RegisterProvider(std::move(offline_page_suggestions_provider)); | 123 service->RegisterProvider(std::move(offline_page_suggestions_provider)); |
| 119 } | 124 } |
| 120 #endif // OS_ANDROID | 125 #endif // OS_ANDROID |
| 121 | 126 |
| 127 // Create the BookmarkSuggestionsProvider. |
| 128 if (base::FeatureList::IsEnabled( |
| 129 ntp_snippets::kBookmarkSuggestionsFeature)) { |
| 130 // TODO(pke): GetForBrowserContext |
| 131 BookmarkModel* bookmark_model = |
| 132 BookmarkModelFactory::GetForProfile(profile); |
| 133 std::unique_ptr<BookmarkSuggestionsProvider> bookmark_suggestions_provider = |
| 134 base::MakeUnique<BookmarkSuggestionsProvider>( |
| 135 service, service->category_factory(), bookmark_model); |
| 136 service->RegisterProvider(std::move(bookmark_suggestions_provider)); |
| 137 } |
| 138 |
| 122 // Create the NTPSnippetsService (articles provider). | 139 // Create the NTPSnippetsService (articles provider). |
| 123 SigninManagerBase* signin_manager = | 140 SigninManagerBase* signin_manager = |
| 124 SigninManagerFactory::GetForProfile(profile); | 141 SigninManagerFactory::GetForProfile(profile); |
| 125 OAuth2TokenService* token_service = | 142 OAuth2TokenService* token_service = |
| 126 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 143 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 127 scoped_refptr<net::URLRequestContextGetter> request_context = | 144 scoped_refptr<net::URLRequestContextGetter> request_context = |
| 128 content::BrowserContext::GetDefaultStoragePartition(context) | 145 content::BrowserContext::GetDefaultStoragePartition(context) |
| 129 ->GetURLRequestContext(); | 146 ->GetURLRequestContext(); |
| 130 SuggestionsService* suggestions_service = | 147 SuggestionsService* suggestions_service = |
| 131 SuggestionsServiceFactory::GetForProfile(profile); | 148 SuggestionsServiceFactory::GetForProfile(profile); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 153 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), | 170 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), |
| 154 base::MakeUnique<ImageDecoderImpl>(), | 171 base::MakeUnique<ImageDecoderImpl>(), |
| 155 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), | 172 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), |
| 156 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, | 173 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, |
| 157 profile->GetPrefs())); | 174 profile->GetPrefs())); |
| 158 service->set_ntp_snippets_service(ntp_snippets_service.get()); | 175 service->set_ntp_snippets_service(ntp_snippets_service.get()); |
| 159 service->RegisterProvider(std::move(ntp_snippets_service)); | 176 service->RegisterProvider(std::move(ntp_snippets_service)); |
| 160 | 177 |
| 161 return service; | 178 return service; |
| 162 } | 179 } |
| OLD | NEW |