1// Copyright 2013 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.
4
5#include "chrome/browser/policy/cloud/user_cloud_policy_invalidator_factory.h"
6
7#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
8#include "chrome/browser/policy/cloud/user_cloud_policy_invalidator.h"
9#include "chrome/browser/profiles/profile.h"
10#include "components/keyed_service/content/browser_context_dependency_manager.h"
11#if defined(OS_CHROMEOS)
12#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
13#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
14#else
15#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
16#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
17#endif
18
19namespace policy {
20
21// static
22UserCloudPolicyInvalidatorFactory*
23    UserCloudPolicyInvalidatorFactory::GetInstance() {
24  return Singleton<UserCloudPolicyInvalidatorFactory>::get();
25}
26
27UserCloudPolicyInvalidatorFactory::UserCloudPolicyInvalidatorFactory()
28    : BrowserContextKeyedServiceFactory(
29          "UserCloudPolicyInvalidator",
30          BrowserContextDependencyManager::GetInstance()) {
31  DependsOn(invalidation::ProfileInvalidationProviderFactory::GetInstance());
32#if defined(OS_CHROMEOS)
33  DependsOn(UserCloudPolicyManagerFactoryChromeOS::GetInstance());
34#else
35  DependsOn(UserCloudPolicyManagerFactory::GetInstance());
36#endif
37}
38
39UserCloudPolicyInvalidatorFactory::~UserCloudPolicyInvalidatorFactory() {}
40
41KeyedService* UserCloudPolicyInvalidatorFactory::BuildServiceInstanceFor(
42    content::BrowserContext* context) const {
43  Profile* profile = static_cast<Profile*>(context);
44#if defined(OS_CHROMEOS)
45  CloudPolicyManager* policy_manager =
46      UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
47#else
48  CloudPolicyManager* policy_manager =
49      UserCloudPolicyManagerFactory::GetForBrowserContext(context);
50#endif
51  if (!policy_manager)
52    return NULL;
53
54  return new UserCloudPolicyInvalidator(profile, policy_manager);
55}
56
57bool UserCloudPolicyInvalidatorFactory::
58ServiceIsCreatedWithBrowserContext() const {
59  // Must be automatically created to enable user policy invalidations.
60  return true;
61}
62
63bool UserCloudPolicyInvalidatorFactory::ServiceIsNULLWhileTesting() const {
64  // Not used in tests.
65  return true;
66}
67
68}  // namespace policy
69