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

Side by Side Diff: chrome/browser/chromeos/drive/drive_api_service.cc

Issue 11783025: Run parsing JSON into ResourceList on blocking pool. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: use base::Passed(scoped_ptr*) instead of base::Passed(scoped_ptr::RValue). Created 7 years, 11 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/drive/drive_api_service.h" 5 #include "chrome/browser/chromeos/drive/drive_api_service.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/message_loop_proxy.h" 11 #include "base/message_loop_proxy.h"
12 #include "base/threading/sequenced_worker_pool.h"
12 #include "base/values.h" 13 #include "base/values.h"
13 #include "chrome/browser/google_apis/drive_api_operations.h" 14 #include "chrome/browser/google_apis/drive_api_operations.h"
14 #include "chrome/browser/google_apis/drive_api_parser.h" 15 #include "chrome/browser/google_apis/drive_api_parser.h"
15 #include "chrome/browser/google_apis/gdata_wapi_parser.h" 16 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
16 #include "chrome/browser/google_apis/operation_runner.h" 17 #include "chrome/browser/google_apis/operation_runner.h"
17 #include "chrome/browser/google_apis/time_util.h" 18 #include "chrome/browser/google_apis/time_util.h"
18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/net/url_util.h" 20 #include "chrome/common/net/url_util.h"
20 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
21 22
22 using content::BrowserThread; 23 using content::BrowserThread;
23 24
24 namespace drive { 25 namespace drive {
25 26
26 namespace { 27 namespace {
27 28
28 // OAuth2 scopes for Drive API. 29 // OAuth2 scopes for Drive API.
29 const char kDriveScope[] = "https://www.googleapis.com/auth/drive"; 30 const char kDriveScope[] = "https://www.googleapis.com/auth/drive";
30 const char kDriveAppsReadonlyScope[] = 31 const char kDriveAppsReadonlyScope[] =
31 "https://www.googleapis.com/auth/drive.apps.readonly"; 32 "https://www.googleapis.com/auth/drive.apps.readonly";
32 33
33 // Parses the JSON value into ResourceList and runs |callback|. 34 struct OnParseResourceListCompletedParams {
34 void ParseResourceListAndRun( 35 google_apis::GDataErrorCode error;
36 scoped_ptr<google_apis::ResourceList> resource_list;
37 };
38
39 void ParseResourceListOnBlockingPool(
40 scoped_ptr<base::Value> value,
41 OnParseResourceListCompletedParams* params) {
42 if (!value) {
43 // JSON value is not available.
44 return;
45 }
46
47 // Parse the value into ResourceList via ChangeList.
48 // If failed, set (i.e. overwrite) the error flag and return immediately.
49 scoped_ptr<google_apis::ChangeList> change_list(
50 google_apis::ChangeList::CreateFrom(*value));
51 if (!change_list) {
52 params->error = google_apis::GDATA_PARSE_ERROR;
53 return;
54 }
55
56 scoped_ptr<google_apis::ResourceList> resource_list =
57 google_apis::ResourceList::CreateFromChangeList(*change_list);
58 if (!resource_list) {
59 params->error = google_apis::GDATA_PARSE_ERROR;
60 return;
61 }
62
63 // Pass the result to the params, so that OnParseResourceListCompleted
64 // defined below can process it.
65 params->resource_list = resource_list.Pass();
66 }
67
68 // Callback invoked when the parsing of resource list is completed,
69 // regardless whether it is succeeded or not.
70 void OnParseResourceListCompleted(
satorux1 2013/01/09 00:25:20 Please rename this to DidParseResourceListOnBlocki
hidehiko 2013/01/09 05:21:51 Done.
71 const google_apis::GetResourceListCallback& callback,
72 OnParseResourceListCompletedParams* params) {
73 callback.Run(params->error, params->resource_list.Pass());
74 }
75
76 // Sends a task to parse the JSON value into ResourceList on blocking pool,
77 // with a callback which is called when the task is done.
78 void ParseResourceListOnBlockingPoolAndRun(
35 const google_apis::GetResourceListCallback& callback, 79 const google_apis::GetResourceListCallback& callback,
36 google_apis::GDataErrorCode error, 80 google_apis::GDataErrorCode error,
37 scoped_ptr<base::Value> value) { 81 scoped_ptr<base::Value> value) {
38 if (!value) { 82 OnParseResourceListCompletedParams* params =
39 callback.Run(error, scoped_ptr<google_apis::ResourceList>()); 83 new OnParseResourceListCompletedParams;
satorux1 2013/01/09 00:25:20 I think you don't need a struct.
hidehiko 2013/01/09 05:21:51 At the moment we need, IMHO, though there are some
satorux1 2013/01/09 05:34:15 I thought we could do something like: google_ap
hidehiko 2013/01/09 07:34:07 I see. Done.
40 return; 84 // The error is initialized here to the given |error|, but may be
41 } 85 // overwritten in ParseResourceListOnBlockingPool before used in
86 // OnParseResourceListCompleted.
87 params->error = error;
42 88
43 // TODO(satorux): Parse the JSON value on a blocking pool. crbug.com/165088 89 BrowserThread::GetBlockingPool()->PostTaskAndReply(
44 scoped_ptr<google_apis::ChangeList> change_list( 90 FROM_HERE,
45 google_apis::ChangeList::CreateFrom(*value)); 91 base::Bind(&ParseResourceListOnBlockingPool,
46 if (!change_list) { 92 base::Passed(&value), params),
47 callback.Run(google_apis::GDATA_PARSE_ERROR, 93 base::Bind(&OnParseResourceListCompleted,
48 scoped_ptr<google_apis::ResourceList>()); 94 callback, base::Owned(params)));
49 return;
50 }
51
52 // TODO(satorux): Do the conversion on a blocking pool too. crbug.com/165088
53 scoped_ptr<google_apis::ResourceList> resource_list =
54 google_apis::ResourceList::CreateFromChangeList(*change_list);
55 if (!resource_list) {
56 callback.Run(google_apis::GDATA_PARSE_ERROR,
57 scoped_ptr<google_apis::ResourceList>());
58 return;
59 }
60
61 callback.Run(error, resource_list.Pass());
62 } 95 }
63 96
64 // Parses the JSON value to ResourceEntry runs |callback|. 97 // Parses the JSON value to ResourceEntry runs |callback|.
65 void ParseResourceEntryAndRun( 98 void ParseResourceEntryAndRun(
66 const google_apis::GetResourceEntryCallback& callback, 99 const google_apis::GetResourceEntryCallback& callback,
67 google_apis::GDataErrorCode error, 100 google_apis::GDataErrorCode error,
68 scoped_ptr<base::Value> value) { 101 scoped_ptr<base::Value> value) {
69 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); 102 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
70 103
71 if (!value) { 104 if (!value) {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 DCHECK(!callback.is_null()); 248 DCHECK(!callback.is_null());
216 249
217 runner_->StartOperationWithRetry( 250 runner_->StartOperationWithRetry(
218 new google_apis::GetFilelistOperation( 251 new google_apis::GetFilelistOperation(
219 operation_registry(), 252 operation_registry(),
220 url_request_context_getter_, 253 url_request_context_getter_,
221 url_generator_, 254 url_generator_,
222 url, 255 url,
223 search_query, 256 search_query,
224 base::Bind(&ParseResourceListAndRun, callback))); 257 base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback)));
225 } 258 }
226 259
227 void DriveAPIService::GetChangelist( 260 void DriveAPIService::GetChangelist(
228 const GURL& url, 261 const GURL& url,
229 int64 start_changestamp, 262 int64 start_changestamp,
230 const google_apis::GetResourceListCallback& callback) { 263 const google_apis::GetResourceListCallback& callback) {
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
232 DCHECK(!callback.is_null()); 265 DCHECK(!callback.is_null());
233 266
234 runner_->StartOperationWithRetry( 267 runner_->StartOperationWithRetry(
235 new google_apis::GetChangelistOperation( 268 new google_apis::GetChangelistOperation(
236 operation_registry(), 269 operation_registry(),
237 url_request_context_getter_, 270 url_request_context_getter_,
238 url_generator_, 271 url_generator_,
239 url, 272 url,
240 start_changestamp, 273 start_changestamp,
241 base::Bind(&ParseResourceListAndRun, callback))); 274 base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback)));
242 } 275 }
243 276
244 void DriveAPIService::GetResourceEntry( 277 void DriveAPIService::GetResourceEntry(
245 const std::string& resource_id, 278 const std::string& resource_id,
246 const google_apis::GetResourceEntryCallback& callback) { 279 const google_apis::GetResourceEntryCallback& callback) {
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 DCHECK(!callback.is_null()); 281 DCHECK(!callback.is_null());
249 282
250 runner_->StartOperationWithRetry(new google_apis::GetFileOperation( 283 runner_->StartOperationWithRetry(new google_apis::GetFileOperation(
251 operation_registry(), 284 operation_registry(),
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 470
438 void DriveAPIService::OnAuthenticationFailed( 471 void DriveAPIService::OnAuthenticationFailed(
439 google_apis::GDataErrorCode error) { 472 google_apis::GDataErrorCode error) {
440 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
441 FOR_EACH_OBSERVER( 474 FOR_EACH_OBSERVER(
442 google_apis::DriveServiceObserver, observers_, 475 google_apis::DriveServiceObserver, observers_,
443 OnAuthenticationFailed(error)); 476 OnAuthenticationFailed(error));
444 } 477 }
445 478
446 } // namespace drive 479 } // namespace drive
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698