| 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" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 : ConfigurationPolicyProvider(policy_list), | 60 : ConfigurationPolicyProvider(policy_list), |
| 61 backend_(backend), | 61 backend_(backend), |
| 62 storage_dir_(GetOrCreateDeviceManagementDir(storage_dir)), | 62 storage_dir_(GetOrCreateDeviceManagementDir(storage_dir)), |
| 63 policy_request_pending_(false) { | 63 policy_request_pending_(false) { |
| 64 Initialize(); | 64 Initialize(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider( | 67 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider( |
| 68 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list) | 68 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list) |
| 69 : ConfigurationPolicyProvider(policy_list), | 69 : ConfigurationPolicyProvider(policy_list), |
| 70 backend_(DeviceManagementBackendImpl::Get()), |
| 70 policy_request_pending_(false) { | 71 policy_request_pending_(false) { |
| 71 FilePath user_dir; | 72 FilePath user_dir; |
| 72 if (!PathService::Get(chrome::DIR_USER_DATA, &user_dir)) | 73 if (!PathService::Get(chrome::DIR_USER_DATA, &user_dir)) |
| 73 NOTREACHED(); | 74 NOTREACHED(); |
| 74 storage_dir_ = GetOrCreateDeviceManagementDir(user_dir); | 75 storage_dir_ = GetOrCreateDeviceManagementDir(user_dir); |
| 75 Initialize(); | 76 Initialize(); |
| 76 } | 77 } |
| 77 | 78 |
| 78 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {} | 79 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {} |
| 79 | 80 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 107 } | 108 } |
| 108 | 109 |
| 109 void DeviceManagementPolicyProvider::OnError( | 110 void DeviceManagementPolicyProvider::OnError( |
| 110 DeviceManagementBackend::ErrorCode code) { | 111 DeviceManagementBackend::ErrorCode code) { |
| 111 LOG(WARNING) << "could not provide policy from the device manager (error = " | 112 LOG(WARNING) << "could not provide policy from the device manager (error = " |
| 112 << code << ")"; | 113 << code << ")"; |
| 113 policy_request_pending_ = false; | 114 policy_request_pending_ = false; |
| 114 // TODO(danno): do something sensible in the error case, perhaps retry later? | 115 // TODO(danno): do something sensible in the error case, perhaps retry later? |
| 115 } | 116 } |
| 116 | 117 |
| 117 DeviceManagementBackend* DeviceManagementPolicyProvider::GetBackend() { | |
| 118 if (!backend_.get()) { | |
| 119 backend_.reset(new DeviceManagementBackendImpl( | |
| 120 GetDeviceManagementURL())); | |
| 121 } | |
| 122 return backend_.get(); | |
| 123 } | |
| 124 | |
| 125 void DeviceManagementPolicyProvider::Initialize() { | 118 void DeviceManagementPolicyProvider::Initialize() { |
| 126 registrar_.Add(this, | 119 registrar_.Add(this, |
| 127 NotificationType::DEVICE_TOKEN_AVAILABLE, | 120 NotificationType::DEVICE_TOKEN_AVAILABLE, |
| 128 NotificationService::AllSources()); | 121 NotificationService::AllSources()); |
| 129 | 122 |
| 130 const FilePath policy_path = storage_dir_.Append( | 123 const FilePath policy_path = storage_dir_.Append( |
| 131 FILE_PATH_LITERAL("Policy")); | 124 FILE_PATH_LITERAL("Policy")); |
| 132 cache_.reset(new DeviceManagementPolicyCache(policy_path)); | 125 cache_.reset(new DeviceManagementPolicyCache(policy_path)); |
| 133 cache_->LoadPolicyFromFile(); | 126 cache_->LoadPolicyFromFile(); |
| 134 | 127 |
| 135 // Defer initialization that requires the IOThread until after the IOThread | 128 // Defer initialization that requires the IOThread until after the IOThread |
| 136 // has been initialized. | 129 // has been initialized. |
| 137 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 130 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 138 new InitializeAfterIOThreadExistsTask(AsWeakPtr())); | 131 new InitializeAfterIOThreadExistsTask(AsWeakPtr())); |
| 139 } | 132 } |
| 140 | 133 |
| 141 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() { | 134 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() { |
| 142 const FilePath token_path = storage_dir_.Append( | 135 const FilePath token_path = storage_dir_.Append( |
| 143 FILE_PATH_LITERAL("Token")); | 136 FILE_PATH_LITERAL("Token")); |
| 144 token_fetcher_ = new DeviceTokenFetcher(GetBackend(), token_path); | 137 token_fetcher_ = new DeviceTokenFetcher(backend_.get(), token_path); |
| 145 token_fetcher_->StartFetching(); | 138 token_fetcher_->StartFetching(); |
| 146 } | 139 } |
| 147 | 140 |
| 148 void DeviceManagementPolicyProvider::SendPolicyRequest() { | 141 void DeviceManagementPolicyProvider::SendPolicyRequest() { |
| 149 if (!policy_request_pending_) { | 142 if (!policy_request_pending_) { |
| 150 em::DevicePolicyRequest policy_request; | 143 em::DevicePolicyRequest policy_request; |
| 151 policy_request.set_policy_scope(kChromePolicyScope); | 144 policy_request.set_policy_scope(kChromePolicyScope); |
| 152 em::DevicePolicySettingRequest* setting = | 145 em::DevicePolicySettingRequest* setting = |
| 153 policy_request.add_setting_request(); | 146 policy_request.add_setting_request(); |
| 154 setting->set_key(kChromeDevicePolicySettingKey); | 147 setting->set_key(kChromeDevicePolicySettingKey); |
| 155 GetBackend()->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(), | 148 backend_->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(), |
| 156 policy_request, | 149 policy_request, this); |
| 157 this); | |
| 158 policy_request_pending_ = true; | 150 policy_request_pending_ = true; |
| 159 } | 151 } |
| 160 } | 152 } |
| 161 | 153 |
| 162 bool DeviceManagementPolicyProvider::IsPolicyStale() const { | 154 bool DeviceManagementPolicyProvider::IsPolicyStale() const { |
| 163 base::Time now(base::Time::NowFromSystemTime()); | 155 base::Time now(base::Time::NowFromSystemTime()); |
| 164 base::Time last_policy_refresh_time = | 156 base::Time last_policy_refresh_time = |
| 165 cache_->last_policy_refresh_time(); | 157 cache_->last_policy_refresh_time(); |
| 166 base::Time policy_expiration_time = | 158 base::Time policy_expiration_time = |
| 167 last_policy_refresh_time + base::TimeDelta::FromMinutes( | 159 last_policy_refresh_time + base::TimeDelta::FromMinutes( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 181 const FilePath device_management_dir = user_data_dir.Append( | 173 const FilePath device_management_dir = user_data_dir.Append( |
| 182 FILE_PATH_LITERAL("Device Management")); | 174 FILE_PATH_LITERAL("Device Management")); |
| 183 if (!file_util::DirectoryExists(device_management_dir)) { | 175 if (!file_util::DirectoryExists(device_management_dir)) { |
| 184 if (!file_util::CreateDirectory(device_management_dir)) | 176 if (!file_util::CreateDirectory(device_management_dir)) |
| 185 NOTREACHED(); | 177 NOTREACHED(); |
| 186 } | 178 } |
| 187 return device_management_dir; | 179 return device_management_dir; |
| 188 } | 180 } |
| 189 | 181 |
| 190 } // namespace policy | 182 } // namespace policy |
| OLD | NEW |