| 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/drive/drive_protocol_handler.h" | 5 #include "chrome/browser/chromeos/drive/drive_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 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 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" |
| 34 #include "net/http/http_response_headers.h" | 34 #include "net/http/http_response_headers.h" |
| 35 #include "net/http/http_response_info.h" | 35 #include "net/http/http_response_info.h" |
| 36 #include "net/url_request/url_request.h" | 36 #include "net/url_request/url_request.h" |
| 37 #include "net/url_request/url_request_job.h" | 37 #include "net/url_request/url_request_job.h" |
| 38 | 38 |
| 39 using content::BrowserThread; | 39 using content::BrowserThread; |
| 40 | 40 |
| 41 namespace gdata { | 41 namespace drive { |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 const net::UnescapeRule::Type kUrlPathUnescapeMask = | 45 const net::UnescapeRule::Type kUrlPathUnescapeMask = |
| 46 net::UnescapeRule::SPACES | | 46 net::UnescapeRule::SPACES | |
| 47 net::UnescapeRule::URL_SPECIAL_CHARS | | 47 net::UnescapeRule::URL_SPECIAL_CHARS | |
| 48 net::UnescapeRule::CONTROL_CHARS; | 48 net::UnescapeRule::CONTROL_CHARS; |
| 49 | 49 |
| 50 const int kHTTPOk = 200; | 50 const int kHTTPOk = 200; |
| 51 const int kHTTPNotAllowed = 403; | 51 const int kHTTPNotAllowed = 403; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 146 |
| 147 private: | 147 private: |
| 148 // Helper for Start() to let us start asynchronously. | 148 // Helper for Start() to let us start asynchronously. |
| 149 void StartAsync(DriveFileSystemInterface** file_system); | 149 void StartAsync(DriveFileSystemInterface** file_system); |
| 150 | 150 |
| 151 // Helper methods for Delegate::OnUrlFetchDownloadData and ReadRawData to | 151 // Helper methods for Delegate::OnUrlFetchDownloadData and ReadRawData to |
| 152 // receive download data and copy to response buffer. | 152 // receive download data and copy to response buffer. |
| 153 // For detailed description of logic, refer to comments in definitions of | 153 // For detailed description of logic, refer to comments in definitions of |
| 154 // Start() and ReadRawData(). | 154 // Start() and ReadRawData(). |
| 155 | 155 |
| 156 void OnUrlFetchDownloadData(GDataErrorCode error, | 156 void OnUrlFetchDownloadData(gdata::GDataErrorCode error, |
| 157 scoped_ptr<std::string> download_data); | 157 scoped_ptr<std::string> download_data); |
| 158 // Called from ReadRawData, returns true if data is ready, false otherwise. | 158 // Called from ReadRawData, returns true if data is ready, false otherwise. |
| 159 bool ContinueReadFromDownloadData(int* bytes_read); | 159 bool ContinueReadFromDownloadData(int* bytes_read); |
| 160 // Copies from download buffer into response buffer. | 160 // Copies from download buffer into response buffer. |
| 161 bool ReadFromDownloadData(); | 161 bool ReadFromDownloadData(); |
| 162 | 162 |
| 163 // Helper callback for handling async responses from | 163 // Helper callback for handling async responses from |
| 164 // DriveFileSystem::GetFileByResourceId(). | 164 // DriveFileSystem::GetFileByResourceId(). |
| 165 void OnGetFileByResourceId(DriveFileError error, | 165 void OnGetFileByResourceId(DriveFileError error, |
| 166 const FilePath& local_file_path, | 166 const FilePath& local_file_path, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 // should still be asynchronous, so that all error reporting and data | 254 // should still be asynchronous, so that all error reporting and data |
| 255 // callbacks happen as they would for network requests, so we should still | 255 // callbacks happen as they would for network requests, so we should still |
| 256 // post task to same thread to actually start request. | 256 // post task to same thread to actually start request. |
| 257 // 2) Reply task StartAsync is invoked. | 257 // 2) Reply task StartAsync is invoked. |
| 258 // 3) If unable to get file system or request method is not GET, report start | 258 // 3) If unable to get file system or request method is not GET, report start |
| 259 // error and bail out. | 259 // error and bail out. |
| 260 // 4) Otherwise, parse request url to get resource id and file name. | 260 // 4) Otherwise, parse request url to get resource id and file name. |
| 261 // 5) Find file from file system to get its mime type, drive file path and | 261 // 5) Find file from file system to get its mime type, drive file path and |
| 262 // size of physical file. | 262 // size of physical file. |
| 263 // 6) Get file from file system asynchronously with both GetFileCallback and | 263 // 6) Get file from file system asynchronously with both GetFileCallback and |
| 264 // GetContentCallback - this would either get it from cache or | 264 // gdata::GetContentCallback - this would either get it from cache or |
| 265 // download it from Drive. | 265 // download it from Drive. |
| 266 // 7) If file is downloaded from Drive: | 266 // 7) If file is downloaded from Drive: |
| 267 // 7.1) Whenever net::URLFetcherCore::OnReadCompleted() receives a part | 267 // 7.1) Whenever net::URLFetcherCore::OnReadCompleted() receives a part |
| 268 // of the response, it invokes | 268 // of the response, it invokes |
| 269 // net::URLFetcherDelegate::OnURLFetchDownloadData() if | 269 // net::URLFetcherDelegate::OnURLFetchDownloadData() if |
| 270 // net::URLFetcherDelegate::ShouldSendDownloadData() is true. | 270 // net::URLFetcherDelegate::ShouldSendDownloadData() is true. |
| 271 // 7.2) gdata::DownloadFileOperation overrides the default implementations | 271 // 7.2) gdata::DownloadFileOperation overrides the default implementations |
| 272 // of the following methods of net::URLFetcherDelegate: | 272 // of the following methods of net::URLFetcherDelegate: |
| 273 // - ShouldSendDownloadData(): returns true for non-null | 273 // - ShouldSendDownloadData(): returns true for non-null |
| 274 // GetContentCallback. | 274 // gdata::GetContentCallback. |
| 275 // - OnURLFetchDownloadData(): invokes non-null | 275 // - OnURLFetchDownloadData(): invokes non-null |
| 276 // GetContentCallback | 276 // gdata::GetContentCallback |
| 277 // 7.3) DriveProtolHandler::OnURLFetchDownloadData (i.e. this class) | 277 // 7.3) DriveProtolHandler::OnURLFetchDownloadData (i.e. this class) |
| 278 // is at the end of the invocation chain and actually implements the | 278 // is at the end of the invocation chain and actually implements the |
| 279 // method. | 279 // method. |
| 280 // 7.4) Copies the formal download data into a growable-drainable download | 280 // 7.4) Copies the formal download data into a growable-drainable download |
| 281 // IOBuffer | 281 // IOBuffer |
| 282 // - IOBuffer has initial size 4096, same as buffer used in | 282 // - IOBuffer has initial size 4096, same as buffer used in |
| 283 // net::URLFetcherCore::OnReadCompleted. | 283 // net::URLFetcherCore::OnReadCompleted. |
| 284 // - We may end up with multiple chunks, so we use GrowableIOBuffer. | 284 // - We may end up with multiple chunks, so we use GrowableIOBuffer. |
| 285 // - We then wrap the growable buffer within a DrainableIOBuffer for | 285 // - We then wrap the growable buffer within a DrainableIOBuffer for |
| 286 // ease of progressively writing into the buffer. | 286 // ease of progressively writing into the buffer. |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 DVLOG(1) << "Getting file for resource id"; | 530 DVLOG(1) << "Getting file for resource id"; |
| 531 file_system_->GetFileByResourceId( | 531 file_system_->GetFileByResourceId( |
| 532 resource_id, | 532 resource_id, |
| 533 base::Bind(&DriveURLRequestJob::OnGetFileByResourceId, | 533 base::Bind(&DriveURLRequestJob::OnGetFileByResourceId, |
| 534 weak_ptr_factory_.GetWeakPtr()), | 534 weak_ptr_factory_.GetWeakPtr()), |
| 535 base::Bind(&DriveURLRequestJob::OnUrlFetchDownloadData, | 535 base::Bind(&DriveURLRequestJob::OnUrlFetchDownloadData, |
| 536 weak_ptr_factory_.GetWeakPtr())); | 536 weak_ptr_factory_.GetWeakPtr())); |
| 537 } | 537 } |
| 538 | 538 |
| 539 void DriveURLRequestJob::OnUrlFetchDownloadData( | 539 void DriveURLRequestJob::OnUrlFetchDownloadData( |
| 540 GDataErrorCode error, | 540 gdata::GDataErrorCode error, |
| 541 scoped_ptr<std::string> download_data) { | 541 scoped_ptr<std::string> download_data) { |
| 542 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 542 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 543 | 543 |
| 544 if (download_data->empty()) | 544 if (download_data->empty()) |
| 545 return; | 545 return; |
| 546 | 546 |
| 547 // If there's insufficient space remaining in download buffer to copy download | 547 // If there's insufficient space remaining in download buffer to copy download |
| 548 // data, expand download buffer. | 548 // data, expand download buffer. |
| 549 int bytes_needed = download_data->length() - | 549 int bytes_needed = download_data->length() - |
| 550 download_drainable_buf_->BytesRemaining(); | 550 download_drainable_buf_->BytesRemaining(); |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 933 | 933 |
| 934 DriveProtocolHandler::~DriveProtocolHandler() { | 934 DriveProtocolHandler::~DriveProtocolHandler() { |
| 935 } | 935 } |
| 936 | 936 |
| 937 net::URLRequestJob* DriveProtocolHandler::MaybeCreateJob( | 937 net::URLRequestJob* DriveProtocolHandler::MaybeCreateJob( |
| 938 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 938 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 939 DVLOG(1) << "Handling url: " << request->url().spec(); | 939 DVLOG(1) << "Handling url: " << request->url().spec(); |
| 940 return new DriveURLRequestJob(request, network_delegate); | 940 return new DriveURLRequestJob(request, network_delegate); |
| 941 } | 941 } |
| 942 | 942 |
| 943 } // namespace gdata | 943 } // namespace drive |
| OLD | NEW |