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_
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 "chrome/browser/drive/drive_service_interface.h"
16 #include "google_apis/drive/drive_api_error_codes.h"
25 namespace google_apis
{
26 struct UploadRangeResponse
;
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
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
{
44 virtual ~DriveUploaderInterface() {}
46 // Uploads a new file to a directory specified by |upload_location|.
47 // Returns a callback for cancelling the uploading job.
49 // parent_resource_id:
50 // resource id of the destination directory.
53 // The path to the local file to be uploaded.
56 // The title (file name) of the file to be uploaded.
59 // The content type of the file to be uploaded.
62 // Called when an upload is done regardless of it was successful or not.
66 // Periodically called back with the total number of bytes sent so far.
67 // May be null if the information is not needed.
68 virtual google_apis::CancelCallback
UploadNewFile(
69 const std::string
& parent_resource_id
,
70 const base::FilePath
& local_file_path
,
71 const std::string
& title
,
72 const std::string
& content_type
,
73 const UploadNewFileOptions
& options
,
74 const UploadCompletionCallback
& callback
,
75 const google_apis::ProgressCallback
& progress_callback
) = 0;
77 // Uploads an existing file (a file that already exists on Drive).
79 // See comments at UploadNewFile about common parameters and the return value.
82 // resource id of the existing file to be overwritten.
85 // Expected ETag for the destination file. If it does not match, the upload
86 // fails with UPLOAD_ERROR_CONFLICT.
87 // If |etag| is empty, the test is skipped.
88 virtual google_apis::CancelCallback
UploadExistingFile(
89 const std::string
& resource_id
,
90 const base::FilePath
& local_file_path
,
91 const std::string
& content_type
,
92 const UploadExistingFileOptions
& options
,
93 const UploadCompletionCallback
& callback
,
94 const google_apis::ProgressCallback
& progress_callback
) = 0;
96 // Resumes the uploading process terminated before the completion.
97 // |upload_location| should be the one returned via UploadCompletionCallback
98 // for previous invocation. |drive_file_path|, |local_file_path| and
99 // |content_type| must be set to the same ones for previous invocation.
101 // See comments at UploadNewFile about common parameters and the return value.
102 virtual google_apis::CancelCallback
ResumeUploadFile(
103 const GURL
& upload_location
,
104 const base::FilePath
& local_file_path
,
105 const std::string
& content_type
,
106 const UploadCompletionCallback
& callback
,
107 const google_apis::ProgressCallback
& progress_callback
) = 0;
110 class DriveUploader
: public DriveUploaderInterface
{
112 DriveUploader(DriveServiceInterface
* drive_service
,
113 const scoped_refptr
<base::TaskRunner
>& blocking_task_runner
);
114 ~DriveUploader() override
;
116 // DriveUploaderInterface overrides.
117 google_apis::CancelCallback
UploadNewFile(
118 const std::string
& parent_resource_id
,
119 const base::FilePath
& local_file_path
,
120 const std::string
& title
,
121 const std::string
& content_type
,
122 const UploadNewFileOptions
& options
,
123 const UploadCompletionCallback
& callback
,
124 const google_apis::ProgressCallback
& progress_callback
) override
;
125 google_apis::CancelCallback
UploadExistingFile(
126 const std::string
& resource_id
,
127 const base::FilePath
& local_file_path
,
128 const std::string
& content_type
,
129 const UploadExistingFileOptions
& options
,
130 const UploadCompletionCallback
& callback
,
131 const google_apis::ProgressCallback
& progress_callback
) override
;
132 google_apis::CancelCallback
ResumeUploadFile(
133 const GURL
& upload_location
,
134 const base::FilePath
& local_file_path
,
135 const std::string
& content_type
,
136 const UploadCompletionCallback
& callback
,
137 const google_apis::ProgressCallback
& progress_callback
) override
;
140 struct UploadFileInfo
;
141 typedef base::Callback
<void(scoped_ptr
<UploadFileInfo
> upload_file_info
)>
142 StartInitiateUploadCallback
;
144 // Starts uploading a file with |upload_file_info|.
145 google_apis::CancelCallback
StartUploadFile(
146 scoped_ptr
<UploadFileInfo
> upload_file_info
,
147 const StartInitiateUploadCallback
& start_initiate_upload_callback
);
148 void StartUploadFileAfterGetFileSize(
149 scoped_ptr
<UploadFileInfo
> upload_file_info
,
150 const StartInitiateUploadCallback
& start_initiate_upload_callback
,
151 bool get_file_size_result
);
153 // Checks file size and call InitiateUploadNewFile or MultipartUploadNewFile
154 // API. Upon completion, OnUploadLocationReceived (for InitiateUploadNewFile)
155 // or OnMultipartUploadComplete (for MultipartUploadNewFile) should be called.
156 void CallUploadServiceAPINewFile(const std::string
& parent_resource_id
,
157 const std::string
& title
,
158 const UploadNewFileOptions
& options
,
159 scoped_ptr
<UploadFileInfo
> upload_file_info
);
161 // Checks file size and call InitiateUploadExistingFile or
162 // MultipartUploadExistingFile API. Upon completion, OnUploadLocationReceived
163 // (for InitiateUploadExistingFile) or OnMultipartUploadComplete (for
164 // MultipartUploadExistingFile) should be called.
165 void CallUploadServiceAPIExistingFile(
166 const std::string
& resource_id
,
167 const UploadExistingFileOptions
& options
,
168 scoped_ptr
<UploadFileInfo
> upload_file_info
);
170 // DriveService callback for InitiateUpload.
171 void OnUploadLocationReceived(scoped_ptr
<UploadFileInfo
> upload_file_info
,
172 google_apis::DriveApiErrorCode code
,
173 const GURL
& upload_location
);
175 // Starts to get the current upload status for the file uploading.
176 // Upon completion, OnUploadRangeResponseReceived should be called.
177 void StartGetUploadStatus(scoped_ptr
<UploadFileInfo
> upload_file_info
);
179 // Uploads the next chunk of data from the file.
180 void UploadNextChunk(scoped_ptr
<UploadFileInfo
> upload_file_info
);
182 // DriveService callback for ResumeUpload.
183 void OnUploadRangeResponseReceived(
184 scoped_ptr
<UploadFileInfo
> upload_file_info
,
185 const google_apis::UploadRangeResponse
& response
,
186 scoped_ptr
<google_apis::FileResource
> entry
);
187 void OnUploadProgress(const google_apis::ProgressCallback
& callback
,
188 int64 start_position
,
190 int64 progress_of_chunk
,
191 int64 total_of_chunk
);
193 // Handles failed uploads.
194 void UploadFailed(scoped_ptr
<UploadFileInfo
> upload_file_info
,
195 google_apis::DriveApiErrorCode error
);
197 // Handles completion/error of multipart uploading.
198 void OnMultipartUploadComplete(scoped_ptr
<UploadFileInfo
> upload_file_info
,
199 google_apis::DriveApiErrorCode error
,
200 scoped_ptr
<google_apis::FileResource
> entry
);
202 // The class is expected to run on UI thread.
203 base::ThreadChecker thread_checker_
;
205 // The lifetime of this object should be guaranteed to exceed that of the
206 // DriveUploader instance.
207 DriveServiceInterface
* drive_service_
; // Not owned by this class.
209 scoped_refptr
<base::TaskRunner
> blocking_task_runner_
;
211 // Note: This should remain the last member so it'll be destroyed and
212 // invalidate its weak pointers before any other members are destroyed.
213 base::WeakPtrFactory
<DriveUploader
> weak_ptr_factory_
;
214 DISALLOW_COPY_AND_ASSIGN(DriveUploader
);
219 #endif // CHROME_BROWSER_DRIVE_DRIVE_UPLOADER_H_