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() {
base::BindRepeating(&PrivetLocalPrintOperationImpl::OnPrivetInfoDone,
base::Unretained(this)));
info_operation_->Start();
started_ = true;
}
void PrivetLocalPrintOperationImpl::OnPrivetInfoDone(
const base::DictionaryValue* value) {
if (value && !value->HasKey(kPrivetKeyError)) {
if (!value || value->HasKey(kPrivetKeyError)) {
delegate_->OnPrivetPrintingError(this, -1);
return;
}
has_extended_workflow_ = false;
bool has_printing = false;
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) {
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 (api == kPrivetCreatejobPath) {
else if (!has_extended_workflow_ && api_str == kPrivetCreatejobPath)
has_extended_workflow_ = true;
}
if (has_printing && has_extended_workflow_)
break;
}
}
......@@ -419,17 +426,15 @@ void PrivetLocalPrintOperationImpl::OnPrivetInfoDone(
}
StartInitialRequest();
} else {
delegate_->OnPrivetPrintingError(this, -1);
}
}
void PrivetLocalPrintOperationImpl::StartInitialRequest() {
use_pdf_ = false;
cloud_devices::printer::ContentTypesCapability content_types;
if (content_types.LoadFrom(capabilities_)) {
use_pdf_ = content_types.Contains(kPrivetContentTypePDF) ||
content_types.Contains(kPrivetContentTypeAny);
} else {
use_pdf_ = false;
}
if (use_pdf_) {
......
......@@ -296,39 +296,33 @@ void PrivetURLFetcher::OnURLFetchCompleteParseData(
base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS);
std::unique_ptr<base::Value> value = json_reader.ReadToValue(response_str);
if (!value) {
if (!value || !value->is_dict()) {
delegate_->OnError(0, JSON_PARSE_ERROR);
return;
}
const base::DictionaryValue* dictionary_value = NULL;
if (!value->GetAsDictionary(&dictionary_value)) {
delegate_->OnError(0, JSON_PARSE_ERROR);
return;
}
std::string error;
if (dictionary_value->GetString(kPrivetKeyError, &error)) {
const base::Value* error_value =
value->FindKeyOfType(kPrivetKeyError, base::Value::Type::STRING);
if (error_value) {
const std::string& error = error_value->GetString();
if (error == kPrivetErrorInvalidXPrivetToken) {
RequestTokenRefresh();
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;
}
ScheduleRetry(timeout_seconds);
if (PrivetErrorTransient(error)) {
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;
}
}
is_error_response = true;
}
delegate_->OnParsedJson(response_code(), *dictionary_value,
delegate_->OnParsedJson(
response_code(), *static_cast<const base::DictionaryValue*>(value.get()),
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