15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/bookmarks/bookmark_model_factory.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/command_line.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/deferred_sequenced_task_runner.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/singleton.h"
105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "base/prefs/pref_service.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/values.h"
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/bookmarks/chrome_bookmark_client.h"
13f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/profiles/incognito_helpers.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/profiles/startup_task_runner_service.h"
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/profiles/startup_task_runner_service_factory.h"
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/undo/bookmark_undo_service.h"
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/undo/bookmark_undo_service_factory.h"
20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/common/pref_names.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/bookmarks/browser/bookmark_model.h"
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/bookmarks/common/bookmark_pref_names.h"
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_dependency_manager.h"
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/public/browser/browser_thread.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BookmarkModel* BookmarkModelFactory::GetForProfile(Profile* profile) {
30f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return static_cast<BookmarkModel*>(
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      GetInstance()->GetServiceForBrowserContext(profile, true));
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// static
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BookmarkModel* BookmarkModelFactory::GetForProfileIfExists(Profile* profile) {
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return static_cast<BookmarkModel*>(
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetInstance()->GetServiceForBrowserContext(profile, false));
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// static
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BookmarkModelFactory* BookmarkModelFactory::GetInstance() {
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Singleton<BookmarkModelFactory>::get();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BookmarkModelFactory::BookmarkModelFactory()
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : BrowserContextKeyedServiceFactory(
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        "BookmarkModel",
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        BrowserContextDependencyManager::GetInstance()) {
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DependsOn(ChromeBookmarkClientFactory::GetInstance());
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DependsOn(StartupTaskRunnerServiceFactory::GetInstance());
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
53f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)BookmarkModelFactory::~BookmarkModelFactory() {
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)KeyedService* BookmarkModelFactory::BuildServiceInstanceFor(
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::BrowserContext* context) const {
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Profile* profile = static_cast<Profile*>(context);
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ChromeBookmarkClient* bookmark_client =
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ChromeBookmarkClientFactory::GetForProfile(profile);
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  BookmarkModel* bookmark_model = new BookmarkModel(bookmark_client);
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  bookmark_client->Init(bookmark_model);
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  bookmark_model->Load(profile->GetPrefs(),
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       profile->GetPath(),
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       StartupTaskRunnerServiceFactory::GetForProfile(profile)
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           ->GetBookmarkTaskRunner(),
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       content::BrowserThread::GetMessageLoopProxyForThread(
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           content::BrowserThread::UI));
70010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  bool register_bookmark_undo_service_as_observer = true;
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if !defined(OS_IOS) && !defined(OS_ANDROID)
72010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  register_bookmark_undo_service_as_observer =
73010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          switches::kEnableBookmarkUndo);
75010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#endif  // !defined(OS_IOS)
76010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (register_bookmark_undo_service_as_observer) {
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    bookmark_model->AddObserver(
78a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        BookmarkUndoServiceFactory::GetForProfile(profile));
79a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return bookmark_model;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid BookmarkModelFactory::RegisterProfilePrefs(
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't sync this, as otherwise, due to a limitation in sync, it
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will cause a deadlock (see http://crbug.com/97955).  If we truly
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // want to sync the expanded state of folders, it should be part of
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bookmark sync itself (i.e., a property of the sync folder nodes).
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  registry->RegisterListPref(bookmarks::prefs::kBookmarkEditorExpandedNodes,
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             new base::ListValue,
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  registry->RegisterListPref(
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      bookmarks::prefs::kManagedBookmarks,
9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)content::BrowserContext* BookmarkModelFactory::GetBrowserContextToUse(
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::BrowserContext* context) const {
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return chrome::GetBrowserContextRedirectedInIncognito(context);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BookmarkModelFactory::ServiceIsNULLWhileTesting() const {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
105