| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/drive/file_system/download_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/download_operation.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 class DownloadOperation::DownloadParams { | 256 class DownloadOperation::DownloadParams { |
| 257 public: | 257 public: |
| 258 DownloadParams( | 258 DownloadParams( |
| 259 const GetFileContentInitializedCallback initialized_callback, | 259 const GetFileContentInitializedCallback initialized_callback, |
| 260 const google_apis::GetContentCallback get_content_callback, | 260 const google_apis::GetContentCallback get_content_callback, |
| 261 const GetFileCallback completion_callback, | 261 const GetFileCallback completion_callback, |
| 262 scoped_ptr<ResourceEntry> entry) | 262 scoped_ptr<ResourceEntry> entry) |
| 263 : initialized_callback_(initialized_callback), | 263 : initialized_callback_(initialized_callback), |
| 264 get_content_callback_(get_content_callback), | 264 get_content_callback_(get_content_callback), |
| 265 completion_callback_(completion_callback), | 265 completion_callback_(completion_callback), |
| 266 entry_(entry.Pass()) { | 266 entry_(entry.Pass()), |
| 267 weak_ptr_factory_(this) { |
| 267 DCHECK(!completion_callback_.is_null()); | 268 DCHECK(!completion_callback_.is_null()); |
| 268 DCHECK(entry_); | 269 DCHECK(entry_); |
| 269 } | 270 } |
| 270 | 271 |
| 272 base::Closure GetCancelClosure() { |
| 273 return base::Bind(&DownloadParams::Cancel, weak_ptr_factory_.GetWeakPtr()); |
| 274 } |
| 275 |
| 271 void OnCacheFileFound(const base::FilePath& cache_file_path) const { | 276 void OnCacheFileFound(const base::FilePath& cache_file_path) const { |
| 272 if (initialized_callback_.is_null()) | 277 if (initialized_callback_.is_null()) |
| 273 return; | 278 return; |
| 274 | 279 |
| 275 DCHECK(entry_); | 280 DCHECK(entry_); |
| 276 initialized_callback_.Run( | 281 initialized_callback_.Run(FILE_ERROR_OK, cache_file_path, |
| 277 FILE_ERROR_OK, make_scoped_ptr(new ResourceEntry(*entry_)), | 282 make_scoped_ptr(new ResourceEntry(*entry_))); |
| 278 cache_file_path, base::Closure()); | |
| 279 } | 283 } |
| 280 | 284 |
| 281 void OnStartDownloading(const base::Closure& cancel_download_closure) const { | 285 void OnStartDownloading(const base::Closure& cancel_download_closure) { |
| 286 cancel_download_closure_ = cancel_download_closure; |
| 282 if (initialized_callback_.is_null()) { | 287 if (initialized_callback_.is_null()) { |
| 283 return; | 288 return; |
| 284 } | 289 } |
| 285 | 290 |
| 286 DCHECK(entry_); | 291 DCHECK(entry_); |
| 287 initialized_callback_.Run( | 292 initialized_callback_.Run(FILE_ERROR_OK, base::FilePath(), |
| 288 FILE_ERROR_OK, make_scoped_ptr(new ResourceEntry(*entry_)), | 293 make_scoped_ptr(new ResourceEntry(*entry_))); |
| 289 base::FilePath(), cancel_download_closure); | |
| 290 } | 294 } |
| 291 | 295 |
| 292 void OnError(FileError error) const { | 296 void OnError(FileError error) const { |
| 293 completion_callback_.Run( | 297 completion_callback_.Run( |
| 294 error, base::FilePath(), scoped_ptr<ResourceEntry>()); | 298 error, base::FilePath(), scoped_ptr<ResourceEntry>()); |
| 295 } | 299 } |
| 296 | 300 |
| 297 void OnComplete(const base::FilePath& cache_file_path) { | 301 void OnComplete(const base::FilePath& cache_file_path) { |
| 298 completion_callback_.Run(FILE_ERROR_OK, cache_file_path, entry_.Pass()); | 302 completion_callback_.Run(FILE_ERROR_OK, cache_file_path, entry_.Pass()); |
| 299 } | 303 } |
| 300 | 304 |
| 301 const google_apis::GetContentCallback& get_content_callback() const { | 305 const google_apis::GetContentCallback& get_content_callback() const { |
| 302 return get_content_callback_; | 306 return get_content_callback_; |
| 303 } | 307 } |
| 304 | 308 |
| 305 const ResourceEntry& entry() const { return *entry_; } | 309 const ResourceEntry& entry() const { return *entry_; } |
| 306 | 310 |
| 307 private: | 311 private: |
| 312 void Cancel() { |
| 313 if (!cancel_download_closure_.is_null()) |
| 314 cancel_download_closure_.Run(); |
| 315 } |
| 316 |
| 308 const GetFileContentInitializedCallback initialized_callback_; | 317 const GetFileContentInitializedCallback initialized_callback_; |
| 309 const google_apis::GetContentCallback get_content_callback_; | 318 const google_apis::GetContentCallback get_content_callback_; |
| 310 const GetFileCallback completion_callback_; | 319 const GetFileCallback completion_callback_; |
| 311 | 320 |
| 312 scoped_ptr<ResourceEntry> entry_; | 321 scoped_ptr<ResourceEntry> entry_; |
| 322 base::Closure cancel_download_closure_; |
| 313 | 323 |
| 324 base::WeakPtrFactory<DownloadParams> weak_ptr_factory_; |
| 314 DISALLOW_COPY_AND_ASSIGN(DownloadParams); | 325 DISALLOW_COPY_AND_ASSIGN(DownloadParams); |
| 315 }; | 326 }; |
| 316 | 327 |
| 317 DownloadOperation::DownloadOperation( | 328 DownloadOperation::DownloadOperation( |
| 318 base::SequencedTaskRunner* blocking_task_runner, | 329 base::SequencedTaskRunner* blocking_task_runner, |
| 319 OperationObserver* observer, | 330 OperationObserver* observer, |
| 320 JobScheduler* scheduler, | 331 JobScheduler* scheduler, |
| 321 internal::ResourceMetadata* metadata, | 332 internal::ResourceMetadata* metadata, |
| 322 internal::FileCache* cache, | 333 internal::FileCache* cache, |
| 323 const base::FilePath& temporary_file_directory) | 334 const base::FilePath& temporary_file_directory) |
| 324 : blocking_task_runner_(blocking_task_runner), | 335 : blocking_task_runner_(blocking_task_runner), |
| 325 observer_(observer), | 336 observer_(observer), |
| 326 scheduler_(scheduler), | 337 scheduler_(scheduler), |
| 327 metadata_(metadata), | 338 metadata_(metadata), |
| 328 cache_(cache), | 339 cache_(cache), |
| 329 temporary_file_directory_(temporary_file_directory), | 340 temporary_file_directory_(temporary_file_directory), |
| 330 weak_ptr_factory_(this) { | 341 weak_ptr_factory_(this) { |
| 331 } | 342 } |
| 332 | 343 |
| 333 DownloadOperation::~DownloadOperation() { | 344 DownloadOperation::~DownloadOperation() { |
| 334 } | 345 } |
| 335 | 346 |
| 336 void DownloadOperation::EnsureFileDownloadedByLocalId( | 347 base::Closure DownloadOperation::EnsureFileDownloadedByLocalId( |
| 337 const std::string& local_id, | 348 const std::string& local_id, |
| 338 const ClientContext& context, | 349 const ClientContext& context, |
| 339 const GetFileContentInitializedCallback& initialized_callback, | 350 const GetFileContentInitializedCallback& initialized_callback, |
| 340 const google_apis::GetContentCallback& get_content_callback, | 351 const google_apis::GetContentCallback& get_content_callback, |
| 341 const GetFileCallback& completion_callback) { | 352 const GetFileCallback& completion_callback) { |
| 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 343 DCHECK(!completion_callback.is_null()); | 354 DCHECK(!completion_callback.is_null()); |
| 344 | 355 |
| 345 base::FilePath* drive_file_path = new base::FilePath; | 356 base::FilePath* drive_file_path = new base::FilePath; |
| 346 base::FilePath* cache_file_path = new base::FilePath; | 357 base::FilePath* cache_file_path = new base::FilePath; |
| 347 ResourceEntry* entry = new ResourceEntry; | 358 ResourceEntry* entry = new ResourceEntry; |
| 348 scoped_ptr<DownloadParams> params(new DownloadParams( | 359 scoped_ptr<DownloadParams> params(new DownloadParams( |
| 349 initialized_callback, get_content_callback, completion_callback, | 360 initialized_callback, get_content_callback, completion_callback, |
| 350 make_scoped_ptr(entry))); | 361 make_scoped_ptr(entry))); |
| 362 base::Closure cancel_closure = params->GetCancelClosure(); |
| 351 base::PostTaskAndReplyWithResult( | 363 base::PostTaskAndReplyWithResult( |
| 352 blocking_task_runner_.get(), | 364 blocking_task_runner_.get(), |
| 353 FROM_HERE, | 365 FROM_HERE, |
| 354 base::Bind(&CheckPreConditionForEnsureFileDownloadedByLocalId, | 366 base::Bind(&CheckPreConditionForEnsureFileDownloadedByLocalId, |
| 355 base::Unretained(metadata_), | 367 base::Unretained(metadata_), |
| 356 base::Unretained(cache_), | 368 base::Unretained(cache_), |
| 357 local_id, | 369 local_id, |
| 358 temporary_file_directory_, | 370 temporary_file_directory_, |
| 359 drive_file_path, | 371 drive_file_path, |
| 360 cache_file_path, | 372 cache_file_path, |
| 361 entry), | 373 entry), |
| 362 base::Bind(&DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition, | 374 base::Bind(&DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition, |
| 363 weak_ptr_factory_.GetWeakPtr(), | 375 weak_ptr_factory_.GetWeakPtr(), |
| 364 base::Passed(¶ms), | 376 base::Passed(¶ms), |
| 365 context, | 377 context, |
| 366 base::Owned(drive_file_path), | 378 base::Owned(drive_file_path), |
| 367 base::Owned(cache_file_path))); | 379 base::Owned(cache_file_path))); |
| 380 return cancel_closure; |
| 368 } | 381 } |
| 369 | 382 |
| 370 void DownloadOperation::EnsureFileDownloadedByPath( | 383 base::Closure DownloadOperation::EnsureFileDownloadedByPath( |
| 371 const base::FilePath& file_path, | 384 const base::FilePath& file_path, |
| 372 const ClientContext& context, | 385 const ClientContext& context, |
| 373 const GetFileContentInitializedCallback& initialized_callback, | 386 const GetFileContentInitializedCallback& initialized_callback, |
| 374 const google_apis::GetContentCallback& get_content_callback, | 387 const google_apis::GetContentCallback& get_content_callback, |
| 375 const GetFileCallback& completion_callback) { | 388 const GetFileCallback& completion_callback) { |
| 376 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 377 DCHECK(!completion_callback.is_null()); | 390 DCHECK(!completion_callback.is_null()); |
| 378 | 391 |
| 379 base::FilePath* drive_file_path = new base::FilePath(file_path); | 392 base::FilePath* drive_file_path = new base::FilePath(file_path); |
| 380 base::FilePath* cache_file_path = new base::FilePath; | 393 base::FilePath* cache_file_path = new base::FilePath; |
| 381 ResourceEntry* entry = new ResourceEntry; | 394 ResourceEntry* entry = new ResourceEntry; |
| 382 scoped_ptr<DownloadParams> params(new DownloadParams( | 395 scoped_ptr<DownloadParams> params(new DownloadParams( |
| 383 initialized_callback, get_content_callback, completion_callback, | 396 initialized_callback, get_content_callback, completion_callback, |
| 384 make_scoped_ptr(entry))); | 397 make_scoped_ptr(entry))); |
| 398 base::Closure cancel_closure = params->GetCancelClosure(); |
| 385 base::PostTaskAndReplyWithResult( | 399 base::PostTaskAndReplyWithResult( |
| 386 blocking_task_runner_.get(), | 400 blocking_task_runner_.get(), |
| 387 FROM_HERE, | 401 FROM_HERE, |
| 388 base::Bind(&CheckPreConditionForEnsureFileDownloadedByPath, | 402 base::Bind(&CheckPreConditionForEnsureFileDownloadedByPath, |
| 389 base::Unretained(metadata_), | 403 base::Unretained(metadata_), |
| 390 base::Unretained(cache_), | 404 base::Unretained(cache_), |
| 391 file_path, | 405 file_path, |
| 392 temporary_file_directory_, | 406 temporary_file_directory_, |
| 393 cache_file_path, | 407 cache_file_path, |
| 394 entry), | 408 entry), |
| 395 base::Bind(&DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition, | 409 base::Bind(&DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition, |
| 396 weak_ptr_factory_.GetWeakPtr(), | 410 weak_ptr_factory_.GetWeakPtr(), |
| 397 base::Passed(¶ms), | 411 base::Passed(¶ms), |
| 398 context, | 412 context, |
| 399 base::Owned(drive_file_path), | 413 base::Owned(drive_file_path), |
| 400 base::Owned(cache_file_path))); | 414 base::Owned(cache_file_path))); |
| 415 return cancel_closure; |
| 401 } | 416 } |
| 402 | 417 |
| 403 void DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition( | 418 void DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition( |
| 404 scoped_ptr<DownloadParams> params, | 419 scoped_ptr<DownloadParams> params, |
| 405 const ClientContext& context, | 420 const ClientContext& context, |
| 406 base::FilePath* drive_file_path, | 421 base::FilePath* drive_file_path, |
| 407 base::FilePath* cache_file_path, | 422 base::FilePath* cache_file_path, |
| 408 FileError error) { | 423 FileError error) { |
| 409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 410 DCHECK(params); | 425 DCHECK(params); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 observer_->OnDirectoryChangedByOperation(file_path.DirName()); | 542 observer_->OnDirectoryChangedByOperation(file_path.DirName()); |
| 528 params->OnComplete(*cache_file_path); | 543 params->OnComplete(*cache_file_path); |
| 529 } | 544 } |
| 530 | 545 |
| 531 void DownloadOperation::CancelJob(JobID job_id) { | 546 void DownloadOperation::CancelJob(JobID job_id) { |
| 532 scheduler_->CancelJob(job_id); | 547 scheduler_->CancelJob(job_id); |
| 533 } | 548 } |
| 534 | 549 |
| 535 } // namespace file_system | 550 } // namespace file_system |
| 536 } // namespace drive | 551 } // namespace drive |
| OLD | NEW |