bookmark_model_factory.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#include "chrome/browser/bookmarks/bookmark_model_factory.h"
6
7#include "base/command_line.h"
8#include "base/deferred_sequenced_task_runner.h"
9#include "base/memory/singleton.h"
10#include "base/prefs/pref_service.h"
11#include "base/values.h"
12#include "chrome/browser/bookmarks/chrome_bookmark_client.h"
13#include "chrome/browser/omnibox/omnibox_field_trial.h"
14#include "chrome/browser/profiles/incognito_helpers.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/profiles/startup_task_runner_service.h"
17#include "chrome/browser/profiles/startup_task_runner_service_factory.h"
18#include "chrome/browser/undo/bookmark_undo_service.h"
19#include "chrome/browser/undo/bookmark_undo_service_factory.h"
20#include "chrome/common/chrome_switches.h"
21#include "chrome/common/pref_names.h"
22#include "components/bookmarks/browser/bookmark_model.h"
23#include "components/bookmarks/common/bookmark_pref_names.h"
24#include "components/keyed_service/content/browser_context_dependency_manager.h"
25#include "components/pref_registry/pref_registry_syncable.h"
26#include "content/public/browser/browser_thread.h"
27
28// static
29BookmarkModel* BookmarkModelFactory::GetForProfile(Profile* profile) {
30  ChromeBookmarkClient* bookmark_client =
31      GetChromeBookmarkClientForProfile(profile);
32  return bookmark_client ? bookmark_client->model() : NULL;
33}
34
35// static
36BookmarkModel* BookmarkModelFactory::GetForProfileIfExists(Profile* profile) {
37  ChromeBookmarkClient* bookmark_client = static_cast<ChromeBookmarkClient*>(
38      GetInstance()->GetServiceForBrowserContext(profile, false));
39  return bookmark_client ? bookmark_client->model() : NULL;
40}
41
42// static
43ChromeBookmarkClient* BookmarkModelFactory::GetChromeBookmarkClientForProfile(
44    Profile* profile) {
45  return static_cast<ChromeBookmarkClient*>(
46      GetInstance()->GetServiceForBrowserContext(profile, true));
47}
48
49// static
50BookmarkModelFactory* BookmarkModelFactory::GetInstance() {
51  return Singleton<BookmarkModelFactory>::get();
52}
53
54BookmarkModelFactory::BookmarkModelFactory()
55    : BrowserContextKeyedServiceFactory(
56        "BookmarkModel",
57        BrowserContextDependencyManager::GetInstance()) {
58}
59
60BookmarkModelFactory::~BookmarkModelFactory() {}
61
62KeyedService* BookmarkModelFactory::BuildServiceInstanceFor(
63    content::BrowserContext* context) const {
64  Profile* profile = static_cast<Profile*>(context);
65  ChromeBookmarkClient* bookmark_client = new ChromeBookmarkClient(
66      profile, OmniboxFieldTrial::BookmarksIndexURLsValue());
67  bookmark_client->model()->Load(
68      profile->GetPrefs(),
69      profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
70      profile->GetPath(),
71      StartupTaskRunnerServiceFactory::GetForProfile(profile)
72          ->GetBookmarkTaskRunner(),
73      content::BrowserThread::GetMessageLoopProxyForThread(
74          content::BrowserThread::UI));
75#if !defined(OS_ANDROID)
76  bool register_bookmark_undo_service_as_observer = true;
77#if !defined(OS_IOS)
78  register_bookmark_undo_service_as_observer =
79      CommandLine::ForCurrentProcess()->HasSwitch(
80          switches::kEnableBookmarkUndo);
81#endif  // !defined(OS_IOS)
82  if (register_bookmark_undo_service_as_observer) {
83    bookmark_client->model()->AddObserver(
84        BookmarkUndoServiceFactory::GetForProfile(profile));
85  }
86#endif  // !defined(OS_ANDROID)
87  return bookmark_client;
88}
89
90void BookmarkModelFactory::RegisterProfilePrefs(
91    user_prefs::PrefRegistrySyncable* registry) {
92  // Don't sync this, as otherwise, due to a limitation in sync, it
93  // will cause a deadlock (see http://crbug.com/97955).  If we truly
94  // want to sync the expanded state of folders, it should be part of
95  // bookmark sync itself (i.e., a property of the sync folder nodes).
96  registry->RegisterListPref(prefs::kBookmarkEditorExpandedNodes,
97                             new base::ListValue,
98                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
99  registry->RegisterListPref(
100      prefs::kManagedBookmarks,
101      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
102}
103
104content::BrowserContext* BookmarkModelFactory::GetBrowserContextToUse(
105    content::BrowserContext* context) const {
106  return chrome::GetBrowserContextRedirectedInIncognito(context);
107}
108
109bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const {
110  return true;
111}
112