browser_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 "chrome/browser/policy/browser_policy_connector.h" 6 7#include "base/command_line.h" 8#include "base/file_util.h" 9#include "base/path_service.h" 10#include "chrome/browser/browser_process.h" 11#include "chrome/browser/policy/cloud_policy_identity_strategy.h" 12#include "chrome/browser/policy/cloud_policy_subsystem.h" 13#include "chrome/browser/policy/configuration_policy_pref_store.h" 14#include "chrome/browser/policy/configuration_policy_provider.h" 15#include "chrome/browser/policy/dummy_configuration_policy_provider.h" 16#include "chrome/browser/prefs/pref_service.h" 17#include "chrome/browser/profiles/profile.h" 18#include "chrome/common/chrome_paths.h" 19#include "chrome/common/chrome_switches.h" 20#include "chrome/common/notification_service.h" 21#include "chrome/common/pref_names.h" 22 23#if defined(OS_WIN) 24#include "chrome/browser/policy/configuration_policy_provider_win.h" 25#elif defined(OS_MACOSX) 26#include "chrome/browser/policy/configuration_policy_provider_mac.h" 27#elif defined(OS_POSIX) 28#include "chrome/browser/policy/config_dir_policy_provider.h" 29#endif 30 31#if defined(OS_CHROMEOS) 32#include "chrome/browser/policy/device_policy_identity_strategy.h" 33#endif 34 35namespace { 36 37const FilePath::CharType kDevicePolicyCacheFile[] = 38 FILE_PATH_LITERAL("Policy"); 39 40} // namespace 41 42namespace policy { 43 44BrowserPolicyConnector::BrowserPolicyConnector() { 45 managed_platform_provider_.reset(CreateManagedPlatformProvider()); 46 recommended_platform_provider_.reset(CreateRecommendedPlatformProvider()); 47 registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, 48 NotificationService::AllSources()); 49 50#if defined(OS_CHROMEOS) 51 CommandLine* command_line = CommandLine::ForCurrentProcess(); 52 if (command_line->HasSwitch(switches::kDevicePolicyCacheDir)) { 53 FilePath cache_dir(command_line->GetSwitchValuePath( 54 switches::kDevicePolicyCacheDir)); 55 56 if (!file_util::CreateDirectory(cache_dir)) { 57 LOG(WARNING) << "Device policy cache directory " 58 << cache_dir.value() 59 << " is not accessible, skipping initialization."; 60 } else { 61 identity_strategy_.reset(new DevicePolicyIdentityStrategy()); 62 cloud_policy_subsystem_.reset( 63 new CloudPolicySubsystem(cache_dir.Append(kDevicePolicyCacheFile), 64 identity_strategy_.get())); 65 } 66 } 67#endif 68} 69 70BrowserPolicyConnector::BrowserPolicyConnector( 71 ConfigurationPolicyProvider* managed_platform_provider, 72 ConfigurationPolicyProvider* recommended_platform_provider) 73 : managed_platform_provider_(managed_platform_provider), 74 recommended_platform_provider_(recommended_platform_provider) {} 75 76BrowserPolicyConnector::~BrowserPolicyConnector() { 77 if (cloud_policy_subsystem_.get()) 78 cloud_policy_subsystem_->Shutdown(); 79 cloud_policy_subsystem_.reset(); 80 identity_strategy_.reset(); 81} 82 83ConfigurationPolicyProvider* 84 BrowserPolicyConnector::GetManagedPlatformProvider() const { 85 return managed_platform_provider_.get(); 86} 87 88ConfigurationPolicyProvider* 89 BrowserPolicyConnector::GetManagedCloudProvider() const { 90 if (cloud_policy_subsystem_.get()) 91 return cloud_policy_subsystem_->GetManagedPolicyProvider(); 92 93 return NULL; 94} 95 96ConfigurationPolicyProvider* 97 BrowserPolicyConnector::GetRecommendedPlatformProvider() const { 98 return recommended_platform_provider_.get(); 99} 100 101ConfigurationPolicyProvider* 102 BrowserPolicyConnector::GetRecommendedCloudProvider() const { 103 if (cloud_policy_subsystem_.get()) 104 return cloud_policy_subsystem_->GetRecommendedPolicyProvider(); 105 106 return NULL; 107} 108 109ConfigurationPolicyProvider* 110 BrowserPolicyConnector::CreateManagedPlatformProvider() { 111 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list = 112 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); 113#if defined(OS_WIN) 114 return new ConfigurationPolicyProviderWin(policy_list); 115#elif defined(OS_MACOSX) 116 return new ConfigurationPolicyProviderMac(policy_list); 117#elif defined(OS_POSIX) 118 FilePath config_dir_path; 119 if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) { 120 return new ConfigDirPolicyProvider( 121 policy_list, 122 config_dir_path.Append(FILE_PATH_LITERAL("managed"))); 123 } else { 124 return new DummyConfigurationPolicyProvider(policy_list); 125 } 126#else 127 return new DummyConfigurationPolicyProvider(policy_list); 128#endif 129} 130 131ConfigurationPolicyProvider* 132 BrowserPolicyConnector::CreateRecommendedPlatformProvider() { 133 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list = 134 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); 135#if defined(OS_POSIX) && !defined(OS_MACOSX) 136 FilePath config_dir_path; 137 if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) { 138 return new ConfigDirPolicyProvider( 139 policy_list, 140 config_dir_path.Append(FILE_PATH_LITERAL("recommended"))); 141 } else { 142 return new DummyConfigurationPolicyProvider(policy_list); 143 } 144#else 145 return new DummyConfigurationPolicyProvider(policy_list); 146#endif 147} 148 149// static 150void BrowserPolicyConnector::RegisterPrefs(PrefService* local_state) { 151 local_state->RegisterIntegerPref(prefs::kPolicyDevicePolicyRefreshRate, 152 kDefaultPolicyRefreshRateInMilliseconds); 153} 154 155void BrowserPolicyConnector::Observe(NotificationType type, 156 const NotificationSource& source, 157 const NotificationDetails& details) { 158 if (type == NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE) { 159 Initialize(g_browser_process->local_state(), 160 Profile::GetDefaultRequestContext()); 161 } else { 162 NOTREACHED(); 163 } 164} 165 166void BrowserPolicyConnector::Initialize( 167 PrefService* local_state, 168 URLRequestContextGetter* request_context) { 169 // TODO(jkummerow, mnissler): Move this out of the browser startup path. 170 DCHECK(local_state); 171 DCHECK(request_context); 172 if (cloud_policy_subsystem_.get()) { 173 cloud_policy_subsystem_->Initialize(local_state, 174 prefs::kPolicyDevicePolicyRefreshRate, 175 request_context); 176 } 177} 178 179} // namespace 180