Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / search / suggestions / suggestions_service_factory.cc
blob6e680b4734f1ffb1e80c2a88d49afa13e2136a1c
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/search/suggestions/suggestions_service_factory.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/incognito_helpers.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/suggestions/image_fetcher_impl.h"
12 #include "components/keyed_service/content/browser_context_dependency_manager.h"
13 #include "components/leveldb_proto/proto_database.h"
14 #include "components/leveldb_proto/proto_database_impl.h"
15 #include "components/pref_registry/pref_registry_syncable.h"
16 #include "components/suggestions/blacklist_store.h"
17 #include "components/suggestions/image_fetcher.h"
18 #include "components/suggestions/image_manager.h"
19 #include "components/suggestions/proto/suggestions.pb.h"
20 #include "components/suggestions/suggestions_service.h"
21 #include "components/suggestions/suggestions_store.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/browser_thread.h"
25 using content::BrowserThread;
27 namespace suggestions {
29 // static
30 SuggestionsService* SuggestionsServiceFactory::GetForProfile(Profile* profile) {
31 if (!SuggestionsService::IsEnabled() || profile->IsOffTheRecord())
32 return NULL;
34 return static_cast<SuggestionsService*>(
35 GetInstance()->GetServiceForBrowserContext(profile, true));
38 // static
39 SuggestionsServiceFactory* SuggestionsServiceFactory::GetInstance() {
40 return Singleton<SuggestionsServiceFactory>::get();
43 SuggestionsServiceFactory::SuggestionsServiceFactory()
44 : BrowserContextKeyedServiceFactory(
45 "SuggestionsService",
46 BrowserContextDependencyManager::GetInstance()) {
47 // No dependencies.
50 SuggestionsServiceFactory::~SuggestionsServiceFactory() {}
52 content::BrowserContext* SuggestionsServiceFactory::GetBrowserContextToUse(
53 content::BrowserContext* context) const {
54 return chrome::GetBrowserContextRedirectedInIncognito(context);
57 KeyedService* SuggestionsServiceFactory::BuildServiceInstanceFor(
58 content::BrowserContext* profile) const {
59 scoped_refptr<base::SequencedTaskRunner> background_task_runner =
60 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
61 BrowserThread::GetBlockingPool()->GetSequenceToken());
63 Profile* the_profile = static_cast<Profile*>(profile);
64 scoped_ptr<SuggestionsStore> suggestions_store(
65 new SuggestionsStore(the_profile->GetPrefs()));
66 scoped_ptr<BlacklistStore> blacklist_store(
67 new BlacklistStore(the_profile->GetPrefs()));
69 scoped_ptr<leveldb_proto::ProtoDatabaseImpl<ImageData> > db(
70 new leveldb_proto::ProtoDatabaseImpl<ImageData>(background_task_runner));
72 base::FilePath database_dir(
73 the_profile->GetPath().Append(FILE_PATH_LITERAL("Thumbnails")));
75 scoped_ptr<ImageFetcherImpl> image_fetcher(
76 new ImageFetcherImpl(the_profile->GetRequestContext()));
77 scoped_ptr<ImageManager> thumbnail_manager(
78 new ImageManager(image_fetcher.Pass(), db.Pass(), database_dir));
79 return new SuggestionsService(
80 the_profile->GetRequestContext(), suggestions_store.Pass(),
81 thumbnail_manager.Pass(), blacklist_store.Pass());
84 void SuggestionsServiceFactory::RegisterProfilePrefs(
85 user_prefs::PrefRegistrySyncable* registry) {
86 SuggestionsService::RegisterProfilePrefs(registry);
89 } // namespace suggestions