bookmark_model_factory.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 = static_cast<ChromeBookmarkClient*>( 31 GetInstance()->GetServiceForBrowserContext(profile, true)); 32 return bookmark_client ? bookmark_client->model() : NULL; 33} 34 35BookmarkModel* BookmarkModelFactory::GetForProfileIfExists(Profile* profile) { 36 ChromeBookmarkClient* bookmark_client = static_cast<ChromeBookmarkClient*>( 37 GetInstance()->GetServiceForBrowserContext(profile, false)); 38 return bookmark_client ? bookmark_client->model() : NULL; 39} 40 41// static 42BookmarkModelFactory* BookmarkModelFactory::GetInstance() { 43 return Singleton<BookmarkModelFactory>::get(); 44} 45 46BookmarkModelFactory::BookmarkModelFactory() 47 : BrowserContextKeyedServiceFactory( 48 "BookmarkModel", 49 BrowserContextDependencyManager::GetInstance()) { 50} 51 52BookmarkModelFactory::~BookmarkModelFactory() {} 53 54KeyedService* 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( 60 profile->GetPrefs(), 61 profile->GetPrefs()->GetString(prefs::kAcceptLanguages), 62 profile->GetPath(), 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; 69#if !defined(OS_IOS) 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)); 77 } 78#endif // !defined(OS_ANDROID) 79 return bookmark_client; 80} 81 82void 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, 89 new base::ListValue, 90 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 91} 92 93content::BrowserContext* BookmarkModelFactory::GetBrowserContextToUse( 94 content::BrowserContext* context) const { 95 return chrome::GetBrowserContextRedirectedInIncognito(context); 96} 97 98bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const { 99 return true; 100} 101