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