profile_policy_connector.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2011 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 <algorithm>
6#include <string>
7
8#include "base/command_line.h"
9#include "base/file_util.h"
10#include "chrome/browser/policy/cloud_policy_subsystem.h"
11#include "chrome/browser/policy/profile_policy_connector.h"
12#include "chrome/browser/policy/user_policy_identity_strategy.h"
13#include "chrome/browser/prefs/pref_service.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/common/net/url_request_context_getter.h"
17#include "chrome/common/pref_names.h"
18
19namespace {
20
21const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management");
22const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token");
23const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy");
24
25}  // namespace
26
27namespace policy {
28
29ProfilePolicyConnector::ProfilePolicyConnector(Profile* profile)
30    : profile_(profile) {
31  // TODO(mnissler): We access the file system here. The cloud policy context
32  // below needs to do so anyway, since it needs to read the policy cache from
33  // disk. If this proves to be a problem, we need to do this initialization
34  // asynchronously on the file thread and put in synchronization that allows us
35  // to wait for the cache to be read during the browser startup code paths.
36  // Another option would be to provide a generic IO-safe initializer called
37  // from the PrefService that we could hook up with through the policy
38  // provider.
39  CommandLine* command_line = CommandLine::ForCurrentProcess();
40  if (command_line->HasSwitch(switches::kDeviceManagementUrl)) {
41    FilePath policy_cache_dir(profile_->GetPath());
42    policy_cache_dir = policy_cache_dir.Append(kPolicyDir);
43    if (!file_util::CreateDirectory(policy_cache_dir)) {
44      LOG(WARNING) << "Failed to create policy state dir "
45                   << policy_cache_dir.value()
46                   << ", skipping cloud policy initialization.";
47      return;
48    }
49
50    identity_strategy_.reset(new UserPolicyIdentityStrategy(
51        profile_,
52        policy_cache_dir.Append(kTokenCacheFile)));
53    cloud_policy_subsystem_.reset(new CloudPolicySubsystem(
54        policy_cache_dir.Append(kPolicyCacheFile),
55        identity_strategy_.get()));
56  }
57}
58
59ProfilePolicyConnector::~ProfilePolicyConnector() {
60  cloud_policy_subsystem_.reset();
61  identity_strategy_.reset();
62}
63
64void ProfilePolicyConnector::Initialize() {
65  // TODO(jkummerow, mnissler): Move this out of the browser startup path.
66  if (cloud_policy_subsystem_.get()) {
67    cloud_policy_subsystem_->Initialize(profile_->GetPrefs(),
68                                        prefs::kPolicyUserPolicyRefreshRate,
69                                        profile_->GetRequestContext());
70  }
71}
72
73void ProfilePolicyConnector::Shutdown() {
74  if (cloud_policy_subsystem_.get())
75    cloud_policy_subsystem_->Shutdown();
76}
77
78ConfigurationPolicyProvider*
79    ProfilePolicyConnector::GetManagedCloudProvider() {
80  if (cloud_policy_subsystem_.get())
81    return cloud_policy_subsystem_->GetManagedPolicyProvider();
82
83  return NULL;
84}
85
86ConfigurationPolicyProvider*
87    ProfilePolicyConnector::GetRecommendedCloudProvider() {
88  if (cloud_policy_subsystem_.get())
89    return cloud_policy_subsystem_->GetRecommendedPolicyProvider();
90
91  return NULL;
92}
93
94// static
95void ProfilePolicyConnector::RegisterPrefs(PrefService* user_prefs) {
96  user_prefs->RegisterIntegerPref(prefs::kPolicyUserPolicyRefreshRate,
97                                  kDefaultPolicyRefreshRateInMilliseconds);
98}
99
100}  // namespace policy
101