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/bookmark_utils.h"
22 #include "components/bookmarks/browser/startup_task_runner_service.h"
23 #include "components/keyed_service/content/browser_context_dependency_manager.h"
24 #include "components/undo/bookmark_undo_service.h"
25 #include "content/public/browser/browser_thread.h"
27 using bookmarks::BookmarkModel
;
30 BookmarkModel
* BookmarkModelFactory::GetForProfile(Profile
* profile
) {
31 return static_cast<BookmarkModel
*>(
32 GetInstance()->GetServiceForBrowserContext(profile
, true));
36 BookmarkModel
* BookmarkModelFactory::GetForProfileIfExists(Profile
* profile
) {
37 return static_cast<BookmarkModel
*>(
38 GetInstance()->GetServiceForBrowserContext(profile
, false));
42 BookmarkModelFactory
* BookmarkModelFactory::GetInstance() {
43 return Singleton
<BookmarkModelFactory
>::get();
46 BookmarkModelFactory::BookmarkModelFactory()
47 : BrowserContextKeyedServiceFactory(
49 BrowserContextDependencyManager::GetInstance()) {
50 DependsOn(BookmarkUndoServiceFactory::GetInstance());
51 DependsOn(ChromeBookmarkClientFactory::GetInstance());
52 DependsOn(StartupTaskRunnerServiceFactory::GetInstance());
55 BookmarkModelFactory::~BookmarkModelFactory() {
58 KeyedService
* BookmarkModelFactory::BuildServiceInstanceFor(
59 content::BrowserContext
* context
) const {
60 Profile
* profile
= Profile::FromBrowserContext(context
);
61 ChromeBookmarkClient
* bookmark_client
=
62 ChromeBookmarkClientFactory::GetForProfile(profile
);
63 BookmarkModel
* bookmark_model
= new BookmarkModel(bookmark_client
);
64 bookmark_client
->Init(bookmark_model
);
65 bookmark_model
->Load(profile
->GetPrefs(),
66 profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
),
68 StartupTaskRunnerServiceFactory::GetForProfile(profile
)
69 ->GetBookmarkTaskRunner(),
70 content::BrowserThread::GetMessageLoopProxyForThread(
71 content::BrowserThread::UI
));
72 bool register_bookmark_undo_service_as_observer
= true;
73 #if !defined(OS_IOS) && !defined(OS_ANDROID)
74 register_bookmark_undo_service_as_observer
=
75 base::CommandLine::ForCurrentProcess()->HasSwitch(
76 switches::kEnableBookmarkUndo
);
77 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)
78 if (register_bookmark_undo_service_as_observer
)
79 BookmarkUndoServiceFactory::GetForProfile(profile
)->Start(bookmark_model
);
80 return bookmark_model
;
83 void BookmarkModelFactory::RegisterProfilePrefs(
84 user_prefs::PrefRegistrySyncable
* registry
) {
85 bookmarks::RegisterProfilePrefs(registry
);
88 content::BrowserContext
* BookmarkModelFactory::GetBrowserContextToUse(
89 content::BrowserContext
* context
) const {
90 return chrome::GetBrowserContextRedirectedInIncognito(context
);
93 bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const {