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 // This file provides base classes used to issue HTTP requests for Google | 5 // This file provides base classes used to issue HTTP requests for Google |
6 // APIs. | 6 // APIs. |
7 | 7 |
8 #ifndef CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 8 #ifndef CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
9 #define CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 9 #define CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 class RequestSender; | 29 class RequestSender; |
30 | 30 |
31 // Callback used to pass parsed JSON from ParseJson(). If parsing error occurs, | 31 // Callback used to pass parsed JSON from ParseJson(). If parsing error occurs, |
32 // then the passed argument is null. | 32 // then the passed argument is null. |
33 typedef base::Callback<void(scoped_ptr<base::Value> value)> ParseJsonCallback; | 33 typedef base::Callback<void(scoped_ptr<base::Value> value)> ParseJsonCallback; |
34 | 34 |
35 // Callback used for DownloadFileRequest and ResumeUploadRequestBase. | 35 // Callback used for DownloadFileRequest and ResumeUploadRequestBase. |
36 typedef base::Callback<void(int64 progress, int64 total)> ProgressCallback; | 36 typedef base::Callback<void(int64 progress, int64 total)> ProgressCallback; |
37 | 37 |
38 // Parses JSON passed in |json| on blocking pool. Runs |callback| on the calling | 38 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on |
39 // thread when finished with either success or failure. | 39 // the calling thread when finished with either success or failure. |
40 // The callback must not be null. | 40 // The callback must not be null. |
41 void ParseJson(const std::string& json, const ParseJsonCallback& callback); | 41 void ParseJson(base::TaskRunner* blocking_task_runner, |
| 42 const std::string& json, |
| 43 const ParseJsonCallback& callback); |
42 | 44 |
43 //======================= AuthenticatedRequestInterface ====================== | 45 //======================= AuthenticatedRequestInterface ====================== |
44 | 46 |
45 // An interface class for implementing a request which requires OAuth2 | 47 // An interface class for implementing a request which requires OAuth2 |
46 // authentication. | 48 // authentication. |
47 class AuthenticatedRequestInterface { | 49 class AuthenticatedRequestInterface { |
48 public: | 50 public: |
49 // Called when re-authentication is required. See Start() for details. | 51 // Called when re-authentication is required. See Start() for details. |
50 typedef base::Callback<void(AuthenticatedRequestInterface* request)> | 52 typedef base::Callback<void(AuthenticatedRequestInterface* request)> |
51 ReAuthenticateCallback; | 53 ReAuthenticateCallback; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // Invoked when ProcessURLFetchResults() is completed. | 141 // Invoked when ProcessURLFetchResults() is completed. |
140 void OnProcessURLFetchResultsComplete(bool result); | 142 void OnProcessURLFetchResultsComplete(bool result); |
141 | 143 |
142 // Returns an appropriate GDataErrorCode based on the HTTP response code and | 144 // Returns an appropriate GDataErrorCode based on the HTTP response code and |
143 // the status of the URLFetcher. | 145 // the status of the URLFetcher. |
144 static GDataErrorCode GetErrorCode(const net::URLFetcher* source); | 146 static GDataErrorCode GetErrorCode(const net::URLFetcher* source); |
145 | 147 |
146 // Returns true if called on the thread where the constructor was called. | 148 // Returns true if called on the thread where the constructor was called. |
147 bool CalledOnValidThread(); | 149 bool CalledOnValidThread(); |
148 | 150 |
| 151 // Returns the task runner that should be used for blocking tasks. |
| 152 base::TaskRunner* blocking_task_runner() const; |
| 153 |
149 private: | 154 private: |
150 // URLFetcherDelegate overrides. | 155 // URLFetcherDelegate overrides. |
151 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 156 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
152 | 157 |
153 // AuthenticatedRequestInterface overrides. | 158 // AuthenticatedRequestInterface overrides. |
154 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE; | 159 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE; |
155 | 160 |
156 ReAuthenticateCallback re_authenticate_callback_; | 161 ReAuthenticateCallback re_authenticate_callback_; |
157 int re_authenticate_count_; | 162 int re_authenticate_count_; |
158 scoped_ptr<net::URLFetcher> url_fetcher_; | 163 scoped_ptr<net::URLFetcher> url_fetcher_; |
(...skipping 10 matching lines...) Expand all Loading... |
169 | 174 |
170 //============================ EntryActionRequest ============================ | 175 //============================ EntryActionRequest ============================ |
171 | 176 |
172 // Callback type for Delete/Move DocumentServiceInterface calls. | 177 // Callback type for Delete/Move DocumentServiceInterface calls. |
173 typedef base::Callback<void(GDataErrorCode error)> EntryActionCallback; | 178 typedef base::Callback<void(GDataErrorCode error)> EntryActionCallback; |
174 | 179 |
175 // This class performs a simple action over a given entry (document/file). | 180 // This class performs a simple action over a given entry (document/file). |
176 // It is meant to be used for requests that return no JSON blobs. | 181 // It is meant to be used for requests that return no JSON blobs. |
177 class EntryActionRequest : public UrlFetchRequestBase { | 182 class EntryActionRequest : public UrlFetchRequestBase { |
178 public: | 183 public: |
179 // |url_request_context_getter| is used to initialize URLFetcher. | 184 // |callback| is called when the request is finished either by success or by |
180 // |callback| must not be null. | 185 // failure. It must not be null. |
181 EntryActionRequest(RequestSender* sender, | 186 EntryActionRequest(RequestSender* sender, |
182 const EntryActionCallback& callback); | 187 const EntryActionCallback& callback); |
183 virtual ~EntryActionRequest(); | 188 virtual ~EntryActionRequest(); |
184 | 189 |
185 protected: | 190 protected: |
186 // Overridden from UrlFetchRequestBase. | 191 // Overridden from UrlFetchRequestBase. |
187 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 192 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
188 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; | 193 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; |
189 | 194 |
190 private: | 195 private: |
191 const EntryActionCallback callback_; | 196 const EntryActionCallback callback_; |
192 | 197 |
193 DISALLOW_COPY_AND_ASSIGN(EntryActionRequest); | 198 DISALLOW_COPY_AND_ASSIGN(EntryActionRequest); |
194 }; | 199 }; |
195 | 200 |
196 //============================== GetDataRequest ============================== | 201 //============================== GetDataRequest ============================== |
197 | 202 |
198 // Callback type for DocumentServiceInterface::GetResourceList. | 203 // Callback type for DocumentServiceInterface::GetResourceList. |
199 // Note: json_data argument should be passed using base::Passed(&json_data), not | 204 // Note: json_data argument should be passed using base::Passed(&json_data), not |
200 // json_data.Pass(). | 205 // json_data.Pass(). |
201 typedef base::Callback<void(GDataErrorCode error, | 206 typedef base::Callback<void(GDataErrorCode error, |
202 scoped_ptr<base::Value> json_data)> GetDataCallback; | 207 scoped_ptr<base::Value> json_data)> GetDataCallback; |
203 | 208 |
204 // This class performs the request for fetching and converting the fetched | 209 // This class performs the request for fetching and converting the fetched |
205 // content into a base::Value. | 210 // content into a base::Value. |
206 class GetDataRequest : public UrlFetchRequestBase { | 211 class GetDataRequest : public UrlFetchRequestBase { |
207 public: | 212 public: |
208 // |callback| must not be null. | 213 // |callback| is called when the request finishes either by success or by |
| 214 // failure. On success, a JSON Value object is passed. It must not be null. |
209 GetDataRequest(RequestSender* sender, const GetDataCallback& callback); | 215 GetDataRequest(RequestSender* sender, const GetDataCallback& callback); |
210 virtual ~GetDataRequest(); | 216 virtual ~GetDataRequest(); |
211 | 217 |
212 // Parses JSON response. | 218 // Parses JSON response. |
213 void ParseResponse(GDataErrorCode fetch_error_code, const std::string& data); | 219 void ParseResponse(GDataErrorCode fetch_error_code, const std::string& data); |
214 | 220 |
215 protected: | 221 protected: |
216 // UrlFetchRequestBase overrides. | 222 // UrlFetchRequestBase overrides. |
217 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 223 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
218 virtual void RunCallbackOnPrematureFailure( | 224 virtual void RunCallbackOnPrematureFailure( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 // | 257 // |
252 // Here's the flow of uploading: | 258 // Here's the flow of uploading: |
253 // 1) Get the upload URL with a class inheriting InitiateUploadRequestBase. | 259 // 1) Get the upload URL with a class inheriting InitiateUploadRequestBase. |
254 // 2) Upload the first 512KB (see kUploadChunkSize in drive_uploader.cc) | 260 // 2) Upload the first 512KB (see kUploadChunkSize in drive_uploader.cc) |
255 // of the target file to the upload URL | 261 // of the target file to the upload URL |
256 // 3) If there is more data to upload, go to 2). | 262 // 3) If there is more data to upload, go to 2). |
257 // | 263 // |
258 class InitiateUploadRequestBase : public UrlFetchRequestBase { | 264 class InitiateUploadRequestBase : public UrlFetchRequestBase { |
259 protected: | 265 protected: |
260 // |callback| will be called with the upload URL, where upload data is | 266 // |callback| will be called with the upload URL, where upload data is |
261 // uploaded to with ResumeUploadRequestBase. | 267 // uploaded to with ResumeUploadRequestBase. It must not be null. |
262 // |callback| must not be null. | |
263 // |content_type| and |content_length| should be the attributes of the | 268 // |content_type| and |content_length| should be the attributes of the |
264 // uploading file. | 269 // uploading file. |
265 InitiateUploadRequestBase(RequestSender* sender, | 270 InitiateUploadRequestBase(RequestSender* sender, |
266 const InitiateUploadCallback& callback, | 271 const InitiateUploadCallback& callback, |
267 const std::string& content_type, | 272 const std::string& content_type, |
268 int64 content_length); | 273 int64 content_length); |
269 virtual ~InitiateUploadRequestBase(); | 274 virtual ~InitiateUploadRequestBase(); |
270 | 275 |
271 // UrlFetchRequestBase overrides. | 276 // UrlFetchRequestBase overrides. |
272 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 277 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 const ProgressCallback progress_callback_; | 493 const ProgressCallback progress_callback_; |
489 const GURL download_url_; | 494 const GURL download_url_; |
490 const base::FilePath output_file_path_; | 495 const base::FilePath output_file_path_; |
491 | 496 |
492 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); | 497 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); |
493 }; | 498 }; |
494 | 499 |
495 } // namespace google_apis | 500 } // namespace google_apis |
496 | 501 |
497 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 502 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
OLD | NEW |