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