| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_DRIVE_UPLOADER_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_DRIVE_UPLOADER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "chrome/browser/chromeos/gdata/drive_upload_mode.h" | |
| 14 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" | |
| 15 #include "chrome/browser/google_apis/gdata_errorcode.h" | |
| 16 #include "net/base/file_stream.h" | |
| 17 | |
| 18 class GURL; | |
| 19 | |
| 20 namespace content { | |
| 21 class DownloadItem; | |
| 22 } | |
| 23 | |
| 24 namespace gdata { | |
| 25 | |
| 26 class MockDriveUploader; | |
| 27 class DriveServiceInterface; | |
| 28 struct ResumeUploadResponse; | |
| 29 | |
| 30 // Callback to be invoked once the upload has completed. | |
| 31 typedef base::Callback<void(DriveFileError error, | |
| 32 const FilePath& drive_path, | |
| 33 const FilePath& file_path, | |
| 34 scoped_ptr<DocumentEntry> document_entry)> UploadCompletionCallback; | |
| 35 | |
| 36 class DriveUploaderInterface { | |
| 37 public: | |
| 38 virtual ~DriveUploaderInterface() {} | |
| 39 | |
| 40 // Uploads a new file. | |
| 41 // Returns the upload_id. | |
| 42 virtual int UploadNewFile( | |
| 43 const GURL& upload_location, | |
| 44 const FilePath& drive_file_path, | |
| 45 const FilePath& local_file_path, | |
| 46 const std::string& title, | |
| 47 const std::string& content_type, | |
| 48 int64 content_length, | |
| 49 int64 file_size, | |
| 50 const UploadCompletionCallback& callback) = 0; | |
| 51 | |
| 52 // Stream data to an existing file. | |
| 53 // Transfers ownership. Returns the upload_id. | |
| 54 virtual int StreamExistingFile( | |
| 55 const GURL& upload_location, | |
| 56 const FilePath& drive_file_path, | |
| 57 const FilePath& local_file_path, | |
| 58 const std::string& content_type, | |
| 59 int64 content_length, | |
| 60 int64 file_size, | |
| 61 const UploadCompletionCallback& callback) = 0; | |
| 62 | |
| 63 // Uploads an existing file (a file that already exists on Drive). | |
| 64 virtual int UploadExistingFile( | |
| 65 const GURL& upload_location, | |
| 66 const FilePath& drive_file_path, | |
| 67 const FilePath& local_file_path, | |
| 68 const std::string& content_type, | |
| 69 int64 file_size, | |
| 70 const UploadCompletionCallback& callback) = 0; | |
| 71 | |
| 72 // Updates attributes of streaming upload. | |
| 73 virtual void UpdateUpload(int upload_id, | |
| 74 content::DownloadItem* download) = 0; | |
| 75 | |
| 76 // Returns the count of bytes confirmed as uploaded so far. | |
| 77 virtual int64 GetUploadedBytes(int upload_id) const = 0; | |
| 78 }; | |
| 79 | |
| 80 class DriveUploader : public DriveUploaderInterface { | |
| 81 friend class MockDriveUploader; | |
| 82 public: | |
| 83 explicit DriveUploader(DriveServiceInterface* drive_service); | |
| 84 virtual ~DriveUploader(); | |
| 85 | |
| 86 // DriveUploaderInterface overrides. | |
| 87 virtual int UploadNewFile( | |
| 88 const GURL& upload_location, | |
| 89 const FilePath& drive_file_path, | |
| 90 const FilePath& local_file_path, | |
| 91 const std::string& title, | |
| 92 const std::string& content_type, | |
| 93 int64 content_length, | |
| 94 int64 file_size, | |
| 95 const UploadCompletionCallback& callback) OVERRIDE; | |
| 96 virtual int StreamExistingFile( | |
| 97 const GURL& upload_location, | |
| 98 const FilePath& drive_file_path, | |
| 99 const FilePath& local_file_path, | |
| 100 const std::string& content_type, | |
| 101 int64 content_length, | |
| 102 int64 file_size, | |
| 103 const UploadCompletionCallback& callback) OVERRIDE; | |
| 104 virtual int UploadExistingFile( | |
| 105 const GURL& upload_location, | |
| 106 const FilePath& drive_file_path, | |
| 107 const FilePath& local_file_path, | |
| 108 const std::string& content_type, | |
| 109 int64 file_size, | |
| 110 const UploadCompletionCallback& callback) OVERRIDE; | |
| 111 virtual void UpdateUpload( | |
| 112 int upload_id, content::DownloadItem* download) OVERRIDE; | |
| 113 virtual int64 GetUploadedBytes(int upload_id) const OVERRIDE; | |
| 114 | |
| 115 private: | |
| 116 // Structure containing current upload information of file, passed between | |
| 117 // DriveServiceInterface methods and callbacks. | |
| 118 struct UploadFileInfo { | |
| 119 // To be able to access UploadFileInfo from tests. | |
| 120 UploadFileInfo(); | |
| 121 ~UploadFileInfo(); | |
| 122 | |
| 123 // Bytes left to upload. | |
| 124 int64 SizeRemaining() const; | |
| 125 | |
| 126 // Useful for printf debugging. | |
| 127 std::string DebugString() const; | |
| 128 | |
| 129 int upload_id; // id of this upload. | |
| 130 FilePath file_path; // The path of the file to be uploaded. | |
| 131 int64 file_size; // Last known size of the file. | |
| 132 | |
| 133 // TODO(zelirag, achuith): Make this string16. | |
| 134 std::string title; // Title to be used for file to be uploaded. | |
| 135 std::string content_type; // Content-Type of file. | |
| 136 int64 content_length; // Header content-Length. | |
| 137 | |
| 138 UploadMode upload_mode; | |
| 139 | |
| 140 // Location URL used to get |upload_location| with InitiateUpload. | |
| 141 GURL initial_upload_location; | |
| 142 | |
| 143 // Location URL where file is to be uploaded to, returned from | |
| 144 // InitiateUpload. Used for the subsequent ResumeUpload requests. | |
| 145 GURL upload_location; | |
| 146 | |
| 147 // Final path in gdata. Looks like /special/drive/MyFolder/MyFile. | |
| 148 FilePath drive_path; | |
| 149 | |
| 150 // TODO(achuith): Use generic stream object after FileStream is refactored | |
| 151 // to extend a generic stream. | |
| 152 // | |
| 153 // For opening and reading from physical file. | |
| 154 net::FileStream* file_stream; | |
| 155 scoped_refptr<net::IOBuffer> buf; // Holds current content to be uploaded. | |
| 156 // Size of |buf|, max is 512KB; Google Docs requires size of each upload | |
| 157 // chunk to be a multiple of 512KB. | |
| 158 int64 buf_len; | |
| 159 | |
| 160 // Data to be updated by caller before sending each ResumeUpload request. | |
| 161 // Note that end_range is inclusive, so start_range=0, end_range=8 is 9 | |
| 162 // bytes. | |
| 163 // | |
| 164 // Start of range of contents currently stored in |buf|. | |
| 165 int64 start_range; | |
| 166 int64 end_range; // End of range of contents currently stored in |buf|. | |
| 167 | |
| 168 bool all_bytes_present; // Whether all bytes of this file are present. | |
| 169 bool upload_paused; // Whether this file's upload has been paused. | |
| 170 | |
| 171 bool should_retry_file_open; // Whether we should retry opening this file. | |
| 172 int num_file_open_tries; // Number of times we've tried to open this file. | |
| 173 | |
| 174 // Will be set once the upload is complete. | |
| 175 scoped_ptr<DocumentEntry> entry; | |
| 176 | |
| 177 UploadCompletionCallback completion_callback; | |
| 178 }; | |
| 179 | |
| 180 // Lookup UploadFileInfo* in pending_uploads_. | |
| 181 UploadFileInfo* GetUploadFileInfo(int upload_id) const; | |
| 182 | |
| 183 // Open the file. | |
| 184 void OpenFile(UploadFileInfo* upload_file_info); | |
| 185 | |
| 186 // net::FileStream::Open completion callback. The result of the file | |
| 187 // open operation is passed as |result|. | |
| 188 void OpenCompletionCallback(int upload_id, int result); | |
| 189 | |
| 190 // DriveService callback for InitiateUpload. | |
| 191 void OnUploadLocationReceived(int upload_id, | |
| 192 GDataErrorCode code, | |
| 193 const GURL& upload_location); | |
| 194 | |
| 195 // Uploads the next chunk of data from the file. | |
| 196 void UploadNextChunk(UploadFileInfo* upload_file_info); | |
| 197 | |
| 198 // net::FileStream::Read completion callback. | |
| 199 void ReadCompletionCallback(int upload_id, | |
| 200 int bytes_to_read, | |
| 201 int bytes_read); | |
| 202 | |
| 203 // Calls DriveService's ResumeUpload with the current upload info. | |
| 204 void ResumeUpload(int upload_id); | |
| 205 | |
| 206 // DriveService callback for ResumeUpload. | |
| 207 void OnResumeUploadResponseReceived(int upload_id, | |
| 208 const ResumeUploadResponse& response, | |
| 209 scoped_ptr<DocumentEntry> entry); | |
| 210 | |
| 211 // Initiate the upload. | |
| 212 void InitiateUpload(UploadFileInfo* uploader_file_info); | |
| 213 | |
| 214 // Handle failed uploads. | |
| 215 void UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info, | |
| 216 DriveFileError error); | |
| 217 | |
| 218 // Removes |upload_id| from UploadFileInfoMap |pending_uploads_|. | |
| 219 // Note that this does not delete the UploadFileInfo object itself, | |
| 220 // because it may still be in use by an asynchronous function. | |
| 221 void RemoveUpload(int upload_id); | |
| 222 | |
| 223 // Starts uploading a file with |upload_file_info|. Returns a new upload | |
| 224 // ID assigned to |upload_file_info|. | |
| 225 int StartUploadFile(scoped_ptr<UploadFileInfo> upload_file_info); | |
| 226 | |
| 227 // Pointers to DriveServiceInterface object owned by DriveSystemService. | |
| 228 // The lifetime of this object is guaranteed to exceed that of the | |
| 229 // DriveUploader instance. | |
| 230 DriveServiceInterface* drive_service_; | |
| 231 | |
| 232 int next_upload_id_; // id counter. | |
| 233 | |
| 234 typedef std::map<int, UploadFileInfo*> UploadFileInfoMap; | |
| 235 UploadFileInfoMap pending_uploads_; | |
| 236 | |
| 237 // Note: This should remain the last member so it'll be destroyed and | |
| 238 // invalidate its weak pointers before any other members are destroyed. | |
| 239 base::WeakPtrFactory<DriveUploader> weak_ptr_factory_; | |
| 240 | |
| 241 DISALLOW_COPY_AND_ASSIGN(DriveUploader); | |
| 242 }; | |
| 243 | |
| 244 } // namespace gdata | |
| 245 | |
| 246 #endif // CHROME_BROWSER_CHROMEOS_GDATA_DRIVE_UPLOADER_H_ | |
| OLD | NEW |