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/deferred_sequenced_task_runner.h"
8 #include "base/memory/singleton.h"
9 #include "base/values.h"
10 #include "chrome/browser/bookmarks/bookmark_model.h"
11 #include "chrome/browser/profiles/incognito_helpers.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/startup_task_runner_service.h"
14 #include "chrome/browser/profiles/startup_task_runner_service_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
17 #include "components/user_prefs/pref_registry_syncable.h"
20 BookmarkModel* BookmarkModelFactory::GetForProfile(Profile* profile) {
21 return static_cast<BookmarkModel*>(
22 GetInstance()->GetServiceForBrowserContext(profile, true));
25 BookmarkModel* BookmarkModelFactory::GetForProfileIfExists(Profile* profile) {
26 return static_cast<BookmarkModel*>(
27 GetInstance()->GetServiceForBrowserContext(profile, false));
31 BookmarkModelFactory* BookmarkModelFactory::GetInstance() {
32 return Singleton<BookmarkModelFactory>::get();
35 BookmarkModelFactory::BookmarkModelFactory()
36 : BrowserContextKeyedServiceFactory(
38 BrowserContextDependencyManager::GetInstance()) {
41 BookmarkModelFactory::~BookmarkModelFactory() {}
43 BrowserContextKeyedService* BookmarkModelFactory::BuildServiceInstanceFor(
44 content::BrowserContext* context) const {
45 Profile* profile = static_cast<Profile*>(context);
46 BookmarkModel* bookmark_model = new BookmarkModel(profile);
47 bookmark_model->Load(StartupTaskRunnerServiceFactory::GetForProfile(profile)->
48 GetBookmarkTaskRunner());
49 return bookmark_model;
52 void BookmarkModelFactory::RegisterProfilePrefs(
53 user_prefs::PrefRegistrySyncable* registry) {
54 // Don't sync this, as otherwise, due to a limitation in sync, it
55 // will cause a deadlock (see http://crbug.com/97955). If we truly
56 // want to sync the expanded state of folders, it should be part of
57 // bookmark sync itself (i.e., a property of the sync folder nodes).
58 registry->RegisterListPref(prefs::kBookmarkEditorExpandedNodes,
60 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
63 content::BrowserContext* BookmarkModelFactory::GetBrowserContextToUse(
64 content::BrowserContext* context) const {
65 return chrome::GetBrowserContextRedirectedInIncognito(context);
68 bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const {