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/bookmarks/chrome_bookmark_client_factory.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
GetForProfile(Profile * profile)29 BookmarkModel* BookmarkModelFactory::GetForProfile(Profile* profile) {
30 return static_cast<BookmarkModel*>(
31 GetInstance()->GetServiceForBrowserContext(profile, true));
32 }
33
34 // static
GetForProfileIfExists(Profile * profile)35 BookmarkModel* BookmarkModelFactory::GetForProfileIfExists(Profile* profile) {
36 return static_cast<BookmarkModel*>(
37 GetInstance()->GetServiceForBrowserContext(profile, false));
38 }
39
40 // static
GetInstance()41 BookmarkModelFactory* BookmarkModelFactory::GetInstance() {
42 return Singleton<BookmarkModelFactory>::get();
43 }
44
BookmarkModelFactory()45 BookmarkModelFactory::BookmarkModelFactory()
46 : BrowserContextKeyedServiceFactory(
47 "BookmarkModel",
48 BrowserContextDependencyManager::GetInstance()) {
49 DependsOn(ChromeBookmarkClientFactory::GetInstance());
50 DependsOn(StartupTaskRunnerServiceFactory::GetInstance());
51 }
52
~BookmarkModelFactory()53 BookmarkModelFactory::~BookmarkModelFactory() {
54 }
55
BuildServiceInstanceFor(content::BrowserContext * context) const56 KeyedService* BookmarkModelFactory::BuildServiceInstanceFor(
57 content::BrowserContext* context) const {
58 Profile* profile = static_cast<Profile*>(context);
59 ChromeBookmarkClient* bookmark_client =
60 ChromeBookmarkClientFactory::GetForProfile(profile);
61 BookmarkModel* bookmark_model = new BookmarkModel(bookmark_client);
62 bookmark_client->Init(bookmark_model);
63 bookmark_model->Load(profile->GetPrefs(),
64 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
65 profile->GetPath(),
66 StartupTaskRunnerServiceFactory::GetForProfile(profile)
67 ->GetBookmarkTaskRunner(),
68 content::BrowserThread::GetMessageLoopProxyForThread(
69 content::BrowserThread::UI));
70 bool register_bookmark_undo_service_as_observer = true;
71 #if !defined(OS_IOS) && !defined(OS_ANDROID)
72 register_bookmark_undo_service_as_observer =
73 CommandLine::ForCurrentProcess()->HasSwitch(
74 switches::kEnableBookmarkUndo);
75 #endif // !defined(OS_IOS)
76 if (register_bookmark_undo_service_as_observer) {
77 bookmark_model->AddObserver(
78 BookmarkUndoServiceFactory::GetForProfile(profile));
79 }
80 return bookmark_model;
81 }
82
RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)83 void BookmarkModelFactory::RegisterProfilePrefs(
84 user_prefs::PrefRegistrySyncable* registry) {
85 // Don't sync this, as otherwise, due to a limitation in sync, it
86 // will cause a deadlock (see http://crbug.com/97955). If we truly
87 // want to sync the expanded state of folders, it should be part of
88 // bookmark sync itself (i.e., a property of the sync folder nodes).
89 registry->RegisterListPref(bookmarks::prefs::kBookmarkEditorExpandedNodes,
90 new base::ListValue,
91 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
92 registry->RegisterListPref(
93 bookmarks::prefs::kManagedBookmarks,
94 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
95 }
96
GetBrowserContextToUse(content::BrowserContext * context) const97 content::BrowserContext* BookmarkModelFactory::GetBrowserContextToUse(
98 content::BrowserContext* context) const {
99 return chrome::GetBrowserContextRedirectedInIncognito(context);
100 }
101
ServiceIsNULLWhileTesting() const102 bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const {
103 return true;
104 }
105