Commit 72bce115 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Use early returns in some Privet printing code.

Also rewrite some if-else statements without if (!cond).

Change-Id: If70a32a7baa5d2e70480831646ebf46eea8b8993
Reviewed-on: https://chromium-review.googlesource.com/804833Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521808}
parent 0bf75bf8
...@@ -148,18 +148,15 @@ void PrivetRegisterOperationImpl::Start() { ...@@ -148,18 +148,15 @@ void PrivetRegisterOperationImpl::Start() {
void PrivetRegisterOperationImpl::Cancel() { void PrivetRegisterOperationImpl::Cancel() {
url_fetcher_.reset(); url_fetcher_.reset();
if (ongoing_) { if (!ongoing_)
// Owned by the message loop. return;
Cancelation* cancelation = new Cancelation(privet_client_, user_);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&PrivetRegisterOperationImpl::Cancelation::Cleanup,
base::Owned(cancelation)),
base::TimeDelta::FromSeconds(kPrivetCancelationTimeoutSeconds));
ongoing_ = false; base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
} FROM_HERE,
base::BindOnce(&PrivetRegisterOperationImpl::Cancelation::Cleanup,
base::Owned(new Cancelation(privet_client_, user_))),
base::TimeDelta::FromSeconds(kPrivetCancelationTimeoutSeconds));
ongoing_ = false;
} }
void PrivetRegisterOperationImpl::CompleteRegistration() { void PrivetRegisterOperationImpl::CompleteRegistration() {
...@@ -496,14 +493,14 @@ void PrivetLocalPrintOperationImpl::DoSubmitdoc() { ...@@ -496,14 +493,14 @@ void PrivetLocalPrintOperationImpl::DoSubmitdoc() {
url_fetcher_ = url_fetcher_ =
privet_client_->CreateURLFetcher(url, net::URLFetcher::POST, this); privet_client_->CreateURLFetcher(url, net::URLFetcher::POST, this);
if (!use_pdf_) { if (use_pdf_) {
url_fetcher_->SetUploadFilePath(kPrivetContentTypePWGRaster,
pwg_file_path_);
} else {
// TODO(noamsml): Move to file-based upload data? // TODO(noamsml): Move to file-based upload data?
std::string data_str(reinterpret_cast<const char*>(data_->front()), std::string data_str(reinterpret_cast<const char*>(data_->front()),
data_->size()); data_->size());
url_fetcher_->SetUploadData(kPrivetContentTypePDF, data_str); url_fetcher_->SetUploadData(kPrivetContentTypePDF, data_str);
} else {
url_fetcher_->SetUploadFilePath(kPrivetContentTypePWGRaster,
pwg_file_path_);
} }
url_fetcher_->Start(); url_fetcher_->Start();
......
...@@ -146,51 +146,50 @@ void PrivetURLFetcher::SetByteRange(int start, int end) { ...@@ -146,51 +146,50 @@ void PrivetURLFetcher::SetByteRange(int start, int end) {
void PrivetURLFetcher::Try() { void PrivetURLFetcher::Try() {
tries_++; tries_++;
if (tries_ <= max_retries_) { if (tries_ > max_retries_) {
DVLOG(1) << "Attempt: " << tries_; delegate_->OnError(0, UNKNOWN_ERROR);
url_fetcher_ = return;
net::URLFetcher::Create(url_, request_type_, this, traffic_annotation_); }
data_use_measurement::DataUseUserData::AttachToFetcher(
url_fetcher_.get(), data_use_measurement::DataUseUserData::CLOUD_PRINT);
// Privet requests are relevant to hosts on local network only.
url_fetcher_->SetLoadFlags(
url_fetcher_->GetLoadFlags() | net::LOAD_BYPASS_PROXY |
net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_COOKIES);
url_fetcher_->SetRequestContext(context_getter_.get());
std::string token = GetPrivetAccessToken();
if (token.empty())
token = kXPrivetEmptyToken;
url_fetcher_->AddExtraRequestHeader(
std::string(kXPrivetTokenHeaderPrefix) + token);
if (has_byte_range_) {
url_fetcher_->AddExtraRequestHeader(
MakeRangeHeader(byte_range_start_, byte_range_end_));
}
if (make_response_file_) DVLOG(1) << "Attempt: " << tries_;
url_fetcher_->SaveResponseToTemporaryFile(GetFileTaskRunner()); url_fetcher_ =
net::URLFetcher::Create(url_, request_type_, this, traffic_annotation_);
// URLFetcher requires us to set upload data for POST requests. data_use_measurement::DataUseUserData::AttachToFetcher(
if (request_type_ == net::URLFetcher::POST) { url_fetcher_.get(), data_use_measurement::DataUseUserData::CLOUD_PRINT);
if (!upload_file_path_.empty()) {
url_fetcher_->SetUploadFilePath(
upload_content_type_, upload_file_path_, 0 /*offset*/,
std::numeric_limits<uint64_t>::max() /*length*/,
GetFileTaskRunner());
} else {
url_fetcher_->SetUploadData(upload_content_type_, upload_data_);
}
}
url_fetcher_->Start(); // Privet requests are relevant to hosts on local network only.
} else { url_fetcher_->SetLoadFlags(url_fetcher_->GetLoadFlags() |
delegate_->OnError(0, UNKNOWN_ERROR); net::LOAD_BYPASS_PROXY | net::LOAD_DISABLE_CACHE |
net::LOAD_DO_NOT_SEND_COOKIES);
url_fetcher_->SetRequestContext(context_getter_.get());
std::string token = GetPrivetAccessToken();
if (token.empty())
token = kXPrivetEmptyToken;
url_fetcher_->AddExtraRequestHeader(std::string(kXPrivetTokenHeaderPrefix) +
token);
if (has_byte_range_) {
url_fetcher_->AddExtraRequestHeader(
MakeRangeHeader(byte_range_start_, byte_range_end_));
} }
if (make_response_file_)
url_fetcher_->SaveResponseToTemporaryFile(GetFileTaskRunner());
// URLFetcher requires us to set upload data for POST requests.
if (request_type_ == net::URLFetcher::POST) {
if (upload_file_path_.empty()) {
url_fetcher_->SetUploadData(upload_content_type_, upload_data_);
} else {
url_fetcher_->SetUploadFilePath(
upload_content_type_, upload_file_path_, 0 /*offset*/,
std::numeric_limits<uint64_t>::max() /*length*/, GetFileTaskRunner());
}
}
url_fetcher_->Start();
} }
void PrivetURLFetcher::Start() { void PrivetURLFetcher::Start() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment