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