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