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

Unified Diff: chrome/browser/chromeos/gdata/operations_base.cc

Issue 10808027: gdrive: Get JSON feeds parsing off the UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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: chrome/browser/chromeos/gdata/operations_base.cc
diff --git a/chrome/browser/chromeos/gdata/operations_base.cc b/chrome/browser/chromeos/gdata/operations_base.cc
index 44d7a426f4bbfbfeb6d4cc7c1b30bdafac8bf5f8..015e6c7bebfe00047c1a6b29d11e0bdec84bfa0f 100644
--- a/chrome/browser/chromeos/gdata/operations_base.cc
+++ b/chrome/browser/chromeos/gdata/operations_base.cc
@@ -4,6 +4,8 @@
#include "chrome/browser/chromeos/gdata/operations_base.h"
+#include "base/debug/stack_trace.h"
hashimoto 2012/07/19 03:57:57 nit: Remove this.
yoshiki 2012/07/19 04:58:45 Done.
+
#include "base/json/json_reader.h"
#include "base/metrics/histogram.h"
#include "base/string_number_conversions.h"
@@ -45,6 +47,28 @@ const char kDocsListScope[] = "https://docs.google.com/feeds/";
const char kSpreadsheetsScope[] = "https://spreadsheets.google.com/feeds/";
const char kUserContentScope[] = "https://docs.googleusercontent.com/";
+// Parse JSON string to base::Value object.
+void ParseJsonOnBlockingPool(const std::string& data,
+ scoped_ptr<base::Value>* value) {
+ DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
+
+ int error_code = -1;
+ std::string error_message;
+ value->reset(base::JSONReader::ReadAndReturnError(data,
+ base::JSON_PARSE_RFC,
+ &error_code,
+ &error_message));
+
+ if (!value->get()) {
+ LOG(ERROR) << "Error while parsing entry response: "
+ << error_message
+ << ", code: "
+ << error_code
+ << ", data:\n"
+ << data;
+ }
+}
+
} // namespace
namespace gdata {
@@ -328,7 +352,9 @@ void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) {
GetDataOperation::GetDataOperation(GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback)
- : UrlFetchOperationBase(registry, profile), callback_(callback) {
+ : UrlFetchOperationBase(registry, profile),
+ callback_(callback),
+ weak_ptr_factory_(this) {
}
GetDataOperation::~GetDataOperation() {}
@@ -337,48 +363,78 @@ bool GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) {
std::string data;
source->GetResponseAsString(&data);
scoped_ptr<base::Value> root_value;
- GDataErrorCode code = GetErrorCode(source);
+ GDataErrorCode fetch_error_code = GetErrorCode(source);
+ bool ret = false;
- switch (code) {
+ switch (fetch_error_code) {
case HTTP_SUCCESS:
- case HTTP_CREATED: {
- root_value.reset(ParseResponse(data));
- if (!root_value.get())
- code = GDATA_PARSE_ERROR;
-
+ case HTTP_CREATED:
+ ret = ParseResponse(fetch_error_code, data);
break;
- }
default:
+ RunCallback(fetch_error_code, scoped_ptr<base::Value>());
break;
}
- if (!callback_.is_null())
- callback_.Run(code, root_value.Pass());
- return root_value.get() != NULL;
+ return ret;
}
-void GetDataOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) {
+void GetDataOperation::RunCallbackOnPrematureFailure(
+ GDataErrorCode fetch_error_code) {
if (!callback_.is_null()) {
scoped_ptr<base::Value> root_value;
- callback_.Run(code, root_value.Pass());
+ callback_.Run(fetch_error_code, root_value.Pass());
}
}
-base::Value* GetDataOperation::ParseResponse(const std::string& data) {
- int error_code = -1;
- std::string error_message;
- scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError(
- data, base::JSON_PARSE_RFC, &error_code, &error_message));
- if (!root_value.get()) {
- LOG(ERROR) << "Error while parsing entry response: "
- << error_message
- << ", code: "
- << error_code
- << ", data:\n"
- << data;
- return NULL;
- }
- return root_value.release();
+bool GetDataOperation::ParseResponse(GDataErrorCode fetch_error_code,
+ const std::string& data) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ base::debug::StackTrace().PrintBacktrace();
+
+ // Uses this hack to avoid deep-copy of json object because json might be so
+ // big.
+ // This pointer of scped_ptr is to ensure a deletion of the parsed json value
+ // object, even when OnDataParsed() is not called.
+ scoped_ptr<base::Value>* parsed_value = new scoped_ptr<base::Value>();
+
+ ParseJsonOnBlockingPool(data, parsed_value);
hashimoto 2012/07/19 03:57:57 The comment-outed block below should be used inste
yoshiki 2012/07/19 04:58:45 Oops, I forgot to remove debugging code. sorry. O
+ OnDataParsed(fetch_error_code, parsed_value);
+ delete parsed_value;
+ return true;
+
+ /*
+ return BrowserThread::PostBlockingPoolTaskAndReply(
+ FROM_HERE,
+ base::Bind(&ParseJsonOnBlockingPool,
+ data,
+ parsed_value),
+ base::Bind(&GetDataOperation::OnDataParsed,
+ weak_ptr_factory_.GetWeakPtr(),
+ fetch_error_code,
+ base::Owned(parsed_value)));
+ */
+}
+
+void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code,
+ scoped_ptr<base::Value> value) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ if (!callback_.is_null())
+ callback_.Run(fetch_error_code, value.Pass());
+}
+
+void GetDataOperation::OnDataParsed(GDataErrorCode fetch_error_code,
+ scoped_ptr<base::Value>* value) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ if (!value->get())
+ fetch_error_code = GDATA_PARSE_ERROR;
+
+ // The ownership of the parsed json object is transfered to RunCallBack(),
+ // keeping the ownership of the |value| here.
+ RunCallback(fetch_error_code, value->Pass());
+ DCHECK(!value->get());
+
+ // |value| will be deleted after return beause it is base::Owned()'d.
}
} // namespace gdata

Powered by Google App Engine
This is Rietveld 408576698