| 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // the path to the JSON file. | 30 // the path to the JSON file. |
| 31 // If the resource is a regular file and its local cache is available, | 31 // If the resource is a regular file and its local cache is available, |
| 32 // returns FILE_ERROR_OK with |cache_file_path| storing the path to the | 32 // returns FILE_ERROR_OK with |cache_file_path| storing the path to the |
| 33 // cache file. | 33 // cache file. |
| 34 // If the resource is a regular file but its local cache is NOT available, | 34 // If the resource is a regular file but its local cache is NOT available, |
| 35 // returns FILE_ERROR_OK, but |cache_file_path| is kept empty. | 35 // returns FILE_ERROR_OK, but |cache_file_path| is kept empty. |
| 36 // Otherwise returns error code. | 36 // Otherwise returns error code. |
| 37 FileError CheckPreConditionForEnsureFileDownloaded( | 37 FileError CheckPreConditionForEnsureFileDownloaded( |
| 38 internal::ResourceMetadata* metadata, | 38 internal::ResourceMetadata* metadata, |
| 39 internal::FileCache* cache, | 39 internal::FileCache* cache, |
| 40 const base::FilePath& file_path, | 40 const ResourceEntry& entry, |
| 41 base::FilePath* cache_file_path, | 41 base::FilePath* cache_file_path) { |
| 42 ResourceEntry* entry) { | |
| 43 DCHECK(metadata); | 42 DCHECK(metadata); |
| 44 DCHECK(cache); | 43 DCHECK(cache); |
| 45 DCHECK(cache_file_path); | 44 DCHECK(cache_file_path); |
| 46 DCHECK(entry); | |
| 47 | 45 |
| 48 FileError error = metadata->GetResourceEntryByPath(file_path, entry); | 46 if (entry.file_info().is_directory()) |
| 49 if (error != FILE_ERROR_OK) | |
| 50 return error; | |
| 51 | |
| 52 if (entry->file_info().is_directory()) | |
| 53 return FILE_ERROR_NOT_A_FILE; | 47 return FILE_ERROR_NOT_A_FILE; |
| 54 | 48 |
| 55 // The file's entry should have its file specific info. | 49 // The file's entry should have its file specific info. |
| 56 DCHECK(entry->has_file_specific_info()); | 50 DCHECK(entry.has_file_specific_info()); |
| 57 | 51 |
| 58 // For a hosted document, we create a special JSON file to represent the | 52 // For a hosted document, we create a special JSON file to represent the |
| 59 // document instead of fetching the document content in one of the exported | 53 // document instead of fetching the document content in one of the exported |
| 60 // formats. The JSON file contains the edit URL and resource ID of the | 54 // formats. The JSON file contains the edit URL and resource ID of the |
| 61 // document. | 55 // document. |
| 62 if (entry->file_specific_info().is_hosted_document()) { | 56 if (entry.file_specific_info().is_hosted_document()) { |
| 63 base::FilePath gdoc_file_path; | 57 base::FilePath gdoc_file_path; |
| 64 if (!file_util::CreateTemporaryFileInDir( | 58 if (!file_util::CreateTemporaryFileInDir( |
| 65 cache->GetCacheDirectoryPath( | 59 cache->GetCacheDirectoryPath( |
| 66 internal::FileCache::CACHE_TYPE_TMP_DOCUMENTS), | 60 internal::FileCache::CACHE_TYPE_TMP_DOCUMENTS), |
| 67 &gdoc_file_path) || | 61 &gdoc_file_path) || |
| 68 !util::CreateGDocFile(gdoc_file_path, | 62 !util::CreateGDocFile(gdoc_file_path, |
| 69 GURL(entry->file_specific_info().alternate_url()), | 63 GURL(entry.file_specific_info().alternate_url()), |
| 70 entry->resource_id())) | 64 entry.resource_id())) |
| 71 return FILE_ERROR_FAILED; | 65 return FILE_ERROR_FAILED; |
| 72 | 66 |
| 73 *cache_file_path = gdoc_file_path; | 67 *cache_file_path = gdoc_file_path; |
| 74 return FILE_ERROR_OK; | 68 return FILE_ERROR_OK; |
| 75 } | 69 } |
| 76 | 70 |
| 77 // Look up if there exists the cache file. | 71 // Get the cache file path if available. |
| 78 FileError cache_error = cache->GetFile( | 72 cache->GetFile(entry.resource_id(), |
| 79 entry->resource_id(), | 73 entry.file_specific_info().file_md5(), |
| 80 entry->file_specific_info().file_md5(), | 74 cache_file_path); |
| 81 cache_file_path); | 75 return FILE_ERROR_OK; |
| 82 DCHECK((cache_error == FILE_ERROR_OK && !cache_file_path->empty()) || | 76 } |
| 83 (cache_error == FILE_ERROR_NOT_FOUND && cache_file_path->empty())); | |
| 84 | 77 |
| 85 return error; | 78 // Calls CheckPreConditionForEnsureFileDownloaded() with the entry specified by |
| 79 // the given ID. |
| 80 FileError CheckPreConditionForEnsureFileDownloadedByResourceId( |
| 81 internal::ResourceMetadata* metadata, |
| 82 internal::FileCache* cache, |
| 83 const std::string& resource_id, |
| 84 base::FilePath* cache_file_path, |
| 85 ResourceEntry* entry) { |
| 86 FileError error = metadata->GetResourceEntryById(resource_id, NULL, entry); |
| 87 if (error != FILE_ERROR_OK) |
| 88 return error; |
| 89 return CheckPreConditionForEnsureFileDownloaded( |
| 90 metadata, cache, *entry, cache_file_path); |
| 91 } |
| 92 |
| 93 // Calls CheckPreConditionForEnsureFileDownloaded() with the entry specified by |
| 94 // the given file path. |
| 95 FileError CheckPreConditionForEnsureFileDownloadedByPath( |
| 96 internal::ResourceMetadata* metadata, |
| 97 internal::FileCache* cache, |
| 98 const base::FilePath& file_path, |
| 99 base::FilePath* cache_file_path, |
| 100 ResourceEntry* entry) { |
| 101 FileError error = metadata->GetResourceEntryByPath(file_path, entry); |
| 102 if (error != FILE_ERROR_OK) |
| 103 return error; |
| 104 return CheckPreConditionForEnsureFileDownloaded( |
| 105 metadata, cache, *entry, cache_file_path); |
| 86 } | 106 } |
| 87 | 107 |
| 88 // Creates a file with unique name in |dir| and stores the path to |temp_file|. | 108 // Creates a file with unique name in |dir| and stores the path to |temp_file|. |
| 89 // Additionally, sets the permission of the file to allow read access from | 109 // Additionally, sets the permission of the file to allow read access from |
| 90 // others and group member users (i.e, "-rw-r--r--"). | 110 // others and group member users (i.e, "-rw-r--r--"). |
| 91 // We need this wrapper because Drive cache files may be read from other | 111 // We need this wrapper because Drive cache files may be read from other |
| 92 // processes (e.g., cros_disks for mounting zip files). | 112 // processes (e.g., cros_disks for mounting zip files). |
| 93 bool CreateTemporaryReadableFileInDir(const base::FilePath& dir, | 113 bool CreateTemporaryReadableFileInDir(const base::FilePath& dir, |
| 94 base::FilePath* temp_file) { | 114 base::FilePath* temp_file) { |
| 95 if (!file_util::CreateTemporaryFileInDir(dir, temp_file)) | 115 if (!file_util::CreateTemporaryFileInDir(dir, temp_file)) |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 observer_(observer), | 282 observer_(observer), |
| 263 scheduler_(scheduler), | 283 scheduler_(scheduler), |
| 264 metadata_(metadata), | 284 metadata_(metadata), |
| 265 cache_(cache), | 285 cache_(cache), |
| 266 weak_ptr_factory_(this) { | 286 weak_ptr_factory_(this) { |
| 267 } | 287 } |
| 268 | 288 |
| 269 DownloadOperation::~DownloadOperation() { | 289 DownloadOperation::~DownloadOperation() { |
| 270 } | 290 } |
| 271 | 291 |
| 272 void DownloadOperation::EnsureFileDownloaded( | 292 void DownloadOperation::EnsureFileDownloadedByResourceId( |
| 293 const std::string& resource_id, |
| 294 const ClientContext& context, |
| 295 const GetFileContentInitializedCallback& initialized_callback, |
| 296 const google_apis::GetContentCallback& get_content_callback, |
| 297 const GetFileCallback& completion_callback) { |
| 298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 299 DCHECK(!completion_callback.is_null()); |
| 300 |
| 301 DownloadCallback callback( |
| 302 initialized_callback, get_content_callback, completion_callback); |
| 303 |
| 304 ResourceEntry* entry = new ResourceEntry; |
| 305 base::FilePath* cache_file_path = new base::FilePath; |
| 306 base::PostTaskAndReplyWithResult( |
| 307 blocking_task_runner_, |
| 308 FROM_HERE, |
| 309 base::Bind(&CheckPreConditionForEnsureFileDownloadedByResourceId, |
| 310 base::Unretained(metadata_), |
| 311 base::Unretained(cache_), |
| 312 resource_id, |
| 313 cache_file_path, |
| 314 entry), |
| 315 base::Bind(&DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition, |
| 316 weak_ptr_factory_.GetWeakPtr(), |
| 317 context, |
| 318 callback, |
| 319 base::Passed(make_scoped_ptr(entry)), |
| 320 base::Owned(cache_file_path))); |
| 321 } |
| 322 |
| 323 void DownloadOperation::EnsureFileDownloadedByPath( |
| 273 const base::FilePath& file_path, | 324 const base::FilePath& file_path, |
| 274 const ClientContext& context, | 325 const ClientContext& context, |
| 275 const GetFileContentInitializedCallback& initialized_callback, | 326 const GetFileContentInitializedCallback& initialized_callback, |
| 276 const google_apis::GetContentCallback& get_content_callback, | 327 const google_apis::GetContentCallback& get_content_callback, |
| 277 const GetFileCallback& completion_callback) { | 328 const GetFileCallback& completion_callback) { |
| 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 279 DCHECK(!completion_callback.is_null()); | 330 DCHECK(!completion_callback.is_null()); |
| 280 | 331 |
| 281 DownloadCallback callback( | 332 DownloadCallback callback( |
| 282 initialized_callback, get_content_callback, completion_callback); | 333 initialized_callback, get_content_callback, completion_callback); |
| 283 | 334 |
| 284 ResourceEntry* entry = new ResourceEntry; | 335 ResourceEntry* entry = new ResourceEntry; |
| 285 base::FilePath* cache_file_path = new base::FilePath; | 336 base::FilePath* cache_file_path = new base::FilePath; |
| 286 base::PostTaskAndReplyWithResult( | 337 base::PostTaskAndReplyWithResult( |
| 287 blocking_task_runner_, | 338 blocking_task_runner_, |
| 288 FROM_HERE, | 339 FROM_HERE, |
| 289 base::Bind(&CheckPreConditionForEnsureFileDownloaded, | 340 base::Bind(&CheckPreConditionForEnsureFileDownloadedByPath, |
| 290 base::Unretained(metadata_), | 341 base::Unretained(metadata_), |
| 291 base::Unretained(cache_), | 342 base::Unretained(cache_), |
| 292 file_path, | 343 file_path, |
| 293 cache_file_path, | 344 cache_file_path, |
| 294 entry), | 345 entry), |
| 295 base::Bind(&DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition, | 346 base::Bind(&DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition, |
| 296 weak_ptr_factory_.GetWeakPtr(), | 347 weak_ptr_factory_.GetWeakPtr(), |
| 297 file_path, | |
| 298 context, | 348 context, |
| 299 callback, | 349 callback, |
| 300 base::Passed(make_scoped_ptr(entry)), | 350 base::Passed(make_scoped_ptr(entry)), |
| 301 base::Owned(cache_file_path))); | 351 base::Owned(cache_file_path))); |
| 302 } | 352 } |
| 303 | 353 |
| 304 void DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition( | 354 void DownloadOperation::EnsureFileDownloadedAfterCheckPreCondition( |
| 305 const base::FilePath& file_path, | |
| 306 const ClientContext& context, | 355 const ClientContext& context, |
| 307 const DownloadCallback& callback, | 356 const DownloadCallback& callback, |
| 308 scoped_ptr<ResourceEntry> entry, | 357 scoped_ptr<ResourceEntry> entry, |
| 309 base::FilePath* cache_file_path, | 358 base::FilePath* cache_file_path, |
| 310 FileError error) { | 359 FileError error) { |
| 311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 312 DCHECK(entry); | 361 DCHECK(entry); |
| 313 DCHECK(cache_file_path); | 362 DCHECK(cache_file_path); |
| 314 | 363 |
| 315 if (error != FILE_ERROR_OK) { | 364 if (error != FILE_ERROR_OK) { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 observer_->OnDirectoryChangedByOperation(file_path.DirName()); | 517 observer_->OnDirectoryChangedByOperation(file_path.DirName()); |
| 469 callback.OnComplete(*cache_file_path, entry.Pass()); | 518 callback.OnComplete(*cache_file_path, entry.Pass()); |
| 470 } | 519 } |
| 471 | 520 |
| 472 void DownloadOperation::CancelJob(JobID job_id) { | 521 void DownloadOperation::CancelJob(JobID job_id) { |
| 473 scheduler_->CancelJob(job_id); | 522 scheduler_->CancelJob(job_id); |
| 474 } | 523 } |
| 475 | 524 |
| 476 } // namespace file_system | 525 } // namespace file_system |
| 477 } // namespace drive | 526 } // namespace drive |
| OLD | NEW |