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/gdata/operations_base.h" | 5 #include "chrome/browser/chromeos/gdata/operations_base.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 const char kGDataVersionHeader[] = "GData-Version: 3.0"; | 38 const char kGDataVersionHeader[] = "GData-Version: 3.0"; |
39 | 39 |
40 // Maximum number of attempts for re-authentication per operation. | 40 // Maximum number of attempts for re-authentication per operation. |
41 const int kMaxReAuthenticateAttemptsPerOperation = 1; | 41 const int kMaxReAuthenticateAttemptsPerOperation = 1; |
42 | 42 |
43 // OAuth scope for the documents API. | 43 // OAuth scope for the documents API. |
44 const char kDocsListScope[] = "https://docs.google.com/feeds/"; | 44 const char kDocsListScope[] = "https://docs.google.com/feeds/"; |
45 const char kSpreadsheetsScope[] = "https://spreadsheets.google.com/feeds/"; | 45 const char kSpreadsheetsScope[] = "https://spreadsheets.google.com/feeds/"; |
46 const char kUserContentScope[] = "https://docs.googleusercontent.com/"; | 46 const char kUserContentScope[] = "https://docs.googleusercontent.com/"; |
47 | 47 |
48 // Parse JSON string to base::Value object. | |
49 void ParseJsonOnBlockingPool(const std::string& data, | |
50 scoped_ptr<base::Value>* value) { | |
51 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
52 | |
53 int error_code = -1; | |
54 std::string error_message; | |
55 value->reset(base::JSONReader::ReadAndReturnError(data, | |
56 base::JSON_PARSE_RFC, | |
57 &error_code, | |
58 &error_message)); | |
59 | |
60 if (!value->get()) { | |
61 LOG(ERROR) << "Error while parsing entry response: " | |
62 << error_message | |
63 << ", code: " | |
64 << error_code | |
65 << ", data:\n" | |
66 << data; | |
67 } | |
68 } | |
69 | |
70 } // namespace | 48 } // namespace |
71 | 49 |
72 namespace gdata { | 50 namespace gdata { |
73 | 51 |
74 //================================ AuthOperation =============================== | 52 //================================ AuthOperation =============================== |
75 | 53 |
76 AuthOperation::AuthOperation(GDataOperationRegistry* registry, | 54 AuthOperation::AuthOperation(GDataOperationRegistry* registry, |
77 Profile* profile, | 55 Profile* profile, |
78 const AuthStatusCallback& callback, | 56 const AuthStatusCallback& callback, |
79 const std::string& refresh_token) | 57 const std::string& refresh_token) |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 322 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
345 if (!callback_.is_null()) | 323 if (!callback_.is_null()) |
346 callback_.Run(code, document_url_); | 324 callback_.Run(code, document_url_); |
347 } | 325 } |
348 | 326 |
349 //============================== GetDataOperation ============================== | 327 //============================== GetDataOperation ============================== |
350 | 328 |
351 GetDataOperation::GetDataOperation(GDataOperationRegistry* registry, | 329 GetDataOperation::GetDataOperation(GDataOperationRegistry* registry, |
352 Profile* profile, | 330 Profile* profile, |
353 const GetDataCallback& callback) | 331 const GetDataCallback& callback) |
354 : UrlFetchOperationBase(registry, profile), | 332 : UrlFetchOperationBase(registry, profile), callback_(callback) { |
355 callback_(callback), | |
356 weak_ptr_factory_(this) { | |
357 } | 333 } |
358 | 334 |
359 GetDataOperation::~GetDataOperation() {} | 335 GetDataOperation::~GetDataOperation() {} |
360 | 336 |
361 bool GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { | 337 bool GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { |
362 std::string data; | 338 std::string data; |
363 source->GetResponseAsString(&data); | 339 source->GetResponseAsString(&data); |
364 scoped_ptr<base::Value> root_value; | 340 scoped_ptr<base::Value> root_value; |
365 GDataErrorCode fetch_error_code = GetErrorCode(source); | 341 GDataErrorCode code = GetErrorCode(source); |
366 bool ret = false; | |
367 | 342 |
368 switch (fetch_error_code) { | 343 switch (code) { |
369 case HTTP_SUCCESS: | 344 case HTTP_SUCCESS: |
370 case HTTP_CREATED: | 345 case HTTP_CREATED: { |
371 ret = ParseResponse(fetch_error_code, data); | 346 root_value.reset(ParseResponse(data)); |
| 347 if (!root_value.get()) |
| 348 code = GDATA_PARSE_ERROR; |
| 349 |
372 break; | 350 break; |
| 351 } |
373 default: | 352 default: |
374 RunCallback(fetch_error_code, scoped_ptr<base::Value>()); | |
375 break; | 353 break; |
376 } | 354 } |
377 | 355 |
378 return ret; | 356 if (!callback_.is_null()) |
| 357 callback_.Run(code, root_value.Pass()); |
| 358 return root_value.get() != NULL; |
379 } | 359 } |
380 | 360 |
381 void GetDataOperation::RunCallbackOnPrematureFailure( | 361 void GetDataOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
382 GDataErrorCode fetch_error_code) { | |
383 if (!callback_.is_null()) { | 362 if (!callback_.is_null()) { |
384 scoped_ptr<base::Value> root_value; | 363 scoped_ptr<base::Value> root_value; |
385 callback_.Run(fetch_error_code, root_value.Pass()); | 364 callback_.Run(code, root_value.Pass()); |
386 } | 365 } |
387 } | 366 } |
388 | 367 |
389 bool GetDataOperation::ParseResponse(GDataErrorCode fetch_error_code, | 368 base::Value* GetDataOperation::ParseResponse(const std::string& data) { |
390 const std::string& data) { | 369 int error_code = -1; |
391 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 370 std::string error_message; |
392 | 371 scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError( |
393 // Uses this hack to avoid deep-copy of json object because json might be so | 372 data, base::JSON_PARSE_RFC, &error_code, &error_message)); |
394 // big. | 373 if (!root_value.get()) { |
395 // This pointer of scped_ptr is to ensure a deletion of the parsed json value | 374 LOG(ERROR) << "Error while parsing entry response: " |
396 // object, even when OnDataParsed() is not called. | 375 << error_message |
397 scoped_ptr<base::Value>* parsed_value = new scoped_ptr<base::Value>(); | 376 << ", code: " |
398 | 377 << error_code |
399 return BrowserThread::PostBlockingPoolTaskAndReply( | 378 << ", data:\n" |
400 FROM_HERE, | 379 << data; |
401 base::Bind(&ParseJsonOnBlockingPool, | 380 return NULL; |
402 data, | 381 } |
403 parsed_value), | 382 return root_value.release(); |
404 base::Bind(&GetDataOperation::OnDataParsed, | |
405 weak_ptr_factory_.GetWeakPtr(), | |
406 fetch_error_code, | |
407 base::Owned(parsed_value))); | |
408 } | |
409 | |
410 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, | |
411 scoped_ptr<base::Value> value) { | |
412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
413 if (!callback_.is_null()) | |
414 callback_.Run(fetch_error_code, value.Pass()); | |
415 } | |
416 | |
417 void GetDataOperation::OnDataParsed(GDataErrorCode fetch_error_code, | |
418 scoped_ptr<base::Value>* value) { | |
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
420 if (!value->get()) | |
421 fetch_error_code = GDATA_PARSE_ERROR; | |
422 | |
423 // The ownership of the parsed json object is transfered to RunCallBack(), | |
424 // keeping the ownership of the |value| here. | |
425 RunCallback(fetch_error_code, value->Pass()); | |
426 DCHECK(!value->get()); | |
427 | |
428 // |value| will be deleted after return beause it is base::Owned()'d. | |
429 } | 383 } |
430 | 384 |
431 } // namespace gdata | 385 } // namespace gdata |
OLD | NEW |