user_cloud_policy_manager_factory.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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
3// found in the LICENSE file.
4
5#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
6
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "chrome/browser/policy/cloud/user_cloud_policy_manager.h"
10#include "chrome/browser/policy/cloud/user_cloud_policy_store.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/common/chrome_switches.h"
13#include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
14
15namespace policy {
16
17// static
18UserCloudPolicyManagerFactory* UserCloudPolicyManagerFactory::GetInstance() {
19  return Singleton<UserCloudPolicyManagerFactory>::get();
20}
21
22// static
23UserCloudPolicyManager* UserCloudPolicyManagerFactory::GetForProfile(
24    Profile* profile) {
25  return GetInstance()->GetManagerForProfile(profile);
26}
27
28// static
29scoped_ptr<UserCloudPolicyManager>
30    UserCloudPolicyManagerFactory::CreateForProfile(Profile* profile,
31                                                    bool force_immediate_load) {
32  return GetInstance()->CreateManagerForProfile(profile, force_immediate_load);
33}
34
35UserCloudPolicyManagerFactory::UserCloudPolicyManagerFactory()
36    : BrowserContextKeyedBaseFactory(
37        "UserCloudPolicyManager",
38        BrowserContextDependencyManager::GetInstance()) {}
39
40UserCloudPolicyManagerFactory::~UserCloudPolicyManagerFactory() {}
41
42UserCloudPolicyManager* UserCloudPolicyManagerFactory::GetManagerForProfile(
43    Profile* profile) {
44  // Get the manager for the original profile, since the PolicyService is
45  // also shared between the incognito Profile and the original Profile.
46  ManagerMap::const_iterator it = managers_.find(profile->GetOriginalProfile());
47  return it != managers_.end() ? it->second : NULL;
48}
49
50scoped_ptr<UserCloudPolicyManager>
51    UserCloudPolicyManagerFactory::CreateManagerForProfile(
52        Profile* profile,
53        bool force_immediate_load) {
54  if (CommandLine::ForCurrentProcess()->HasSwitch(
55          switches::kDisableCloudPolicyOnSignin)) {
56    return scoped_ptr<UserCloudPolicyManager>();
57  }
58  scoped_ptr<UserCloudPolicyStore> store(UserCloudPolicyStore::Create(profile));
59  if (force_immediate_load)
60    store->LoadImmediately();
61  scoped_ptr<UserCloudPolicyManager> manager(
62      new UserCloudPolicyManager(profile, store.Pass()));
63  manager->Init();
64  return manager.Pass();
65}
66
67void UserCloudPolicyManagerFactory::BrowserContextShutdown(
68    content::BrowserContext* context) {
69  Profile* profile = static_cast<Profile*>(context);
70  if (profile->IsOffTheRecord())
71    return;
72  UserCloudPolicyManager* manager = GetManagerForProfile(profile);
73  if (manager) {
74    manager->CloudPolicyManager::Shutdown();
75    manager->BrowserContextKeyedService::Shutdown();
76  }
77}
78
79void UserCloudPolicyManagerFactory::SetEmptyTestingFactory(
80    content::BrowserContext* profile) {
81}
82
83void UserCloudPolicyManagerFactory::CreateServiceNow(
84    content::BrowserContext* profile) {
85}
86
87void UserCloudPolicyManagerFactory::Register(Profile* profile,
88                                             UserCloudPolicyManager* instance) {
89  UserCloudPolicyManager*& entry = managers_[profile];
90  DCHECK(!entry);
91  entry = instance;
92}
93
94void UserCloudPolicyManagerFactory::Unregister(
95    Profile* profile,
96    UserCloudPolicyManager* instance) {
97  ManagerMap::iterator entry = managers_.find(profile);
98  if (entry != managers_.end()) {
99    DCHECK_EQ(instance, entry->second);
100    managers_.erase(entry);
101  } else {
102    NOTREACHED();
103  }
104}
105
106}  // namespace policy
107