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 |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
18 #include "chrome/browser/google_apis/gdata_errorcode.h" | 18 #include "chrome/browser/google_apis/gdata_errorcode.h" |
19 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 20 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "net/url_request/url_fetcher_response_writer.h" | |
21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
22 | 23 |
23 namespace base { | 24 namespace base { |
24 class Value; | 25 class Value; |
25 } // namespace base | 26 } // namespace base |
26 | 27 |
27 namespace google_apis { | 28 namespace google_apis { |
28 | 29 |
29 class RequestSender; | 30 class RequestSender; |
30 | 31 |
31 // Callback used to pass parsed JSON from ParseJson(). If parsing error occurs, | 32 // Callback used to pass parsed JSON from ParseJson(). If parsing error occurs, |
32 // then the passed argument is null. | 33 // then the passed argument is null. |
33 typedef base::Callback<void(scoped_ptr<base::Value> value)> ParseJsonCallback; | 34 typedef base::Callback<void(scoped_ptr<base::Value> value)> ParseJsonCallback; |
34 | 35 |
35 // Callback used for DownloadFileRequest and ResumeUploadRequestBase. | 36 // Callback used for DownloadFileRequest and ResumeUploadRequestBase. |
36 typedef base::Callback<void(int64 progress, int64 total)> ProgressCallback; | 37 typedef base::Callback<void(int64 progress, int64 total)> ProgressCallback; |
37 | 38 |
39 // Callback used to get the content from DownloadFileRequest. | |
40 typedef base::Callback<void( | |
41 GDataErrorCode error, | |
42 scoped_ptr<std::string> content)> GetContentCallback; | |
43 | |
38 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on | 44 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on |
39 // the calling thread when finished with either success or failure. | 45 // the calling thread when finished with either success or failure. |
40 // The callback must not be null. | 46 // The callback must not be null. |
41 void ParseJson(base::TaskRunner* blocking_task_runner, | 47 void ParseJson(base::TaskRunner* blocking_task_runner, |
42 const std::string& json, | 48 const std::string& json, |
43 const ParseJsonCallback& callback); | 49 const ParseJsonCallback& callback); |
44 | 50 |
45 //======================= AuthenticatedRequestInterface ====================== | 51 //======================= AuthenticatedRequestInterface ====================== |
46 | 52 |
47 // An interface class for implementing a request which requires OAuth2 | 53 // An interface class for implementing a request which requires OAuth2 |
(...skipping 26 matching lines...) Expand all Loading... | |
74 // on the authentication request object, weak pointers have to be used. | 80 // on the authentication request object, weak pointers have to be used. |
75 // TODO(kinaba): crbug.com/134814 use more clean life time management than | 81 // TODO(kinaba): crbug.com/134814 use more clean life time management than |
76 // using weak pointers. | 82 // using weak pointers. |
77 virtual base::WeakPtr<AuthenticatedRequestInterface> GetWeakPtr() = 0; | 83 virtual base::WeakPtr<AuthenticatedRequestInterface> GetWeakPtr() = 0; |
78 | 84 |
79 // Cancels the request. It will invoke the callback object passed in | 85 // Cancels the request. It will invoke the callback object passed in |
80 // each request's constructor with error code GDATA_CANCELLED. | 86 // each request's constructor with error code GDATA_CANCELLED. |
81 virtual void Cancel() = 0; | 87 virtual void Cancel() = 0; |
82 }; | 88 }; |
83 | 89 |
90 //=========================== ResponseWriter ================================== | |
91 | |
92 // Saves the response for the request to a file or string. | |
93 class ResponseWriter : public net::URLFetcherResponseWriter { | |
94 public: | |
95 // If file_path is not empty, the response will be saved with file_writer_, | |
96 // otherwise it will be saved to data_. | |
97 ResponseWriter(base::TaskRunner* file_task_runner, | |
98 const base::FilePath& file_path, | |
99 const GetContentCallback& get_content_callback); | |
100 virtual ~ResponseWriter(); | |
101 | |
102 const std::string& data() const { return data_; } | |
103 | |
104 // Disowns the output file. | |
105 void DisownFile(); | |
106 | |
107 // URLFetcherResponseWriter overrides: | |
108 virtual int Initialize(const net::CompletionCallback& callback) OVERRIDE; | |
109 virtual int Write(net::IOBuffer* buffer, | |
110 int num_bytes, | |
111 const net::CompletionCallback& callback) OVERRIDE; | |
112 virtual int Finish(const net::CompletionCallback& callback) OVERRIDE; | |
113 | |
114 private: | |
115 const GetContentCallback get_content_callback_; | |
116 std::string data_; | |
117 scoped_ptr<net::URLFetcherFileWriter> file_writer_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(ResponseWriter); | |
120 }; | |
121 | |
84 //============================ UrlFetchRequestBase =========================== | 122 //============================ UrlFetchRequestBase =========================== |
85 | 123 |
86 // Base class for requests that are fetching URLs. | 124 // Base class for requests that are fetching URLs. |
87 class UrlFetchRequestBase : public AuthenticatedRequestInterface, | 125 class UrlFetchRequestBase : public AuthenticatedRequestInterface, |
88 public net::URLFetcherDelegate { | 126 public net::URLFetcherDelegate { |
89 public: | 127 public: |
90 // AuthenticatedRequestInterface overrides. | 128 // AuthenticatedRequestInterface overrides. |
91 virtual void Start(const std::string& access_token, | 129 virtual void Start(const std::string& access_token, |
92 const std::string& custom_user_agent, | 130 const std::string& custom_user_agent, |
93 const ReAuthenticateCallback& callback) OVERRIDE; | 131 const ReAuthenticateCallback& callback) OVERRIDE; |
(...skipping 27 matching lines...) Expand all Loading... | |
121 // Returns true if all the arguments are updated for the content being | 159 // Returns true if all the arguments are updated for the content being |
122 // uploaded. | 160 // uploaded. |
123 // Note that this and GetContentData() cannot be used together. | 161 // Note that this and GetContentData() cannot be used together. |
124 virtual bool GetContentFile(base::FilePath* local_file_path, | 162 virtual bool GetContentFile(base::FilePath* local_file_path, |
125 int64* range_offset, | 163 int64* range_offset, |
126 int64* range_length, | 164 int64* range_length, |
127 std::string* upload_content_type); | 165 std::string* upload_content_type); |
128 | 166 |
129 // Used by a derived class to set an output file path if they want to save | 167 // Used by a derived class to set an output file path if they want to save |
130 // the downloaded content to a file at a specific path. | 168 // the downloaded content to a file at a specific path. |
131 virtual bool GetOutputFilePath(base::FilePath* local_file_path); | 169 // get_content_callback is called when some part of the response is read. |
wtc
2013/10/24 18:36:39
Nit: Please stress the fact that get_content_callb
hashimoto
2013/10/25 05:29:14
Done.
| |
170 virtual void GetOutputFilePath(base::FilePath* local_file_path, | |
171 GetContentCallback* get_content_callback); | |
132 | 172 |
133 // Invoked by OnURLFetchComplete when the request completes without an | 173 // Invoked by OnURLFetchComplete when the request completes without an |
134 // authentication error. Must be implemented by a derived class. | 174 // authentication error. Must be implemented by a derived class. |
135 virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0; | 175 virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0; |
136 | 176 |
137 // Invoked by this base class upon an authentication error or cancel by | 177 // Invoked by this base class upon an authentication error or cancel by |
138 // a user request. Must be implemented by a derived class. | 178 // a user request. Must be implemented by a derived class. |
139 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) = 0; | 179 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) = 0; |
140 | 180 |
141 // Invoked from derived classes when ProcessURLFetchResults() is completed. | 181 // Invoked from derived classes when ProcessURLFetchResults() is completed. |
142 void OnProcessURLFetchResultsComplete(); | 182 void OnProcessURLFetchResultsComplete(); |
143 | 183 |
144 // Returns an appropriate GDataErrorCode based on the HTTP response code and | 184 // Returns an appropriate GDataErrorCode based on the HTTP response code and |
145 // the status of the URLFetcher. | 185 // the status of the URLFetcher. |
146 static GDataErrorCode GetErrorCode(const net::URLFetcher* source); | 186 static GDataErrorCode GetErrorCode(const net::URLFetcher* source); |
147 | 187 |
148 // Returns true if called on the thread where the constructor was called. | 188 // Returns true if called on the thread where the constructor was called. |
149 bool CalledOnValidThread(); | 189 bool CalledOnValidThread(); |
150 | 190 |
191 // Returns the writer which is used to save the response for the request. | |
192 ResponseWriter* response_writer() const { return response_writer_; } | |
193 | |
151 // Returns the task runner that should be used for blocking tasks. | 194 // Returns the task runner that should be used for blocking tasks. |
152 base::TaskRunner* blocking_task_runner() const; | 195 base::TaskRunner* blocking_task_runner() const; |
153 | 196 |
154 private: | 197 private: |
155 // URLFetcherDelegate overrides. | 198 // URLFetcherDelegate overrides. |
156 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 199 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
157 | 200 |
158 // AuthenticatedRequestInterface overrides. | 201 // AuthenticatedRequestInterface overrides. |
159 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE; | 202 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE; |
160 | 203 |
161 ReAuthenticateCallback re_authenticate_callback_; | 204 ReAuthenticateCallback re_authenticate_callback_; |
162 int re_authenticate_count_; | 205 int re_authenticate_count_; |
163 scoped_ptr<net::URLFetcher> url_fetcher_; | 206 scoped_ptr<net::URLFetcher> url_fetcher_; |
207 ResponseWriter* response_writer_; // Owned by |url_fetcher_|. | |
164 RequestSender* sender_; | 208 RequestSender* sender_; |
165 | 209 |
166 base::ThreadChecker thread_checker_; | 210 base::ThreadChecker thread_checker_; |
167 | 211 |
168 // Note: This should remain the last member so it'll be destroyed and | 212 // Note: This should remain the last member so it'll be destroyed and |
169 // invalidate its weak pointers before any other members are destroyed. | 213 // invalidate its weak pointers before any other members are destroyed. |
170 base::WeakPtrFactory<UrlFetchRequestBase> weak_ptr_factory_; | 214 base::WeakPtrFactory<UrlFetchRequestBase> weak_ptr_factory_; |
171 | 215 |
172 DISALLOW_COPY_AND_ASSIGN(UrlFetchRequestBase); | 216 DISALLOW_COPY_AND_ASSIGN(UrlFetchRequestBase); |
173 }; | 217 }; |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; | 463 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; |
420 | 464 |
421 private: | 465 private: |
422 const int64 content_length_; | 466 const int64 content_length_; |
423 | 467 |
424 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequestBase); | 468 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequestBase); |
425 }; | 469 }; |
426 | 470 |
427 //============================ DownloadFileRequest =========================== | 471 //============================ DownloadFileRequest =========================== |
428 | 472 |
429 // Callback type for getting the content from DownloadFileRequest. | |
430 typedef base::Callback<void( | |
431 GDataErrorCode error, | |
432 scoped_ptr<std::string> content)> GetContentCallback; | |
433 | |
434 // Callback type for receiving the completion of DownloadFileRequest. | 473 // Callback type for receiving the completion of DownloadFileRequest. |
435 typedef base::Callback<void(GDataErrorCode error, | 474 typedef base::Callback<void(GDataErrorCode error, |
436 const base::FilePath& temp_file)> | 475 const base::FilePath& temp_file)> |
437 DownloadActionCallback; | 476 DownloadActionCallback; |
438 | 477 |
439 // This is a base class for performing the request for downloading a file. | 478 // This is a base class for performing the request for downloading a file. |
440 class DownloadFileRequestBase : public UrlFetchRequestBase { | 479 class DownloadFileRequestBase : public UrlFetchRequestBase { |
441 public: | 480 public: |
442 // download_action_callback: | 481 // download_action_callback: |
443 // This callback is called when the download is complete. Must not be null. | 482 // This callback is called when the download is complete. Must not be null. |
(...skipping 17 matching lines...) Expand all Loading... | |
461 const DownloadActionCallback& download_action_callback, | 500 const DownloadActionCallback& download_action_callback, |
462 const GetContentCallback& get_content_callback, | 501 const GetContentCallback& get_content_callback, |
463 const ProgressCallback& progress_callback, | 502 const ProgressCallback& progress_callback, |
464 const GURL& download_url, | 503 const GURL& download_url, |
465 const base::FilePath& output_file_path); | 504 const base::FilePath& output_file_path); |
466 virtual ~DownloadFileRequestBase(); | 505 virtual ~DownloadFileRequestBase(); |
467 | 506 |
468 protected: | 507 protected: |
469 // UrlFetchRequestBase overrides. | 508 // UrlFetchRequestBase overrides. |
470 virtual GURL GetURL() const OVERRIDE; | 509 virtual GURL GetURL() const OVERRIDE; |
471 virtual bool GetOutputFilePath(base::FilePath* local_file_path) OVERRIDE; | 510 virtual void GetOutputFilePath( |
511 base::FilePath* local_file_path, | |
512 GetContentCallback* get_content_callback) OVERRIDE; | |
472 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 513 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
473 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; | 514 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; |
474 | 515 |
475 // net::URLFetcherDelegate overrides. | 516 // net::URLFetcherDelegate overrides. |
476 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 517 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
477 int64 current, int64 total) OVERRIDE; | 518 int64 current, int64 total) OVERRIDE; |
478 virtual bool ShouldSendDownloadData() OVERRIDE; | |
479 virtual void OnURLFetchDownloadData( | |
480 const net::URLFetcher* source, | |
481 scoped_ptr<std::string> download_data) OVERRIDE; | |
482 | 519 |
483 private: | 520 private: |
484 const DownloadActionCallback download_action_callback_; | 521 const DownloadActionCallback download_action_callback_; |
485 const GetContentCallback get_content_callback_; | 522 const GetContentCallback get_content_callback_; |
486 const ProgressCallback progress_callback_; | 523 const ProgressCallback progress_callback_; |
487 const GURL download_url_; | 524 const GURL download_url_; |
488 const base::FilePath output_file_path_; | 525 const base::FilePath output_file_path_; |
489 | 526 |
490 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); | 527 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); |
491 }; | 528 }; |
492 | 529 |
493 } // namespace google_apis | 530 } // namespace google_apis |
494 | 531 |
495 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 532 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
OLD | NEW |