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,26 +390,33 @@ void PrivetLocalPrintOperationImpl::Start() { ...@@ -390,26 +390,33 @@ 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)) {
delegate_->OnPrivetPrintingError(this, -1);
return;
}
has_extended_workflow_ = false; has_extended_workflow_ = false;
bool has_printing = false; bool has_printing = false;
const base::Value* api_list =
const base::ListValue* api_list; value->FindKeyOfType(kPrivetInfoKeyAPIList, base::Value::Type::LIST);
if (value->GetList(kPrivetInfoKeyAPIList, &api_list)) { if (api_list) {
for (size_t i = 0; i < api_list->GetSize(); i++) { for (const auto& api : api_list->GetList()) {
std::string api; if (!api.is_string())
api_list->GetString(i, &api); continue;
if (api == kPrivetSubmitdocPath) {
const std::string& api_str = api.GetString();
if (!has_printing && api_str == kPrivetSubmitdocPath)
has_printing = true; has_printing = true;
} else if (api == kPrivetCreatejobPath) { else if (!has_extended_workflow_ && api_str == kPrivetCreatejobPath)
has_extended_workflow_ = true; has_extended_workflow_ = true;
}
if (has_printing && has_extended_workflow_)
break;
} }
} }
...@@ -419,17 +426,15 @@ void PrivetLocalPrintOperationImpl::OnPrivetInfoDone( ...@@ -419,17 +426,15 @@ void PrivetLocalPrintOperationImpl::OnPrivetInfoDone(
} }
StartInitialRequest(); StartInitialRequest();
} else {
delegate_->OnPrivetPrintingError(this, -1);
}
} }
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,39 +296,33 @@ void PrivetURLFetcher::OnURLFetchCompleteParseData( ...@@ -296,39 +296,33 @@ 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 (!do_not_retry_on_transient_error_) {
int timeout_seconds;
if (!dictionary_value->GetInteger(kPrivetKeyTimeout,
&timeout_seconds)) {
timeout_seconds = kPrivetDefaultTimeout;
} }
if (PrivetErrorTransient(error)) {
ScheduleRetry(timeout_seconds); if (!do_not_retry_on_transient_error_) {
const base::Value* timeout_value =
value->FindKeyOfType(kPrivetKeyTimeout, base::Value::Type::INTEGER);
ScheduleRetry(timeout_value ? timeout_value->GetInt()
: kPrivetDefaultTimeout);
return; return;
} }
} }
is_error_response = true; is_error_response = true;
} }
delegate_->OnParsedJson(response_code(), *dictionary_value, delegate_->OnParsedJson(
response_code(), *static_cast<const base::DictionaryValue*>(value.get()),
is_error_response); is_error_response);
} }
......
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