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 #include "google_apis/drive/base_requests.h" | 5 #include "google_apis/drive/base_requests.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 const std::string& json, | 100 const std::string& json, |
101 const ParseJsonCallback& callback) { | 101 const ParseJsonCallback& callback) { |
102 base::PostTaskAndReplyWithResult( | 102 base::PostTaskAndReplyWithResult( |
103 blocking_task_runner, | 103 blocking_task_runner, |
104 FROM_HERE, | 104 FROM_HERE, |
105 base::Bind(&ParseJsonInternal, json), | 105 base::Bind(&ParseJsonInternal, json), |
106 callback); | 106 callback); |
107 } | 107 } |
108 | 108 |
109 //=========================== ResponseWriter ================================== | 109 //=========================== ResponseWriter ================================== |
110 ResponseWriter::ResponseWriter(net::URLFetcher* url_fetcher, | 110 ResponseWriter::ResponseWriter(base::SequencedTaskRunner* file_task_runner, |
111 base::SequencedTaskRunner* file_task_runner, | |
112 const base::FilePath& file_path, | 111 const base::FilePath& file_path, |
113 const GetContentCallback& get_content_callback) | 112 const GetContentCallback& get_content_callback) |
114 : url_fetcher_(url_fetcher), | 113 : get_content_callback_(get_content_callback), |
115 get_content_callback_(get_content_callback) { | 114 weak_ptr_factory_(this) { |
116 if (!file_path.empty()) { | 115 if (!file_path.empty()) { |
117 file_writer_.reset( | 116 file_writer_.reset( |
118 new net::URLFetcherFileWriter(file_task_runner, file_path)); | 117 new net::URLFetcherFileWriter(file_task_runner, file_path)); |
119 } | 118 } |
120 } | 119 } |
121 | 120 |
122 ResponseWriter::~ResponseWriter() { | 121 ResponseWriter::~ResponseWriter() { |
123 } | 122 } |
124 | 123 |
125 void ResponseWriter::DisownFile() { | 124 void ResponseWriter::DisownFile() { |
126 DCHECK(file_writer_); | 125 DCHECK(file_writer_); |
127 file_writer_->DisownFile(); | 126 file_writer_->DisownFile(); |
128 } | 127 } |
129 | 128 |
130 int ResponseWriter::Initialize(const net::CompletionCallback& callback) { | 129 int ResponseWriter::Initialize(const net::CompletionCallback& callback) { |
131 if (file_writer_) | 130 if (file_writer_) |
132 return file_writer_->Initialize(callback); | 131 return file_writer_->Initialize(callback); |
133 | 132 |
134 data_.clear(); | 133 data_.clear(); |
135 return net::OK; | 134 return net::OK; |
136 } | 135 } |
137 | 136 |
138 int ResponseWriter::Write(net::IOBuffer* buffer, | 137 int ResponseWriter::Write(net::IOBuffer* buffer, |
139 int num_bytes, | 138 int num_bytes, |
140 const net::CompletionCallback& callback) { | 139 const net::CompletionCallback& callback) { |
141 // |get_content_callback_| and |file_writer_| are used only when the response | 140 if (!get_content_callback_.is_null()) { |
142 // code is successful one. | 141 get_content_callback_.Run( |
143 if (IsSuccessfulResponseCode(url_fetcher_->GetResponseCode())) { | 142 HTTP_SUCCESS, |
144 if (!get_content_callback_.is_null()) { | 143 make_scoped_ptr(new std::string(buffer->data(), num_bytes))); |
145 get_content_callback_.Run( | 144 } |
146 HTTP_SUCCESS, | |
147 make_scoped_ptr(new std::string(buffer->data(), num_bytes))); | |
148 } | |
149 | 145 |
150 if (file_writer_) | 146 if (file_writer_) { |
151 return file_writer_->Write(buffer, num_bytes, callback); | 147 const int result = file_writer_->Write( |
| 148 buffer, num_bytes, |
| 149 base::Bind(&ResponseWriter::DidWrite, |
| 150 weak_ptr_factory_.GetWeakPtr(), |
| 151 make_scoped_refptr(buffer), callback)); |
| 152 if (result != net::ERR_IO_PENDING) |
| 153 DidWrite(buffer, net::CompletionCallback(), result); |
| 154 return result; |
152 } | 155 } |
153 | 156 |
154 data_.append(buffer->data(), num_bytes); | 157 data_.append(buffer->data(), num_bytes); |
155 return num_bytes; | 158 return num_bytes; |
156 } | 159 } |
157 | 160 |
158 int ResponseWriter::Finish(const net::CompletionCallback& callback) { | 161 int ResponseWriter::Finish(const net::CompletionCallback& callback) { |
159 if (file_writer_) | 162 if (file_writer_) |
160 return file_writer_->Finish(callback); | 163 return file_writer_->Finish(callback); |
161 | 164 |
162 return net::OK; | 165 return net::OK; |
163 } | 166 } |
164 | 167 |
| 168 void ResponseWriter::DidWrite(scoped_refptr<net::IOBuffer> buffer, |
| 169 const net::CompletionCallback& callback, |
| 170 int result) { |
| 171 if (result > 0) { |
| 172 // Even if file_writer_ is used, append the data to |data_|, so that it can |
| 173 // be used to get error information in case of server side errors. |
| 174 // The size limit is to avoid consuming too much redundant memory. |
| 175 const size_t kMaxStringSize = 1024*1024; |
| 176 if (data_.size() < kMaxStringSize) { |
| 177 data_.append(buffer->data(), std::min(static_cast<size_t>(result), |
| 178 kMaxStringSize - data_.size())); |
| 179 } |
| 180 } |
| 181 |
| 182 if (!callback.is_null()) |
| 183 callback.Run(result); |
| 184 } |
| 185 |
165 //============================ UrlFetchRequestBase =========================== | 186 //============================ UrlFetchRequestBase =========================== |
166 | 187 |
167 UrlFetchRequestBase::UrlFetchRequestBase(RequestSender* sender) | 188 UrlFetchRequestBase::UrlFetchRequestBase(RequestSender* sender) |
168 : re_authenticate_count_(0), | 189 : re_authenticate_count_(0), |
169 sender_(sender), | 190 sender_(sender), |
170 error_code_(GDATA_OTHER_ERROR), | 191 error_code_(GDATA_OTHER_ERROR), |
171 weak_ptr_factory_(this) { | 192 weak_ptr_factory_(this) { |
172 } | 193 } |
173 | 194 |
174 UrlFetchRequestBase::~UrlFetchRequestBase() {} | 195 UrlFetchRequestBase::~UrlFetchRequestBase() {} |
(...skipping 25 matching lines...) Expand all Loading... |
200 // Always set flags to neither send nor save cookies. | 221 // Always set flags to neither send nor save cookies. |
201 url_fetcher_->SetLoadFlags( | 222 url_fetcher_->SetLoadFlags( |
202 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | | 223 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | |
203 net::LOAD_DISABLE_CACHE); | 224 net::LOAD_DISABLE_CACHE); |
204 | 225 |
205 base::FilePath output_file_path; | 226 base::FilePath output_file_path; |
206 GetContentCallback get_content_callback; | 227 GetContentCallback get_content_callback; |
207 GetOutputFilePath(&output_file_path, &get_content_callback); | 228 GetOutputFilePath(&output_file_path, &get_content_callback); |
208 if (!get_content_callback.is_null()) | 229 if (!get_content_callback.is_null()) |
209 get_content_callback = CreateRelayCallback(get_content_callback); | 230 get_content_callback = CreateRelayCallback(get_content_callback); |
210 response_writer_ = new ResponseWriter(url_fetcher_.get(), | 231 response_writer_ = new ResponseWriter(blocking_task_runner(), |
211 blocking_task_runner(), | |
212 output_file_path, | 232 output_file_path, |
213 get_content_callback); | 233 get_content_callback); |
214 url_fetcher_->SaveResponseWithWriter( | 234 url_fetcher_->SaveResponseWithWriter( |
215 scoped_ptr<net::URLFetcherResponseWriter>(response_writer_)); | 235 scoped_ptr<net::URLFetcherResponseWriter>(response_writer_)); |
216 | 236 |
217 // Add request headers. | 237 // Add request headers. |
218 // Note that SetExtraRequestHeaders clears the current headers and sets it | 238 // Note that SetExtraRequestHeaders clears the current headers and sets it |
219 // to the passed-in headers, so calling it for each header will result in | 239 // to the passed-in headers, so calling it for each header will result in |
220 // only the last header being set in request headers. | 240 // only the last header being set in request headers. |
221 if (!custom_user_agent.empty()) | 241 if (!custom_user_agent.empty()) |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 download_action_callback_.Run(code, temp_file); | 790 download_action_callback_.Run(code, temp_file); |
771 OnProcessURLFetchResultsComplete(); | 791 OnProcessURLFetchResultsComplete(); |
772 } | 792 } |
773 | 793 |
774 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 794 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
775 GDataErrorCode code) { | 795 GDataErrorCode code) { |
776 download_action_callback_.Run(code, base::FilePath()); | 796 download_action_callback_.Run(code, base::FilePath()); |
777 } | 797 } |
778 | 798 |
779 } // namespace google_apis | 799 } // namespace google_apis |
OLD | NEW |