Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / autocomplete / shortcuts_backend_factory.cc
blobeb14bf1115cf2b07440a219be6a6637946178f3f
1 // Copyright 2015 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/autocomplete/shortcuts_backend_factory.h"
7 #include "base/memory/singleton.h"
8 #include "base/prefs/pref_service.h"
9 #include "components/keyed_service/core/service_access_type.h"
10 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
11 #include "components/omnibox/browser/shortcuts_backend.h"
12 #include "components/omnibox/browser/shortcuts_constants.h"
13 #include "ios/chrome/browser/history/history_service_factory.h"
14 #include "ios/chrome/browser/pref_names.h"
15 #include "ios/chrome/browser/search_engines/template_url_service_factory.h"
16 #include "ios/chrome/browser/search_engines/ui_thread_search_terms_data.h"
17 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
18 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
19 #include "ios/web/public/web_thread.h"
21 namespace ios {
22 namespace {
24 scoped_refptr<ShortcutsBackend> CreateShortcutsBackend(
25 ios::ChromeBrowserState* browser_state,
26 bool suppress_db) {
27 scoped_refptr<ShortcutsBackend> shortcuts_backend(new ShortcutsBackend(
28 ios::TemplateURLServiceFactory::GetForBrowserState(browser_state),
29 make_scoped_ptr(new ios::UIThreadSearchTermsData(browser_state)),
30 ios::HistoryServiceFactory::GetForBrowserState(
31 browser_state, ServiceAccessType::EXPLICIT_ACCESS),
32 web::WebThread::GetTaskRunnerForThread(web::WebThread::DB),
33 browser_state->GetStatePath().Append(kShortcutsDatabaseName),
34 suppress_db));
35 return shortcuts_backend->Init() ? shortcuts_backend : nullptr;
38 } // namespace
40 // static
41 scoped_refptr<ShortcutsBackend> ShortcutsBackendFactory::GetForBrowserState(
42 ios::ChromeBrowserState* browser_state) {
43 return make_scoped_refptr(static_cast<ShortcutsBackend*>(
44 GetInstance()->GetServiceForBrowserState(browser_state, true).get()));
47 // static
48 scoped_refptr<ShortcutsBackend>
49 ShortcutsBackendFactory::GetForBrowserStateIfExists(
50 ios::ChromeBrowserState* browser_state) {
51 return make_scoped_refptr(static_cast<ShortcutsBackend*>(
52 GetInstance()->GetServiceForBrowserState(browser_state, false).get()));
55 // static
56 ShortcutsBackendFactory* ShortcutsBackendFactory::GetInstance() {
57 return base::Singleton<ShortcutsBackendFactory>::get();
60 ShortcutsBackendFactory::ShortcutsBackendFactory()
61 : RefcountedBrowserStateKeyedServiceFactory(
62 "ShortcutsBackend",
63 BrowserStateDependencyManager::GetInstance()) {
64 DependsOn(ios::HistoryServiceFactory::GetInstance());
65 DependsOn(ios::TemplateURLServiceFactory::GetInstance());
68 ShortcutsBackendFactory::~ShortcutsBackendFactory() {}
70 scoped_refptr<RefcountedKeyedService>
71 ShortcutsBackendFactory::BuildServiceInstanceFor(
72 web::BrowserState* context) const {
73 ios::ChromeBrowserState* browser_state =
74 ios::ChromeBrowserState::FromBrowserState(context);
75 return CreateShortcutsBackend(browser_state, false /* suppress_db */);
78 bool ShortcutsBackendFactory::ServiceIsNULLWhileTesting() const {
79 return true;
82 } // namespace ios