Add 'did_proceed' and 'repeat_visit' to ClientMalwareReportRequest to track CTR.
[chromium-blink-merge.git] / components / drive / drive_uploader.h
blobe383614fdd7fcb1fc9286bed5666f27425f4d77a
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 COMPONENTS_DRIVE_DRIVE_UPLOADER_H_
6 #define COMPONENTS_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 "base/threading/thread_checker.h"
15 #include "components/drive/service/drive_service_interface.h"
16 #include "google_apis/drive/drive_api_error_codes.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::DriveApiErrorCode error,
38 const GURL& upload_location,
39 scoped_ptr<google_apis::FileResource> resource_entry)>
40 UploadCompletionCallback;
42 class DriveUploaderInterface {
43 public:
44 virtual ~DriveUploaderInterface() {}
46 // Starts batch processing for upload requests. All requests which upload
47 // small files (less than kMaxMultipartUploadSize) between
48 // |StartBatchProcessing| and |StopBatchProcessing| are sent as a single batch
49 // request.
50 virtual void StartBatchProcessing() = 0;
52 // Stops batch processing. Must be called after calling |StartBatchProcessing|
53 // to commit requests.
54 virtual void StopBatchProcessing() = 0;
56 // Uploads a new file to a directory specified by |upload_location|.
57 // Returns a callback for cancelling the uploading job.
59 // parent_resource_id:
60 // resource id of the destination directory.
62 // local_file_path:
63 // The path to the local file to be uploaded.
65 // title:
66 // The title (file name) of the file to be uploaded.
68 // content_type:
69 // The content type of the file to be uploaded.
71 // callback:
72 // Called when an upload is done regardless of it was successful or not.
73 // Must not be null.
75 // progress_callback:
76 // Periodically called back with the total number of bytes sent so far.
77 // May be null if the information is not needed.
78 virtual google_apis::CancelCallback UploadNewFile(
79 const std::string& parent_resource_id,
80 const base::FilePath& local_file_path,
81 const std::string& title,
82 const std::string& content_type,
83 const UploadNewFileOptions& options,
84 const UploadCompletionCallback& callback,
85 const google_apis::ProgressCallback& progress_callback) = 0;
87 // Uploads an existing file (a file that already exists on Drive).
89 // See comments at UploadNewFile about common parameters and the return value.
91 // resource_id:
92 // resource id of the existing file to be overwritten.
94 // etag:
95 // Expected ETag for the destination file. If it does not match, the upload
96 // fails with UPLOAD_ERROR_CONFLICT.
97 // If |etag| is empty, the test is skipped.
98 virtual google_apis::CancelCallback UploadExistingFile(
99 const std::string& resource_id,
100 const base::FilePath& local_file_path,
101 const std::string& content_type,
102 const UploadExistingFileOptions& options,
103 const UploadCompletionCallback& callback,
104 const google_apis::ProgressCallback& progress_callback) = 0;
106 // Resumes the uploading process terminated before the completion.
107 // |upload_location| should be the one returned via UploadCompletionCallback
108 // for previous invocation. |drive_file_path|, |local_file_path| and
109 // |content_type| must be set to the same ones for previous invocation.
111 // See comments at UploadNewFile about common parameters and the return value.
112 virtual google_apis::CancelCallback ResumeUploadFile(
113 const GURL& upload_location,
114 const base::FilePath& local_file_path,
115 const std::string& content_type,
116 const UploadCompletionCallback& callback,
117 const google_apis::ProgressCallback& progress_callback) = 0;
120 class DriveUploader : public DriveUploaderInterface {
121 public:
122 DriveUploader(DriveServiceInterface* drive_service,
123 const scoped_refptr<base::TaskRunner>& blocking_task_runner);
124 ~DriveUploader() override;
126 // DriveUploaderInterface overrides.
127 void StartBatchProcessing() override;
128 void StopBatchProcessing() override;
129 google_apis::CancelCallback UploadNewFile(
130 const std::string& parent_resource_id,
131 const base::FilePath& local_file_path,
132 const std::string& title,
133 const std::string& content_type,
134 const UploadNewFileOptions& options,
135 const UploadCompletionCallback& callback,
136 const google_apis::ProgressCallback& progress_callback) override;
137 google_apis::CancelCallback UploadExistingFile(
138 const std::string& resource_id,
139 const base::FilePath& local_file_path,
140 const std::string& content_type,
141 const UploadExistingFileOptions& options,
142 const UploadCompletionCallback& callback,
143 const google_apis::ProgressCallback& progress_callback) override;
144 google_apis::CancelCallback ResumeUploadFile(
145 const GURL& upload_location,
146 const base::FilePath& local_file_path,
147 const std::string& content_type,
148 const UploadCompletionCallback& callback,
149 const google_apis::ProgressCallback& progress_callback) override;
151 private:
152 class RefCountedBatchRequest;
153 struct UploadFileInfo;
154 typedef base::Callback<void(scoped_ptr<UploadFileInfo> upload_file_info)>
155 StartInitiateUploadCallback;
157 // Starts uploading a file with |upload_file_info|.
158 google_apis::CancelCallback StartUploadFile(
159 scoped_ptr<UploadFileInfo> upload_file_info,
160 const StartInitiateUploadCallback& start_initiate_upload_callback);
161 void StartUploadFileAfterGetFileSize(
162 scoped_ptr<UploadFileInfo> upload_file_info,
163 const StartInitiateUploadCallback& start_initiate_upload_callback,
164 bool get_file_size_result);
166 // Checks file size and call InitiateUploadNewFile or MultipartUploadNewFile
167 // API. Upon completion, OnUploadLocationReceived (for InitiateUploadNewFile)
168 // or OnMultipartUploadComplete (for MultipartUploadNewFile) should be called.
169 // If |batch_request| is non-null, it calls the API function on the batch
170 // request.
171 void CallUploadServiceAPINewFile(
172 const std::string& parent_resource_id,
173 const std::string& title,
174 const UploadNewFileOptions& options,
175 const scoped_refptr<RefCountedBatchRequest>& batch_request,
176 scoped_ptr<UploadFileInfo> upload_file_info);
178 // Checks file size and call InitiateUploadExistingFile or
179 // MultipartUploadExistingFile API. Upon completion, OnUploadLocationReceived
180 // (for InitiateUploadExistingFile) or OnMultipartUploadComplete (for
181 // MultipartUploadExistingFile) should be called.
182 // If |batch_request| is non-null, it calls the API function on the batch
183 // request.
184 void CallUploadServiceAPIExistingFile(
185 const std::string& resource_id,
186 const UploadExistingFileOptions& options,
187 const scoped_refptr<RefCountedBatchRequest>& batch_request,
188 scoped_ptr<UploadFileInfo> upload_file_info);
190 // DriveService callback for InitiateUpload.
191 void OnUploadLocationReceived(scoped_ptr<UploadFileInfo> upload_file_info,
192 google_apis::DriveApiErrorCode code,
193 const GURL& upload_location);
195 // Starts to get the current upload status for the file uploading.
196 // Upon completion, OnUploadRangeResponseReceived should be called.
197 void StartGetUploadStatus(scoped_ptr<UploadFileInfo> upload_file_info);
199 // Uploads the next chunk of data from the file.
200 void UploadNextChunk(scoped_ptr<UploadFileInfo> upload_file_info);
202 // DriveService callback for ResumeUpload.
203 void OnUploadRangeResponseReceived(
204 scoped_ptr<UploadFileInfo> upload_file_info,
205 const google_apis::UploadRangeResponse& response,
206 scoped_ptr<google_apis::FileResource> entry);
207 void OnUploadProgress(const google_apis::ProgressCallback& callback,
208 int64 start_position,
209 int64 total_size,
210 int64 progress_of_chunk,
211 int64 total_of_chunk);
213 // Handles failed uploads.
214 void UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info,
215 google_apis::DriveApiErrorCode error);
217 // Handles completion/error of multipart uploading.
218 void OnMultipartUploadComplete(scoped_ptr<UploadFileInfo> upload_file_info,
219 google_apis::DriveApiErrorCode error,
220 scoped_ptr<google_apis::FileResource> entry);
222 // The class is expected to run on UI thread.
223 base::ThreadChecker thread_checker_;
225 // The lifetime of this object should be guaranteed to exceed that of the
226 // DriveUploader instance.
227 DriveServiceInterface* drive_service_; // Not owned by this class.
229 scoped_refptr<base::TaskRunner> blocking_task_runner_;
230 scoped_refptr<RefCountedBatchRequest> current_batch_request_;
232 // Note: This should remain the last member so it'll be destroyed and
233 // invalidate its weak pointers before any other members are destroyed.
234 base::WeakPtrFactory<DriveUploader> weak_ptr_factory_;
235 DISALLOW_COPY_AND_ASSIGN(DriveUploader);
238 } // namespace drive
240 #endif // COMPONENTS_DRIVE_DRIVE_UPLOADER_H_