| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/chromeos/policy/remote_commands/device_command_screensh
ot_job.h" | 5 #include "chrome/browser/chromeos/policy/remote_commands/device_command_screensh
ot_job.h" |
| 6 | 6 |
| 7 #include <fstream> | 7 #include <fstream> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 DeviceCommandScreenshotJob::Payload::Payload(ResultCode result_code) { | 85 DeviceCommandScreenshotJob::Payload::Payload(ResultCode result_code) { |
| 86 base::DictionaryValue root_dict; | 86 base::DictionaryValue root_dict; |
| 87 if (result_code != SUCCESS) | 87 if (result_code != SUCCESS) |
| 88 root_dict.SetInteger(kResultFieldName, result_code); | 88 root_dict.SetInteger(kResultFieldName, result_code); |
| 89 base::JSONWriter::Write(root_dict, &payload_); | 89 base::JSONWriter::Write(root_dict, &payload_); |
| 90 } | 90 } |
| 91 | 91 |
| 92 std::unique_ptr<std::string> DeviceCommandScreenshotJob::Payload::Serialize() { | 92 std::unique_ptr<std::string> DeviceCommandScreenshotJob::Payload::Serialize() { |
| 93 return base::WrapUnique(new std::string(payload_)); | 93 return base::MakeUnique<std::string>(payload_); |
| 94 } | 94 } |
| 95 | 95 |
| 96 DeviceCommandScreenshotJob::DeviceCommandScreenshotJob( | 96 DeviceCommandScreenshotJob::DeviceCommandScreenshotJob( |
| 97 std::unique_ptr<Delegate> screenshot_delegate) | 97 std::unique_ptr<Delegate> screenshot_delegate) |
| 98 : num_pending_screenshots_(0), | 98 : num_pending_screenshots_(0), |
| 99 screenshot_delegate_(std::move(screenshot_delegate)), | 99 screenshot_delegate_(std::move(screenshot_delegate)), |
| 100 weak_ptr_factory_(this) { | 100 weak_ptr_factory_(this) { |
| 101 DCHECK(screenshot_delegate_); | 101 DCHECK(screenshot_delegate_); |
| 102 } | 102 } |
| 103 | 103 |
| 104 DeviceCommandScreenshotJob::~DeviceCommandScreenshotJob() { | 104 DeviceCommandScreenshotJob::~DeviceCommandScreenshotJob() { |
| 105 } | 105 } |
| 106 | 106 |
| 107 enterprise_management::RemoteCommand_Type DeviceCommandScreenshotJob::GetType() | 107 enterprise_management::RemoteCommand_Type DeviceCommandScreenshotJob::GetType() |
| 108 const { | 108 const { |
| 109 return enterprise_management::RemoteCommand_Type_DEVICE_SCREENSHOT; | 109 return enterprise_management::RemoteCommand_Type_DEVICE_SCREENSHOT; |
| 110 } | 110 } |
| 111 | 111 |
| 112 void DeviceCommandScreenshotJob::OnSuccess() { | 112 void DeviceCommandScreenshotJob::OnSuccess() { |
| 113 CHROMEOS_SYSLOG(WARNING) << "Upload successful."; | 113 CHROMEOS_SYSLOG(WARNING) << "Upload successful."; |
| 114 base::ThreadTaskRunnerHandle::Get()->PostTask( | 114 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 115 FROM_HERE, | 115 FROM_HERE, base::Bind(succeeded_callback_, |
| 116 base::Bind(succeeded_callback_, | 116 base::Passed(base::MakeUnique<Payload>(SUCCESS)))); |
| 117 base::Passed(base::WrapUnique(new Payload(SUCCESS))))); | |
| 118 } | 117 } |
| 119 | 118 |
| 120 void DeviceCommandScreenshotJob::OnFailure(UploadJob::ErrorCode error_code) { | 119 void DeviceCommandScreenshotJob::OnFailure(UploadJob::ErrorCode error_code) { |
| 121 CHROMEOS_SYSLOG(ERROR) << "Upload failure: " << error_code; | 120 CHROMEOS_SYSLOG(ERROR) << "Upload failure: " << error_code; |
| 122 ResultCode result_code = FAILURE_CLIENT; | 121 ResultCode result_code = FAILURE_CLIENT; |
| 123 switch (error_code) { | 122 switch (error_code) { |
| 124 case UploadJob::AUTHENTICATION_ERROR: | 123 case UploadJob::AUTHENTICATION_ERROR: |
| 125 result_code = FAILURE_AUTHENTICATION; | 124 result_code = FAILURE_AUTHENTICATION; |
| 126 break; | 125 break; |
| 127 case UploadJob::NETWORK_ERROR: | 126 case UploadJob::NETWORK_ERROR: |
| 128 case UploadJob::SERVER_ERROR: | 127 case UploadJob::SERVER_ERROR: |
| 129 result_code = FAILURE_SERVER; | 128 result_code = FAILURE_SERVER; |
| 130 break; | 129 break; |
| 131 } | 130 } |
| 132 base::ThreadTaskRunnerHandle::Get()->PostTask( | 131 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 133 FROM_HERE, | 132 FROM_HERE, |
| 134 base::Bind(failed_callback_, | 133 base::Bind(failed_callback_, |
| 135 base::Passed(base::WrapUnique(new Payload(result_code))))); | 134 base::Passed(base::MakeUnique<Payload>(result_code)))); |
| 136 } | 135 } |
| 137 | 136 |
| 138 bool DeviceCommandScreenshotJob::IsExpired(base::TimeTicks now) { | 137 bool DeviceCommandScreenshotJob::IsExpired(base::TimeTicks now) { |
| 139 return now > issued_time() + base::TimeDelta::FromMinutes( | 138 return now > issued_time() + base::TimeDelta::FromMinutes( |
| 140 kCommandExpirationTimeInMinutes); | 139 kCommandExpirationTimeInMinutes); |
| 141 } | 140 } |
| 142 | 141 |
| 143 bool DeviceCommandScreenshotJob::ParseCommandPayload( | 142 bool DeviceCommandScreenshotJob::ParseCommandPayload( |
| 144 const std::string& command_payload) { | 143 const std::string& command_payload) { |
| 145 std::unique_ptr<base::Value> root( | 144 std::unique_ptr<base::Value> root( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 169 | 168 |
| 170 void DeviceCommandScreenshotJob::StartScreenshotUpload() { | 169 void DeviceCommandScreenshotJob::StartScreenshotUpload() { |
| 171 for (const auto& screenshot_entry : screenshots_) { | 170 for (const auto& screenshot_entry : screenshots_) { |
| 172 std::map<std::string, std::string> header_fields; | 171 std::map<std::string, std::string> header_fields; |
| 173 header_fields.insert( | 172 header_fields.insert( |
| 174 std::make_pair(kFileTypeHeaderName, kFileTypeScreenshotFile)); | 173 std::make_pair(kFileTypeHeaderName, kFileTypeScreenshotFile)); |
| 175 header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType, | 174 header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType, |
| 176 kContentTypeImagePng)); | 175 kContentTypeImagePng)); |
| 177 header_fields.insert(std::make_pair(kCommandIdHeaderName, | 176 header_fields.insert(std::make_pair(kCommandIdHeaderName, |
| 178 base::Uint64ToString(unique_id()))); | 177 base::Uint64ToString(unique_id()))); |
| 179 std::unique_ptr<std::string> data = base::WrapUnique( | 178 std::unique_ptr<std::string> data = base::MakeUnique<std::string>( |
| 180 new std::string((const char*)screenshot_entry.second->front(), | 179 (const char*)screenshot_entry.second->front(), |
| 181 screenshot_entry.second->size())); | 180 screenshot_entry.second->size()); |
| 182 upload_job_->AddDataSegment( | 181 upload_job_->AddDataSegment( |
| 183 base::StringPrintf(kNameFieldTemplate, screenshot_entry.first), | 182 base::StringPrintf(kNameFieldTemplate, screenshot_entry.first), |
| 184 base::StringPrintf(kFilenameFieldTemplate, screenshot_entry.first), | 183 base::StringPrintf(kFilenameFieldTemplate, screenshot_entry.first), |
| 185 header_fields, std::move(data)); | 184 header_fields, std::move(data)); |
| 186 } | 185 } |
| 187 upload_job_->Start(); | 186 upload_job_->Start(); |
| 188 } | 187 } |
| 189 | 188 |
| 190 void DeviceCommandScreenshotJob::RunImpl( | 189 void DeviceCommandScreenshotJob::RunImpl( |
| 191 const CallbackWithResult& succeeded_callback, | 190 const CallbackWithResult& succeeded_callback, |
| 192 const CallbackWithResult& failed_callback) { | 191 const CallbackWithResult& failed_callback) { |
| 193 succeeded_callback_ = succeeded_callback; | 192 succeeded_callback_ = succeeded_callback; |
| 194 failed_callback_ = failed_callback; | 193 failed_callback_ = failed_callback; |
| 195 | 194 |
| 196 CHROMEOS_SYSLOG(WARNING) << "Executing screenshot command."; | 195 CHROMEOS_SYSLOG(WARNING) << "Executing screenshot command."; |
| 197 | 196 |
| 198 // Fail if the delegate says screenshots are not allowed in this session. | 197 // Fail if the delegate says screenshots are not allowed in this session. |
| 199 if (!screenshot_delegate_->IsScreenshotAllowed()) { | 198 if (!screenshot_delegate_->IsScreenshotAllowed()) { |
| 200 CHROMEOS_SYSLOG(ERROR) << "Screenshots are not allowed."; | 199 CHROMEOS_SYSLOG(ERROR) << "Screenshots are not allowed."; |
| 201 base::ThreadTaskRunnerHandle::Get()->PostTask( | 200 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 202 FROM_HERE, | 201 FROM_HERE, |
| 203 base::Bind(failed_callback_, base::Passed(base::WrapUnique( | 202 base::Bind(failed_callback_, base::Passed(base::MakeUnique<Payload>( |
| 204 new Payload(FAILURE_USER_INPUT))))); | 203 FAILURE_USER_INPUT)))); |
| 205 } | 204 } |
| 206 | 205 |
| 207 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); | 206 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); |
| 208 | 207 |
| 209 // Immediately fail if the upload url is invalid. | 208 // Immediately fail if the upload url is invalid. |
| 210 if (!upload_url_.is_valid()) { | 209 if (!upload_url_.is_valid()) { |
| 211 CHROMEOS_SYSLOG(ERROR) << upload_url_ << " is not a valid URL."; | 210 CHROMEOS_SYSLOG(ERROR) << upload_url_ << " is not a valid URL."; |
| 212 base::ThreadTaskRunnerHandle::Get()->PostTask( | 211 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 213 FROM_HERE, | 212 FROM_HERE, |
| 214 base::Bind(failed_callback_, base::Passed(base::WrapUnique( | 213 base::Bind(failed_callback_, base::Passed(base::MakeUnique<Payload>( |
| 215 new Payload(FAILURE_INVALID_URL))))); | 214 FAILURE_INVALID_URL)))); |
| 216 return; | 215 return; |
| 217 } | 216 } |
| 218 | 217 |
| 219 // Immediately fail if there are no attached screens. | 218 // Immediately fail if there are no attached screens. |
| 220 if (root_windows.size() == 0) { | 219 if (root_windows.size() == 0) { |
| 221 CHROMEOS_SYSLOG(ERROR) << "No attached screens."; | 220 CHROMEOS_SYSLOG(ERROR) << "No attached screens."; |
| 222 base::ThreadTaskRunnerHandle::Get()->PostTask( | 221 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 223 FROM_HERE, | 222 FROM_HERE, |
| 224 base::Bind(failed_callback_, base::Passed(base::WrapUnique(new Payload( | 223 base::Bind(failed_callback_, base::Passed(base::MakeUnique<Payload>( |
| 225 FAILURE_SCREENSHOT_ACQUISITION))))); | 224 FAILURE_SCREENSHOT_ACQUISITION)))); |
| 226 return; | 225 return; |
| 227 } | 226 } |
| 228 | 227 |
| 229 upload_job_ = screenshot_delegate_->CreateUploadJob(upload_url_, this); | 228 upload_job_ = screenshot_delegate_->CreateUploadJob(upload_url_, this); |
| 230 DCHECK(upload_job_); | 229 DCHECK(upload_job_); |
| 231 | 230 |
| 232 // Post tasks to the sequenced worker pool for taking screenshots on each | 231 // Post tasks to the sequenced worker pool for taking screenshots on each |
| 233 // attached screen. | 232 // attached screen. |
| 234 num_pending_screenshots_ = root_windows.size(); | 233 num_pending_screenshots_ = root_windows.size(); |
| 235 for (size_t screen = 0; screen < root_windows.size(); ++screen) { | 234 for (size_t screen = 0; screen < root_windows.size(); ++screen) { |
| 236 aura::Window* root_window = root_windows[screen]; | 235 aura::Window* root_window = root_windows[screen]; |
| 237 gfx::Rect rect = root_window->bounds(); | 236 gfx::Rect rect = root_window->bounds(); |
| 238 screenshot_delegate_->TakeSnapshot( | 237 screenshot_delegate_->TakeSnapshot( |
| 239 root_window, rect, | 238 root_window, rect, |
| 240 base::Bind(&RunStoreScreenshotOnTaskRunner, | 239 base::Bind(&RunStoreScreenshotOnTaskRunner, |
| 241 base::Bind(&DeviceCommandScreenshotJob::StoreScreenshot, | 240 base::Bind(&DeviceCommandScreenshotJob::StoreScreenshot, |
| 242 weak_ptr_factory_.GetWeakPtr(), screen), | 241 weak_ptr_factory_.GetWeakPtr(), screen), |
| 243 base::ThreadTaskRunnerHandle::Get())); | 242 base::ThreadTaskRunnerHandle::Get())); |
| 244 } | 243 } |
| 245 } | 244 } |
| 246 | 245 |
| 247 void DeviceCommandScreenshotJob::TerminateImpl() { | 246 void DeviceCommandScreenshotJob::TerminateImpl() { |
| 248 weak_ptr_factory_.InvalidateWeakPtrs(); | 247 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 249 } | 248 } |
| 250 | 249 |
| 251 } // namespace policy | 250 } // namespace policy |
| OLD | NEW |