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