OLD | NEW |
(Empty) | |
| 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 "base/command_line.h" |
| 6 #include "chrome/browser/policy/cloud_policy_cache.h" |
| 7 #include "chrome/browser/policy/cloud_policy_client.h" |
| 8 #include "chrome/browser/policy/cloud_policy_context.h" |
| 9 #include "chrome/browser/policy/cloud_policy_controller.h" |
| 10 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 11 #include "chrome/browser/policy/device_management_service.h" |
| 12 #include "chrome/browser/policy/device_token_fetcher.h" |
| 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/notification_details.h" |
| 15 #include "chrome/common/notification_source.h" |
| 16 #include "chrome/common/notification_type.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Refresh rate sanity interval bounds. |
| 21 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes |
| 22 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day |
| 23 |
| 24 } |
| 25 |
| 26 namespace policy { |
| 27 |
| 28 CloudPolicyContext::CloudPolicyContext(const FilePath& policy_cache_file, |
| 29 CloudPolicyController* controller) |
| 30 : prefs_(NULL) { |
| 31 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 32 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { |
| 33 device_management_service_.reset(new DeviceManagementService( |
| 34 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); |
| 35 cloud_policy_cache_.reset(new CloudPolicyCache(policy_cache_file)); |
| 36 cloud_policy_cache_->LoadFromFile(); |
| 37 |
| 38 device_token_fetcher_.reset( |
| 39 new DeviceTokenFetcher(device_management_service_.get(), |
| 40 cloud_policy_cache_.get())); |
| 41 |
| 42 cloud_policy_client_.reset( |
| 43 new CloudPolicyClient(cloud_policy_cache_.get(), |
| 44 device_management_service_->CreateBackend(), |
| 45 device_token_fetcher_.get(), |
| 46 controller)); |
| 47 } |
| 48 } |
| 49 |
| 50 CloudPolicyContext::~CloudPolicyContext() { |
| 51 DCHECK(!prefs_); |
| 52 cloud_policy_client_.reset(); |
| 53 device_token_fetcher_.reset(); |
| 54 cloud_policy_cache_.reset(); |
| 55 device_management_service_.reset(); |
| 56 } |
| 57 |
| 58 void CloudPolicyContext::Initialize(PrefService* prefs, |
| 59 const char* refresh_rate_pref_name, |
| 60 URLRequestContextGetter* request_context) { |
| 61 DCHECK(!prefs_); |
| 62 prefs_ = prefs; |
| 63 |
| 64 if (device_management_service_.get()) |
| 65 device_management_service_->Initialize(request_context); |
| 66 |
| 67 policy_refresh_rate_.Init(refresh_rate_pref_name, prefs_, this); |
| 68 UpdatePolicyRefreshRate(); |
| 69 } |
| 70 |
| 71 void CloudPolicyContext::Shutdown() { |
| 72 if (device_management_service_.get()) |
| 73 device_management_service_->Shutdown(); |
| 74 cloud_policy_client_.reset(); |
| 75 cloud_policy_cache_.reset(); |
| 76 policy_refresh_rate_.Destroy(); |
| 77 prefs_ = NULL; |
| 78 } |
| 79 |
| 80 ConfigurationPolicyProvider* CloudPolicyContext::GetManagedPolicyProvider() { |
| 81 if (cloud_policy_cache_.get()) |
| 82 return cloud_policy_cache_->GetManagedPolicyProvider(); |
| 83 |
| 84 return NULL; |
| 85 } |
| 86 |
| 87 ConfigurationPolicyProvider* |
| 88 CloudPolicyContext::GetRecommendedPolicyProvider() { |
| 89 if (cloud_policy_cache_.get()) |
| 90 return cloud_policy_cache_->GetRecommendedPolicyProvider(); |
| 91 |
| 92 return NULL; |
| 93 } |
| 94 |
| 95 void CloudPolicyContext::UpdatePolicyRefreshRate() { |
| 96 if (cloud_policy_client_.get()) { |
| 97 // Clamp to sane values. |
| 98 int64 refresh_rate = policy_refresh_rate_.GetValue(); |
| 99 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); |
| 100 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); |
| 101 cloud_policy_client_->SetRefreshRate(refresh_rate); |
| 102 } |
| 103 } |
| 104 |
| 105 void CloudPolicyContext::Observe(NotificationType type, |
| 106 const NotificationSource& source, |
| 107 const NotificationDetails& details) { |
| 108 if (type == NotificationType::PREF_CHANGED && |
| 109 policy_refresh_rate_.GetPrefName() |
| 110 == *(Details<std::string>(details).ptr()) && |
| 111 prefs_ == Source<PrefService>(source).ptr()) { |
| 112 UpdatePolicyRefreshRate(); |
| 113 } else { |
| 114 NOTREACHED(); |
| 115 } |
| 116 } |
| 117 |
| 118 } // namespace policy |
OLD | NEW |