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