Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / autocomplete / shortcuts_backend_factory.cc
blobcf4b42f9be1b88785fe6b5282ee4ce917950f239
1 // Copyright (c) 2012 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/autocomplete/shortcuts_backend_factory.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/autocomplete/shortcuts_extensions_manager.h"
10 #include "chrome/browser/history/history_service_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
14 #include "components/keyed_service/content/browser_context_dependency_manager.h"
15 #include "components/omnibox/browser/shortcuts_backend.h"
16 #include "components/omnibox/browser/shortcuts_constants.h"
17 #include "content/public/browser/browser_thread.h"
19 namespace {
20 #if defined(ENABLE_EXTENSIONS)
21 const char kShortcutsExtensionsManagerKey[] = "ShortcutsExtensionsManager";
22 #endif
25 // static
26 scoped_refptr<ShortcutsBackend> ShortcutsBackendFactory::GetForProfile(
27 Profile* profile) {
28 return static_cast<ShortcutsBackend*>(
29 GetInstance()->GetServiceForBrowserContext(profile, true).get());
32 // static
33 scoped_refptr<ShortcutsBackend> ShortcutsBackendFactory::GetForProfileIfExists(
34 Profile* profile) {
35 return static_cast<ShortcutsBackend*>(
36 GetInstance()->GetServiceForBrowserContext(profile, false).get());
39 // static
40 ShortcutsBackendFactory* ShortcutsBackendFactory::GetInstance() {
41 return base::Singleton<ShortcutsBackendFactory>::get();
44 // static
45 scoped_refptr<RefcountedKeyedService>
46 ShortcutsBackendFactory::BuildProfileForTesting(
47 content::BrowserContext* profile) {
48 return CreateShortcutsBackend(Profile::FromBrowserContext(profile), false);
51 // static
52 scoped_refptr<RefcountedKeyedService>
53 ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting(
54 content::BrowserContext* profile) {
55 return CreateShortcutsBackend(Profile::FromBrowserContext(profile), true);
58 ShortcutsBackendFactory::ShortcutsBackendFactory()
59 : RefcountedBrowserContextKeyedServiceFactory(
60 "ShortcutsBackend",
61 BrowserContextDependencyManager::GetInstance()) {
62 DependsOn(HistoryServiceFactory::GetInstance());
63 DependsOn(TemplateURLServiceFactory::GetInstance());
66 ShortcutsBackendFactory::~ShortcutsBackendFactory() {}
68 scoped_refptr<RefcountedKeyedService>
69 ShortcutsBackendFactory::BuildServiceInstanceFor(
70 content::BrowserContext* profile) const {
71 return CreateShortcutsBackend(Profile::FromBrowserContext(profile), false);
74 bool ShortcutsBackendFactory::ServiceIsNULLWhileTesting() const {
75 return true;
78 void ShortcutsBackendFactory::BrowserContextShutdown(
79 content::BrowserContext* context) {
80 #if defined(ENABLE_EXTENSIONS)
81 context->RemoveUserData(kShortcutsExtensionsManagerKey);
82 #endif
84 RefcountedBrowserContextKeyedServiceFactory::BrowserContextShutdown(context);
87 // static
88 scoped_refptr<ShortcutsBackend> ShortcutsBackendFactory::CreateShortcutsBackend(
89 Profile* profile,
90 bool suppress_db) {
91 scoped_refptr<ShortcutsBackend> backend(new ShortcutsBackend(
92 TemplateURLServiceFactory::GetForProfile(profile),
93 make_scoped_ptr(new UIThreadSearchTermsData(profile)),
94 HistoryServiceFactory::GetForProfile(profile,
95 ServiceAccessType::EXPLICIT_ACCESS),
96 content::BrowserThread::GetMessageLoopProxyForThread(
97 content::BrowserThread::DB),
98 profile->GetPath().Append(kShortcutsDatabaseName), suppress_db));
99 #if defined(ENABLE_EXTENSIONS)
100 ShortcutsExtensionsManager* extensions_manager =
101 new ShortcutsExtensionsManager(profile);
102 profile->SetUserData(kShortcutsExtensionsManagerKey, extensions_manager);
103 #endif
104 return backend->Init() ? backend : nullptr;