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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/browser/chromeos/drive/file_errors.h" 11 #include "chrome/browser/chromeos/drive/file_system_interface.h" 12 #include "chrome/browser/chromeos/drive/job_list.h" 13 #include "google_apis/drive/gdata_errorcode.h" 14 15 namespace base { 16 class FilePath; 17 class SequencedTaskRunner; 18 } // namespace base 19 20 namespace google_apis { 21 class ResourceEntry; 22 } // namespace google_apis 23 24 namespace drive { 25 26 class JobScheduler; 27 class ResourceEntry; 28 struct ClientContext; 29 30 namespace internal { 31 class FileCache; 32 class ResourceMetadata; 33 } // namespace internal 34 35 namespace file_system { 36 37 class OperationObserver; 38 39 class DownloadOperation { 40 public: 41 DownloadOperation(base::SequencedTaskRunner* blocking_task_runner, 42 OperationObserver* observer, 43 JobScheduler* scheduler, 44 internal::ResourceMetadata* metadata, 45 internal::FileCache* cache, 46 const base::FilePath& temporary_file_directory); 47 ~DownloadOperation(); 48 49 // Ensures that the file content specified by |local_id| is locally 50 // downloaded and returns a closure to cancel the task. 51 // For hosted documents, this method may create a JSON file representing the 52 // file. 53 // For regular files, if the locally cached file is found, returns it. 54 // If not found, start to download the file from the server. 55 // When a JSON file is created, the cache file is found or downloading is 56 // being started, |initialized_callback| is called with |local_file| 57 // for JSON file or the cache file, or with |cancel_download_closure| for 58 // downloading. 59 // During the downloading |get_content_callback| will be called periodically 60 // with the downloaded content. 61 // Upon completion or an error is found, |completion_callback| will be called. 62 // |initialized_callback| and |get_content_callback| can be null if not 63 // needed. 64 // |completion_callback| must not be null. 65 base::Closure EnsureFileDownloadedByLocalId( 66 const std::string& local_id, 67 const ClientContext& context, 68 const GetFileContentInitializedCallback& initialized_callback, 69 const google_apis::GetContentCallback& get_content_callback, 70 const GetFileCallback& completion_callback); 71 72 // Does the same thing as EnsureFileDownloadedByLocalId for the file 73 // specified by |file_path|. 74 base::Closure EnsureFileDownloadedByPath( 75 const base::FilePath& file_path, 76 const ClientContext& context, 77 const GetFileContentInitializedCallback& initialized_callback, 78 const google_apis::GetContentCallback& get_content_callback, 79 const GetFileCallback& completion_callback); 80 81 private: 82 // Parameters for EnsureFileDownloaded. 83 class DownloadParams; 84 85 // Part of EnsureFileDownloaded(). Called upon the completion of precondition 86 // check. 87 void EnsureFileDownloadedAfterCheckPreCondition( 88 scoped_ptr<DownloadParams> params, 89 const ClientContext& context, 90 base::FilePath* drive_file_path, 91 base::FilePath* cache_file_path, 92 base::FilePath* temp_download_file_path, 93 FileError error); 94 95 // Part of EnsureFileDownloaded(). Called after the actual downloading. 96 void EnsureFileDownloadedAfterDownloadFile( 97 const base::FilePath& drive_file_path, 98 scoped_ptr<DownloadParams> params, 99 google_apis::GDataErrorCode gdata_error, 100 const base::FilePath& downloaded_file_path); 101 102 // Part of EnsureFileDownloaded(). Called after updating local state is 103 // completed. 104 void EnsureFileDownloadedAfterUpdateLocalState( 105 const base::FilePath& file_path, 106 scoped_ptr<DownloadParams> params, 107 scoped_ptr<ResourceEntry> entry_after_update, 108 base::FilePath* cache_file_path, 109 FileError error); 110 111 // Cancels the job with |job_id| in the scheduler. 112 void CancelJob(JobID job_id); 113 114 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 115 OperationObserver* observer_; 116 JobScheduler* scheduler_; 117 internal::ResourceMetadata* metadata_; 118 internal::FileCache* cache_; 119 const base::FilePath temporary_file_directory_; 120 121 // Note: This should remain the last member so it'll be destroyed and 122 // invalidate its weak pointers before any other members are destroyed. 123 base::WeakPtrFactory<DownloadOperation> weak_ptr_factory_; 124 DISALLOW_COPY_AND_ASSIGN(DownloadOperation); 125 }; 126 127 } // namespace file_system 128 } // namespace drive 129 130 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_ 131