| 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 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ | 5 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ |
| 6 #define GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ | 6 #define GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "google_apis/drive/base_requests.h" | 16 #include "google_apis/drive/base_requests.h" |
| 17 #include "google_apis/drive/drive_api_parser.h" | 17 #include "google_apis/drive/drive_api_parser.h" |
| 18 #include "google_apis/drive/drive_api_url_generator.h" | 18 #include "google_apis/drive/drive_api_url_generator.h" |
| 19 #include "google_apis/drive/drive_common_callbacks.h" | 19 #include "google_apis/drive/drive_common_callbacks.h" |
| 20 | 20 |
| 21 namespace google_apis { | 21 namespace google_apis { |
| 22 | 22 |
| 23 // Callback used for requests that the server returns FileList data | 23 // Callback used for requests that the server returns FileList data |
| 24 // formatted into JSON value. | 24 // formatted into JSON value. |
| 25 typedef base::Callback<void(GDataErrorCode error, | 25 typedef base::Callback<void(DriveApiErrorCode error, |
| 26 scoped_ptr<FileList> entry)> FileListCallback; | 26 scoped_ptr<FileList> entry)> FileListCallback; |
| 27 | 27 |
| 28 // Callback used for requests that the server returns ChangeList data | 28 // Callback used for requests that the server returns ChangeList data |
| 29 // formatted into JSON value. | 29 // formatted into JSON value. |
| 30 typedef base::Callback<void(GDataErrorCode error, | 30 typedef base::Callback<void(DriveApiErrorCode error, |
| 31 scoped_ptr<ChangeList> entry)> ChangeListCallback; | 31 scoped_ptr<ChangeList> entry)> ChangeListCallback; |
| 32 | 32 |
| 33 namespace drive { | 33 namespace drive { |
| 34 | 34 |
| 35 //============================ DriveApiPartialFieldRequest ==================== | 35 //============================ DriveApiPartialFieldRequest ==================== |
| 36 | 36 |
| 37 // This is base class of the Drive API related requests. All Drive API requests | 37 // This is base class of the Drive API related requests. All Drive API requests |
| 38 // support partial request (to improve the performance). The function can be | 38 // support partial request (to improve the performance). The function can be |
| 39 // shared among the Drive API requests. | 39 // shared among the Drive API requests. |
| 40 // See also https://developers.google.com/drive/performance | 40 // See also https://developers.google.com/drive/performance |
| (...skipping 20 matching lines...) Expand all Loading... |
| 61 DISALLOW_COPY_AND_ASSIGN(DriveApiPartialFieldRequest); | 61 DISALLOW_COPY_AND_ASSIGN(DriveApiPartialFieldRequest); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 //============================ DriveApiDataRequest =========================== | 64 //============================ DriveApiDataRequest =========================== |
| 65 | 65 |
| 66 // The base class of Drive API related requests that receive a JSON response | 66 // The base class of Drive API related requests that receive a JSON response |
| 67 // representing |DataType|. | 67 // representing |DataType|. |
| 68 template<class DataType> | 68 template<class DataType> |
| 69 class DriveApiDataRequest : public DriveApiPartialFieldRequest { | 69 class DriveApiDataRequest : public DriveApiPartialFieldRequest { |
| 70 public: | 70 public: |
| 71 typedef base::Callback<void(GDataErrorCode error, | 71 typedef base::Callback<void(DriveApiErrorCode error, |
| 72 scoped_ptr<DataType> data)> Callback; | 72 scoped_ptr<DataType> data)> Callback; |
| 73 | 73 |
| 74 // |callback| is called when the request finishes either by success or by | 74 // |callback| is called when the request finishes either by success or by |
| 75 // failure. On success, a JSON Value object is passed. It must not be null. | 75 // failure. On success, a JSON Value object is passed. It must not be null. |
| 76 DriveApiDataRequest(RequestSender* sender, const Callback& callback) | 76 DriveApiDataRequest(RequestSender* sender, const Callback& callback) |
| 77 : DriveApiPartialFieldRequest(sender), | 77 : DriveApiPartialFieldRequest(sender), |
| 78 callback_(callback), | 78 callback_(callback), |
| 79 weak_ptr_factory_(this) { | 79 weak_ptr_factory_(this) { |
| 80 DCHECK(!callback_.is_null()); | 80 DCHECK(!callback_.is_null()); |
| 81 } | 81 } |
| 82 virtual ~DriveApiDataRequest() {} | 82 virtual ~DriveApiDataRequest() {} |
| 83 | 83 |
| 84 protected: | 84 protected: |
| 85 // UrlFetchRequestBase overrides. | 85 // UrlFetchRequestBase overrides. |
| 86 virtual void ProcessURLFetchResults(const net::URLFetcher* source) override { | 86 virtual void ProcessURLFetchResults(const net::URLFetcher* source) override { |
| 87 GDataErrorCode error = GetErrorCode(); | 87 DriveApiErrorCode error = GetErrorCode(); |
| 88 switch (error) { | 88 switch (error) { |
| 89 case HTTP_SUCCESS: | 89 case HTTP_SUCCESS: |
| 90 case HTTP_CREATED: | 90 case HTTP_CREATED: |
| 91 base::PostTaskAndReplyWithResult( | 91 base::PostTaskAndReplyWithResult( |
| 92 blocking_task_runner(), | 92 blocking_task_runner(), |
| 93 FROM_HERE, | 93 FROM_HERE, |
| 94 base::Bind(&DriveApiDataRequest::Parse, response_writer()->data()), | 94 base::Bind(&DriveApiDataRequest::Parse, response_writer()->data()), |
| 95 base::Bind(&DriveApiDataRequest::OnDataParsed, | 95 base::Bind(&DriveApiDataRequest::OnDataParsed, |
| 96 weak_ptr_factory_.GetWeakPtr(), error)); | 96 weak_ptr_factory_.GetWeakPtr(), error)); |
| 97 break; | 97 break; |
| 98 default: | 98 default: |
| 99 RunCallbackOnPrematureFailure(error); | 99 RunCallbackOnPrematureFailure(error); |
| 100 OnProcessURLFetchResultsComplete(); | 100 OnProcessURLFetchResultsComplete(); |
| 101 break; | 101 break; |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 virtual void RunCallbackOnPrematureFailure(GDataErrorCode error) override { | 105 virtual void RunCallbackOnPrematureFailure(DriveApiErrorCode error) override { |
| 106 callback_.Run(error, scoped_ptr<DataType>()); | 106 callback_.Run(error, scoped_ptr<DataType>()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 // Parses the |json| string by using DataType::CreateFrom. | 110 // Parses the |json| string by using DataType::CreateFrom. |
| 111 static scoped_ptr<DataType> Parse(const std::string& json) { | 111 static scoped_ptr<DataType> Parse(const std::string& json) { |
| 112 scoped_ptr<base::Value> value = ParseJson(json); | 112 scoped_ptr<base::Value> value = ParseJson(json); |
| 113 return value ? DataType::CreateFrom(*value) : scoped_ptr<DataType>(); | 113 return value ? DataType::CreateFrom(*value) : scoped_ptr<DataType>(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Receives the parsed result and invokes the callback. | 116 // Receives the parsed result and invokes the callback. |
| 117 void OnDataParsed(GDataErrorCode error, scoped_ptr<DataType> value) { | 117 void OnDataParsed(DriveApiErrorCode error, scoped_ptr<DataType> value) { |
| 118 if (!value) | 118 if (!value) |
| 119 error = GDATA_PARSE_ERROR; | 119 error = DRIVE_PARSE_ERROR; |
| 120 callback_.Run(error, value.Pass()); | 120 callback_.Run(error, value.Pass()); |
| 121 OnProcessURLFetchResultsComplete(); | 121 OnProcessURLFetchResultsComplete(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 const Callback callback_; | 124 const Callback callback_; |
| 125 | 125 |
| 126 // Note: This should remain the last member so it'll be destroyed and | 126 // Note: This should remain the last member so it'll be destroyed and |
| 127 // invalidate its weak pointers before any other members are destroyed. | 127 // invalidate its weak pointers before any other members are destroyed. |
| 128 base::WeakPtrFactory<DriveApiDataRequest> weak_ptr_factory_; | 128 base::WeakPtrFactory<DriveApiDataRequest> weak_ptr_factory_; |
| 129 | 129 |
| (...skipping 901 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1031 PermissionRole role_; | 1031 PermissionRole role_; |
| 1032 std::string value_; | 1032 std::string value_; |
| 1033 | 1033 |
| 1034 DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest); | 1034 DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest); |
| 1035 }; | 1035 }; |
| 1036 | 1036 |
| 1037 } // namespace drive | 1037 } // namespace drive |
| 1038 } // namespace google_apis | 1038 } // namespace google_apis |
| 1039 | 1039 |
| 1040 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ | 1040 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ |
| OLD | NEW |