| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 5 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/deferred_sequenced_task_runner.h" | 8 #include "base/deferred_sequenced_task_runner.h" |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model.h" | 11 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 12 #include "chrome/browser/profiles/incognito_helpers.h" | 12 #include "chrome/browser/profiles/incognito_helpers.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/startup_task_runner_service.h" | 14 #include "chrome/browser/profiles/startup_task_runner_service.h" |
| 15 #include "chrome/browser/profiles/startup_task_runner_service_factory.h" | 15 #include "chrome/browser/profiles/startup_task_runner_service_factory.h" |
| 16 #include "chrome/browser/undo/bookmark_undo_service.h" | 16 #include "chrome/browser/undo/bookmark_undo_service.h" |
| 17 #include "chrome/browser/undo/bookmark_undo_service_factory.h" | 17 #include "chrome/browser/undo/bookmark_undo_service_factory.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "components/bookmarks/core/common/bookmark_pref_names.h" | 19 #include "components/bookmarks/core/common/bookmark_pref_names.h" |
| 20 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 20 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 21 #include "components/user_prefs/pref_registry_syncable.h" | 21 #include "components/user_prefs/pref_registry_syncable.h" |
| 22 #include "content/public/browser/browser_thread.h" |
| 22 | 23 |
| 23 // static | 24 // static |
| 24 BookmarkModel* BookmarkModelFactory::GetForProfile(Profile* profile) { | 25 BookmarkModel* BookmarkModelFactory::GetForProfile(Profile* profile) { |
| 25 return static_cast<BookmarkModel*>( | 26 ChromeBookmarkClient* bookmark_client = static_cast<ChromeBookmarkClient*>( |
| 26 GetInstance()->GetServiceForBrowserContext(profile, true)); | 27 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 28 return bookmark_client ? bookmark_client->model() : NULL; |
| 27 } | 29 } |
| 28 | 30 |
| 29 BookmarkModel* BookmarkModelFactory::GetForProfileIfExists(Profile* profile) { | 31 BookmarkModel* BookmarkModelFactory::GetForProfileIfExists(Profile* profile) { |
| 30 return static_cast<BookmarkModel*>( | 32 ChromeBookmarkClient* bookmark_client = static_cast<ChromeBookmarkClient*>( |
| 31 GetInstance()->GetServiceForBrowserContext(profile, false)); | 33 GetInstance()->GetServiceForBrowserContext(profile, false)); |
| 34 return bookmark_client ? bookmark_client->model() : NULL; |
| 32 } | 35 } |
| 33 | 36 |
| 34 // static | 37 // static |
| 35 BookmarkModelFactory* BookmarkModelFactory::GetInstance() { | 38 BookmarkModelFactory* BookmarkModelFactory::GetInstance() { |
| 36 return Singleton<BookmarkModelFactory>::get(); | 39 return Singleton<BookmarkModelFactory>::get(); |
| 37 } | 40 } |
| 38 | 41 |
| 39 BookmarkModelFactory::BookmarkModelFactory() | 42 BookmarkModelFactory::BookmarkModelFactory() |
| 40 : BrowserContextKeyedServiceFactory( | 43 : BrowserContextKeyedServiceFactory( |
| 41 "BookmarkModel", | 44 "BookmarkModel", |
| 42 BrowserContextDependencyManager::GetInstance()) { | 45 BrowserContextDependencyManager::GetInstance()) { |
| 43 } | 46 } |
| 44 | 47 |
| 45 BookmarkModelFactory::~BookmarkModelFactory() {} | 48 BookmarkModelFactory::~BookmarkModelFactory() {} |
| 46 | 49 |
| 47 KeyedService* BookmarkModelFactory::BuildServiceInstanceFor( | 50 KeyedService* BookmarkModelFactory::BuildServiceInstanceFor( |
| 48 content::BrowserContext* context) const { | 51 content::BrowserContext* context) const { |
| 49 Profile* profile = static_cast<Profile*>(context); | 52 Profile* profile = static_cast<Profile*>(context); |
| 50 BookmarkModel* bookmark_model = new BookmarkModel(profile); | 53 ChromeBookmarkClient* bookmark_client = new ChromeBookmarkClient(profile); |
| 51 bookmark_model->Load(StartupTaskRunnerServiceFactory::GetForProfile(profile)-> | 54 bookmark_client->model()->Load( |
| 52 GetBookmarkTaskRunner()); | 55 profile->GetPrefs(), |
| 56 profile->GetPath(), |
| 57 StartupTaskRunnerServiceFactory::GetForProfile(profile) |
| 58 ->GetBookmarkTaskRunner(), |
| 59 content::BrowserThread::GetMessageLoopProxyForThread( |
| 60 content::BrowserThread::UI)); |
| 53 #if !defined(OS_ANDROID) | 61 #if !defined(OS_ANDROID) |
| 54 if (CommandLine::ForCurrentProcess()->HasSwitch( | 62 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 55 switches::kEnableBookmarkUndo)) { | 63 switches::kEnableBookmarkUndo)) { |
| 56 bookmark_model->AddObserver( | 64 bookmark_client->model()->AddObserver( |
| 57 BookmarkUndoServiceFactory::GetForProfile(profile)); | 65 BookmarkUndoServiceFactory::GetForProfile(profile)); |
| 58 } | 66 } |
| 59 #endif // !defined(OS_ANDROID) | 67 #endif // !defined(OS_ANDROID) |
| 60 return bookmark_model; | 68 return bookmark_client; |
| 61 } | 69 } |
| 62 | 70 |
| 63 void BookmarkModelFactory::RegisterProfilePrefs( | 71 void BookmarkModelFactory::RegisterProfilePrefs( |
| 64 user_prefs::PrefRegistrySyncable* registry) { | 72 user_prefs::PrefRegistrySyncable* registry) { |
| 65 // Don't sync this, as otherwise, due to a limitation in sync, it | 73 // Don't sync this, as otherwise, due to a limitation in sync, it |
| 66 // will cause a deadlock (see http://crbug.com/97955). If we truly | 74 // will cause a deadlock (see http://crbug.com/97955). If we truly |
| 67 // want to sync the expanded state of folders, it should be part of | 75 // want to sync the expanded state of folders, it should be part of |
| 68 // bookmark sync itself (i.e., a property of the sync folder nodes). | 76 // bookmark sync itself (i.e., a property of the sync folder nodes). |
| 69 registry->RegisterListPref(prefs::kBookmarkEditorExpandedNodes, | 77 registry->RegisterListPref(prefs::kBookmarkEditorExpandedNodes, |
| 70 new base::ListValue, | 78 new base::ListValue, |
| 71 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 79 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 72 } | 80 } |
| 73 | 81 |
| 74 content::BrowserContext* BookmarkModelFactory::GetBrowserContextToUse( | 82 content::BrowserContext* BookmarkModelFactory::GetBrowserContextToUse( |
| 75 content::BrowserContext* context) const { | 83 content::BrowserContext* context) const { |
| 76 return chrome::GetBrowserContextRedirectedInIncognito(context); | 84 return chrome::GetBrowserContextRedirectedInIncognito(context); |
| 77 } | 85 } |
| 78 | 86 |
| 79 bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const { | 87 bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const { |
| 80 return true; | 88 return true; |
| 81 } | 89 } |
| OLD | NEW |