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

Unified Diff: google_apis/drive/base_requests.cc

Issue 442193002: Parse Drive API responses all at once in the blocking pool. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: google_apis/drive/base_requests.cc
diff --git a/google_apis/drive/base_requests.cc b/google_apis/drive/base_requests.cc
index 0c1a238057efe298b85bb75de878acee98c3b2bc..2db5c4a3f8c167a121f8d259470f2adfa96df23d 100644
--- a/google_apis/drive/base_requests.cc
+++ b/google_apis/drive/base_requests.cc
@@ -43,29 +43,18 @@ const char kUploadResponseLocation[] = "location";
const char kUploadContentRange[] = "Content-Range: bytes ";
const char kUploadResponseRange[] = "range";
-// Parse JSON string to base::Value object.
-scoped_ptr<base::Value> ParseJsonInternal(const std::string& json) {
- int error_code = -1;
- std::string error_message;
- scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
- json, base::JSON_PARSE_RFC, &error_code, &error_message));
-
- if (!value.get()) {
- std::string trimmed_json;
- if (json.size() < 80) {
- trimmed_json = json;
- } else {
- // Take the first 50 and the last 10 bytes.
- trimmed_json = base::StringPrintf(
- "%s [%s bytes] %s",
- json.substr(0, 50).c_str(),
- base::Uint64ToString(json.size() - 60).c_str(),
- json.substr(json.size() - 10).c_str());
- }
- LOG(WARNING) << "Error while parsing entry response: " << error_message
- << ", code: " << error_code << ", json:\n" << trimmed_json;
- }
- return value.Pass();
+// Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on
+// the calling thread when finished with either success or failure.
+// The callback must not be null.
+void ParseJsonOnBlockingPool(
+ base::TaskRunner* blocking_task_runner,
+ const std::string& json,
+ const base::Callback<void(scoped_ptr<base::Value> value)>& callback) {
+ base::PostTaskAndReplyWithResult(
+ blocking_task_runner,
+ FROM_HERE,
+ base::Bind(&google_apis::ParseJson, json),
+ callback);
}
// Returns response headers as a string. Returns a warning message if
@@ -96,14 +85,28 @@ bool IsSuccessfulResponseCode(int response_code) {
namespace google_apis {
-void ParseJson(base::TaskRunner* blocking_task_runner,
- const std::string& json,
- const ParseJsonCallback& callback) {
- base::PostTaskAndReplyWithResult(
- blocking_task_runner,
- FROM_HERE,
- base::Bind(&ParseJsonInternal, json),
- callback);
+scoped_ptr<base::Value> ParseJson(const std::string& json) {
+ int error_code = -1;
+ std::string error_message;
+ scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
+ json, base::JSON_PARSE_RFC, &error_code, &error_message));
+
+ if (!value.get()) {
+ std::string trimmed_json;
+ if (json.size() < 80) {
+ trimmed_json = json;
+ } else {
+ // Take the first 50 and the last 10 bytes.
+ trimmed_json = base::StringPrintf(
+ "%s [%s bytes] %s",
+ json.substr(0, 50).c_str(),
+ base::Uint64ToString(json.size() - 60).c_str(),
+ json.substr(json.size() - 10).c_str());
+ }
+ LOG(WARNING) << "Error while parsing entry response: " << error_message
+ << ", code: " << error_code << ", json:\n" << trimmed_json;
+ }
+ return value.Pass();
}
//=========================== ResponseWriter ==================================
@@ -360,7 +363,7 @@ void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) {
const char kErrorReasonUserRateLimitExceeded[] = "userRateLimitExceeded";
const char kErrorReasonQuotaExceeded[] = "quotaExceeded";
- scoped_ptr<base::Value> value(ParseJsonInternal(response_writer_->data()));
+ scoped_ptr<base::Value> value(ParseJson(response_writer_->data()));
base::DictionaryValue* dictionary = NULL;
base::DictionaryValue* error = NULL;
if (value &&
@@ -453,11 +456,11 @@ void GetDataRequest::ParseResponse(GDataErrorCode fetch_error_code,
VLOG(1) << "JSON received from " << GetURL().spec() << ": "
<< data.size() << " bytes";
- ParseJson(blocking_task_runner(),
- data,
- base::Bind(&GetDataRequest::OnDataParsed,
- weak_ptr_factory_.GetWeakPtr(),
- fetch_error_code));
+ ParseJsonOnBlockingPool(blocking_task_runner(),
+ data,
+ base::Bind(&GetDataRequest::OnDataParsed,
+ weak_ptr_factory_.GetWeakPtr(),
+ fetch_error_code));
}
void GetDataRequest::ProcessURLFetchResults(const URLFetcher* source) {
@@ -619,11 +622,11 @@ void UploadRangeRequestBase::ProcessURLFetchResults(
} else if (code == HTTP_CREATED || code == HTTP_SUCCESS) {
// The upload is successfully done. Parse the response which should be
// the entry's metadata.
- ParseJson(blocking_task_runner(),
- response_writer()->data(),
- base::Bind(&UploadRangeRequestBase::OnDataParsed,
- weak_ptr_factory_.GetWeakPtr(),
- code));
+ ParseJsonOnBlockingPool(blocking_task_runner(),
+ response_writer()->data(),
+ base::Bind(&UploadRangeRequestBase::OnDataParsed,
+ weak_ptr_factory_.GetWeakPtr(),
+ code));
} else {
// Failed to upload. Run callbacks to notify the error.
OnRangeRequestComplete(

Powered by Google App Engine
This is Rietveld 408576698