Chromium Code Reviews| Index: chrome/browser/google_apis/base_requests.h |
| diff --git a/chrome/browser/google_apis/base_requests.h b/chrome/browser/google_apis/base_requests.h |
| index b0eec46202d9c7eef30bea147fba672a9cb04ec0..5ad19c2a1d0317f843601a98ddb5f4b6896a342b 100644 |
| --- a/chrome/browser/google_apis/base_requests.h |
| +++ b/chrome/browser/google_apis/base_requests.h |
| @@ -18,6 +18,7 @@ |
| #include "chrome/browser/google_apis/gdata_errorcode.h" |
| #include "net/url_request/url_fetcher.h" |
| #include "net/url_request/url_fetcher_delegate.h" |
| +#include "net/url_request/url_fetcher_response_writer.h" |
| #include "url/gurl.h" |
| namespace base { |
| @@ -35,6 +36,11 @@ typedef base::Callback<void(scoped_ptr<base::Value> value)> ParseJsonCallback; |
| // Callback used for DownloadFileRequest and ResumeUploadRequestBase. |
| typedef base::Callback<void(int64 progress, int64 total)> ProgressCallback; |
| +// 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.
|
| +typedef base::Callback<void( |
| + GDataErrorCode error, |
| + scoped_ptr<std::string> content)> GetContentCallback; |
| + |
| // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on |
| // the calling thread when finished with either success or failure. |
| // The callback must not be null. |
| @@ -81,6 +87,38 @@ class AuthenticatedRequestInterface { |
| virtual void Cancel() = 0; |
| }; |
| +//=========================== ResponseWriter ================================== |
| + |
| +// 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.
|
| +class ResponseWriter : public net::URLFetcherResponseWriter { |
| + public: |
| + // If file_path is not empty, the response will be saved with file_writer_, |
| + // otherwise it will be saved to data_. |
| + ResponseWriter(base::TaskRunner* file_task_runner, |
| + const base::FilePath& file_path, |
| + const GetContentCallback& get_content_callback); |
| + virtual ~ResponseWriter(); |
| + |
| + const std::string& data() const { return data_; } |
| + |
| + // Disowns the output file. |
| + void DisownFile(); |
| + |
| + // URLFetcherResponseWriter overrides: |
| + virtual int Initialize(const net::CompletionCallback& callback) OVERRIDE; |
| + virtual int Write(net::IOBuffer* buffer, |
| + int num_bytes, |
| + const net::CompletionCallback& callback) OVERRIDE; |
| + virtual int Finish(const net::CompletionCallback& callback) OVERRIDE; |
| + |
| + private: |
| + const GetContentCallback get_content_callback_; |
| + std::string data_; |
| + scoped_ptr<net::URLFetcherFileWriter> file_writer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ResponseWriter); |
| +}; |
| + |
| //============================ UrlFetchRequestBase =========================== |
| // Base class for requests that are fetching URLs. |
| @@ -128,7 +166,8 @@ class UrlFetchRequestBase : public AuthenticatedRequestInterface, |
| // 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
|
| // the downloaded content to a file at a specific path. |
| - virtual bool GetOutputFilePath(base::FilePath* local_file_path); |
| + virtual void GetOutputFilePath(base::FilePath* local_file_path, |
| + 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.
|
| // Invoked by OnURLFetchComplete when the request completes without an |
| // authentication error. Must be implemented by a derived class. |
| @@ -148,6 +187,9 @@ class UrlFetchRequestBase : public AuthenticatedRequestInterface, |
| // Returns true if called on the thread where the constructor was called. |
| bool CalledOnValidThread(); |
| + // Returns the writer which is used to save the response for the request. |
| + ResponseWriter* response_writer() const { return response_writer_; } |
| + |
| // Returns the task runner that should be used for blocking tasks. |
| base::TaskRunner* blocking_task_runner() const; |
| @@ -161,6 +203,7 @@ class UrlFetchRequestBase : public AuthenticatedRequestInterface, |
| ReAuthenticateCallback re_authenticate_callback_; |
| int re_authenticate_count_; |
| scoped_ptr<net::URLFetcher> url_fetcher_; |
| + ResponseWriter* response_writer_; // Owned by |url_fetcher_|. |
| RequestSender* sender_; |
| base::ThreadChecker thread_checker_; |
| @@ -426,11 +469,6 @@ class GetUploadStatusRequestBase : public UploadRangeRequestBase { |
| //============================ DownloadFileRequest =========================== |
| -// Callback type for getting the content from DownloadFileRequest. |
| -typedef base::Callback<void( |
| - GDataErrorCode error, |
| - scoped_ptr<std::string> content)> GetContentCallback; |
| - |
| // Callback type for receiving the completion of DownloadFileRequest. |
| typedef base::Callback<void(GDataErrorCode error, |
| const base::FilePath& temp_file)> |
| @@ -468,17 +506,15 @@ class DownloadFileRequestBase : public UrlFetchRequestBase { |
| protected: |
| // UrlFetchRequestBase overrides. |
| virtual GURL GetURL() const OVERRIDE; |
| - virtual bool GetOutputFilePath(base::FilePath* local_file_path) OVERRIDE; |
| + virtual void GetOutputFilePath( |
| + base::FilePath* local_file_path, |
| + GetContentCallback* get_content_callback) OVERRIDE; |
| virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
| virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; |
| // net::URLFetcherDelegate overrides. |
| virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| int64 current, int64 total) OVERRIDE; |
| - virtual bool ShouldSendDownloadData() OVERRIDE; |
| - virtual void OnURLFetchDownloadData( |
| - const net::URLFetcher* source, |
| - scoped_ptr<std::string> download_data) OVERRIDE; |
| private: |
| const DownloadActionCallback download_action_callback_; |