Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / bookmarks / bookmark_model_factory.cc
blob8e956ffb24b8bbf2bcb2454df62bab917802a8a0
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/bookmarks/bookmark_model_factory.h"
7 #include "base/memory/singleton.h"
8 #include "base/prefs/pref_service.h"
9 #include "components/bookmarks/browser/bookmark_model.h"
10 #include "components/bookmarks/browser/bookmark_utils.h"
11 #include "components/bookmarks/browser/startup_task_runner_service.h"
12 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
13 #include "components/undo/bookmark_undo_service.h"
14 #include "ios/chrome/browser/bookmarks/bookmark_client_factory.h"
15 #include "ios/chrome/browser/bookmarks/bookmark_client_impl.h"
16 #include "ios/chrome/browser/bookmarks/startup_task_runner_service_factory.h"
17 #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
18 #include "ios/chrome/browser/history/history_service_factory.h"
19 #include "ios/chrome/browser/pref_names.h"
20 #include "ios/chrome/browser/undo/bookmark_undo_service_factory.h"
21 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
22 #include "ios/web/public/web_thread.h"
24 namespace ios {
26 // static
27 bookmarks::BookmarkModel* BookmarkModelFactory::GetForBrowserState(
28 ios::ChromeBrowserState* browser_state) {
29 return static_cast<bookmarks::BookmarkModel*>(
30 GetInstance()->GetServiceForBrowserState(browser_state, true));
33 // static
34 bookmarks::BookmarkModel* BookmarkModelFactory::GetForBrowserStateIfExists(
35 ios::ChromeBrowserState* browser_state) {
36 return static_cast<bookmarks::BookmarkModel*>(
37 GetInstance()->GetServiceForBrowserState(browser_state, false));
40 // static
41 BookmarkModelFactory* BookmarkModelFactory::GetInstance() {
42 return base::Singleton<BookmarkModelFactory>::get();
45 BookmarkModelFactory::BookmarkModelFactory()
46 : BrowserStateKeyedServiceFactory(
47 "BookmarkModel",
48 BrowserStateDependencyManager::GetInstance()) {
49 DependsOn(BookmarkClientFactory::GetInstance());
50 DependsOn(ios::BookmarkUndoServiceFactory::GetInstance());
51 DependsOn(ios::StartupTaskRunnerServiceFactory::GetInstance());
54 BookmarkModelFactory::~BookmarkModelFactory() {}
56 void BookmarkModelFactory::RegisterBrowserStatePrefs(
57 user_prefs::PrefRegistrySyncable* registry) {
58 bookmarks::RegisterProfilePrefs(registry);
61 scoped_ptr<KeyedService> BookmarkModelFactory::BuildServiceInstanceFor(
62 web::BrowserState* context) const {
63 ios::ChromeBrowserState* browser_state =
64 ios::ChromeBrowserState::FromBrowserState(context);
65 BookmarkClientImpl* bookmark_client =
66 BookmarkClientFactory::GetForBrowserState(browser_state);
67 scoped_ptr<bookmarks::BookmarkModel> bookmark_model(
68 new bookmarks::BookmarkModel(bookmark_client));
69 bookmark_client->Init(bookmark_model.get());
70 bookmark_model->Load(
71 browser_state->GetPrefs(),
72 browser_state->GetPrefs()->GetString(ios::prefs::kAcceptLanguages),
73 browser_state->GetStatePath(),
74 ios::StartupTaskRunnerServiceFactory::GetForBrowserState(browser_state)
75 ->GetBookmarkTaskRunner(),
76 web::WebThread::GetTaskRunnerForThread(web::WebThread::UI));
77 ios::BookmarkUndoServiceFactory::GetForBrowserState(browser_state)
78 ->Start(bookmark_model.get());
79 return bookmark_model.Pass();
82 web::BrowserState* BookmarkModelFactory::GetBrowserStateToUse(
83 web::BrowserState* context) const {
84 return GetBrowserStateRedirectedInIncognito(context);
87 bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const {
88 return true;
91 } // namespace ios