NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / drive / drive_uploader.h
blob1fd8191482ffb5d3cb47510ba8f4f1bd138aa14d
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.
5 #ifndef CHROME_BROWSER_DRIVE_DRIVE_UPLOADER_H_
6 #define CHROME_BROWSER_DRIVE_DRIVE_UPLOADER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/drive/drive_service_interface.h"
15 #include "google_apis/drive/gdata_errorcode.h"
16 #include "google_apis/drive/gdata_wapi_parser.h"
18 class GURL;
20 namespace base {
21 class FilePath;
22 class TaskRunner;
25 namespace google_apis {
26 struct UploadRangeResponse;
29 namespace drive {
30 class DriveServiceInterface;
32 // Callback to be invoked once the upload has completed.
33 // |upload_location| will be returned when the uploading process is started but
34 // terminated before the completion due to some errors. It can be used to
35 // resume it.
36 typedef base::Callback<void(
37 google_apis::GDataErrorCode error,
38 const GURL& upload_location,
39 scoped_ptr<google_apis::ResourceEntry> resource_entry)>
40 UploadCompletionCallback;
42 class DriveUploaderInterface {
43 public:
44 typedef DriveServiceInterface::InitiateUploadNewFileOptions
45 UploadNewFileOptions;
46 typedef DriveServiceInterface::InitiateUploadExistingFileOptions
47 UploadExistingFileOptions;
49 virtual ~DriveUploaderInterface() {}
51 // Uploads a new file to a directory specified by |upload_location|.
52 // Returns a callback for cancelling the uploading job.
54 // parent_resource_id:
55 // resource id of the destination directory.
57 // local_file_path:
58 // The path to the local file to be uploaded.
60 // title:
61 // The title (file name) of the file to be uploaded.
63 // content_type:
64 // The content type of the file to be uploaded.
66 // callback:
67 // Called when an upload is done regardless of it was successful or not.
68 // Must not be null.
70 // progress_callback:
71 // Periodically called back with the total number of bytes sent so far.
72 // May be null if the information is not needed.
73 virtual google_apis::CancelCallback UploadNewFile(
74 const std::string& parent_resource_id,
75 const base::FilePath& local_file_path,
76 const std::string& title,
77 const std::string& content_type,
78 const UploadNewFileOptions& options,
79 const UploadCompletionCallback& callback,
80 const google_apis::ProgressCallback& progress_callback) = 0;
82 // Uploads an existing file (a file that already exists on Drive).
84 // See comments at UploadNewFile about common parameters and the return value.
86 // resource_id:
87 // resource id of the existing file to be overwritten.
89 // etag:
90 // Expected ETag for the destination file. If it does not match, the upload
91 // fails with UPLOAD_ERROR_CONFLICT.
92 // If |etag| is empty, the test is skipped.
93 virtual google_apis::CancelCallback UploadExistingFile(
94 const std::string& resource_id,
95 const base::FilePath& local_file_path,
96 const std::string& content_type,
97 const UploadExistingFileOptions& options,
98 const UploadCompletionCallback& callback,
99 const google_apis::ProgressCallback& progress_callback) = 0;
101 // Resumes the uploading process terminated before the completion.
102 // |upload_location| should be the one returned via UploadCompletionCallback
103 // for previous invocation. |drive_file_path|, |local_file_path| and
104 // |content_type| must be set to the same ones for previous invocation.
106 // See comments at UploadNewFile about common parameters and the return value.
107 virtual google_apis::CancelCallback ResumeUploadFile(
108 const GURL& upload_location,
109 const base::FilePath& local_file_path,
110 const std::string& content_type,
111 const UploadCompletionCallback& callback,
112 const google_apis::ProgressCallback& progress_callback) = 0;
115 class DriveUploader : public DriveUploaderInterface {
116 public:
117 DriveUploader(DriveServiceInterface* drive_service,
118 base::TaskRunner* blocking_task_runner);
119 virtual ~DriveUploader();
121 // DriveUploaderInterface overrides.
122 virtual google_apis::CancelCallback UploadNewFile(
123 const std::string& parent_resource_id,
124 const base::FilePath& local_file_path,
125 const std::string& title,
126 const std::string& content_type,
127 const UploadNewFileOptions& options,
128 const UploadCompletionCallback& callback,
129 const google_apis::ProgressCallback& progress_callback) OVERRIDE;
130 virtual google_apis::CancelCallback UploadExistingFile(
131 const std::string& resource_id,
132 const base::FilePath& local_file_path,
133 const std::string& content_type,
134 const UploadExistingFileOptions& options,
135 const UploadCompletionCallback& callback,
136 const google_apis::ProgressCallback& progress_callback) OVERRIDE;
137 virtual google_apis::CancelCallback ResumeUploadFile(
138 const GURL& upload_location,
139 const base::FilePath& local_file_path,
140 const std::string& content_type,
141 const UploadCompletionCallback& callback,
142 const google_apis::ProgressCallback& progress_callback) OVERRIDE;
144 private:
145 struct UploadFileInfo;
146 typedef base::Callback<void(scoped_ptr<UploadFileInfo> upload_file_info)>
147 StartInitiateUploadCallback;
149 // Starts uploading a file with |upload_file_info|.
150 google_apis::CancelCallback StartUploadFile(
151 scoped_ptr<UploadFileInfo> upload_file_info,
152 const StartInitiateUploadCallback& start_initiate_upload_callback);
153 void StartUploadFileAfterGetFileSize(
154 scoped_ptr<UploadFileInfo> upload_file_info,
155 const StartInitiateUploadCallback& start_initiate_upload_callback,
156 bool get_file_size_result);
158 // Starts to initiate the new file uploading.
159 // Upon completion, OnUploadLocationReceived should be called.
160 void StartInitiateUploadNewFile(
161 const std::string& parent_resource_id,
162 const std::string& title,
163 const UploadNewFileOptions& options,
164 scoped_ptr<UploadFileInfo> upload_file_info);
166 // Starts to initiate the existing file uploading.
167 // Upon completion, OnUploadLocationReceived should be called.
168 void StartInitiateUploadExistingFile(
169 const std::string& resource_id,
170 const UploadExistingFileOptions& options,
171 scoped_ptr<UploadFileInfo> upload_file_info);
173 // DriveService callback for InitiateUpload.
174 void OnUploadLocationReceived(scoped_ptr<UploadFileInfo> upload_file_info,
175 google_apis::GDataErrorCode code,
176 const GURL& upload_location);
178 // Starts to get the current upload status for the file uploading.
179 // Upon completion, OnUploadRangeResponseReceived should be called.
180 void StartGetUploadStatus(scoped_ptr<UploadFileInfo> upload_file_info);
182 // Uploads the next chunk of data from the file.
183 void UploadNextChunk(scoped_ptr<UploadFileInfo> upload_file_info);
185 // DriveService callback for ResumeUpload.
186 void OnUploadRangeResponseReceived(
187 scoped_ptr<UploadFileInfo> upload_file_info,
188 const google_apis::UploadRangeResponse& response,
189 scoped_ptr<google_apis::ResourceEntry> entry);
190 void OnUploadProgress(const google_apis::ProgressCallback& callback,
191 int64 start_position,
192 int64 total_size,
193 int64 progress_of_chunk,
194 int64 total_of_chunk);
196 // Handle failed uploads.
197 void UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info,
198 google_apis::GDataErrorCode error);
200 // The lifetime of this object should be guaranteed to exceed that of the
201 // DriveUploader instance.
202 DriveServiceInterface* drive_service_; // Not owned by this class.
204 scoped_refptr<base::TaskRunner> blocking_task_runner_;
206 // Note: This should remain the last member so it'll be destroyed and
207 // invalidate its weak pointers before any other members are destroyed.
208 base::WeakPtrFactory<DriveUploader> weak_ptr_factory_;
209 DISALLOW_COPY_AND_ASSIGN(DriveUploader);
212 } // namespace drive
214 #endif // CHROME_BROWSER_DRIVE_DRIVE_UPLOADER_H_