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/bookmarks/bookmark_model_factory.h"
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"
29 BookmarkModel
* BookmarkModelFactory::GetForProfile(Profile
* profile
) {
30 ChromeBookmarkClient
* bookmark_client
= static_cast<ChromeBookmarkClient
*>(
31 GetInstance()->GetServiceForBrowserContext(profile
, true));
32 return bookmark_client
? bookmark_client
->model() : NULL
;
35 BookmarkModel
* BookmarkModelFactory::GetForProfileIfExists(Profile
* profile
) {
36 ChromeBookmarkClient
* bookmark_client
= static_cast<ChromeBookmarkClient
*>(
37 GetInstance()->GetServiceForBrowserContext(profile
, false));
38 return bookmark_client
? bookmark_client
->model() : NULL
;
42 BookmarkModelFactory
* BookmarkModelFactory::GetInstance() {
43 return Singleton
<BookmarkModelFactory
>::get();
46 BookmarkModelFactory::BookmarkModelFactory()
47 : BrowserContextKeyedServiceFactory(
49 BrowserContextDependencyManager::GetInstance()) {
52 BookmarkModelFactory::~BookmarkModelFactory() {}
54 KeyedService
* BookmarkModelFactory::BuildServiceInstanceFor(
55 content::BrowserContext
* context
) const {
56 Profile
* profile
= static_cast<Profile
*>(context
);
57 ChromeBookmarkClient
* bookmark_client
= new ChromeBookmarkClient(
58 profile
, OmniboxFieldTrial::BookmarksIndexURLsValue());
59 bookmark_client
->model()->Load(
61 profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
),
63 StartupTaskRunnerServiceFactory::GetForProfile(profile
)
64 ->GetBookmarkTaskRunner(),
65 content::BrowserThread::GetMessageLoopProxyForThread(
66 content::BrowserThread::UI
));
67 #if !defined(OS_ANDROID)
68 bool register_bookmark_undo_service_as_observer
= true;
70 register_bookmark_undo_service_as_observer
=
71 CommandLine::ForCurrentProcess()->HasSwitch(
72 switches::kEnableBookmarkUndo
);
73 #endif // !defined(OS_IOS)
74 if (register_bookmark_undo_service_as_observer
) {
75 bookmark_client
->model()->AddObserver(
76 BookmarkUndoServiceFactory::GetForProfile(profile
));
78 #endif // !defined(OS_ANDROID)
79 return bookmark_client
;
82 void BookmarkModelFactory::RegisterProfilePrefs(
83 user_prefs::PrefRegistrySyncable
* registry
) {
84 // Don't sync this, as otherwise, due to a limitation in sync, it
85 // will cause a deadlock (see http://crbug.com/97955). If we truly
86 // want to sync the expanded state of folders, it should be part of
87 // bookmark sync itself (i.e., a property of the sync folder nodes).
88 registry
->RegisterListPref(prefs::kBookmarkEditorExpandedNodes
,
90 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
91 registry
->RegisterListPref(
92 prefs::kManagedBookmarks
,
93 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
96 content::BrowserContext
* BookmarkModelFactory::GetBrowserContextToUse(
97 content::BrowserContext
* context
) const {
98 return chrome::GetBrowserContextRedirectedInIncognito(context
);
101 bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const {