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 type for getting the content from DownloadFileRequest. | |
wtc
2013/10/23 21:53:14
Nit: Callback type for getting => Callback used to
hashimoto
2013/10/24 07:23:37
Done.
| |
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 file or string. | |
wtc
2013/10/23 21:53:14
Nit: add "a" befoe "file".
hashimoto
2013/10/24 07:23:37
Done.
| |
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 25 matching lines...) Expand all Loading... | |
119 // Used by a derived class to add content data which is the whole file or | 157 // Used by a derived class to add content data which is the whole file or |
120 // a part of the file at |local_file_path|. | 158 // a part of the file at |local_file_path|. |
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 |
wtc
2013/10/23 21:53:14
The method's name is "GetOutputFilePath". Do you k
hashimoto
2013/10/24 07:23:37
UrlFetchRequestBase::Start() calls this method to
| |
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 virtual void GetOutputFilePath(base::FilePath* local_file_path, |
170 GetContentCallback* get_content_callback); | |
wtc
2013/10/23 21:53:14
Nit: we should document the get_content_callback o
hashimoto
2013/10/24 07:23:37
Done.
| |
132 | 171 |
133 // Invoked by OnURLFetchComplete when the request completes without an | 172 // Invoked by OnURLFetchComplete when the request completes without an |
134 // authentication error. Must be implemented by a derived class. | 173 // authentication error. Must be implemented by a derived class. |
135 virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0; | 174 virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0; |
136 | 175 |
137 // Invoked by this base class upon an authentication error or cancel by | 176 // Invoked by this base class upon an authentication error or cancel by |
138 // a user request. Must be implemented by a derived class. | 177 // a user request. Must be implemented by a derived class. |
139 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) = 0; | 178 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) = 0; |
140 | 179 |
141 // Invoked from derived classes when ProcessURLFetchResults() is completed. | 180 // Invoked from derived classes when ProcessURLFetchResults() is completed. |
142 void OnProcessURLFetchResultsComplete(); | 181 void OnProcessURLFetchResultsComplete(); |
143 | 182 |
144 // Returns an appropriate GDataErrorCode based on the HTTP response code and | 183 // Returns an appropriate GDataErrorCode based on the HTTP response code and |
145 // the status of the URLFetcher. | 184 // the status of the URLFetcher. |
146 static GDataErrorCode GetErrorCode(const net::URLFetcher* source); | 185 static GDataErrorCode GetErrorCode(const net::URLFetcher* source); |
147 | 186 |
148 // Returns true if called on the thread where the constructor was called. | 187 // Returns true if called on the thread where the constructor was called. |
149 bool CalledOnValidThread(); | 188 bool CalledOnValidThread(); |
150 | 189 |
190 // Returns the writer which is used to save the response for the request. | |
191 ResponseWriter* response_writer() const { return response_writer_; } | |
192 | |
151 // Returns the task runner that should be used for blocking tasks. | 193 // Returns the task runner that should be used for blocking tasks. |
152 base::TaskRunner* blocking_task_runner() const; | 194 base::TaskRunner* blocking_task_runner() const; |
153 | 195 |
154 private: | 196 private: |
155 // URLFetcherDelegate overrides. | 197 // URLFetcherDelegate overrides. |
156 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 198 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
157 | 199 |
158 // AuthenticatedRequestInterface overrides. | 200 // AuthenticatedRequestInterface overrides. |
159 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE; | 201 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE; |
160 | 202 |
161 ReAuthenticateCallback re_authenticate_callback_; | 203 ReAuthenticateCallback re_authenticate_callback_; |
162 int re_authenticate_count_; | 204 int re_authenticate_count_; |
163 scoped_ptr<net::URLFetcher> url_fetcher_; | 205 scoped_ptr<net::URLFetcher> url_fetcher_; |
206 ResponseWriter* response_writer_; // Owned by |url_fetcher_|. | |
164 RequestSender* sender_; | 207 RequestSender* sender_; |
165 | 208 |
166 base::ThreadChecker thread_checker_; | 209 base::ThreadChecker thread_checker_; |
167 | 210 |
168 // Note: This should remain the last member so it'll be destroyed and | 211 // Note: This should remain the last member so it'll be destroyed and |
169 // invalidate its weak pointers before any other members are destroyed. | 212 // invalidate its weak pointers before any other members are destroyed. |
170 base::WeakPtrFactory<UrlFetchRequestBase> weak_ptr_factory_; | 213 base::WeakPtrFactory<UrlFetchRequestBase> weak_ptr_factory_; |
171 | 214 |
172 DISALLOW_COPY_AND_ASSIGN(UrlFetchRequestBase); | 215 DISALLOW_COPY_AND_ASSIGN(UrlFetchRequestBase); |
173 }; | 216 }; |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; | 462 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; |
420 | 463 |
421 private: | 464 private: |
422 const int64 content_length_; | 465 const int64 content_length_; |
423 | 466 |
424 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequestBase); | 467 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequestBase); |
425 }; | 468 }; |
426 | 469 |
427 //============================ DownloadFileRequest =========================== | 470 //============================ DownloadFileRequest =========================== |
428 | 471 |
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. | 472 // Callback type for receiving the completion of DownloadFileRequest. |
435 typedef base::Callback<void(GDataErrorCode error, | 473 typedef base::Callback<void(GDataErrorCode error, |
436 const base::FilePath& temp_file)> | 474 const base::FilePath& temp_file)> |
437 DownloadActionCallback; | 475 DownloadActionCallback; |
438 | 476 |
439 // This is a base class for performing the request for downloading a file. | 477 // This is a base class for performing the request for downloading a file. |
440 class DownloadFileRequestBase : public UrlFetchRequestBase { | 478 class DownloadFileRequestBase : public UrlFetchRequestBase { |
441 public: | 479 public: |
442 // download_action_callback: | 480 // download_action_callback: |
443 // This callback is called when the download is complete. Must not be null. | 481 // 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, | 499 const DownloadActionCallback& download_action_callback, |
462 const GetContentCallback& get_content_callback, | 500 const GetContentCallback& get_content_callback, |
463 const ProgressCallback& progress_callback, | 501 const ProgressCallback& progress_callback, |
464 const GURL& download_url, | 502 const GURL& download_url, |
465 const base::FilePath& output_file_path); | 503 const base::FilePath& output_file_path); |
466 virtual ~DownloadFileRequestBase(); | 504 virtual ~DownloadFileRequestBase(); |
467 | 505 |
468 protected: | 506 protected: |
469 // UrlFetchRequestBase overrides. | 507 // UrlFetchRequestBase overrides. |
470 virtual GURL GetURL() const OVERRIDE; | 508 virtual GURL GetURL() const OVERRIDE; |
471 virtual bool GetOutputFilePath(base::FilePath* local_file_path) OVERRIDE; | 509 virtual void GetOutputFilePath( |
510 base::FilePath* local_file_path, | |
511 GetContentCallback* get_content_callback) OVERRIDE; | |
472 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 512 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
473 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; | 513 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; |
474 | 514 |
475 // net::URLFetcherDelegate overrides. | 515 // net::URLFetcherDelegate overrides. |
476 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 516 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
477 int64 current, int64 total) OVERRIDE; | 517 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 | 518 |
483 private: | 519 private: |
484 const DownloadActionCallback download_action_callback_; | 520 const DownloadActionCallback download_action_callback_; |
485 const GetContentCallback get_content_callback_; | 521 const GetContentCallback get_content_callback_; |
486 const ProgressCallback progress_callback_; | 522 const ProgressCallback progress_callback_; |
487 const GURL download_url_; | 523 const GURL download_url_; |
488 const base::FilePath output_file_path_; | 524 const base::FilePath output_file_path_; |
489 | 525 |
490 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); | 526 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); |
491 }; | 527 }; |
492 | 528 |
493 } // namespace google_apis | 529 } // namespace google_apis |
494 | 530 |
495 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 531 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
OLD | NEW |