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"
20 #if defined(ENABLE_EXTENSIONS)
21 const char kShortcutsExtensionsManagerKey
[] = "ShortcutsExtensionsManager";
26 scoped_refptr
<ShortcutsBackend
> ShortcutsBackendFactory::GetForProfile(
28 return static_cast<ShortcutsBackend
*>(
29 GetInstance()->GetServiceForBrowserContext(profile
, true).get());
33 scoped_refptr
<ShortcutsBackend
> ShortcutsBackendFactory::GetForProfileIfExists(
35 return static_cast<ShortcutsBackend
*>(
36 GetInstance()->GetServiceForBrowserContext(profile
, false).get());
40 ShortcutsBackendFactory
* ShortcutsBackendFactory::GetInstance() {
41 return Singleton
<ShortcutsBackendFactory
>::get();
45 scoped_refptr
<RefcountedKeyedService
>
46 ShortcutsBackendFactory::BuildProfileForTesting(
47 content::BrowserContext
* profile
) {
48 return CreateShortcutsBackend(Profile::FromBrowserContext(profile
), false);
52 scoped_refptr
<RefcountedKeyedService
>
53 ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting(
54 content::BrowserContext
* profile
) {
55 return CreateShortcutsBackend(Profile::FromBrowserContext(profile
), true);
58 ShortcutsBackendFactory::ShortcutsBackendFactory()
59 : RefcountedBrowserContextKeyedServiceFactory(
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 {
78 void ShortcutsBackendFactory::BrowserContextShutdown(
79 content::BrowserContext
* context
) {
80 #if defined(ENABLE_EXTENSIONS)
81 context
->RemoveUserData(kShortcutsExtensionsManagerKey
);
84 RefcountedBrowserContextKeyedServiceFactory::BrowserContextShutdown(context
);
88 scoped_refptr
<ShortcutsBackend
> ShortcutsBackendFactory::CreateShortcutsBackend(
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
);
104 return backend
->Init() ? backend
: nullptr;