| 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_errorcode.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_file_system_interface.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" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(GDataFileSystemInterface** 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(DriveErrorCode 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 // GDataFileSystem::GetFileByResourceId(). |
| 166 void OnGetFileByResourceId(GDataFileError 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. |
| 176 void OnGetEntryInfoByResourceId(const std::string& resource_id, | 176 void OnGetEntryInfoByResourceId(const std::string& resource_id, |
| 177 GDataFileError error, | 177 DriveFileError error, |
| 178 const FilePath& gdata_file_path, | 178 const FilePath& gdata_file_path, |
| 179 scoped_ptr<DriveEntryProto> entry_proto); | 179 scoped_ptr<DriveEntryProto> entry_proto); |
| 180 | 180 |
| 181 // Helper methods for ReadRawData to open file and read from its corresponding | 181 // Helper methods for ReadRawData to open file and read from its corresponding |
| 182 // stream in a streaming fashion. | 182 // stream in a streaming fashion. |
| 183 bool ContinueReadFromFile(int* bytes_read); | 183 bool ContinueReadFromFile(int* bytes_read); |
| 184 void ReadFromFile(); | 184 void ReadFromFile(); |
| 185 void ReadFileStream(int bytes_to_read); | 185 void ReadFileStream(int bytes_to_read); |
| 186 | 186 |
| 187 // Helper callback for handling async responses from FileStream::Open(). | 187 // Helper callback for handling async responses from FileStream::Open(). |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 | 501 |
| 502 file_system_->GetEntryInfoByResourceId( | 502 file_system_->GetEntryInfoByResourceId( |
| 503 resource_id, | 503 resource_id, |
| 504 base::Bind(&GDataURLRequestJob::OnGetEntryInfoByResourceId, | 504 base::Bind(&GDataURLRequestJob::OnGetEntryInfoByResourceId, |
| 505 weak_ptr_factory_->GetWeakPtr(), | 505 weak_ptr_factory_->GetWeakPtr(), |
| 506 resource_id)); | 506 resource_id)); |
| 507 } | 507 } |
| 508 | 508 |
| 509 void GDataURLRequestJob::OnGetEntryInfoByResourceId( | 509 void GDataURLRequestJob::OnGetEntryInfoByResourceId( |
| 510 const std::string& resource_id, | 510 const std::string& resource_id, |
| 511 GDataFileError error, | 511 DriveFileError error, |
| 512 const FilePath& gdata_file_path, | 512 const FilePath& gdata_file_path, |
| 513 scoped_ptr<DriveEntryProto> entry_proto) { | 513 scoped_ptr<DriveEntryProto> entry_proto) { |
| 514 if (entry_proto.get() && !entry_proto->has_file_specific_info()) | 514 if (entry_proto.get() && !entry_proto->has_file_specific_info()) |
| 515 error = GDATA_FILE_ERROR_NOT_FOUND; | 515 error = DRIVE_FILE_ERROR_NOT_FOUND; |
| 516 | 516 |
| 517 if (error == GDATA_FILE_OK) { | 517 if (error == DRIVE_FILE_OK) { |
| 518 DCHECK(entry_proto.get()); | 518 DCHECK(entry_proto.get()); |
| 519 mime_type_ = entry_proto->file_specific_info().content_mime_type(); | 519 mime_type_ = entry_proto->file_specific_info().content_mime_type(); |
| 520 gdata_file_path_ = gdata_file_path; | 520 gdata_file_path_ = gdata_file_path; |
| 521 initial_file_size_ = entry_proto->file_info().size(); | 521 initial_file_size_ = entry_proto->file_info().size(); |
| 522 } else { | 522 } else { |
| 523 mime_type_.clear(); | 523 mime_type_.clear(); |
| 524 gdata_file_path_.clear(); | 524 gdata_file_path_.clear(); |
| 525 initial_file_size_ = 0; | 525 initial_file_size_ = 0; |
| 526 } | 526 } |
| 527 remaining_bytes_ = initial_file_size_; | 527 remaining_bytes_ = initial_file_size_; |
| 528 | 528 |
| 529 DVLOG(1) << "Getting file for resource id"; | 529 DVLOG(1) << "Getting file for resource id"; |
| 530 file_system_->GetFileByResourceId( | 530 file_system_->GetFileByResourceId( |
| 531 resource_id, | 531 resource_id, |
| 532 base::Bind(&GDataURLRequestJob::OnGetFileByResourceId, | 532 base::Bind(&GDataURLRequestJob::OnGetFileByResourceId, |
| 533 weak_ptr_factory_->GetWeakPtr()), | 533 weak_ptr_factory_->GetWeakPtr()), |
| 534 base::Bind(&GDataURLRequestJob::OnUrlFetchDownloadData, | 534 base::Bind(&GDataURLRequestJob::OnUrlFetchDownloadData, |
| 535 weak_ptr_factory_->GetWeakPtr())); | 535 weak_ptr_factory_->GetWeakPtr())); |
| 536 } | 536 } |
| 537 | 537 |
| 538 void GDataURLRequestJob::OnUrlFetchDownloadData( | 538 void GDataURLRequestJob::OnUrlFetchDownloadData( |
| 539 GDataErrorCode error, | 539 DriveErrorCode error, |
| 540 scoped_ptr<std::string> download_data) { | 540 scoped_ptr<std::string> download_data) { |
| 541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 542 | 542 |
| 543 if (download_data->empty()) | 543 if (download_data->empty()) |
| 544 return; | 544 return; |
| 545 | 545 |
| 546 // If there's insufficient space remaining in download buffer to copy download | 546 // If there's insufficient space remaining in download buffer to copy download |
| 547 // data, expand download buffer. | 547 // data, expand download buffer. |
| 548 int bytes_needed = download_data->length() - | 548 int bytes_needed = download_data->length() - |
| 549 download_drainable_buf_->BytesRemaining(); | 549 download_drainable_buf_->BytesRemaining(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 // Set offset in download buffer to where we finished moving. | 647 // Set offset in download buffer to where we finished moving. |
| 648 download_drainable_buf_->SetOffset(bytes_not_copied); | 648 download_drainable_buf_->SetOffset(bytes_not_copied); |
| 649 } | 649 } |
| 650 DVLOG(1) << "Copied from download data: bytes_read=" << bytes_to_read; | 650 DVLOG(1) << "Copied from download data: bytes_read=" << bytes_to_read; |
| 651 | 651 |
| 652 // Return true if read buffer is filled up or there's no more bytes to read. | 652 // Return true if read buffer is filled up or there's no more bytes to read. |
| 653 return read_buf_->BytesRemaining() == 0 || remaining_bytes_ == 0; | 653 return read_buf_->BytesRemaining() == 0 || remaining_bytes_ == 0; |
| 654 } | 654 } |
| 655 | 655 |
| 656 void GDataURLRequestJob::OnGetFileByResourceId( | 656 void GDataURLRequestJob::OnGetFileByResourceId( |
| 657 GDataFileError error, | 657 DriveFileError error, |
| 658 const FilePath& local_file_path, | 658 const FilePath& local_file_path, |
| 659 const std::string& mime_type, | 659 const std::string& mime_type, |
| 660 DriveFileType file_type) { | 660 DriveFileType file_type) { |
| 661 DVLOG(1) << "Got OnGetFileByResourceId"; | 661 DVLOG(1) << "Got OnGetFileByResourceId"; |
| 662 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 662 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 663 | 663 |
| 664 if (error != GDATA_FILE_OK || file_type != REGULAR_FILE) { | 664 if (error != DRIVE_FILE_OK || file_type != REGULAR_FILE) { |
| 665 LOG(WARNING) << "Failed to start request: can't get file for resource id"; | 665 LOG(WARNING) << "Failed to start request: can't get file for resource id"; |
| 666 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 666 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 667 net::ERR_FILE_NOT_FOUND)); | 667 net::ERR_FILE_NOT_FOUND)); |
| 668 return; | 668 return; |
| 669 } | 669 } |
| 670 | 670 |
| 671 local_file_path_ = local_file_path; | 671 local_file_path_ = local_file_path; |
| 672 | 672 |
| 673 // If we've already streamed download data to response, we're done. | 673 // If we've already streamed download data to response, we're done. |
| 674 if (streaming_download_) | 674 if (streaming_download_) |
| (...skipping 258 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 |