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 "chrome/browser/google_apis/base_operations.h" | 5 #include "chrome/browser/google_apis/base_requests.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
11 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/google_apis/operation_runner.h" | 13 #include "chrome/browser/google_apis/operation_runner.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/http/http_byte_range.h" | 17 #include "net/http/http_byte_range.h" |
18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
19 #include "net/http/http_util.h" | 19 #include "net/http/http_util.h" |
20 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
21 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
22 | 22 |
23 using content::BrowserThread; | 23 using content::BrowserThread; |
24 using net::URLFetcher; | 24 using net::URLFetcher; |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 // Template for optional OAuth2 authorization HTTP header. | 28 // Template for optional OAuth2 authorization HTTP header. |
29 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | 29 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
30 // Template for GData API version HTTP header. | 30 // Template for GData API version HTTP header. |
31 const char kGDataVersionHeader[] = "GData-Version: 3.0"; | 31 const char kGDataVersionHeader[] = "GData-Version: 3.0"; |
32 | 32 |
33 // Maximum number of attempts for re-authentication per operation. | 33 // Maximum number of attempts for re-authentication per request. |
34 const int kMaxReAuthenticateAttemptsPerOperation = 1; | 34 const int kMaxReAuthenticateAttemptsPerRequest = 1; |
35 | 35 |
36 // Template for initiate upload of both GData WAPI and Drive API v2. | 36 // Template for initiate upload of both GData WAPI and Drive API v2. |
37 const char kUploadContentType[] = "X-Upload-Content-Type: "; | 37 const char kUploadContentType[] = "X-Upload-Content-Type: "; |
38 const char kUploadContentLength[] = "X-Upload-Content-Length: "; | 38 const char kUploadContentLength[] = "X-Upload-Content-Length: "; |
39 const char kUploadResponseLocation[] = "location"; | 39 const char kUploadResponseLocation[] = "location"; |
40 | 40 |
41 // Template for upload data range of both GData WAPI and Drive API v2. | 41 // Template for upload data range of both GData WAPI and Drive API v2. |
42 const char kUploadContentRange[] = "Content-Range: bytes "; | 42 const char kUploadContentRange[] = "Content-Range: bytes "; |
43 const char kUploadResponseRange[] = "range"; | 43 const char kUploadResponseRange[] = "range"; |
44 | 44 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 namespace google_apis { | 83 namespace google_apis { |
84 | 84 |
85 void ParseJson(const std::string& json, const ParseJsonCallback& callback) { | 85 void ParseJson(const std::string& json, const ParseJsonCallback& callback) { |
86 base::PostTaskAndReplyWithResult( | 86 base::PostTaskAndReplyWithResult( |
87 BrowserThread::GetBlockingPool(), | 87 BrowserThread::GetBlockingPool(), |
88 FROM_HERE, | 88 FROM_HERE, |
89 base::Bind(&ParseJsonOnBlockingPool, json), | 89 base::Bind(&ParseJsonOnBlockingPool, json), |
90 callback); | 90 callback); |
91 } | 91 } |
92 | 92 |
93 //============================ UrlFetchOperationBase =========================== | 93 //============================ UrlFetchRequestBase =========================== |
94 | 94 |
95 UrlFetchOperationBase::UrlFetchOperationBase( | 95 UrlFetchRequestBase::UrlFetchRequestBase( |
96 OperationRunner* runner, | 96 OperationRunner* runner, |
97 net::URLRequestContextGetter* url_request_context_getter) | 97 net::URLRequestContextGetter* url_request_context_getter) |
98 : OperationRegistry::Operation(runner->operation_registry()), | 98 : OperationRegistry::Operation(runner->operation_registry()), |
99 url_request_context_getter_(url_request_context_getter), | 99 url_request_context_getter_(url_request_context_getter), |
100 re_authenticate_count_(0), | 100 re_authenticate_count_(0), |
101 started_(false), | 101 started_(false), |
102 save_temp_file_(false), | 102 save_temp_file_(false), |
103 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
105 } | 105 } |
106 | 106 |
107 UrlFetchOperationBase::UrlFetchOperationBase( | 107 UrlFetchRequestBase::UrlFetchRequestBase( |
108 OperationRunner* runner, | 108 OperationRunner* runner, |
109 net::URLRequestContextGetter* url_request_context_getter, | 109 net::URLRequestContextGetter* url_request_context_getter, |
110 const base::FilePath& path) | 110 const base::FilePath& path) |
111 : OperationRegistry::Operation(runner->operation_registry(), path), | 111 : OperationRegistry::Operation(runner->operation_registry(), path), |
112 url_request_context_getter_(url_request_context_getter), | 112 url_request_context_getter_(url_request_context_getter), |
113 re_authenticate_count_(0), | 113 re_authenticate_count_(0), |
114 started_(false), | 114 started_(false), |
115 save_temp_file_(false), | 115 save_temp_file_(false), |
116 weak_ptr_factory_(this) { | 116 weak_ptr_factory_(this) { |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
118 } | 118 } |
119 | 119 |
120 UrlFetchOperationBase::~UrlFetchOperationBase() {} | 120 UrlFetchRequestBase::~UrlFetchRequestBase() {} |
121 | 121 |
122 void UrlFetchOperationBase::Start(const std::string& access_token, | 122 void UrlFetchRequestBase::Start(const std::string& access_token, |
123 const std::string& custom_user_agent, | 123 const std::string& custom_user_agent, |
124 const ReAuthenticateCallback& callback) { | 124 const ReAuthenticateCallback& callback) { |
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
126 DCHECK(url_request_context_getter_); | 126 DCHECK(url_request_context_getter_); |
127 DCHECK(!access_token.empty()); | 127 DCHECK(!access_token.empty()); |
128 DCHECK(!callback.is_null()); | 128 DCHECK(!callback.is_null()); |
129 DCHECK(re_authenticate_callback_.is_null()); | 129 DCHECK(re_authenticate_callback_.is_null()); |
130 | 130 |
131 re_authenticate_callback_ = callback; | 131 re_authenticate_callback_ = callback; |
132 | 132 |
133 GURL url = GetURL(); | 133 GURL url = GetURL(); |
134 if (url.is_empty()) { | 134 if (url.is_empty()) { |
(...skipping 10 matching lines...) Expand all Loading... |
145 URLFetcher::Create(url, request_type, this)); | 145 URLFetcher::Create(url, request_type, this)); |
146 url_fetcher_->SetRequestContext(url_request_context_getter_); | 146 url_fetcher_->SetRequestContext(url_request_context_getter_); |
147 // Always set flags to neither send nor save cookies. | 147 // Always set flags to neither send nor save cookies. |
148 url_fetcher_->SetLoadFlags( | 148 url_fetcher_->SetLoadFlags( |
149 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | | 149 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | |
150 net::LOAD_DISABLE_CACHE); | 150 net::LOAD_DISABLE_CACHE); |
151 if (save_temp_file_) { | 151 if (save_temp_file_) { |
152 url_fetcher_->SaveResponseToTemporaryFile( | 152 url_fetcher_->SaveResponseToTemporaryFile( |
153 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 153 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
154 } else if (!output_file_path_.empty()) { | 154 } else if (!output_file_path_.empty()) { |
155 url_fetcher_->SaveResponseToFileAtPath(output_file_path_, | 155 url_fetcher_->SaveResponseToFileAtPath( |
| 156 output_file_path_, |
156 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 157 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
157 } | 158 } |
158 | 159 |
159 // Add request headers. | 160 // Add request headers. |
160 // Note that SetExtraRequestHeaders clears the current headers and sets it | 161 // Note that SetExtraRequestHeaders clears the current headers and sets it |
161 // to the passed-in headers, so calling it for each header will result in | 162 // to the passed-in headers, so calling it for each header will result in |
162 // only the last header being set in request headers. | 163 // only the last header being set in request headers. |
163 if (!custom_user_agent.empty()) | 164 if (!custom_user_agent.empty()) |
164 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); | 165 url_fetcher_->AddExtraRequestHeader("User-Agent: " + custom_user_agent); |
165 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); | 166 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); |
166 url_fetcher_->AddExtraRequestHeader( | 167 url_fetcher_->AddExtraRequestHeader( |
167 base::StringPrintf(kAuthorizationHeaderFormat, access_token.data())); | 168 base::StringPrintf(kAuthorizationHeaderFormat, access_token.data())); |
168 std::vector<std::string> headers = GetExtraRequestHeaders(); | 169 std::vector<std::string> headers = GetExtraRequestHeaders(); |
169 for (size_t i = 0; i < headers.size(); ++i) { | 170 for (size_t i = 0; i < headers.size(); ++i) { |
170 url_fetcher_->AddExtraRequestHeader(headers[i]); | 171 url_fetcher_->AddExtraRequestHeader(headers[i]); |
171 DVLOG(1) << "Extra header: " << headers[i]; | 172 DVLOG(1) << "Extra header: " << headers[i]; |
172 } | 173 } |
173 | 174 |
174 // Set upload data if available. | 175 // Set upload data if available. |
175 std::string upload_content_type; | 176 std::string upload_content_type; |
176 std::string upload_content; | 177 std::string upload_content; |
177 if (GetContentData(&upload_content_type, &upload_content)) { | 178 if (GetContentData(&upload_content_type, &upload_content)) { |
(...skipping 25 matching lines...) Expand all Loading... |
203 } | 204 } |
204 } | 205 } |
205 | 206 |
206 // Register to operation registry. | 207 // Register to operation registry. |
207 NotifyStartToOperationRegistry(); | 208 NotifyStartToOperationRegistry(); |
208 | 209 |
209 url_fetcher_->Start(); | 210 url_fetcher_->Start(); |
210 started_ = true; | 211 started_ = true; |
211 } | 212 } |
212 | 213 |
213 URLFetcher::RequestType UrlFetchOperationBase::GetRequestType() const { | 214 URLFetcher::RequestType UrlFetchRequestBase::GetRequestType() const { |
214 return URLFetcher::GET; | 215 return URLFetcher::GET; |
215 } | 216 } |
216 | 217 |
217 std::vector<std::string> UrlFetchOperationBase::GetExtraRequestHeaders() const { | 218 std::vector<std::string> UrlFetchRequestBase::GetExtraRequestHeaders() const { |
218 return std::vector<std::string>(); | 219 return std::vector<std::string>(); |
219 } | 220 } |
220 | 221 |
221 bool UrlFetchOperationBase::GetContentData(std::string* upload_content_type, | 222 bool UrlFetchRequestBase::GetContentData(std::string* upload_content_type, |
222 std::string* upload_content) { | 223 std::string* upload_content) { |
223 return false; | 224 return false; |
224 } | 225 } |
225 | 226 |
226 bool UrlFetchOperationBase::GetContentFile(base::FilePath* local_file_path, | 227 bool UrlFetchRequestBase::GetContentFile(base::FilePath* local_file_path, |
227 int64* range_offset, | 228 int64* range_offset, |
228 int64* range_length, | 229 int64* range_length, |
229 std::string* upload_content_type) { | 230 std::string* upload_content_type) { |
230 return false; | 231 return false; |
231 } | 232 } |
232 | 233 |
233 void UrlFetchOperationBase::DoCancel() { | 234 void UrlFetchRequestBase::DoCancel() { |
234 url_fetcher_.reset(NULL); | 235 url_fetcher_.reset(NULL); |
235 RunCallbackOnPrematureFailure(GDATA_CANCELLED); | 236 RunCallbackOnPrematureFailure(GDATA_CANCELLED); |
236 } | 237 } |
237 | 238 |
238 // static | 239 // static |
239 GDataErrorCode UrlFetchOperationBase::GetErrorCode(const URLFetcher* source) { | 240 GDataErrorCode UrlFetchRequestBase::GetErrorCode(const URLFetcher* source) { |
240 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 241 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
241 if (!source->GetStatus().is_success()) { | 242 if (!source->GetStatus().is_success()) { |
242 switch (source->GetStatus().error()) { | 243 switch (source->GetStatus().error()) { |
243 case net::ERR_NETWORK_CHANGED: | 244 case net::ERR_NETWORK_CHANGED: |
244 code = GDATA_NO_CONNECTION; | 245 code = GDATA_NO_CONNECTION; |
245 break; | 246 break; |
246 default: | 247 default: |
247 code = GDATA_OTHER_ERROR; | 248 code = GDATA_OTHER_ERROR; |
248 } | 249 } |
249 } | 250 } |
250 return code; | 251 return code; |
251 } | 252 } |
252 | 253 |
253 void UrlFetchOperationBase::OnProcessURLFetchResultsComplete(bool result) { | 254 void UrlFetchRequestBase::OnProcessURLFetchResultsComplete(bool result) { |
254 if (result) | 255 if (result) |
255 NotifySuccessToOperationRegistry(); | 256 NotifySuccessToOperationRegistry(); |
256 else | 257 else |
257 NotifyFinish(OPERATION_FAILED); | 258 NotifyFinish(OPERATION_FAILED); |
258 } | 259 } |
259 | 260 |
260 void UrlFetchOperationBase::OnURLFetchComplete(const URLFetcher* source) { | 261 void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) { |
261 GDataErrorCode code = GetErrorCode(source); | 262 GDataErrorCode code = GetErrorCode(source); |
262 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); | 263 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); |
263 | 264 |
264 if (code == HTTP_UNAUTHORIZED) { | 265 if (code == HTTP_UNAUTHORIZED) { |
265 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerOperation) { | 266 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerRequest) { |
266 // Reset re_authenticate_callback_ so Start() can be called again. | 267 // Reset re_authenticate_callback_ so Start() can be called again. |
267 ReAuthenticateCallback callback = re_authenticate_callback_; | 268 ReAuthenticateCallback callback = re_authenticate_callback_; |
268 re_authenticate_callback_.Reset(); | 269 re_authenticate_callback_.Reset(); |
269 callback.Run(this); | 270 callback.Run(this); |
270 return; | 271 return; |
271 } | 272 } |
272 | 273 |
273 OnAuthFailed(code); | 274 OnAuthFailed(code); |
274 return; | 275 return; |
275 } | 276 } |
276 | 277 |
277 // Overridden by each specialization | 278 // Overridden by each specialization |
278 ProcessURLFetchResults(source); | 279 ProcessURLFetchResults(source); |
279 } | 280 } |
280 | 281 |
281 void UrlFetchOperationBase::NotifySuccessToOperationRegistry() { | 282 void UrlFetchRequestBase::NotifySuccessToOperationRegistry() { |
282 NotifyFinish(OPERATION_COMPLETED); | 283 NotifyFinish(OPERATION_COMPLETED); |
283 } | 284 } |
284 | 285 |
285 void UrlFetchOperationBase::NotifyStartToOperationRegistry() { | 286 void UrlFetchRequestBase::NotifyStartToOperationRegistry() { |
286 NotifyStart(); | 287 NotifyStart(); |
287 } | 288 } |
288 | 289 |
289 void UrlFetchOperationBase::OnAuthFailed(GDataErrorCode code) { | 290 void UrlFetchRequestBase::OnAuthFailed(GDataErrorCode code) { |
290 RunCallbackOnPrematureFailure(code); | 291 RunCallbackOnPrematureFailure(code); |
291 | 292 |
292 // Check if this failed before we even started fetching. If so, register | 293 // Check if this failed before we even started fetching. If so, register |
293 // for start so we can properly unregister with finish. | 294 // for start so we can properly unregister with finish. |
294 if (!started_) | 295 if (!started_) |
295 NotifyStart(); | 296 NotifyStart(); |
296 | 297 |
297 // Note: NotifyFinish() must be invoked at the end, after all other callbacks | 298 // Note: NotifyFinish() must be invoked at the end, after all other callbacks |
298 // and notifications. Once NotifyFinish() is called, the current instance of | 299 // and notifications. Once NotifyFinish() is called, the current instance of |
299 // gdata operation will be deleted from the OperationRegistry and become | 300 // request will be deleted from the OperationRegistry and become invalid. |
300 // invalid. | |
301 NotifyFinish(OPERATION_FAILED); | 301 NotifyFinish(OPERATION_FAILED); |
302 } | 302 } |
303 | 303 |
304 base::WeakPtr<AuthenticatedOperationInterface> | 304 base::WeakPtr<AuthenticatedRequestInterface> |
305 UrlFetchOperationBase::GetWeakPtr() { | 305 UrlFetchRequestBase::GetWeakPtr() { |
306 return weak_ptr_factory_.GetWeakPtr(); | 306 return weak_ptr_factory_.GetWeakPtr(); |
307 } | 307 } |
308 | 308 |
309 //============================ EntryActionOperation ============================ | 309 //============================ EntryActionRequest ============================ |
310 | 310 |
311 EntryActionOperation::EntryActionOperation( | 311 EntryActionRequest::EntryActionRequest( |
312 OperationRunner* runner, | 312 OperationRunner* runner, |
313 net::URLRequestContextGetter* url_request_context_getter, | 313 net::URLRequestContextGetter* url_request_context_getter, |
314 const EntryActionCallback& callback) | 314 const EntryActionCallback& callback) |
315 : UrlFetchOperationBase(runner, url_request_context_getter), | 315 : UrlFetchRequestBase(runner, url_request_context_getter), |
316 callback_(callback) { | 316 callback_(callback) { |
317 DCHECK(!callback_.is_null()); | 317 DCHECK(!callback_.is_null()); |
318 } | 318 } |
319 | 319 |
320 EntryActionOperation::~EntryActionOperation() {} | 320 EntryActionRequest::~EntryActionRequest() {} |
321 | 321 |
322 void EntryActionOperation::ProcessURLFetchResults(const URLFetcher* source) { | 322 void EntryActionRequest::ProcessURLFetchResults(const URLFetcher* source) { |
323 GDataErrorCode code = GetErrorCode(source); | 323 GDataErrorCode code = GetErrorCode(source); |
324 callback_.Run(code); | 324 callback_.Run(code); |
325 const bool success = true; | 325 const bool success = true; |
326 OnProcessURLFetchResultsComplete(success); | 326 OnProcessURLFetchResultsComplete(success); |
327 } | 327 } |
328 | 328 |
329 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 329 void EntryActionRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
330 callback_.Run(code); | 330 callback_.Run(code); |
331 } | 331 } |
332 | 332 |
333 //============================== GetDataOperation ============================== | 333 //============================== GetDataRequest ============================== |
334 | 334 |
335 GetDataOperation::GetDataOperation( | 335 GetDataRequest::GetDataRequest( |
336 OperationRunner* runner, | 336 OperationRunner* runner, |
337 net::URLRequestContextGetter* url_request_context_getter, | 337 net::URLRequestContextGetter* url_request_context_getter, |
338 const GetDataCallback& callback) | 338 const GetDataCallback& callback) |
339 : UrlFetchOperationBase(runner, url_request_context_getter), | 339 : UrlFetchRequestBase(runner, url_request_context_getter), |
340 callback_(callback), | 340 callback_(callback), |
341 weak_ptr_factory_(this) { | 341 weak_ptr_factory_(this) { |
342 DCHECK(!callback_.is_null()); | 342 DCHECK(!callback_.is_null()); |
343 } | 343 } |
344 | 344 |
345 GetDataOperation::~GetDataOperation() {} | 345 GetDataRequest::~GetDataRequest() {} |
346 | 346 |
347 void GetDataOperation::ParseResponse(GDataErrorCode fetch_error_code, | 347 void GetDataRequest::ParseResponse(GDataErrorCode fetch_error_code, |
348 const std::string& data) { | 348 const std::string& data) { |
349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
350 | 350 |
351 VLOG(1) << "JSON received from " << GetURL().spec() << ": " | 351 VLOG(1) << "JSON received from " << GetURL().spec() << ": " |
352 << data.size() << " bytes"; | 352 << data.size() << " bytes"; |
353 ParseJson(data, | 353 ParseJson(data, |
354 base::Bind(&GetDataOperation::OnDataParsed, | 354 base::Bind(&GetDataRequest::OnDataParsed, |
355 weak_ptr_factory_.GetWeakPtr(), | 355 weak_ptr_factory_.GetWeakPtr(), |
356 fetch_error_code)); | 356 fetch_error_code)); |
357 } | 357 } |
358 | 358 |
359 void GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { | 359 void GetDataRequest::ProcessURLFetchResults(const URLFetcher* source) { |
360 std::string data; | 360 std::string data; |
361 source->GetResponseAsString(&data); | 361 source->GetResponseAsString(&data); |
362 scoped_ptr<base::Value> root_value; | 362 scoped_ptr<base::Value> root_value; |
363 GDataErrorCode fetch_error_code = GetErrorCode(source); | 363 GDataErrorCode fetch_error_code = GetErrorCode(source); |
364 | 364 |
365 switch (fetch_error_code) { | 365 switch (fetch_error_code) { |
366 case HTTP_SUCCESS: | 366 case HTTP_SUCCESS: |
367 case HTTP_CREATED: | 367 case HTTP_CREATED: |
368 ParseResponse(fetch_error_code, data); | 368 ParseResponse(fetch_error_code, data); |
369 break; | 369 break; |
370 default: | 370 default: |
371 RunCallbackOnPrematureFailure(fetch_error_code); | 371 RunCallbackOnPrematureFailure(fetch_error_code); |
372 const bool success = false; | 372 const bool success = false; |
373 OnProcessURLFetchResultsComplete(success); | 373 OnProcessURLFetchResultsComplete(success); |
374 break; | 374 break; |
375 } | 375 } |
376 } | 376 } |
377 | 377 |
378 void GetDataOperation::RunCallbackOnPrematureFailure( | 378 void GetDataRequest::RunCallbackOnPrematureFailure( |
379 GDataErrorCode fetch_error_code) { | 379 GDataErrorCode fetch_error_code) { |
380 callback_.Run(fetch_error_code, scoped_ptr<base::Value>()); | 380 callback_.Run(fetch_error_code, scoped_ptr<base::Value>()); |
381 } | 381 } |
382 | 382 |
383 void GetDataOperation::OnDataParsed( | 383 void GetDataRequest::OnDataParsed( |
384 GDataErrorCode fetch_error_code, | 384 GDataErrorCode fetch_error_code, |
385 scoped_ptr<base::Value> value) { | 385 scoped_ptr<base::Value> value) { |
386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
387 | 387 |
388 bool success = true; | 388 bool success = true; |
389 if (!value.get()) { | 389 if (!value.get()) { |
390 fetch_error_code = GDATA_PARSE_ERROR; | 390 fetch_error_code = GDATA_PARSE_ERROR; |
391 success = false; | 391 success = false; |
392 } | 392 } |
393 | 393 |
394 RunCallbackOnSuccess(fetch_error_code, value.Pass()); | 394 RunCallbackOnSuccess(fetch_error_code, value.Pass()); |
395 | 395 |
396 DCHECK(!value.get()); | 396 DCHECK(!value.get()); |
397 OnProcessURLFetchResultsComplete(success); | 397 OnProcessURLFetchResultsComplete(success); |
398 } | 398 } |
399 | 399 |
400 void GetDataOperation::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, | 400 void GetDataRequest::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, |
401 scoped_ptr<base::Value> value) { | 401 scoped_ptr<base::Value> value) { |
402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
403 callback_.Run(fetch_error_code, value.Pass()); | 403 callback_.Run(fetch_error_code, value.Pass()); |
404 } | 404 } |
405 | 405 |
406 //========================= InitiateUploadOperationBase ======================== | 406 //========================= InitiateUploadRequestBase ======================== |
407 | 407 |
408 InitiateUploadOperationBase::InitiateUploadOperationBase( | 408 InitiateUploadRequestBase::InitiateUploadRequestBase( |
409 OperationRunner* runner, | 409 OperationRunner* runner, |
410 net::URLRequestContextGetter* url_request_context_getter, | 410 net::URLRequestContextGetter* url_request_context_getter, |
411 const InitiateUploadCallback& callback, | 411 const InitiateUploadCallback& callback, |
412 const base::FilePath& drive_file_path, | 412 const base::FilePath& drive_file_path, |
413 const std::string& content_type, | 413 const std::string& content_type, |
414 int64 content_length) | 414 int64 content_length) |
415 : UrlFetchOperationBase(runner, | 415 : UrlFetchRequestBase(runner, |
416 url_request_context_getter, | 416 url_request_context_getter, |
417 drive_file_path), | 417 drive_file_path), |
418 callback_(callback), | 418 callback_(callback), |
419 drive_file_path_(drive_file_path), | 419 drive_file_path_(drive_file_path), |
420 content_type_(content_type), | 420 content_type_(content_type), |
421 content_length_(content_length) { | 421 content_length_(content_length) { |
422 DCHECK(!callback_.is_null()); | 422 DCHECK(!callback_.is_null()); |
423 DCHECK(!content_type_.empty()); | 423 DCHECK(!content_type_.empty()); |
424 DCHECK_GE(content_length_, 0); | 424 DCHECK_GE(content_length_, 0); |
425 } | 425 } |
426 | 426 |
427 InitiateUploadOperationBase::~InitiateUploadOperationBase() {} | 427 InitiateUploadRequestBase::~InitiateUploadRequestBase() {} |
428 | 428 |
429 void InitiateUploadOperationBase::ProcessURLFetchResults( | 429 void InitiateUploadRequestBase::ProcessURLFetchResults( |
430 const URLFetcher* source) { | 430 const URLFetcher* source) { |
431 GDataErrorCode code = GetErrorCode(source); | 431 GDataErrorCode code = GetErrorCode(source); |
432 | 432 |
433 std::string upload_location; | 433 std::string upload_location; |
434 if (code == HTTP_SUCCESS) { | 434 if (code == HTTP_SUCCESS) { |
435 // Retrieve value of the first "Location" header. | 435 // Retrieve value of the first "Location" header. |
436 source->GetResponseHeaders()->EnumerateHeader(NULL, | 436 source->GetResponseHeaders()->EnumerateHeader(NULL, |
437 kUploadResponseLocation, | 437 kUploadResponseLocation, |
438 &upload_location); | 438 &upload_location); |
439 } | 439 } |
440 VLOG(1) << "Got response for [" << drive_file_path_.value() | 440 VLOG(1) << "Got response for [" << drive_file_path_.value() |
441 << "]: code=" << code | 441 << "]: code=" << code |
442 << ", location=[" << upload_location << "]"; | 442 << ", location=[" << upload_location << "]"; |
443 | 443 |
444 callback_.Run(code, GURL(upload_location)); | 444 callback_.Run(code, GURL(upload_location)); |
445 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 445 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
446 } | 446 } |
447 | 447 |
448 void InitiateUploadOperationBase::NotifySuccessToOperationRegistry() { | 448 void InitiateUploadRequestBase::NotifySuccessToOperationRegistry() { |
449 NotifySuspend(); | 449 NotifySuspend(); |
450 } | 450 } |
451 | 451 |
452 void InitiateUploadOperationBase::RunCallbackOnPrematureFailure( | 452 void InitiateUploadRequestBase::RunCallbackOnPrematureFailure( |
453 GDataErrorCode code) { | 453 GDataErrorCode code) { |
454 callback_.Run(code, GURL()); | 454 callback_.Run(code, GURL()); |
455 } | 455 } |
456 | 456 |
457 std::vector<std::string> | 457 std::vector<std::string> |
458 InitiateUploadOperationBase::GetExtraRequestHeaders() const { | 458 InitiateUploadRequestBase::GetExtraRequestHeaders() const { |
459 std::vector<std::string> headers; | 459 std::vector<std::string> headers; |
460 headers.push_back(kUploadContentType + content_type_); | 460 headers.push_back(kUploadContentType + content_type_); |
461 headers.push_back( | 461 headers.push_back( |
462 kUploadContentLength + base::Int64ToString(content_length_)); | 462 kUploadContentLength + base::Int64ToString(content_length_)); |
463 return headers; | 463 return headers; |
464 } | 464 } |
465 | 465 |
466 //============================ UploadRangeResponse ============================= | 466 //============================ UploadRangeResponse ============================= |
467 | 467 |
468 UploadRangeResponse::UploadRangeResponse() | 468 UploadRangeResponse::UploadRangeResponse() |
469 : code(HTTP_SUCCESS), | 469 : code(HTTP_SUCCESS), |
470 start_position_received(0), | 470 start_position_received(0), |
471 end_position_received(0) { | 471 end_position_received(0) { |
472 } | 472 } |
473 | 473 |
474 UploadRangeResponse::UploadRangeResponse(GDataErrorCode code, | 474 UploadRangeResponse::UploadRangeResponse(GDataErrorCode code, |
475 int64 start_position_received, | 475 int64 start_position_received, |
476 int64 end_position_received) | 476 int64 end_position_received) |
477 : code(code), | 477 : code(code), |
478 start_position_received(start_position_received), | 478 start_position_received(start_position_received), |
479 end_position_received(end_position_received) { | 479 end_position_received(end_position_received) { |
480 } | 480 } |
481 | 481 |
482 UploadRangeResponse::~UploadRangeResponse() { | 482 UploadRangeResponse::~UploadRangeResponse() { |
483 } | 483 } |
484 | 484 |
485 //========================== UploadRangeOperationBase ========================== | 485 //========================== UploadRangeRequestBase ========================== |
486 | 486 |
487 UploadRangeOperationBase::UploadRangeOperationBase( | 487 UploadRangeRequestBase::UploadRangeRequestBase( |
488 OperationRunner* runner, | 488 OperationRunner* runner, |
489 net::URLRequestContextGetter* url_request_context_getter, | 489 net::URLRequestContextGetter* url_request_context_getter, |
490 const base::FilePath& drive_file_path, | 490 const base::FilePath& drive_file_path, |
491 const GURL& upload_url) | 491 const GURL& upload_url) |
492 : UrlFetchOperationBase(runner, | 492 : UrlFetchRequestBase(runner, |
493 url_request_context_getter, | 493 url_request_context_getter, |
494 drive_file_path), | 494 drive_file_path), |
495 drive_file_path_(drive_file_path), | 495 drive_file_path_(drive_file_path), |
496 upload_url_(upload_url), | 496 upload_url_(upload_url), |
497 last_chunk_completed_(false), | 497 last_chunk_completed_(false), |
498 weak_ptr_factory_(this) { | 498 weak_ptr_factory_(this) { |
499 } | 499 } |
500 | 500 |
501 UploadRangeOperationBase::~UploadRangeOperationBase() {} | 501 UploadRangeRequestBase::~UploadRangeRequestBase() {} |
502 | 502 |
503 GURL UploadRangeOperationBase::GetURL() const { | 503 GURL UploadRangeRequestBase::GetURL() const { |
504 // This is very tricky to get json from this operation. To do that, &alt=json | 504 // This is very tricky to get json from this request. To do that, &alt=json |
505 // has to be appended not here but in InitiateUploadOperation::GetURL(). | 505 // has to be appended not here but in InitiateUploadRequestBase::GetURL(). |
506 return upload_url_; | 506 return upload_url_; |
507 } | 507 } |
508 | 508 |
509 URLFetcher::RequestType UploadRangeOperationBase::GetRequestType() const { | 509 URLFetcher::RequestType UploadRangeRequestBase::GetRequestType() const { |
510 return URLFetcher::PUT; | 510 return URLFetcher::PUT; |
511 } | 511 } |
512 | 512 |
513 void UploadRangeOperationBase::ProcessURLFetchResults( | 513 void UploadRangeRequestBase::ProcessURLFetchResults( |
514 const URLFetcher* source) { | 514 const URLFetcher* source) { |
515 GDataErrorCode code = GetErrorCode(source); | 515 GDataErrorCode code = GetErrorCode(source); |
516 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); | 516 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); |
517 | 517 |
518 if (code == HTTP_RESUME_INCOMPLETE) { | 518 if (code == HTTP_RESUME_INCOMPLETE) { |
519 // Retrieve value of the first "Range" header. | 519 // Retrieve value of the first "Range" header. |
520 // The Range header is appeared only if there is at least one received | 520 // The Range header is appeared only if there is at least one received |
521 // byte. So, initialize the positions by 0 so that the [0,0) will be | 521 // byte. So, initialize the positions by 0 so that the [0,0) will be |
522 // returned via the |callback_| for empty data case. | 522 // returned via the |callback_| for empty data case. |
523 int64 start_position_received = 0; | 523 int64 start_position_received = 0; |
(...skipping 15 matching lines...) Expand all Loading... |
539 } | 539 } |
540 // The Range header has the received data range, so the start position | 540 // The Range header has the received data range, so the start position |
541 // should be always 0. | 541 // should be always 0. |
542 DCHECK_EQ(start_position_received, 0); | 542 DCHECK_EQ(start_position_received, 0); |
543 DVLOG(1) << "Got response for [" << drive_file_path_.value() | 543 DVLOG(1) << "Got response for [" << drive_file_path_.value() |
544 << "]: code=" << code | 544 << "]: code=" << code |
545 << ", range_hdr=[" << range_received | 545 << ", range_hdr=[" << range_received |
546 << "], range_parsed=" << start_position_received | 546 << "], range_parsed=" << start_position_received |
547 << "," << end_position_received; | 547 << "," << end_position_received; |
548 | 548 |
549 OnRangeOperationComplete(UploadRangeResponse(code, | 549 OnRangeRequestComplete(UploadRangeResponse(code, |
550 start_position_received, | 550 start_position_received, |
551 end_position_received), | 551 end_position_received), |
552 scoped_ptr<base::Value>()); | 552 scoped_ptr<base::Value>()); |
553 | 553 |
554 OnProcessURLFetchResultsComplete(true); | 554 OnProcessURLFetchResultsComplete(true); |
555 } else { | 555 } else { |
556 // There might be explanation of unexpected error code in response. | 556 // There might be explanation of unexpected error code in response. |
557 std::string response_content; | 557 std::string response_content; |
558 source->GetResponseAsString(&response_content); | 558 source->GetResponseAsString(&response_content); |
559 DVLOG(1) << "Got response for [" << drive_file_path_.value() | 559 DVLOG(1) << "Got response for [" << drive_file_path_.value() |
560 << "]: code=" << code | 560 << "]: code=" << code |
561 << ", content=[\n" << response_content << "\n]"; | 561 << ", content=[\n" << response_content << "\n]"; |
562 | 562 |
563 ParseJson(response_content, | 563 ParseJson(response_content, |
564 base::Bind(&UploadRangeOperationBase::OnDataParsed, | 564 base::Bind(&UploadRangeRequestBase::OnDataParsed, |
565 weak_ptr_factory_.GetWeakPtr(), | 565 weak_ptr_factory_.GetWeakPtr(), |
566 code)); | 566 code)); |
567 } | 567 } |
568 } | 568 } |
569 | 569 |
570 void UploadRangeOperationBase::OnDataParsed(GDataErrorCode code, | 570 void UploadRangeRequestBase::OnDataParsed(GDataErrorCode code, |
571 scoped_ptr<base::Value> value) { | 571 scoped_ptr<base::Value> value) { |
572 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 572 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
573 | 573 |
574 // For a new file, HTTP_CREATED is returned. | 574 // For a new file, HTTP_CREATED is returned. |
575 // For an existing file, HTTP_SUCCESS is returned. | 575 // For an existing file, HTTP_SUCCESS is returned. |
576 if (code == HTTP_CREATED || code == HTTP_SUCCESS) | 576 if (code == HTTP_CREATED || code == HTTP_SUCCESS) |
577 last_chunk_completed_ = true; | 577 last_chunk_completed_ = true; |
578 | 578 |
579 OnRangeOperationComplete(UploadRangeResponse(code, -1, -1), value.Pass()); | 579 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), value.Pass()); |
580 OnProcessURLFetchResultsComplete(last_chunk_completed_); | 580 OnProcessURLFetchResultsComplete(last_chunk_completed_); |
581 } | 581 } |
582 | 582 |
583 void UploadRangeOperationBase::NotifySuccessToOperationRegistry() { | 583 void UploadRangeRequestBase::NotifySuccessToOperationRegistry() { |
584 if (last_chunk_completed_) | 584 if (last_chunk_completed_) |
585 NotifyFinish(OPERATION_COMPLETED); | 585 NotifyFinish(OPERATION_COMPLETED); |
586 else | 586 else |
587 NotifySuspend(); | 587 NotifySuspend(); |
588 } | 588 } |
589 | 589 |
590 void UploadRangeOperationBase::RunCallbackOnPrematureFailure( | 590 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( |
591 GDataErrorCode code) { | 591 GDataErrorCode code) { |
592 OnRangeOperationComplete( | 592 OnRangeRequestComplete( |
593 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); | 593 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); |
594 } | 594 } |
595 | 595 |
596 //========================== ResumeUploadOperationBase ========================= | 596 //========================== ResumeUploadRequestBase ========================= |
597 | 597 |
598 ResumeUploadOperationBase::ResumeUploadOperationBase( | 598 ResumeUploadRequestBase::ResumeUploadRequestBase( |
599 OperationRunner* runner, | 599 OperationRunner* runner, |
600 net::URLRequestContextGetter* url_request_context_getter, | 600 net::URLRequestContextGetter* url_request_context_getter, |
601 const base::FilePath& drive_file_path, | 601 const base::FilePath& drive_file_path, |
602 const GURL& upload_location, | 602 const GURL& upload_location, |
603 int64 start_position, | 603 int64 start_position, |
604 int64 end_position, | 604 int64 end_position, |
605 int64 content_length, | 605 int64 content_length, |
606 const std::string& content_type, | 606 const std::string& content_type, |
607 const base::FilePath& local_file_path) | 607 const base::FilePath& local_file_path) |
608 : UploadRangeOperationBase(runner, | 608 : UploadRangeRequestBase(runner, |
609 url_request_context_getter, | 609 url_request_context_getter, |
610 drive_file_path, | 610 drive_file_path, |
611 upload_location), | 611 upload_location), |
612 start_position_(start_position), | 612 start_position_(start_position), |
613 end_position_(end_position), | 613 end_position_(end_position), |
614 content_length_(content_length), | 614 content_length_(content_length), |
615 content_type_(content_type), | 615 content_type_(content_type), |
616 local_file_path_(local_file_path) { | 616 local_file_path_(local_file_path) { |
617 DCHECK_LE(start_position_, end_position_); | 617 DCHECK_LE(start_position_, end_position_); |
618 } | 618 } |
619 | 619 |
620 ResumeUploadOperationBase::~ResumeUploadOperationBase() {} | 620 ResumeUploadRequestBase::~ResumeUploadRequestBase() {} |
621 | 621 |
622 std::vector<std::string> | 622 std::vector<std::string> |
623 ResumeUploadOperationBase::GetExtraRequestHeaders() const { | 623 ResumeUploadRequestBase::GetExtraRequestHeaders() const { |
624 if (content_length_ == 0) { | 624 if (content_length_ == 0) { |
625 // For uploading an empty document, just PUT an empty content. | 625 // For uploading an empty document, just PUT an empty content. |
626 DCHECK_EQ(start_position_, 0); | 626 DCHECK_EQ(start_position_, 0); |
627 DCHECK_EQ(end_position_, 0); | 627 DCHECK_EQ(end_position_, 0); |
628 return std::vector<std::string>(); | 628 return std::vector<std::string>(); |
629 } | 629 } |
630 | 630 |
631 // The header looks like | 631 // The header looks like |
632 // Content-Range: bytes <start_position>-<end_position>/<content_length> | 632 // Content-Range: bytes <start_position>-<end_position>/<content_length> |
633 // for example: | 633 // for example: |
634 // Content-Range: bytes 7864320-8388607/13851821 | 634 // Content-Range: bytes 7864320-8388607/13851821 |
635 // The header takes inclusive range, so we adjust by "end_position - 1". | 635 // The header takes inclusive range, so we adjust by "end_position - 1". |
636 DCHECK_GE(start_position_, 0); | 636 DCHECK_GE(start_position_, 0); |
637 DCHECK_GT(end_position_, 0); | 637 DCHECK_GT(end_position_, 0); |
638 DCHECK_GE(content_length_, 0); | 638 DCHECK_GE(content_length_, 0); |
639 | 639 |
640 std::vector<std::string> headers; | 640 std::vector<std::string> headers; |
641 headers.push_back( | 641 headers.push_back( |
642 std::string(kUploadContentRange) + | 642 std::string(kUploadContentRange) + |
643 base::Int64ToString(start_position_) + "-" + | 643 base::Int64ToString(start_position_) + "-" + |
644 base::Int64ToString(end_position_ - 1) + "/" + | 644 base::Int64ToString(end_position_ - 1) + "/" + |
645 base::Int64ToString(content_length_)); | 645 base::Int64ToString(content_length_)); |
646 return headers; | 646 return headers; |
647 } | 647 } |
648 | 648 |
649 bool ResumeUploadOperationBase::GetContentFile( | 649 bool ResumeUploadRequestBase::GetContentFile( |
650 base::FilePath* local_file_path, | 650 base::FilePath* local_file_path, |
651 int64* range_offset, | 651 int64* range_offset, |
652 int64* range_length, | 652 int64* range_length, |
653 std::string* upload_content_type) { | 653 std::string* upload_content_type) { |
654 if (start_position_ == end_position_) { | 654 if (start_position_ == end_position_) { |
655 // No content data. | 655 // No content data. |
656 return false; | 656 return false; |
657 } | 657 } |
658 | 658 |
659 *local_file_path = local_file_path_; | 659 *local_file_path = local_file_path_; |
660 *range_offset = start_position_; | 660 *range_offset = start_position_; |
661 *range_length = end_position_ - start_position_; | 661 *range_length = end_position_ - start_position_; |
662 *upload_content_type = content_type_; | 662 *upload_content_type = content_type_; |
663 return true; | 663 return true; |
664 } | 664 } |
665 | 665 |
666 void ResumeUploadOperationBase::NotifyStartToOperationRegistry() { | 666 void ResumeUploadRequestBase::NotifyStartToOperationRegistry() { |
667 NotifyResume(); | 667 NotifyResume(); |
668 } | 668 } |
669 | 669 |
670 //======================== GetUploadStatusOperationBase ======================== | 670 //======================== GetUploadStatusRequestBase ======================== |
671 | 671 |
672 GetUploadStatusOperationBase::GetUploadStatusOperationBase( | 672 GetUploadStatusRequestBase::GetUploadStatusRequestBase( |
673 OperationRunner* runner, | 673 OperationRunner* runner, |
674 net::URLRequestContextGetter* url_request_context_getter, | 674 net::URLRequestContextGetter* url_request_context_getter, |
675 const base::FilePath& drive_file_path, | 675 const base::FilePath& drive_file_path, |
676 const GURL& upload_url, | 676 const GURL& upload_url, |
677 int64 content_length) | 677 int64 content_length) |
678 : UploadRangeOperationBase(runner, | 678 : UploadRangeRequestBase(runner, |
679 url_request_context_getter, | 679 url_request_context_getter, |
680 drive_file_path, | 680 drive_file_path, |
681 upload_url), | 681 upload_url), |
682 content_length_(content_length) {} | 682 content_length_(content_length) {} |
683 | 683 |
684 GetUploadStatusOperationBase::~GetUploadStatusOperationBase() {} | 684 GetUploadStatusRequestBase::~GetUploadStatusRequestBase() {} |
685 | 685 |
686 std::vector<std::string> | 686 std::vector<std::string> |
687 GetUploadStatusOperationBase::GetExtraRequestHeaders() const { | 687 GetUploadStatusRequestBase::GetExtraRequestHeaders() const { |
688 // The header looks like | 688 // The header looks like |
689 // Content-Range: bytes */<content_length> | 689 // Content-Range: bytes */<content_length> |
690 // for example: | 690 // for example: |
691 // Content-Range: bytes */13851821 | 691 // Content-Range: bytes */13851821 |
692 DCHECK_GE(content_length_, 0); | 692 DCHECK_GE(content_length_, 0); |
693 | 693 |
694 std::vector<std::string> headers; | 694 std::vector<std::string> headers; |
695 headers.push_back( | 695 headers.push_back( |
696 std::string(kUploadContentRange) + "*/" + | 696 std::string(kUploadContentRange) + "*/" + |
697 base::Int64ToString(content_length_)); | 697 base::Int64ToString(content_length_)); |
698 return headers; | 698 return headers; |
699 } | 699 } |
700 | 700 |
701 //============================ DownloadFileOperation =========================== | 701 //============================ DownloadFileRequest =========================== |
702 | 702 |
703 DownloadFileOperation::DownloadFileOperation( | 703 DownloadFileRequest::DownloadFileRequest( |
704 OperationRunner* runner, | 704 OperationRunner* runner, |
705 net::URLRequestContextGetter* url_request_context_getter, | 705 net::URLRequestContextGetter* url_request_context_getter, |
706 const DownloadActionCallback& download_action_callback, | 706 const DownloadActionCallback& download_action_callback, |
707 const GetContentCallback& get_content_callback, | 707 const GetContentCallback& get_content_callback, |
708 const ProgressCallback& progress_callback, | 708 const ProgressCallback& progress_callback, |
709 const GURL& download_url, | 709 const GURL& download_url, |
710 const base::FilePath& drive_file_path, | 710 const base::FilePath& drive_file_path, |
711 const base::FilePath& output_file_path) | 711 const base::FilePath& output_file_path) |
712 : UrlFetchOperationBase(runner, | 712 : UrlFetchRequestBase(runner, |
713 url_request_context_getter, | 713 url_request_context_getter, |
714 drive_file_path), | 714 drive_file_path), |
715 download_action_callback_(download_action_callback), | 715 download_action_callback_(download_action_callback), |
716 get_content_callback_(get_content_callback), | 716 get_content_callback_(get_content_callback), |
717 progress_callback_(progress_callback), | 717 progress_callback_(progress_callback), |
718 download_url_(download_url) { | 718 download_url_(download_url) { |
719 DCHECK(!download_action_callback_.is_null()); | 719 DCHECK(!download_action_callback_.is_null()); |
720 // get_content_callback may be null. | 720 // get_content_callback may be null. |
721 | 721 |
722 // Make sure we download the content into a temp file. | 722 // Make sure we download the content into a temp file. |
723 if (output_file_path.empty()) | 723 if (output_file_path.empty()) |
724 set_save_temp_file(true); | 724 set_save_temp_file(true); |
725 else | 725 else |
726 set_output_file_path(output_file_path); | 726 set_output_file_path(output_file_path); |
727 } | 727 } |
728 | 728 |
729 DownloadFileOperation::~DownloadFileOperation() {} | 729 DownloadFileRequest::~DownloadFileRequest() {} |
730 | 730 |
731 // Overridden from UrlFetchOperationBase. | 731 // Overridden from UrlFetchRequestBase. |
732 GURL DownloadFileOperation::GetURL() const { | 732 GURL DownloadFileRequest::GetURL() const { |
733 return download_url_; | 733 return download_url_; |
734 } | 734 } |
735 | 735 |
736 void DownloadFileOperation::OnURLFetchDownloadProgress(const URLFetcher* source, | 736 void DownloadFileRequest::OnURLFetchDownloadProgress(const URLFetcher* source, |
737 int64 current, | 737 int64 current, |
738 int64 total) { | 738 int64 total) { |
739 if (!progress_callback_.is_null()) | 739 if (!progress_callback_.is_null()) |
740 progress_callback_.Run(current, total); | 740 progress_callback_.Run(current, total); |
741 } | 741 } |
742 | 742 |
743 bool DownloadFileOperation::ShouldSendDownloadData() { | 743 bool DownloadFileRequest::ShouldSendDownloadData() { |
744 return !get_content_callback_.is_null(); | 744 return !get_content_callback_.is_null(); |
745 } | 745 } |
746 | 746 |
747 void DownloadFileOperation::OnURLFetchDownloadData( | 747 void DownloadFileRequest::OnURLFetchDownloadData( |
748 const URLFetcher* source, | 748 const URLFetcher* source, |
749 scoped_ptr<std::string> download_data) { | 749 scoped_ptr<std::string> download_data) { |
750 if (!get_content_callback_.is_null()) | 750 if (!get_content_callback_.is_null()) |
751 get_content_callback_.Run(HTTP_SUCCESS, download_data.Pass()); | 751 get_content_callback_.Run(HTTP_SUCCESS, download_data.Pass()); |
752 } | 752 } |
753 | 753 |
754 void DownloadFileOperation::ProcessURLFetchResults(const URLFetcher* source) { | 754 void DownloadFileRequest::ProcessURLFetchResults(const URLFetcher* source) { |
755 GDataErrorCode code = GetErrorCode(source); | 755 GDataErrorCode code = GetErrorCode(source); |
756 | 756 |
757 // Take over the ownership of the the downloaded temp file. | 757 // Take over the ownership of the the downloaded temp file. |
758 base::FilePath temp_file; | 758 base::FilePath temp_file; |
759 if (code == HTTP_SUCCESS && | 759 if (code == HTTP_SUCCESS && |
760 !source->GetResponseAsFilePath(true, // take_ownership | 760 !source->GetResponseAsFilePath(true, // take_ownership |
761 &temp_file)) { | 761 &temp_file)) { |
762 code = GDATA_FILE_ERROR; | 762 code = GDATA_FILE_ERROR; |
763 } | 763 } |
764 | 764 |
765 download_action_callback_.Run(code, temp_file); | 765 download_action_callback_.Run(code, temp_file); |
766 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 766 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
767 } | 767 } |
768 | 768 |
769 void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 769 void DownloadFileRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
770 download_action_callback_.Run(code, base::FilePath()); | 770 download_action_callback_.Run(code, base::FilePath()); |
771 } | 771 } |
772 | 772 |
773 } // namespace google_apis | 773 } // namespace google_apis |
OLD | NEW |