OLD | NEW |
---|---|
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( | |
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; |
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.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!
| |
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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 const google_apis::GetResourceListCallback& callback) { | 246 const google_apis::GetResourceListCallback& callback) { |
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, | 254 url, |
222 search_query, | 255 search_query, |
223 base::Bind(&ParseResourceListAndRun, callback))); | 256 base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback))); |
224 } | 257 } |
225 | 258 |
226 void DriveAPIService::GetChangelist( | 259 void DriveAPIService::GetChangelist( |
227 const GURL& url, | 260 const GURL& url, |
228 int64 start_changestamp, | 261 int64 start_changestamp, |
229 const google_apis::GetResourceListCallback& callback) { | 262 const google_apis::GetResourceListCallback& callback) { |
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
231 DCHECK(!callback.is_null()); | 264 DCHECK(!callback.is_null()); |
232 | 265 |
233 runner_->StartOperationWithRetry( | 266 runner_->StartOperationWithRetry( |
234 new google_apis::GetChangelistOperation( | 267 new google_apis::GetChangelistOperation( |
235 operation_registry(), | 268 operation_registry(), |
236 url_request_context_getter_, | 269 url_request_context_getter_, |
237 url, | 270 url, |
238 start_changestamp, | 271 start_changestamp, |
239 base::Bind(&ParseResourceListAndRun, callback))); | 272 base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback))); |
240 } | 273 } |
241 | 274 |
242 void DriveAPIService::GetResourceEntry( | 275 void DriveAPIService::GetResourceEntry( |
243 const std::string& resource_id, | 276 const std::string& resource_id, |
244 const google_apis::GetResourceEntryCallback& callback) { | 277 const google_apis::GetResourceEntryCallback& callback) { |
245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
246 DCHECK(!callback.is_null()); | 279 DCHECK(!callback.is_null()); |
247 | 280 |
248 runner_->StartOperationWithRetry(new google_apis::GetFileOperation( | 281 runner_->StartOperationWithRetry(new google_apis::GetFileOperation( |
249 operation_registry(), | 282 operation_registry(), |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 | 465 |
433 void DriveAPIService::OnAuthenticationFailed( | 466 void DriveAPIService::OnAuthenticationFailed( |
434 google_apis::GDataErrorCode error) { | 467 google_apis::GDataErrorCode error) { |
435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 468 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
436 FOR_EACH_OBSERVER( | 469 FOR_EACH_OBSERVER( |
437 google_apis::DriveServiceObserver, observers_, | 470 google_apis::DriveServiceObserver, observers_, |
438 OnAuthenticationFailed(error)); | 471 OnAuthenticationFailed(error)); |
439 } | 472 } |
440 | 473 |
441 } // namespace drive | 474 } // namespace drive |
OLD | NEW |