Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: chrome/browser/chromeos/policy/upload_job_impl.cc

Issue 2427443004: Replace CHROMEOS_SYSLOG logs with SYSLOG logs so remote command related logs will get uploaded to t… (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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/upload_job_impl.h" 5 #include "chrome/browser/chromeos/policy/upload_job_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/chromeos/logging.h"
12 #include "base/location.h" 11 #include "base/location.h"
13 #include "base/macros.h" 12 #include "base/macros.h"
14 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/syslog_logging.h"
15 #include "google_apis/gaia/gaia_constants.h" 15 #include "google_apis/gaia/gaia_constants.h"
16 #include "google_apis/gaia/google_service_auth_error.h" 16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "net/base/mime_util.h" 17 #include "net/base/mime_util.h"
18 #include "net/http/http_status_code.h" 18 #include "net/http/http_status_code.h"
19 #include "net/url_request/url_request_status.h" 19 #include "net/url_request/url_request_status.h"
20 20
21 namespace policy { 21 namespace policy {
22 22
23 namespace { 23 namespace {
24 24
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 } 173 }
174 174
175 void UploadJobImpl::Start() { 175 void UploadJobImpl::Start() {
176 DCHECK(thread_checker_.CalledOnValidThread()); 176 DCHECK(thread_checker_.CalledOnValidThread());
177 // Cannot start an upload on a busy or failed instance. 177 // Cannot start an upload on a busy or failed instance.
178 DCHECK_EQ(IDLE, state_); 178 DCHECK_EQ(IDLE, state_);
179 if (state_ != IDLE) 179 if (state_ != IDLE)
180 return; 180 return;
181 DCHECK_EQ(0, retry_); 181 DCHECK_EQ(0, retry_);
182 182
183 CHROMEOS_SYSLOG(WARNING) << "Upload job started"; 183 SYSLOG(INFO) << "Upload job started";
184 RequestAccessToken(); 184 RequestAccessToken();
185 } 185 }
186 186
187 // static 187 // static
188 void UploadJobImpl::SetRetryDelayForTesting(long retry_delay_ms) { 188 void UploadJobImpl::SetRetryDelayForTesting(long retry_delay_ms) {
189 CHECK_GE(retry_delay_ms, 0); 189 CHECK_GE(retry_delay_ms, 0);
190 g_retry_delay_ms = retry_delay_ms; 190 g_retry_delay_ms = retry_delay_ms;
191 } 191 }
192 192
193 void UploadJobImpl::RequestAccessToken() { 193 void UploadJobImpl::RequestAccessToken() {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 for (const auto& entry : data_segment->GetHeaderEntries()) { 250 for (const auto& entry : data_segment->GetHeaderEntries()) {
251 post_data_->append(entry.first + ": " + entry.second + "\r\n"); 251 post_data_->append(entry.first + ": " + entry.second + "\r\n");
252 } 252 }
253 std::unique_ptr<std::string> data = data_segment->GetData(); 253 std::unique_ptr<std::string> data = data_segment->GetData();
254 post_data_->append("\r\n" + *data + "\r\n"); 254 post_data_->append("\r\n" + *data + "\r\n");
255 } 255 }
256 post_data_->append("--" + *mime_boundary_.get() + "--\r\n"); 256 post_data_->append("--" + *mime_boundary_.get() + "--\r\n");
257 257
258 // Issues a warning if our buffer size estimate was too small. 258 // Issues a warning if our buffer size estimate was too small.
259 if (post_data_->size() > size) { 259 if (post_data_->size() > size) {
260 CHROMEOS_SYSLOG(WARNING) 260 SYSLOG(INFO)
261 << "Reallocation needed in POST data buffer. Expected maximum size " 261 << "Reallocation needed in POST data buffer. Expected maximum size "
262 << size << " bytes, actual size " << post_data_->size() << " bytes."; 262 << size << " bytes, actual size " << post_data_->size() << " bytes.";
263 } 263 }
264 264
265 // Discard the data segments as they are not needed anymore from here on. 265 // Discard the data segments as they are not needed anymore from here on.
266 data_segments_.clear(); 266 data_segments_.clear();
267 267
268 return true; 268 return true;
269 } 269 }
270 270
(...skipping 11 matching lines...) Expand all
282 upload_fetcher_->SetUploadData(content_type, *post_data_); 282 upload_fetcher_->SetUploadData(content_type, *post_data_);
283 upload_fetcher_->AddExtraRequestHeader( 283 upload_fetcher_->AddExtraRequestHeader(
284 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); 284 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()));
285 upload_fetcher_->Start(); 285 upload_fetcher_->Start();
286 } 286 }
287 287
288 void UploadJobImpl::StartUpload() { 288 void UploadJobImpl::StartUpload() {
289 DCHECK(thread_checker_.CalledOnValidThread()); 289 DCHECK(thread_checker_.CalledOnValidThread());
290 290
291 if (!SetUpMultipart()) { 291 if (!SetUpMultipart()) {
292 CHROMEOS_SYSLOG(ERROR) << "Multipart message assembly failed."; 292 SYSLOG(ERROR) << "Multipart message assembly failed.";
293 state_ = ERROR; 293 state_ = ERROR;
294 return; 294 return;
295 } 295 }
296 CreateAndStartURLFetcher(access_token_); 296 CreateAndStartURLFetcher(access_token_);
297 state_ = UPLOADING; 297 state_ = UPLOADING;
298 } 298 }
299 299
300 void UploadJobImpl::OnGetTokenSuccess( 300 void UploadJobImpl::OnGetTokenSuccess(
301 const OAuth2TokenService::Request* request, 301 const OAuth2TokenService::Request* request,
302 const std::string& access_token, 302 const std::string& access_token,
303 const base::Time& expiration_time) { 303 const base::Time& expiration_time) {
304 DCHECK_EQ(ACQUIRING_TOKEN, state_); 304 DCHECK_EQ(ACQUIRING_TOKEN, state_);
305 DCHECK_EQ(access_token_request_.get(), request); 305 DCHECK_EQ(access_token_request_.get(), request);
306 access_token_request_.reset(); 306 access_token_request_.reset();
307 307
308 // Also cache the token locally, so that we can revoke it later if necessary. 308 // Also cache the token locally, so that we can revoke it later if necessary.
309 access_token_ = access_token; 309 access_token_ = access_token;
310 StartUpload(); 310 StartUpload();
311 } 311 }
312 312
313 void UploadJobImpl::OnGetTokenFailure( 313 void UploadJobImpl::OnGetTokenFailure(
314 const OAuth2TokenService::Request* request, 314 const OAuth2TokenService::Request* request,
315 const GoogleServiceAuthError& error) { 315 const GoogleServiceAuthError& error) {
316 DCHECK_EQ(ACQUIRING_TOKEN, state_); 316 DCHECK_EQ(ACQUIRING_TOKEN, state_);
317 DCHECK_EQ(access_token_request_.get(), request); 317 DCHECK_EQ(access_token_request_.get(), request);
318 access_token_request_.reset(); 318 access_token_request_.reset();
319 CHROMEOS_SYSLOG(ERROR) << "Token request failed: " << error.ToString(); 319 SYSLOG(ERROR) << "Token request failed: " << error.ToString();
320 HandleError(AUTHENTICATION_ERROR); 320 HandleError(AUTHENTICATION_ERROR);
321 } 321 }
322 322
323 void UploadJobImpl::HandleError(ErrorCode error_code) { 323 void UploadJobImpl::HandleError(ErrorCode error_code) {
324 retry_++; 324 retry_++;
325 upload_fetcher_.reset(); 325 upload_fetcher_.reset();
326 326
327 CHROMEOS_SYSLOG(ERROR) << "Upload failed, error code: " << error_code; 327 SYSLOG(ERROR) << "Upload failed, error code: " << error_code;
328 328
329 if (retry_ >= kMaxAttempts) { 329 if (retry_ >= kMaxAttempts) {
330 // Maximum number of attempts reached, failure. 330 // Maximum number of attempts reached, failure.
331 CHROMEOS_SYSLOG(ERROR) << "Maximum number of attempts reached."; 331 SYSLOG(ERROR) << "Maximum number of attempts reached.";
332 access_token_.clear(); 332 access_token_.clear();
333 post_data_.reset(); 333 post_data_.reset();
334 state_ = ERROR; 334 state_ = ERROR;
335 delegate_->OnFailure(error_code); 335 delegate_->OnFailure(error_code);
336 } else { 336 } else {
337 if (error_code == AUTHENTICATION_ERROR) { 337 if (error_code == AUTHENTICATION_ERROR) {
338 CHROMEOS_SYSLOG(ERROR) << "Retrying upload with a new token."; 338 SYSLOG(ERROR) << "Retrying upload with a new token.";
339 // Request new token and retry. 339 // Request new token and retry.
340 OAuth2TokenService::ScopeSet scope_set; 340 OAuth2TokenService::ScopeSet scope_set;
341 scope_set.insert(GaiaConstants::kDeviceManagementServiceOAuth); 341 scope_set.insert(GaiaConstants::kDeviceManagementServiceOAuth);
342 token_service_->InvalidateAccessToken(account_id_, scope_set, 342 token_service_->InvalidateAccessToken(account_id_, scope_set,
343 access_token_); 343 access_token_);
344 access_token_.clear(); 344 access_token_.clear();
345 task_runner_->PostDelayedTask( 345 task_runner_->PostDelayedTask(
346 FROM_HERE, base::Bind(&UploadJobImpl::RequestAccessToken, 346 FROM_HERE, base::Bind(&UploadJobImpl::RequestAccessToken,
347 weak_factory_.GetWeakPtr()), 347 weak_factory_.GetWeakPtr()),
348 base::TimeDelta::FromMilliseconds(g_retry_delay_ms)); 348 base::TimeDelta::FromMilliseconds(g_retry_delay_ms));
349 } else { 349 } else {
350 // Retry without a new token. 350 // Retry without a new token.
351 state_ = ACQUIRING_TOKEN; 351 state_ = ACQUIRING_TOKEN;
352 CHROMEOS_SYSLOG(WARNING) << "Retrying upload with the same token."; 352 SYSLOG(WARNING) << "Retrying upload with the same token.";
353 task_runner_->PostDelayedTask( 353 task_runner_->PostDelayedTask(
354 FROM_HERE, 354 FROM_HERE,
355 base::Bind(&UploadJobImpl::StartUpload, weak_factory_.GetWeakPtr()), 355 base::Bind(&UploadJobImpl::StartUpload, weak_factory_.GetWeakPtr()),
356 base::TimeDelta::FromMilliseconds(g_retry_delay_ms)); 356 base::TimeDelta::FromMilliseconds(g_retry_delay_ms));
357 } 357 }
358 } 358 }
359 } 359 }
360 360
361 void UploadJobImpl::OnURLFetchComplete(const net::URLFetcher* source) { 361 void UploadJobImpl::OnURLFetchComplete(const net::URLFetcher* source) {
362 DCHECK_EQ(upload_fetcher_.get(), source); 362 DCHECK_EQ(upload_fetcher_.get(), source);
363 DCHECK_EQ(UPLOADING, state_); 363 DCHECK_EQ(UPLOADING, state_);
364 const net::URLRequestStatus& status = source->GetStatus(); 364 const net::URLRequestStatus& status = source->GetStatus();
365 if (!status.is_success()) { 365 if (!status.is_success()) {
366 CHROMEOS_SYSLOG(ERROR) << "URLRequestStatus error " << status.error(); 366 SYSLOG(ERROR) << "URLRequestStatus error " << status.error();
367 HandleError(NETWORK_ERROR); 367 HandleError(NETWORK_ERROR);
368 } else { 368 } else {
369 const int response_code = source->GetResponseCode(); 369 const int response_code = source->GetResponseCode();
370 if (response_code == net::HTTP_OK) { 370 if (response_code == net::HTTP_OK) {
371 // Successful upload 371 // Successful upload
372 upload_fetcher_.reset(); 372 upload_fetcher_.reset();
373 access_token_.clear(); 373 access_token_.clear();
374 post_data_.reset(); 374 post_data_.reset();
375 state_ = SUCCESS; 375 state_ = SUCCESS;
376 delegate_->OnSuccess(); 376 delegate_->OnSuccess();
377 } else if (response_code == net::HTTP_UNAUTHORIZED) { 377 } else if (response_code == net::HTTP_UNAUTHORIZED) {
378 CHROMEOS_SYSLOG(ERROR) << "Unauthorized request."; 378 SYSLOG(ERROR) << "Unauthorized request.";
379 HandleError(AUTHENTICATION_ERROR); 379 HandleError(AUTHENTICATION_ERROR);
380 } else { 380 } else {
381 CHROMEOS_SYSLOG(ERROR) << "POST request failed with HTTP status code " 381 SYSLOG(ERROR) << "POST request failed with HTTP status code "
382 << response_code << "."; 382 << response_code << ".";
383 HandleError(SERVER_ERROR); 383 HandleError(SERVER_ERROR);
384 } 384 }
385 } 385 }
386 } 386 }
387 387
388 } // namespace policy 388 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/system_log_uploader.cc ('k') | chrome/browser/policy/cloud/remote_commands_invalidator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698