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 "ios/chrome/browser/suggestions/suggestions_service_factory.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/singleton.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
12 #include "components/leveldb_proto/proto_database_impl.h"
13 #include "components/suggestions/blacklist_store.h"
14 #include "components/suggestions/image_fetcher.h"
15 #include "components/suggestions/image_manager.h"
16 #include "components/suggestions/suggestions_service.h"
17 #include "components/suggestions/suggestions_store.h"
18 #include "ios/chrome/browser/suggestions/image_fetcher_impl.h"
19 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
20 #include "ios/web/public/browser_state.h"
21 #include "ios/web/public/web_thread.h"
23 namespace suggestions {
25 const base::FilePath::CharType kThumbnailDirectory[] =
26 FILE_PATH_LITERAL("Thumbnail");
30 SuggestionsServiceFactory* SuggestionsServiceFactory::GetInstance() {
31 return Singleton<SuggestionsServiceFactory>::get();
35 SuggestionsService* SuggestionsServiceFactory::GetForBrowserState(
36 ios::ChromeBrowserState* browser_state) {
37 return static_cast<SuggestionsService*>(
38 GetInstance()->GetServiceForBrowserState(browser_state, true));
41 SuggestionsServiceFactory::SuggestionsServiceFactory()
42 : BrowserStateKeyedServiceFactory(
44 BrowserStateDependencyManager::GetInstance()) {
48 SuggestionsServiceFactory::~SuggestionsServiceFactory() {
51 scoped_ptr<KeyedService> SuggestionsServiceFactory::BuildServiceInstanceFor(
52 web::BrowserState* context) const {
53 base::SequencedWorkerPool* sequenced_worker_pool =
54 web::WebThread::GetBlockingPool();
55 scoped_refptr<base::SequencedTaskRunner> background_task_runner =
56 sequenced_worker_pool->GetSequencedTaskRunner(
57 sequenced_worker_pool->GetSequenceToken());
59 ios::ChromeBrowserState* browser_state =
60 ios::ChromeBrowserState::FromBrowserState(context);
61 base::FilePath database_dir(
62 browser_state->GetStatePath().Append(kThumbnailDirectory));
63 scoped_ptr<SuggestionsStore> suggestions_store(
64 new SuggestionsStore(browser_state->GetPrefs()));
65 scoped_ptr<BlacklistStore> blacklist_store(
66 new BlacklistStore(browser_state->GetPrefs()));
67 scoped_ptr<leveldb_proto::ProtoDatabaseImpl<ImageData>> db(
68 new leveldb_proto::ProtoDatabaseImpl<ImageData>(background_task_runner));
69 scoped_ptr<ImageFetcher> image_fetcher(new ImageFetcherImpl(
70 browser_state->GetRequestContext(), sequenced_worker_pool));
71 scoped_ptr<ImageManager> thumbnail_manager(new ImageManager(
72 image_fetcher.Pass(), db.Pass(), database_dir,
73 web::WebThread::GetTaskRunnerForThread(web::WebThread::DB)));
74 return make_scoped_ptr(new SuggestionsService(
75 browser_state->GetRequestContext(), suggestions_store.Pass(),
76 thumbnail_manager.Pass(), blacklist_store.Pass()));
79 void SuggestionsServiceFactory::RegisterBrowserStatePrefs(
80 user_prefs::PrefRegistrySyncable* registry) {
81 SuggestionsService::RegisterProfilePrefs(registry);
84 } // namespace suggestions