| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/policy/device_management_policy_provider.h" | 5 #include "chrome/browser/policy/device_management_policy_provider.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "chrome/browser/browser_thread.h" | 12 #include "chrome/browser/browser_thread.h" |
| 13 #include "chrome/browser/policy/device_management_backend.h" | 13 #include "chrome/browser/policy/device_management_backend.h" |
| 14 #include "chrome/browser/policy/device_management_backend_impl.h" | |
| 15 #include "chrome/browser/policy/device_management_policy_cache.h" | 14 #include "chrome/browser/policy/device_management_policy_cache.h" |
| 16 #include "chrome/browser/policy/device_token_fetcher.h" | 15 #include "chrome/browser/policy/device_token_fetcher.h" |
| 17 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
| 20 #include "chrome/common/notification_type.h" | 19 #include "chrome/common/notification_type.h" |
| 21 | 20 |
| 22 namespace { | 21 namespace policy { |
| 23 | 22 |
| 24 const char kChromePolicyScope[] = "cros/device"; | 23 const char kChromePolicyScope[] = "chromeos/device"; |
| 25 const char kChromeDevicePolicySettingKey[] = "chrome-policy"; | 24 const char kChromeDevicePolicySettingKey[] = "chrome-policy"; |
| 26 const int64 kPolicyRefreshRateInMinutes = 3 * 60; // 3 hours | 25 const int64 kPolicyRefreshRateInMinutes = 3 * 60; // 3 hours |
| 27 | 26 |
| 28 } // namespace | |
| 29 | |
| 30 namespace policy { | |
| 31 | |
| 32 // Ensures that the portion of the policy provider implementation that requires | 27 // Ensures that the portion of the policy provider implementation that requires |
| 33 // the IOThread is deferred until the IOThread is fully initialized. The policy | 28 // the IOThread is deferred until the IOThread is fully initialized. The policy |
| 34 // provider posts this task on the UI thread during its constructor, thereby | 29 // provider posts this task on the UI thread during its constructor, thereby |
| 35 // guaranteeing that the code won't get executed until after the UI and IO | 30 // guaranteeing that the code won't get executed until after the UI and IO |
| 36 // threads are fully constructed. | 31 // threads are fully constructed. |
| 37 class DeviceManagementPolicyProvider::InitializeAfterIOThreadExistsTask | 32 class DeviceManagementPolicyProvider::InitializeAfterIOThreadExistsTask |
| 38 : public Task { | 33 : public Task { |
| 39 public: | 34 public: |
| 40 explicit InitializeAfterIOThreadExistsTask( | 35 explicit InitializeAfterIOThreadExistsTask( |
| 41 base::WeakPtr<DeviceManagementPolicyProvider> provider) | 36 base::WeakPtr<DeviceManagementPolicyProvider> provider) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 policy_request_pending_ = false; | 99 policy_request_pending_ = false; |
| 105 // TODO(danno): do something sensible in the error case, perhaps retry later? | 100 // TODO(danno): do something sensible in the error case, perhaps retry later? |
| 106 } | 101 } |
| 107 | 102 |
| 108 void DeviceManagementPolicyProvider::Shutdown() { | 103 void DeviceManagementPolicyProvider::Shutdown() { |
| 109 token_service_ = NULL; | 104 token_service_ = NULL; |
| 110 if (token_fetcher_) | 105 if (token_fetcher_) |
| 111 token_fetcher_->Shutdown(); | 106 token_fetcher_->Shutdown(); |
| 112 } | 107 } |
| 113 | 108 |
| 114 DeviceManagementBackend* DeviceManagementPolicyProvider::GetBackend() { | |
| 115 if (!backend_.get()) { | |
| 116 backend_.reset(new DeviceManagementBackendImpl( | |
| 117 GetDeviceManagementURL())); | |
| 118 } | |
| 119 return backend_.get(); | |
| 120 } | |
| 121 | |
| 122 void DeviceManagementPolicyProvider::Initialize() { | 109 void DeviceManagementPolicyProvider::Initialize() { |
| 123 registrar_.Add(this, | 110 registrar_.Add(this, |
| 124 NotificationType::DEVICE_TOKEN_AVAILABLE, | 111 NotificationType::DEVICE_TOKEN_AVAILABLE, |
| 125 NotificationService::AllSources()); | 112 NotificationService::AllSources()); |
| 126 | 113 |
| 127 const FilePath policy_path = storage_dir_.Append( | 114 const FilePath policy_path = storage_dir_.Append( |
| 128 FILE_PATH_LITERAL("Policy")); | 115 FILE_PATH_LITERAL("Policy")); |
| 129 cache_.reset(new DeviceManagementPolicyCache(policy_path)); | 116 cache_.reset(new DeviceManagementPolicyCache(policy_path)); |
| 130 cache_->LoadPolicyFromFile(); | 117 cache_->LoadPolicyFromFile(); |
| 131 | 118 |
| 132 // Defer initialization that requires the IOThread until after the IOThread | 119 // Defer initialization that requires the IOThread until after the IOThread |
| 133 // has been initialized. | 120 // has been initialized. |
| 134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 121 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 135 new InitializeAfterIOThreadExistsTask(AsWeakPtr())); | 122 new InitializeAfterIOThreadExistsTask(AsWeakPtr())); |
| 136 } | 123 } |
| 137 | 124 |
| 138 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() { | 125 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() { |
| 139 const FilePath token_path = storage_dir_.Append( | 126 const FilePath token_path = storage_dir_.Append( |
| 140 FILE_PATH_LITERAL("Token")); | 127 FILE_PATH_LITERAL("Token")); |
| 141 if (token_service_) { | 128 if (token_service_) { |
| 142 token_fetcher_ = | 129 token_fetcher_ = |
| 143 new DeviceTokenFetcher(GetBackend(), token_service_, token_path); | 130 new DeviceTokenFetcher(backend_.get(), token_service_, token_path); |
| 144 token_fetcher_->StartFetching(); | 131 token_fetcher_->StartFetching(); |
| 145 } | 132 } |
| 146 } | 133 } |
| 147 | 134 |
| 148 void DeviceManagementPolicyProvider::SendPolicyRequest() { | 135 void DeviceManagementPolicyProvider::SendPolicyRequest() { |
| 149 if (!policy_request_pending_) { | 136 if (!policy_request_pending_) { |
| 150 em::DevicePolicyRequest policy_request; | 137 em::DevicePolicyRequest policy_request; |
| 151 policy_request.set_policy_scope(kChromePolicyScope); | 138 policy_request.set_policy_scope(kChromePolicyScope); |
| 152 em::DevicePolicySettingRequest* setting = | 139 em::DevicePolicySettingRequest* setting = |
| 153 policy_request.add_setting_request(); | 140 policy_request.add_setting_request(); |
| 154 setting->set_key(kChromeDevicePolicySettingKey); | 141 setting->set_key(kChromeDevicePolicySettingKey); |
| 155 GetBackend()->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(), | 142 backend_->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(), |
| 156 policy_request, | 143 policy_request, this); |
| 157 this); | |
| 158 policy_request_pending_ = true; | 144 policy_request_pending_ = true; |
| 159 } | 145 } |
| 160 } | 146 } |
| 161 | 147 |
| 162 bool DeviceManagementPolicyProvider::IsPolicyStale() const { | 148 bool DeviceManagementPolicyProvider::IsPolicyStale() const { |
| 163 base::Time now(base::Time::NowFromSystemTime()); | 149 base::Time now(base::Time::NowFromSystemTime()); |
| 164 base::Time last_policy_refresh_time = | 150 base::Time last_policy_refresh_time = |
| 165 cache_->last_policy_refresh_time(); | 151 cache_->last_policy_refresh_time(); |
| 166 base::Time policy_expiration_time = | 152 base::Time policy_expiration_time = |
| 167 last_policy_refresh_time + base::TimeDelta::FromMinutes( | 153 last_policy_refresh_time + base::TimeDelta::FromMinutes( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 181 const FilePath device_management_dir = user_data_dir.Append( | 167 const FilePath device_management_dir = user_data_dir.Append( |
| 182 FILE_PATH_LITERAL("Device Management")); | 168 FILE_PATH_LITERAL("Device Management")); |
| 183 if (!file_util::DirectoryExists(device_management_dir)) { | 169 if (!file_util::DirectoryExists(device_management_dir)) { |
| 184 if (!file_util::CreateDirectory(device_management_dir)) | 170 if (!file_util::CreateDirectory(device_management_dir)) |
| 185 NOTREACHED(); | 171 NOTREACHED(); |
| 186 } | 172 } |
| 187 return device_management_dir; | 173 return device_management_dir; |
| 188 } | 174 } |
| 189 | 175 |
| 190 } // namespace policy | 176 } // namespace policy |
| OLD | NEW |