| 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 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_UPLOADER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_UPLOADER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_UPLOADER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_UPLOADER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "chrome/browser/chromeos/drive/drive_file_error.h" | 13 #include "chrome/browser/chromeos/drive/drive_file_error.h" |
| 14 #include "chrome/browser/google_apis/drive_upload_mode.h" | 14 #include "chrome/browser/google_apis/drive_upload_mode.h" |
| 15 #include "chrome/browser/google_apis/gdata_errorcode.h" | 15 #include "chrome/browser/google_apis/gdata_errorcode.h" |
| 16 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 16 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
| 17 #include "net/base/file_stream.h" | 17 #include "net/base/file_stream.h" |
| 18 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
| 19 #include "net/base/file_stream.h" | 19 #include "net/base/file_stream.h" |
| 20 | 20 |
| 21 class GURL; | 21 class GURL; |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 class DownloadItem; | 24 class DownloadItem; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace gdata { | 27 namespace gdata { |
| 28 struct ResumeUploadResponse; |
| 29 } |
| 30 |
| 31 namespace drive { |
| 28 | 32 |
| 29 class MockDriveUploader; | 33 class MockDriveUploader; |
| 30 class DriveServiceInterface; | 34 class DriveServiceInterface; |
| 31 struct ResumeUploadResponse; | |
| 32 | 35 |
| 33 // Callback to be invoked once the upload has completed. | 36 // Callback to be invoked once the upload has completed. |
| 34 typedef base::Callback<void(DriveFileError error, | 37 typedef base::Callback<void(DriveFileError error, |
| 35 const FilePath& drive_path, | 38 const FilePath& drive_path, |
| 36 const FilePath& file_path, | 39 const FilePath& file_path, |
| 37 scoped_ptr<DocumentEntry> document_entry)> UploadCompletionCallback; | 40 scoped_ptr<gdata::DocumentEntry> document_entry)> UploadCompletionCallback; |
| 38 | 41 |
| 39 // Callback to be invoked once the upoader is ready to upload. | 42 // Callback to be invoked once the upoader is ready to upload. |
| 40 typedef base::Callback<void(int32 upload_id)> UploaderReadyCallback; | 43 typedef base::Callback<void(int32 upload_id)> UploaderReadyCallback; |
| 41 | 44 |
| 42 class DriveUploaderInterface { | 45 class DriveUploaderInterface { |
| 43 public: | 46 public: |
| 44 virtual ~DriveUploaderInterface() {} | 47 virtual ~DriveUploaderInterface() {} |
| 45 | 48 |
| 46 // Uploads a new file. | 49 // Uploads a new file. |
| 47 // Returns the upload_id. | 50 // Returns the upload_id. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 143 |
| 141 int upload_id; // id of this upload. | 144 int upload_id; // id of this upload. |
| 142 FilePath file_path; // The path of the file to be uploaded. | 145 FilePath file_path; // The path of the file to be uploaded. |
| 143 int64 file_size; // Last known size of the file. | 146 int64 file_size; // Last known size of the file. |
| 144 | 147 |
| 145 // TODO(zelirag, achuith): Make this string16. | 148 // TODO(zelirag, achuith): Make this string16. |
| 146 std::string title; // Title to be used for file to be uploaded. | 149 std::string title; // Title to be used for file to be uploaded. |
| 147 std::string content_type; // Content-Type of file. | 150 std::string content_type; // Content-Type of file. |
| 148 int64 content_length; // Header content-Length. | 151 int64 content_length; // Header content-Length. |
| 149 | 152 |
| 150 UploadMode upload_mode; | 153 gdata::UploadMode upload_mode; |
| 151 | 154 |
| 152 // Location URL used to get |upload_location| with InitiateUpload. | 155 // Location URL used to get |upload_location| with InitiateUpload. |
| 153 GURL initial_upload_location; | 156 GURL initial_upload_location; |
| 154 | 157 |
| 155 // Location URL where file is to be uploaded to, returned from | 158 // Location URL where file is to be uploaded to, returned from |
| 156 // InitiateUpload. Used for the subsequent ResumeUpload requests. | 159 // InitiateUpload. Used for the subsequent ResumeUpload requests. |
| 157 GURL upload_location; | 160 GURL upload_location; |
| 158 | 161 |
| 159 // Final path in gdata. Looks like /special/drive/MyFolder/MyFile. | 162 // Final path in gdata. Looks like /special/drive/MyFolder/MyFile. |
| 160 FilePath drive_path; | 163 FilePath drive_path; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 177 int64 start_range; | 180 int64 start_range; |
| 178 int64 end_range; // End of range of contents currently stored in |buf|. | 181 int64 end_range; // End of range of contents currently stored in |buf|. |
| 179 | 182 |
| 180 bool all_bytes_present; // Whether all bytes of this file are present. | 183 bool all_bytes_present; // Whether all bytes of this file are present. |
| 181 bool upload_paused; // Whether this file's upload has been paused. | 184 bool upload_paused; // Whether this file's upload has been paused. |
| 182 | 185 |
| 183 bool should_retry_file_open; // Whether we should retry opening this file. | 186 bool should_retry_file_open; // Whether we should retry opening this file. |
| 184 int num_file_open_tries; // Number of times we've tried to open this file. | 187 int num_file_open_tries; // Number of times we've tried to open this file. |
| 185 | 188 |
| 186 // Will be set once the upload is complete. | 189 // Will be set once the upload is complete. |
| 187 scoped_ptr<DocumentEntry> entry; | 190 scoped_ptr<gdata::DocumentEntry> entry; |
| 188 | 191 |
| 189 // Callback to be invoked once the uploader is ready to upload. | 192 // Callback to be invoked once the uploader is ready to upload. |
| 190 UploaderReadyCallback ready_callback; | 193 UploaderReadyCallback ready_callback; |
| 191 | 194 |
| 192 // Callback to be invoked once the upload has finished. | 195 // Callback to be invoked once the upload has finished. |
| 193 UploadCompletionCallback completion_callback; | 196 UploadCompletionCallback completion_callback; |
| 194 }; | 197 }; |
| 195 | 198 |
| 196 // Indicates step in which we try to open a file. | 199 // Indicates step in which we try to open a file. |
| 197 // Retrying happens in FILE_OPEN_UPDATE_UPLOAD step. | 200 // Retrying happens in FILE_OPEN_UPDATE_UPLOAD step. |
| 198 enum FileOpenType { | 201 enum FileOpenType { |
| 199 FILE_OPEN_START_UPLOAD, | 202 FILE_OPEN_START_UPLOAD, |
| 200 FILE_OPEN_UPDATE_UPLOAD | 203 FILE_OPEN_UPDATE_UPLOAD |
| 201 }; | 204 }; |
| 202 | 205 |
| 203 // Lookup UploadFileInfo* in pending_uploads_. | 206 // Lookup UploadFileInfo* in pending_uploads_. |
| 204 UploadFileInfo* GetUploadFileInfo(int upload_id) const; | 207 UploadFileInfo* GetUploadFileInfo(int upload_id) const; |
| 205 | 208 |
| 206 // Open the file. | 209 // Open the file. |
| 207 void OpenFile(UploadFileInfo* upload_file_info, FileOpenType open_type); | 210 void OpenFile(UploadFileInfo* upload_file_info, FileOpenType open_type); |
| 208 | 211 |
| 209 // net::FileStream::Open completion callback. The result of the file | 212 // net::FileStream::Open completion callback. The result of the file |
| 210 // open operation is passed as |result|. | 213 // open operation is passed as |result|. |
| 211 void OpenCompletionCallback(FileOpenType open_type, | 214 void OpenCompletionCallback(FileOpenType open_type, |
| 212 int upload_id, | 215 int upload_id, |
| 213 int result); | 216 int result); |
| 214 | 217 |
| 215 // DriveService callback for InitiateUpload. | 218 // DriveService callback for InitiateUpload. |
| 216 void OnUploadLocationReceived(int upload_id, | 219 void OnUploadLocationReceived(int upload_id, |
| 217 GDataErrorCode code, | 220 gdata::GDataErrorCode code, |
| 218 const GURL& upload_location); | 221 const GURL& upload_location); |
| 219 | 222 |
| 220 // Uploads the next chunk of data from the file. | 223 // Uploads the next chunk of data from the file. |
| 221 void UploadNextChunk(UploadFileInfo* upload_file_info); | 224 void UploadNextChunk(UploadFileInfo* upload_file_info); |
| 222 | 225 |
| 223 // net::FileStream::Read completion callback. | 226 // net::FileStream::Read completion callback. |
| 224 void ReadCompletionCallback(int upload_id, | 227 void ReadCompletionCallback(int upload_id, |
| 225 int bytes_to_read, | 228 int bytes_to_read, |
| 226 int bytes_read); | 229 int bytes_read); |
| 227 | 230 |
| 228 // Calls DriveService's ResumeUpload with the current upload info. | 231 // Calls DriveService's ResumeUpload with the current upload info. |
| 229 void ResumeUpload(int upload_id); | 232 void ResumeUpload(int upload_id); |
| 230 | 233 |
| 231 // DriveService callback for ResumeUpload. | 234 // DriveService callback for ResumeUpload. |
| 232 void OnResumeUploadResponseReceived(int upload_id, | 235 void OnResumeUploadResponseReceived( |
| 233 const ResumeUploadResponse& response, | 236 int upload_id, |
| 234 scoped_ptr<DocumentEntry> entry); | 237 const gdata::ResumeUploadResponse& response, |
| 238 scoped_ptr<gdata::DocumentEntry> entry); |
| 235 | 239 |
| 236 // Initiate the upload. | 240 // Initiate the upload. |
| 237 void InitiateUpload(UploadFileInfo* uploader_file_info); | 241 void InitiateUpload(UploadFileInfo* uploader_file_info); |
| 238 | 242 |
| 239 // Handle failed uploads. | 243 // Handle failed uploads. |
| 240 void UploadFailed(UploadFileInfo* upload_file_info, | 244 void UploadFailed(UploadFileInfo* upload_file_info, |
| 241 DriveFileError error); | 245 DriveFileError error); |
| 242 | 246 |
| 243 // Removes |upload_file_info| from UploadFileInfoMap |pending_uploads_|. | 247 // Removes |upload_file_info| from UploadFileInfoMap |pending_uploads_|. |
| 244 // After its removal from the map, |upload_file_info| is deleted. | 248 // After its removal from the map, |upload_file_info| is deleted. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 261 // in DriveUploader dtor (i.e. we can assume |this| takes their ownership). | 265 // in DriveUploader dtor (i.e. we can assume |this| takes their ownership). |
| 262 UploadFileInfoMap pending_uploads_; | 266 UploadFileInfoMap pending_uploads_; |
| 263 | 267 |
| 264 // Note: This should remain the last member so it'll be destroyed and | 268 // Note: This should remain the last member so it'll be destroyed and |
| 265 // invalidate its weak pointers before any other members are destroyed. | 269 // invalidate its weak pointers before any other members are destroyed. |
| 266 base::WeakPtrFactory<DriveUploader> weak_ptr_factory_; | 270 base::WeakPtrFactory<DriveUploader> weak_ptr_factory_; |
| 267 | 271 |
| 268 DISALLOW_COPY_AND_ASSIGN(DriveUploader); | 272 DISALLOW_COPY_AND_ASSIGN(DriveUploader); |
| 269 }; | 273 }; |
| 270 | 274 |
| 271 } // namespace gdata | 275 } // namespace drive |
| 272 | 276 |
| 273 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_UPLOADER_H_ | 277 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_UPLOADER_H_ |
| OLD | NEW |