OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_UPLOADER_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_UPLOADER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/platform_file.h" | |
15 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" | |
16 #include "chrome/browser/chromeos/gdata/gdata_operations.h" | |
17 #include "chrome/browser/chromeos/gdata/gdata_upload_file_info.h" | |
18 #include "googleurl/src/gurl.h" | |
19 | |
20 namespace content { | |
21 class DownloadItem; | |
22 } | |
23 | |
24 namespace gdata { | |
25 | |
26 class DriveServiceInterface; | |
27 | |
28 class GDataUploaderInterface { | |
29 public: | |
30 virtual ~GDataUploaderInterface() {} | |
31 | |
32 // Uploads a new file specified by |upload_file_info|. Transfers ownership. | |
33 // Returns the upload_id. | |
34 // | |
35 // WARNING: This is not mockable by gmock because it takes scoped_ptr<>. | |
36 // See "Announcing scoped_ptr<>::Pass(). The latest in pointer ownership | |
37 // technology!" thread on chromium-dev. | |
38 virtual int UploadNewFile(scoped_ptr<UploadFileInfo> upload_file_info) = 0; | |
39 | |
40 // Stream data to an existing file from data specified by |upload_file_info|. | |
41 // Transfers ownership. Returns the upload_id. | |
42 // TODO(zork): Do not use UploadFileInfo to pass data. See: crbug.com/134819 | |
43 virtual int StreamExistingFile( | |
44 scoped_ptr<UploadFileInfo> upload_file_info) = 0; | |
45 | |
46 // Uploads an existing file (a file that already exists on Drive) | |
47 // specified by |local_file_path|. The existing file on Drive will be | |
48 // updated. Returns the upload_id. |callback| is run upon completion or | |
49 // failure. | |
50 virtual int UploadExistingFile( | |
51 const GURL& upload_location, | |
52 const FilePath& gdata_file_path, | |
53 const FilePath& local_file_path, | |
54 int64 file_size, | |
55 const std::string& content_type, | |
56 const UploadFileInfo::UploadCompletionCallback& callback) = 0; | |
57 | |
58 // Updates attributes of streaming upload. | |
59 virtual void UpdateUpload(int upload_id, | |
60 content::DownloadItem* download) = 0; | |
61 | |
62 // Returns the count of bytes confirmed as uploaded so far. | |
63 virtual int64 GetUploadedBytes(int upload_id) const = 0; | |
64 }; | |
65 | |
66 class GDataUploader : public GDataUploaderInterface { | |
67 public: | |
68 explicit GDataUploader(DriveServiceInterface* drive_service); | |
69 virtual ~GDataUploader(); | |
70 | |
71 // GDataUploaderInterface overrides. | |
72 virtual int UploadNewFile( | |
73 scoped_ptr<UploadFileInfo> upload_file_info) OVERRIDE; | |
74 virtual int StreamExistingFile( | |
75 scoped_ptr<UploadFileInfo> upload_file_info) OVERRIDE; | |
76 virtual int UploadExistingFile( | |
77 const GURL& upload_location, | |
78 const FilePath& gdata_file_path, | |
79 const FilePath& local_file_path, | |
80 int64 file_size, | |
81 const std::string& content_type, | |
82 const UploadFileInfo::UploadCompletionCallback& callback) OVERRIDE; | |
83 virtual void UpdateUpload( | |
84 int upload_id, content::DownloadItem* download) OVERRIDE; | |
85 virtual int64 GetUploadedBytes(int upload_id) const OVERRIDE; | |
86 | |
87 private: | |
88 // Lookup UploadFileInfo* in pending_uploads_. | |
89 UploadFileInfo* GetUploadFileInfo(int upload_id) const; | |
90 | |
91 // Open the file. | |
92 void OpenFile(UploadFileInfo* upload_file_info); | |
93 | |
94 // net::FileStream::Open completion callback. The result of the file | |
95 // open operation is passed as |result|. | |
96 void OpenCompletionCallback(int upload_id, int result); | |
97 | |
98 // DriveService callback for InitiateUpload. | |
99 void OnUploadLocationReceived(int upload_id, | |
100 GDataErrorCode code, | |
101 const GURL& upload_location); | |
102 | |
103 // Uploads the next chunk of data from the file. | |
104 void UploadNextChunk(UploadFileInfo* upload_file_info); | |
105 | |
106 // net::FileStream::Read completion callback. | |
107 void ReadCompletionCallback(int upload_id, | |
108 int bytes_to_read, | |
109 int bytes_read); | |
110 | |
111 // Calls DriveService's ResumeUpload with the current upload info. | |
112 void ResumeUpload(int upload_id); | |
113 | |
114 // DriveService callback for ResumeUpload. | |
115 void OnResumeUploadResponseReceived(int upload_id, | |
116 const ResumeUploadResponse& response, | |
117 scoped_ptr<DocumentEntry> entry); | |
118 | |
119 // Initiate the upload. | |
120 void InitiateUpload(UploadFileInfo* uploader_file_info); | |
121 | |
122 // Handle failed uploads. | |
123 void UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info, | |
124 DriveFileError error); | |
125 | |
126 // Removes |upload_id| from UploadFileInfoMap |pending_uploads_|. | |
127 // Note that this does not delete the UploadFileInfo object itself, | |
128 // because it may still be in use by an asynchronous function. | |
129 void RemoveUpload(int upload_id); | |
130 | |
131 // Starts uploading a file with |upload_file_info|. Returns a new upload | |
132 // ID assigned to |upload_file_info|. | |
133 int StartUploadFile(scoped_ptr<UploadFileInfo> upload_file_info); | |
134 | |
135 // Pointers to DriveServiceInterface object owned by DriveSystemService. | |
136 // The lifetime of this object is guaranteed to exceed that of the | |
137 // GDataUploader instance. | |
138 DriveServiceInterface* drive_service_; | |
139 | |
140 int next_upload_id_; // id counter. | |
141 | |
142 typedef std::map<int, UploadFileInfo*> UploadFileInfoMap; | |
143 UploadFileInfoMap pending_uploads_; | |
144 | |
145 // Note: This should remain the last member so it'll be destroyed and | |
146 // invalidate its weak pointers before any other members are destroyed. | |
147 base::WeakPtrFactory<GDataUploader> weak_ptr_factory_; | |
148 | |
149 DISALLOW_COPY_AND_ASSIGN(GDataUploader); | |
150 }; | |
151 | |
152 } // namespace gdata | |
153 | |
154 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_UPLOADER_H_ | |
OLD | NEW |