Chromium Code Reviews| Index: chrome/browser/chromeos/drive/drive_api_service.cc |
| diff --git a/chrome/browser/chromeos/drive/drive_api_service.cc b/chrome/browser/chromeos/drive/drive_api_service.cc |
| index 27abaf1d3ea29446ff8a7b6fe38c0914346c3865..a794d5d317a95f8a3f69f16cff16bfd920b59e4e 100644 |
| --- a/chrome/browser/chromeos/drive/drive_api_service.cc |
| +++ b/chrome/browser/chromeos/drive/drive_api_service.cc |
| @@ -9,6 +9,7 @@ |
| #include "base/bind.h" |
| #include "base/message_loop_proxy.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| #include "base/values.h" |
| #include "chrome/browser/google_apis/drive_api_operations.h" |
| #include "chrome/browser/google_apis/drive_api_parser.h" |
| @@ -30,35 +31,67 @@ const char kDriveScope[] = "https://www.googleapis.com/auth/drive"; |
| const char kDriveAppsReadonlyScope[] = |
| "https://www.googleapis.com/auth/drive.apps.readonly"; |
| -// Parses the JSON value into ResourceList and runs |callback|. |
| -void ParseResourceListAndRun( |
| - const google_apis::GetResourceListCallback& callback, |
| - google_apis::GDataErrorCode error, |
| - scoped_ptr<base::Value> value) { |
| +struct OnParseResourceListCompletedParams { |
| + google_apis::GDataErrorCode error; |
| + scoped_ptr<google_apis::ResourceList> resource_list; |
| +}; |
| + |
| +void ParseResourceListOnBlockingPool( |
| + scoped_ptr<base::Value> value, |
| + OnParseResourceListCompletedParams* params) { |
| if (!value) { |
| - callback.Run(error, scoped_ptr<google_apis::ResourceList>()); |
| + // JSON value is not available. |
| return; |
| } |
| - // TODO(satorux): Parse the JSON value on a blocking pool. crbug.com/165088 |
| + // Parse the value into ResourceList via ChangeList. |
| + // If failed, set (i.e. overwrite) the error flag and return immediately. |
| scoped_ptr<google_apis::ChangeList> change_list( |
| google_apis::ChangeList::CreateFrom(*value)); |
| if (!change_list) { |
| - callback.Run(google_apis::GDATA_PARSE_ERROR, |
| - scoped_ptr<google_apis::ResourceList>()); |
| + params->error = google_apis::GDATA_PARSE_ERROR; |
| return; |
| } |
| - // TODO(satorux): Do the conversion on a blocking pool too. crbug.com/165088 |
| scoped_ptr<google_apis::ResourceList> resource_list = |
| google_apis::ResourceList::CreateFromChangeList(*change_list); |
| if (!resource_list) { |
| - callback.Run(google_apis::GDATA_PARSE_ERROR, |
| - scoped_ptr<google_apis::ResourceList>()); |
| + params->error = google_apis::GDATA_PARSE_ERROR; |
| return; |
| } |
| - callback.Run(error, resource_list.Pass()); |
| + // Pass the result to the params, so that OnParseResourceListCompleted |
| + // defined below can process it. |
| + params->resource_list = resource_list.Pass(); |
| +} |
| + |
| +// Callback invoked when the parsing of resource list is completed, |
| +// regardless whether it is succeeded or not. |
| +void OnParseResourceListCompleted( |
| + const google_apis::GetResourceListCallback& callback, |
| + OnParseResourceListCompletedParams* params) { |
| + callback.Run(params->error, params->resource_list.Pass()); |
| +} |
| + |
| +// Sends a task to parse the JSON value into ResourceList on blocking pool, |
| +// with a callback which is called when the task is done. |
| +void ParseResourceListOnBlockingPoolAndRun( |
| + const google_apis::GetResourceListCallback& callback, |
| + google_apis::GDataErrorCode error, |
| + scoped_ptr<base::Value> value) { |
| + OnParseResourceListCompletedParams* params = |
| + new OnParseResourceListCompletedParams; |
| + // The error is initialized here to the given |error|, but may be |
| + // overwritten in ParseResourceListOnBlockingPool before used in |
| + // OnParseResourceListCompleted. |
| + params->error = error; |
| + |
| + BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| + FROM_HERE, |
| + base::Bind(&ParseResourceListOnBlockingPool, |
| + base::Passed(value.Pass()), params), |
|
kinaba
2013/01/08 11:53:16
You can use base::Passed(&value).
hidehiko
2013/01/08 13:57:00
Good to know. Thank you!
|
| + base::Bind(&OnParseResourceListCompleted, |
| + callback, base::Owned(params))); |
| } |
| // Parses the JSON value to ResourceEntry runs |callback|. |
| @@ -220,7 +253,7 @@ void DriveAPIService::GetFilelist( |
| url_request_context_getter_, |
| url, |
| search_query, |
| - base::Bind(&ParseResourceListAndRun, callback))); |
| + base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback))); |
| } |
| void DriveAPIService::GetChangelist( |
| @@ -236,7 +269,7 @@ void DriveAPIService::GetChangelist( |
| url_request_context_getter_, |
| url, |
| start_changestamp, |
| - base::Bind(&ParseResourceListAndRun, callback))); |
| + base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback))); |
| } |
| void DriveAPIService::GetResourceEntry( |