| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 6 #include "base/file_util.h" |
| 7 #include "chrome/browser/policy/device_management_policy_provider.h" | 7 #include "chrome/browser/policy/cloud_policy_context.h" |
| 8 #include "chrome/browser/policy/device_management_service.h" | |
| 9 #include "chrome/browser/policy/profile_policy_context.h" | 8 #include "chrome/browser/policy/profile_policy_context.h" |
| 9 #include "chrome/browser/policy/user_policy_controller.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/notification_details.h" | 13 #include "chrome/common/net/url_request_context_getter.h" |
| 14 #include "chrome/common/notification_source.h" | |
| 15 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 // Refresh rate sanity interval bounds. | 18 // Refresh rate sanity interval bounds. |
| 20 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes | 19 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes |
| 21 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day | 20 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day |
| 22 | 21 |
| 22 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management"); |
| 23 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token"); |
| 24 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy"); |
| 25 |
| 23 } | 26 } |
| 24 | 27 |
| 25 namespace policy { | 28 namespace policy { |
| 26 | 29 |
| 27 ProfilePolicyContext::ProfilePolicyContext(Profile* profile) | 30 ProfilePolicyContext::ProfilePolicyContext(Profile* profile) |
| 28 : profile_(profile) { | 31 : profile_(profile) { |
| 32 // TODO(mnissler): We access the file system here. The cloud policy context |
| 33 // below needs to do so anyway, since it needs to read the policy cache from |
| 34 // disk. If this proves to be a problem, we need to do this initialization |
| 35 // asynchronously on the file thread and put in synchronization that allows us |
| 36 // to wait for the cache to be read during the browser startup code paths. |
| 37 // Another option would be to provide a generic IO-safe initializer called |
| 38 // from the PrefService that we could hook up with through the policy |
| 39 // provider. |
| 29 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 40 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 30 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { | 41 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { |
| 31 device_management_service_.reset(new DeviceManagementService( | 42 FilePath policy_cache_dir(profile_->GetPath()); |
| 32 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); | 43 policy_cache_dir = policy_cache_dir.Append(kPolicyDir); |
| 33 device_management_policy_provider_.reset( | 44 if (!file_util::CreateDirectory(policy_cache_dir)) { |
| 34 new policy::DeviceManagementPolicyProvider( | 45 LOG(WARNING) << "Failed to create policy state dir " |
| 35 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | 46 << policy_cache_dir.value() |
| 36 device_management_service_->CreateBackend(), | 47 << ", skipping cloud policy initialization."; |
| 37 profile_)); | 48 return; |
| 49 } |
| 50 |
| 51 controller_.reset( |
| 52 new UserPolicyController(profile_, |
| 53 policy_cache_dir.Append(kTokenCacheFile))); |
| 54 cloud_context_.reset( |
| 55 new CloudPolicyContext(policy_cache_dir.Append(kPolicyCacheFile), |
| 56 controller_.get())); |
| 38 } | 57 } |
| 39 } | 58 } |
| 40 | 59 |
| 41 ProfilePolicyContext::~ProfilePolicyContext() { | 60 ProfilePolicyContext::~ProfilePolicyContext() { |
| 42 device_management_policy_provider_.reset(); | 61 cloud_context_.reset(); |
| 43 device_management_service_.reset(); | 62 controller_.reset(); |
| 44 } | 63 } |
| 45 | 64 |
| 46 void ProfilePolicyContext::Initialize() { | 65 void ProfilePolicyContext::Initialize() { |
| 47 if (device_management_service_.get()) | 66 if (cloud_context_.get()) { |
| 48 device_management_service_->Initialize(profile_->GetRequestContext()); | 67 cloud_context_->Initialize(profile_->GetPrefs(), |
| 49 | 68 prefs::kPolicyUserPolicyRefreshRate, |
| 50 policy_refresh_rate_.Init(prefs::kPolicyRefreshRate, profile_->GetPrefs(), | 69 profile_->GetRequestContext()); |
| 51 this); | 70 } |
| 52 UpdatePolicyRefreshRate(); | |
| 53 } | 71 } |
| 54 | 72 |
| 55 void ProfilePolicyContext::Shutdown() { | 73 void ProfilePolicyContext::Shutdown() { |
| 56 if (device_management_service_.get()) | 74 if (cloud_context_.get()) |
| 57 device_management_service_->Shutdown(); | 75 cloud_context_->Shutdown(); |
| 58 } | 76 } |
| 59 | 77 |
| 60 DeviceManagementPolicyProvider* | 78 ConfigurationPolicyProvider* ProfilePolicyContext::GetManagedPolicyProvider() { |
| 61 ProfilePolicyContext::GetDeviceManagementPolicyProvider() { | 79 if (cloud_context_.get()) |
| 62 return device_management_policy_provider_.get(); | 80 return cloud_context_->GetManagedPolicyProvider(); |
| 81 |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 ConfigurationPolicyProvider* |
| 86 ProfilePolicyContext::GetRecommendedPolicyProvider() { |
| 87 if (cloud_context_.get()) |
| 88 return cloud_context_->GetRecommendedPolicyProvider(); |
| 89 |
| 90 return NULL; |
| 63 } | 91 } |
| 64 | 92 |
| 65 // static | 93 // static |
| 66 void ProfilePolicyContext::RegisterUserPrefs(PrefService* user_prefs) { | 94 void ProfilePolicyContext::RegisterPrefs(PrefService* user_prefs) { |
| 67 user_prefs->RegisterIntegerPref(prefs::kPolicyRefreshRate, | 95 user_prefs->RegisterIntegerPref(prefs::kPolicyUserPolicyRefreshRate, |
| 68 kDefaultPolicyRefreshRateInMilliseconds); | 96 kDefaultPolicyRefreshRateInMilliseconds); |
| 69 } | 97 } |
| 70 | 98 |
| 71 void ProfilePolicyContext::UpdatePolicyRefreshRate() { | |
| 72 if (device_management_policy_provider_.get()) { | |
| 73 // Clamp to sane values. | |
| 74 int64 refresh_rate = policy_refresh_rate_.GetValue(); | |
| 75 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); | |
| 76 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); | |
| 77 device_management_policy_provider_->SetRefreshRate(refresh_rate); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void ProfilePolicyContext::Observe(NotificationType type, | |
| 82 const NotificationSource& source, | |
| 83 const NotificationDetails& details) { | |
| 84 if (type == NotificationType::PREF_CHANGED && | |
| 85 prefs::kPolicyRefreshRate == *(Details<std::string>(details).ptr()) && | |
| 86 profile_->GetPrefs() == Source<PrefService>(source).ptr()) { | |
| 87 UpdatePolicyRefreshRate(); | |
| 88 } else { | |
| 89 NOTREACHED(); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 } // namespace policy | 99 } // namespace policy |
| OLD | NEW |