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 #include "chrome/browser/chromeos/gdata/gdata_protocol_handler.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_protocol_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
18 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
20 #include "chrome/browser/chromeos/gdata/drive.pb.h" | 20 #include "chrome/browser/chromeos/gdata/drive.pb.h" |
| 21 #include "chrome/browser/chromeos/gdata/drive_file_system_interface.h" |
21 #include "chrome/browser/chromeos/gdata/drive_service_interface.h" | 22 #include "chrome/browser/chromeos/gdata/drive_service_interface.h" |
22 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" | 23 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" |
23 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" | |
24 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" | 24 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
25 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 25 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/browser/profiles/profile_manager.h" | 27 #include "chrome/browser/profiles/profile_manager.h" |
28 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
29 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
30 #include "net/base/escape.h" | 30 #include "net/base/escape.h" |
31 #include "net/base/file_stream.h" | 31 #include "net/base/file_stream.h" |
32 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
33 #include "net/http/http_request_headers.h" | 33 #include "net/http/http_request_headers.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 *resource_id = net::UnescapeURLComponent(id, kUrlPathUnescapeMask); | 104 *resource_id = net::UnescapeURLComponent(id, kUrlPathUnescapeMask); |
105 return resource_id->size(); | 105 return resource_id->size(); |
106 } | 106 } |
107 | 107 |
108 // Helper function to get GDataSystemService from Profile. | 108 // Helper function to get GDataSystemService from Profile. |
109 GDataSystemService* GetSystemService() { | 109 GDataSystemService* GetSystemService() { |
110 return GDataSystemServiceFactory::GetForProfile( | 110 return GDataSystemServiceFactory::GetForProfile( |
111 ProfileManager::GetDefaultProfile()); | 111 ProfileManager::GetDefaultProfile()); |
112 } | 112 } |
113 | 113 |
114 // Helper function to get GDataFileSystem from Profile on UI thread. | 114 // Helper function to get DriveFileSystem from Profile on UI thread. |
115 void GetFileSystemOnUIThread(GDataFileSystemInterface** file_system) { | 115 void GetFileSystemOnUIThread(DriveFileSystemInterface** file_system) { |
116 GDataSystemService* system_service = GetSystemService(); | 116 GDataSystemService* system_service = GetSystemService(); |
117 *file_system = system_service ? system_service->file_system() : NULL; | 117 *file_system = system_service ? system_service->file_system() : NULL; |
118 } | 118 } |
119 | 119 |
120 // Helper function to cancel GData download operation on UI thread. | 120 // Helper function to cancel GData download operation on UI thread. |
121 void CancelGDataDownloadOnUIThread(const FilePath& gdata_file_path) { | 121 void CancelGDataDownloadOnUIThread(const FilePath& gdata_file_path) { |
122 GDataSystemService* system_service = GetSystemService(); | 122 GDataSystemService* system_service = GetSystemService(); |
123 if (system_service) | 123 if (system_service) |
124 system_service->drive_service()->operation_registry()->CancelForFilePath( | 124 system_service->drive_service()->operation_registry()->CancelForFilePath( |
125 gdata_file_path); | 125 gdata_file_path); |
(...skipping 14 matching lines...) Expand all Loading... |
140 virtual int GetResponseCode() const OVERRIDE; | 140 virtual int GetResponseCode() const OVERRIDE; |
141 virtual bool ReadRawData(net::IOBuffer* buf, | 141 virtual bool ReadRawData(net::IOBuffer* buf, |
142 int buf_size, | 142 int buf_size, |
143 int* bytes_read) OVERRIDE; | 143 int* bytes_read) OVERRIDE; |
144 | 144 |
145 protected: | 145 protected: |
146 virtual ~GDataURLRequestJob(); | 146 virtual ~GDataURLRequestJob(); |
147 | 147 |
148 private: | 148 private: |
149 // Helper for Start() to let us start asynchronously. | 149 // Helper for Start() to let us start asynchronously. |
150 void StartAsync(GDataFileSystemInterface** file_system); | 150 void StartAsync(DriveFileSystemInterface** file_system); |
151 | 151 |
152 // Helper methods for Delegate::OnUrlFetchDownloadData and ReadRawData to | 152 // Helper methods for Delegate::OnUrlFetchDownloadData and ReadRawData to |
153 // receive download data and copy to response buffer. | 153 // receive download data and copy to response buffer. |
154 // For detailed description of logic, refer to comments in definitions of | 154 // For detailed description of logic, refer to comments in definitions of |
155 // Start() and ReadRawData(). | 155 // Start() and ReadRawData(). |
156 | 156 |
157 void OnUrlFetchDownloadData(GDataErrorCode error, | 157 void OnUrlFetchDownloadData(GDataErrorCode error, |
158 scoped_ptr<std::string> download_data); | 158 scoped_ptr<std::string> download_data); |
159 // Called from ReadRawData, returns true if data is ready, false otherwise. | 159 // Called from ReadRawData, returns true if data is ready, false otherwise. |
160 bool ContinueReadFromDownloadData(int* bytes_read); | 160 bool ContinueReadFromDownloadData(int* bytes_read); |
161 // Copies from download buffer into response buffer. | 161 // Copies from download buffer into response buffer. |
162 bool ReadFromDownloadData(); | 162 bool ReadFromDownloadData(); |
163 | 163 |
164 // Helper callback for handling async responses from | 164 // Helper callback for handling async responses from |
165 // GDataFileSystem::GetFileByResourceId(). | 165 // DriveFileSystem::GetFileByResourceId(). |
166 void OnGetFileByResourceId(DriveFileError error, | 166 void OnGetFileByResourceId(DriveFileError error, |
167 const FilePath& local_file_path, | 167 const FilePath& local_file_path, |
168 const std::string& mime_type, | 168 const std::string& mime_type, |
169 DriveFileType file_type); | 169 DriveFileType file_type); |
170 | 170 |
171 // Helper callback for GetFileSizeOnBlockingPool that sets |remaining_bytes_| | 171 // Helper callback for GetFileSizeOnBlockingPool that sets |remaining_bytes_| |
172 // to |file_size|, and notifies result for Start(). | 172 // to |file_size|, and notifies result for Start(). |
173 void OnGetFileSize(int64 *file_size); | 173 void OnGetFileSize(int64 *file_size); |
174 | 174 |
175 // Helper callback for GetEntryInfoByResourceId invoked by StartAsync. | 175 // Helper callback for GetEntryInfoByResourceId invoked by StartAsync. |
(...skipping 21 matching lines...) Expand all Loading... |
197 // Helper methods to fomulate and notify about response status, info and | 197 // Helper methods to fomulate and notify about response status, info and |
198 // headers. | 198 // headers. |
199 void NotifySuccess(); | 199 void NotifySuccess(); |
200 void NotifyFailure(int); | 200 void NotifyFailure(int); |
201 void HeadersCompleted(int status_code, const std::string& status_txt); | 201 void HeadersCompleted(int status_code, const std::string& status_txt); |
202 | 202 |
203 // Helper method to close |stream_|. | 203 // Helper method to close |stream_|. |
204 void CloseFileStream(); | 204 void CloseFileStream(); |
205 | 205 |
206 scoped_ptr<base::WeakPtrFactory<GDataURLRequestJob> > weak_ptr_factory_; | 206 scoped_ptr<base::WeakPtrFactory<GDataURLRequestJob> > weak_ptr_factory_; |
207 GDataFileSystemInterface* file_system_; | 207 DriveFileSystemInterface* file_system_; |
208 | 208 |
209 bool error_; // True if we've encountered an error. | 209 bool error_; // True if we've encountered an error. |
210 bool headers_set_; // True if headers have been set. | 210 bool headers_set_; // True if headers have been set. |
211 | 211 |
212 FilePath local_file_path_; | 212 FilePath local_file_path_; |
213 FilePath gdata_file_path_; | 213 FilePath gdata_file_path_; |
214 std::string mime_type_; | 214 std::string mime_type_; |
215 int64 initial_file_size_; | 215 int64 initial_file_size_; |
216 int64 remaining_bytes_; | 216 int64 remaining_bytes_; |
217 scoped_ptr<net::FileStream> stream_; | 217 scoped_ptr<net::FileStream> stream_; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 // NotifySuccess internally calls ReadRawData, hence we only notify success | 294 // NotifySuccess internally calls ReadRawData, hence we only notify success |
295 // after we have: | 295 // after we have: |
296 // - received the first chunk of download data if file is downloaded | 296 // - received the first chunk of download data if file is downloaded |
297 // - gotten size of physical file if file exists in cache. | 297 // - gotten size of physical file if file exists in cache. |
298 | 298 |
299 // Request job is created and runs on IO thread but getting file system via | 299 // Request job is created and runs on IO thread but getting file system via |
300 // profile needs to happen on UI thread, so post GetFileSystemOnUIThread to | 300 // profile needs to happen on UI thread, so post GetFileSystemOnUIThread to |
301 // UI thread; StartAsync reply task will proceed with actually starting the | 301 // UI thread; StartAsync reply task will proceed with actually starting the |
302 // request. | 302 // request. |
303 | 303 |
304 GDataFileSystemInterface** file_system = new GDataFileSystemInterface*(NULL); | 304 DriveFileSystemInterface** file_system = new DriveFileSystemInterface*(NULL); |
305 BrowserThread::PostTaskAndReply( | 305 BrowserThread::PostTaskAndReply( |
306 BrowserThread::UI, | 306 BrowserThread::UI, |
307 FROM_HERE, | 307 FROM_HERE, |
308 base::Bind(&GetFileSystemOnUIThread, file_system), | 308 base::Bind(&GetFileSystemOnUIThread, file_system), |
309 base::Bind(&GDataURLRequestJob::StartAsync, | 309 base::Bind(&GDataURLRequestJob::StartAsync, |
310 weak_ptr_factory_->GetWeakPtr(), | 310 weak_ptr_factory_->GetWeakPtr(), |
311 base::Owned(file_system))); | 311 base::Owned(file_system))); |
312 } | 312 } |
313 | 313 |
314 void GDataURLRequestJob::Kill() { | 314 void GDataURLRequestJob::Kill() { |
315 DVLOG(1) << "Killing request"; | 315 DVLOG(1) << "Killing request"; |
316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
317 | 317 |
318 CloseFileStream(); | 318 CloseFileStream(); |
319 | 319 |
320 // If download operation for gdata file (via | 320 // If download operation for gdata file (via |
321 // GDataFileSystem::GetFileByResourceId) is still in progress, cancel it by | 321 // DriveFileSystem::GetFileByResourceId) is still in progress, cancel it by |
322 // posting a task on the UI thread. | 322 // posting a task on the UI thread. |
323 // Download operation is still in progress if: | 323 // Download operation is still in progress if: |
324 // 1) |local_file_path_| is still empty; it gets filled when callback for | 324 // 1) |local_file_path_| is still empty; it gets filled when callback for |
325 // GetFileByResourceId is called, AND | 325 // GetFileByResourceId is called, AND |
326 // 2) we're still streaming download data i.e. |remaining_bytes_| > 0; if | 326 // 2) we're still streaming download data i.e. |remaining_bytes_| > 0; if |
327 // we've finished streaming data, we want to avoid possibly killing last | 327 // we've finished streaming data, we want to avoid possibly killing last |
328 // part of the download process where the last chunk is written to file; | 328 // part of the download process where the last chunk is written to file; |
329 // if we're reading directly from cache file, |remaining_bytes_| doesn't | 329 // if we're reading directly from cache file, |remaining_bytes_| doesn't |
330 // matter 'cos |local_file_path_| will not be empty. | 330 // matter 'cos |local_file_path_| will not be empty. |
331 if (file_system_ && !gdata_file_path_.empty() && local_file_path_.empty() && | 331 if (file_system_ && !gdata_file_path_.empty() && local_file_path_.empty() && |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 } | 464 } |
465 | 465 |
466 //======================= GDataURLRequestJob protected methods ================ | 466 //======================= GDataURLRequestJob protected methods ================ |
467 | 467 |
468 GDataURLRequestJob::~GDataURLRequestJob() { | 468 GDataURLRequestJob::~GDataURLRequestJob() { |
469 CloseFileStream(); | 469 CloseFileStream(); |
470 } | 470 } |
471 | 471 |
472 //======================= GDataURLRequestJob private methods =================== | 472 //======================= GDataURLRequestJob private methods =================== |
473 | 473 |
474 void GDataURLRequestJob::StartAsync(GDataFileSystemInterface** file_system) { | 474 void GDataURLRequestJob::StartAsync(DriveFileSystemInterface** file_system) { |
475 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 475 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
476 | 476 |
477 file_system_ = *file_system; | 477 file_system_ = *file_system; |
478 | 478 |
479 if (!request_ || !file_system_) { | 479 if (!request_ || !file_system_) { |
480 LOG(WARNING) << "Failed to start request: null " | 480 LOG(WARNING) << "Failed to start request: null " |
481 << (!request_ ? "request" : "file system"); | 481 << (!request_ ? "request" : "file system"); |
482 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 482 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
483 net::ERR_FAILED)); | 483 net::ERR_FAILED)); |
484 return; | 484 return; |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 GDataProtocolHandler::~GDataProtocolHandler() { | 933 GDataProtocolHandler::~GDataProtocolHandler() { |
934 } | 934 } |
935 | 935 |
936 net::URLRequestJob* GDataProtocolHandler::MaybeCreateJob( | 936 net::URLRequestJob* GDataProtocolHandler::MaybeCreateJob( |
937 net::URLRequest* request) const { | 937 net::URLRequest* request) const { |
938 DVLOG(1) << "Handling url: " << request->url().spec(); | 938 DVLOG(1) << "Handling url: " << request->url().spec(); |
939 return new GDataURLRequestJob(request); | 939 return new GDataURLRequestJob(request); |
940 } | 940 } |
941 | 941 |
942 } // namespace gdata | 942 } // namespace gdata |
OLD | NEW |