Commit c2959396 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Update Privet printing code with new base::Value APIs.

Change-Id: Ie30782130f7c28ad3b271c25bff73fd6ff4bb507
Reviewed-on: https://chromium-review.googlesource.com/804671
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521982}
parent d228126d
...@@ -390,46 +390,51 @@ void PrivetLocalPrintOperationImpl::Start() { ...@@ -390,46 +390,51 @@ void PrivetLocalPrintOperationImpl::Start() {
base::BindRepeating(&PrivetLocalPrintOperationImpl::OnPrivetInfoDone, base::BindRepeating(&PrivetLocalPrintOperationImpl::OnPrivetInfoDone,
base::Unretained(this))); base::Unretained(this)));
info_operation_->Start(); info_operation_->Start();
started_ = true; started_ = true;
} }
void PrivetLocalPrintOperationImpl::OnPrivetInfoDone( void PrivetLocalPrintOperationImpl::OnPrivetInfoDone(
const base::DictionaryValue* value) { const base::DictionaryValue* value) {
if (value && !value->HasKey(kPrivetKeyError)) { if (!value || value->HasKey(kPrivetKeyError)) {
has_extended_workflow_ = false; delegate_->OnPrivetPrintingError(this, -1);
bool has_printing = false; return;
}
const base::ListValue* api_list;
if (value->GetList(kPrivetInfoKeyAPIList, &api_list)) {
for (size_t i = 0; i < api_list->GetSize(); i++) {
std::string api;
api_list->GetString(i, &api);
if (api == kPrivetSubmitdocPath) {
has_printing = true;
} else if (api == kPrivetCreatejobPath) {
has_extended_workflow_ = true;
}
}
}
if (!has_printing) { has_extended_workflow_ = false;
delegate_->OnPrivetPrintingError(this, -1); bool has_printing = false;
return; const base::Value* api_list =
value->FindKeyOfType(kPrivetInfoKeyAPIList, base::Value::Type::LIST);
if (api_list) {
for (const auto& api : api_list->GetList()) {
if (!api.is_string())
continue;
const std::string& api_str = api.GetString();
if (!has_printing && api_str == kPrivetSubmitdocPath)
has_printing = true;
else if (!has_extended_workflow_ && api_str == kPrivetCreatejobPath)
has_extended_workflow_ = true;
if (has_printing && has_extended_workflow_)
break;
} }
}
StartInitialRequest(); if (!has_printing) {
} else {
delegate_->OnPrivetPrintingError(this, -1); delegate_->OnPrivetPrintingError(this, -1);
return;
} }
StartInitialRequest();
} }
void PrivetLocalPrintOperationImpl::StartInitialRequest() { void PrivetLocalPrintOperationImpl::StartInitialRequest() {
use_pdf_ = false;
cloud_devices::printer::ContentTypesCapability content_types; cloud_devices::printer::ContentTypesCapability content_types;
if (content_types.LoadFrom(capabilities_)) { if (content_types.LoadFrom(capabilities_)) {
use_pdf_ = content_types.Contains(kPrivetContentTypePDF) || use_pdf_ = content_types.Contains(kPrivetContentTypePDF) ||
content_types.Contains(kPrivetContentTypeAny); content_types.Contains(kPrivetContentTypeAny);
} else {
use_pdf_ = false;
} }
if (use_pdf_) { if (use_pdf_) {
......
...@@ -296,40 +296,34 @@ void PrivetURLFetcher::OnURLFetchCompleteParseData( ...@@ -296,40 +296,34 @@ void PrivetURLFetcher::OnURLFetchCompleteParseData(
base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS); base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS);
std::unique_ptr<base::Value> value = json_reader.ReadToValue(response_str); std::unique_ptr<base::Value> value = json_reader.ReadToValue(response_str);
if (!value) { if (!value || !value->is_dict()) {
delegate_->OnError(0, JSON_PARSE_ERROR); delegate_->OnError(0, JSON_PARSE_ERROR);
return; return;
} }
const base::DictionaryValue* dictionary_value = NULL; const base::Value* error_value =
value->FindKeyOfType(kPrivetKeyError, base::Value::Type::STRING);
if (!value->GetAsDictionary(&dictionary_value)) { if (error_value) {
delegate_->OnError(0, JSON_PARSE_ERROR); const std::string& error = error_value->GetString();
return;
}
std::string error;
if (dictionary_value->GetString(kPrivetKeyError, &error)) {
if (error == kPrivetErrorInvalidXPrivetToken) { if (error == kPrivetErrorInvalidXPrivetToken) {
RequestTokenRefresh(); RequestTokenRefresh();
return; return;
} else if (PrivetErrorTransient(error)) { }
if (PrivetErrorTransient(error)) {
if (!do_not_retry_on_transient_error_) { if (!do_not_retry_on_transient_error_) {
int timeout_seconds; const base::Value* timeout_value =
if (!dictionary_value->GetInteger(kPrivetKeyTimeout, value->FindKeyOfType(kPrivetKeyTimeout, base::Value::Type::INTEGER);
&timeout_seconds)) { ScheduleRetry(timeout_value ? timeout_value->GetInt()
timeout_seconds = kPrivetDefaultTimeout; : kPrivetDefaultTimeout);
}
ScheduleRetry(timeout_seconds);
return; return;
} }
} }
is_error_response = true; is_error_response = true;
} }
delegate_->OnParsedJson(response_code(), *dictionary_value, delegate_->OnParsedJson(
is_error_response); response_code(), *static_cast<const base::DictionaryValue*>(value.get()),
is_error_response);
} }
void PrivetURLFetcher::ScheduleRetry(int timeout_seconds) { void PrivetURLFetcher::ScheduleRetry(int timeout_seconds) {
......
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