Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / bookmarks / managed_bookmark_service_factory.cc
blob64fed097f24ea0cea85ded76619edcd06a19cc83
1 // Copyright 2015 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/managed_bookmark_service_factory.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/memory/singleton.h"
11 #include "chrome/browser/policy/profile_policy_connector.h"
12 #include "chrome/browser/policy/profile_policy_connector_factory.h"
13 #include "chrome/browser/profiles/incognito_helpers.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "components/bookmarks/managed/managed_bookmark_service.h"
16 #include "components/keyed_service/content/browser_context_dependency_manager.h"
17 #include "policy/policy_constants.h"
19 namespace {
21 std::string GetManagedBookmarksDomain(Profile* profile) {
22 policy::ProfilePolicyConnector* connector =
23 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile);
24 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks))
25 return connector->GetManagementDomain();
26 return std::string();
29 scoped_ptr<KeyedService> BuildManagedBookmarkService(
30 content::BrowserContext* context) {
31 Profile* profile = Profile::FromBrowserContext(context);
32 return make_scoped_ptr(new bookmarks::ManagedBookmarkService(
33 profile->GetPrefs(),
34 base::Bind(&GetManagedBookmarksDomain, base::Unretained(profile))));
37 } // namespace
39 // static
40 bookmarks::ManagedBookmarkService* ManagedBookmarkServiceFactory::GetForProfile(
41 Profile* profile) {
42 return static_cast<bookmarks::ManagedBookmarkService*>(
43 GetInstance()->GetServiceForBrowserContext(profile, true));
46 // static
47 ManagedBookmarkServiceFactory* ManagedBookmarkServiceFactory::GetInstance() {
48 return base::Singleton<ManagedBookmarkServiceFactory>::get();
51 // static
52 BrowserContextKeyedServiceFactory::TestingFactoryFunction
53 ManagedBookmarkServiceFactory::GetDefaultFactory() {
54 return &BuildManagedBookmarkService;
57 ManagedBookmarkServiceFactory::ManagedBookmarkServiceFactory()
58 : BrowserContextKeyedServiceFactory(
59 "ManagedBookmarkService",
60 BrowserContextDependencyManager::GetInstance()) {
61 DependsOn(policy::ProfilePolicyConnectorFactory::GetInstance());
64 ManagedBookmarkServiceFactory::~ManagedBookmarkServiceFactory() {}
66 KeyedService* ManagedBookmarkServiceFactory::BuildServiceInstanceFor(
67 content::BrowserContext* context) const {
68 return BuildManagedBookmarkService(context).release();
71 content::BrowserContext* ManagedBookmarkServiceFactory::GetBrowserContextToUse(
72 content::BrowserContext* context) const {
73 return chrome::GetBrowserContextRedirectedInIncognito(context);
76 bool ManagedBookmarkServiceFactory::ServiceIsNULLWhileTesting() const {
77 return true;