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_GOOGLE_APIS_DRIVE_UPLOADER_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_DRIVE_UPLOADER_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/browser/google_apis/drive_service_interface.h"
15 #include "chrome/browser/google_apis/drive_upload_error.h"
16 #include "chrome/browser/google_apis/gdata_errorcode.h"
17 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
25 namespace google_apis
{
26 class DriveServiceInterface
;
27 struct UploadRangeResponse
;
29 // Callback to be invoked once the upload has completed.
30 typedef base::Callback
<void(DriveUploadError error
,
31 const base::FilePath
& drive_path
,
32 const base::FilePath
& file_path
,
33 scoped_ptr
<ResourceEntry
> resource_entry
)>
34 UploadCompletionCallback
;
36 class DriveUploaderInterface
{
38 virtual ~DriveUploaderInterface() {}
40 // Uploads a new file to a directory specified by |upload_location|.
41 // Returns the upload_id.
43 // parent_resource_id:
44 // resource id of the destination directory.
47 // The destination path like "drive/foo/bar.txt".
50 // The path to the local file to be uploaded.
53 // The title (file name) of the file to be uploaded.
56 // The content type of the file to be uploaded.
59 // Called when an upload is done regardless of it was successful or not.
63 // Periodically called back with the total number of bytes sent so far.
64 // May be null if the information is not needed.
65 virtual void UploadNewFile(const std::string
& parent_resource_id
,
66 const base::FilePath
& drive_file_path
,
67 const base::FilePath
& local_file_path
,
68 const std::string
& title
,
69 const std::string
& content_type
,
70 const UploadCompletionCallback
& callback
,
71 const ProgressCallback
& progress_callback
) = 0;
73 // Uploads an existing file (a file that already exists on Drive).
75 // See comments at UploadNewFile() about common parameters.
78 // resource id of the existing file to be overwritten.
81 // Expected ETag for the destination file. If it does not match, the upload
82 // fails with UPLOAD_ERROR_CONFLICT.
83 // If |etag| is empty, the test is skipped.
84 virtual void UploadExistingFile(
85 const std::string
& resource_id
,
86 const base::FilePath
& drive_file_path
,
87 const base::FilePath
& local_file_path
,
88 const std::string
& content_type
,
89 const std::string
& etag
,
90 const UploadCompletionCallback
& callback
,
91 const ProgressCallback
& progress_callback
) = 0;
94 class DriveUploader
: public DriveUploaderInterface
{
96 explicit DriveUploader(DriveServiceInterface
* drive_service
);
97 virtual ~DriveUploader();
99 // DriveUploaderInterface overrides.
100 virtual void UploadNewFile(
101 const std::string
& parent_resource_id
,
102 const base::FilePath
& drive_file_path
,
103 const base::FilePath
& local_file_path
,
104 const std::string
& title
,
105 const std::string
& content_type
,
106 const UploadCompletionCallback
& callback
,
107 const ProgressCallback
& progress_callback
) OVERRIDE
;
108 virtual void UploadExistingFile(
109 const std::string
& resource_id
,
110 const base::FilePath
& drive_file_path
,
111 const base::FilePath
& local_file_path
,
112 const std::string
& content_type
,
113 const std::string
& etag
,
114 const UploadCompletionCallback
& callback
,
115 const ProgressCallback
& progress_callback
) OVERRIDE
;
118 struct UploadFileInfo
;
119 typedef base::Callback
<void(scoped_ptr
<UploadFileInfo
> upload_file_info
)>
120 StartInitiateUploadCallback
;
122 // Starts uploading a file with |upload_file_info|.
123 void StartUploadFile(
124 scoped_ptr
<UploadFileInfo
> upload_file_info
,
125 const StartInitiateUploadCallback
& start_initiate_upload_callback
);
127 // net::FileStream::Open completion callback. The result of the file open
128 // operation is passed as |result|, and the size is stored in |file_size|.
129 void OpenCompletionCallback(
130 scoped_ptr
<UploadFileInfo
> upload_file_info
,
131 const StartInitiateUploadCallback
& start_initiate_upload_callback
,
134 // Starts to initate the new file uploading.
135 // Upon completion, OnUploadLocationReceived should be called.
136 void StartInitiateUploadNewFile(
137 const std::string
& parent_resource_id
,
138 const std::string
& title
,
139 scoped_ptr
<UploadFileInfo
> upload_file_info
);
141 // Starts to initate the existing file uploading.
142 // Upon completion, OnUploadLocationReceived should be called.
143 void StartInitiateUploadExistingFile(
144 const std::string
& resource_id
,
145 const std::string
& etag
,
146 scoped_ptr
<UploadFileInfo
> upload_file_info
);
148 // DriveService callback for InitiateUpload.
149 void OnUploadLocationReceived(scoped_ptr
<UploadFileInfo
> upload_file_info
,
151 const GURL
& upload_location
);
153 // Uploads the next chunk of data from the file.
154 void UploadNextChunk(scoped_ptr
<UploadFileInfo
> upload_file_info
);
156 // net::FileStream::Read completion callback.
157 void ReadCompletionCallback(scoped_ptr
<UploadFileInfo
> upload_file_info
,
161 // Calls DriveService's ResumeUpload with the current upload info.
162 void ResumeUpload(scoped_ptr
<UploadFileInfo
> upload_file_info
,
165 // DriveService callback for ResumeUpload.
166 void OnUploadRangeResponseReceived(
167 scoped_ptr
<UploadFileInfo
> upload_file_info
,
168 const UploadRangeResponse
& response
,
169 scoped_ptr
<ResourceEntry
> entry
);
170 void OnUploadProgress(const ProgressCallback
& callback
,
171 int64 start_position
,
173 int64 progress_of_chunk
,
174 int64 total_of_chunk
);
176 // Handle failed uploads.
177 void UploadFailed(scoped_ptr
<UploadFileInfo
> upload_file_info
,
178 DriveUploadError error
);
180 // Pointers to DriveServiceInterface object owned by DriveSystemService.
181 // The lifetime of this object is guaranteed to exceed that of the
182 // DriveUploader instance.
183 DriveServiceInterface
* drive_service_
;
185 // TaskRunner for opening, reading, and closing files.
186 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
188 // Note: This should remain the last member so it'll be destroyed and
189 // invalidate its weak pointers before any other members are destroyed.
190 base::WeakPtrFactory
<DriveUploader
> weak_ptr_factory_
;
192 DISALLOW_COPY_AND_ASSIGN(DriveUploader
);
195 } // namespace google_apis
197 #endif // CHROME_BROWSER_GOOGLE_APIS_DRIVE_UPLOADER_H_