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"
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"
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();
29 scoped_ptr
<KeyedService
> BuildManagedBookmarkService(
30 content::BrowserContext
* context
) {
31 Profile
* profile
= Profile::FromBrowserContext(context
);
32 return make_scoped_ptr(new bookmarks::ManagedBookmarkService(
34 base::Bind(&GetManagedBookmarksDomain
, base::Unretained(profile
))));
40 bookmarks::ManagedBookmarkService
* ManagedBookmarkServiceFactory::GetForProfile(
42 return static_cast<bookmarks::ManagedBookmarkService
*>(
43 GetInstance()->GetServiceForBrowserContext(profile
, true));
47 ManagedBookmarkServiceFactory
* ManagedBookmarkServiceFactory::GetInstance() {
48 return base::Singleton
<ManagedBookmarkServiceFactory
>::get();
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 {