| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/sync_file_system/drive_backend_v1/api_util.h" | 5 #include "chrome/browser/sync_file_system/drive_backend_v1/api_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 base::FilePath::StringType extension = | 136 base::FilePath::StringType extension = |
| 137 base::FilePath::FromUTF8Unsafe(title).Extension(); | 137 base::FilePath::FromUTF8Unsafe(title).Extension(); |
| 138 std::string mime_type; | 138 std::string mime_type; |
| 139 if (extension.empty() || | 139 if (extension.empty() || |
| 140 !net::GetWellKnownMimeTypeFromExtension(extension.substr(1), &mime_type)) | 140 !net::GetWellKnownMimeTypeFromExtension(extension.substr(1), &mime_type)) |
| 141 return kMimeTypeOctetStream; | 141 return kMimeTypeOctetStream; |
| 142 return mime_type; | 142 return mime_type; |
| 143 } | 143 } |
| 144 | 144 |
| 145 bool CreateTemporaryFile(const base::FilePath& dir_path, | 145 bool CreateTemporaryFile(const base::FilePath& dir_path, |
| 146 webkit_blob::ScopedFile* temp_file) { | 146 storage::ScopedFile* temp_file) { |
| 147 base::FilePath temp_file_path; | 147 base::FilePath temp_file_path; |
| 148 const bool success = base::CreateDirectory(dir_path) && | 148 const bool success = base::CreateDirectory(dir_path) && |
| 149 base::CreateTemporaryFileInDir(dir_path, &temp_file_path); | 149 base::CreateTemporaryFileInDir(dir_path, &temp_file_path); |
| 150 if (!success) | 150 if (!success) |
| 151 return success; | 151 return success; |
| 152 *temp_file = | 152 *temp_file = storage::ScopedFile(temp_file_path, |
| 153 webkit_blob::ScopedFile(temp_file_path, | 153 storage::ScopedFile::DELETE_ON_SCOPE_OUT, |
| 154 webkit_blob::ScopedFile::DELETE_ON_SCOPE_OUT, | 154 base::ThreadTaskRunnerHandle::Get().get()); |
| 155 base::ThreadTaskRunnerHandle::Get().get()); | |
| 156 return success; | 155 return success; |
| 157 } | 156 } |
| 158 | 157 |
| 159 } // namespace | 158 } // namespace |
| 160 | 159 |
| 161 APIUtil::APIUtil(Profile* profile, | 160 APIUtil::APIUtil(Profile* profile, |
| 162 const base::FilePath& temp_dir_path) | 161 const base::FilePath& temp_dir_path) |
| 163 : oauth_service_(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)), | 162 : oauth_service_(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)), |
| 164 signin_manager_(SigninManagerFactory::GetForProfile(profile)), | 163 signin_manager_(SigninManagerFactory::GetForProfile(profile)), |
| 165 upload_next_key_(0), | 164 upload_next_key_(0), |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 base::Bind(&APIUtil::DidGetFileList, AsWeakPtr(), callback)); | 497 base::Bind(&APIUtil::DidGetFileList, AsWeakPtr(), callback)); |
| 499 } | 498 } |
| 500 | 499 |
| 501 void APIUtil::DownloadFile(const std::string& resource_id, | 500 void APIUtil::DownloadFile(const std::string& resource_id, |
| 502 const std::string& local_file_md5, | 501 const std::string& local_file_md5, |
| 503 const DownloadFileCallback& callback) { | 502 const DownloadFileCallback& callback) { |
| 504 DCHECK(CalledOnValidThread()); | 503 DCHECK(CalledOnValidThread()); |
| 505 DCHECK(!temp_dir_path_.empty()); | 504 DCHECK(!temp_dir_path_.empty()); |
| 506 DVLOG(2) << "Downloading file [" << resource_id << "]"; | 505 DVLOG(2) << "Downloading file [" << resource_id << "]"; |
| 507 | 506 |
| 508 scoped_ptr<webkit_blob::ScopedFile> temp_file(new webkit_blob::ScopedFile); | 507 scoped_ptr<storage::ScopedFile> temp_file(new storage::ScopedFile); |
| 509 webkit_blob::ScopedFile* temp_file_ptr = temp_file.get(); | 508 storage::ScopedFile* temp_file_ptr = temp_file.get(); |
| 510 content::BrowserThread::PostTaskAndReplyWithResult( | 509 content::BrowserThread::PostTaskAndReplyWithResult( |
| 511 content::BrowserThread::FILE, FROM_HERE, | 510 content::BrowserThread::FILE, FROM_HERE, |
| 512 base::Bind(&CreateTemporaryFile, temp_dir_path_, temp_file_ptr), | 511 base::Bind(&CreateTemporaryFile, temp_dir_path_, temp_file_ptr), |
| 513 base::Bind(&APIUtil::DidGetTemporaryFileForDownload, | 512 base::Bind(&APIUtil::DidGetTemporaryFileForDownload, |
| 514 AsWeakPtr(), resource_id, local_file_md5, | 513 AsWeakPtr(), resource_id, local_file_md5, |
| 515 base::Passed(&temp_file), callback)); | 514 base::Passed(&temp_file), callback)); |
| 516 } | 515 } |
| 517 | 516 |
| 518 void APIUtil::UploadNewFile(const std::string& directory_resource_id, | 517 void APIUtil::UploadNewFile(const std::string& directory_resource_id, |
| 519 const base::FilePath& local_file_path, | 518 const base::FilePath& local_file_path, |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 return; | 730 return; |
| 732 } | 731 } |
| 733 | 732 |
| 734 DVLOG(2) << "Got resource entry"; | 733 DVLOG(2) << "Got resource entry"; |
| 735 callback.Run(error, drive::util::ConvertFileResourceToResourceEntry(*entry)); | 734 callback.Run(error, drive::util::ConvertFileResourceToResourceEntry(*entry)); |
| 736 } | 735 } |
| 737 | 736 |
| 738 void APIUtil::DidGetTemporaryFileForDownload( | 737 void APIUtil::DidGetTemporaryFileForDownload( |
| 739 const std::string& resource_id, | 738 const std::string& resource_id, |
| 740 const std::string& local_file_md5, | 739 const std::string& local_file_md5, |
| 741 scoped_ptr<webkit_blob::ScopedFile> local_file, | 740 scoped_ptr<storage::ScopedFile> local_file, |
| 742 const DownloadFileCallback& callback, | 741 const DownloadFileCallback& callback, |
| 743 bool success) { | 742 bool success) { |
| 744 if (!success) { | 743 if (!success) { |
| 745 DVLOG(2) << "Error in creating a temp file under " | 744 DVLOG(2) << "Error in creating a temp file under " |
| 746 << temp_dir_path_.value(); | 745 << temp_dir_path_.value(); |
| 747 callback.Run(google_apis::GDATA_FILE_ERROR, std::string(), 0, base::Time(), | 746 callback.Run(google_apis::GDATA_FILE_ERROR, std::string(), 0, base::Time(), |
| 748 local_file->Pass()); | 747 local_file->Pass()); |
| 749 return; | 748 return; |
| 750 } | 749 } |
| 751 drive_service_->GetFileResource( | 750 drive_service_->GetFileResource( |
| 752 resource_id, | 751 resource_id, |
| 753 base::Bind(&APIUtil::DidGetFileResource, | 752 base::Bind(&APIUtil::DidGetFileResource, |
| 754 AsWeakPtr(), | 753 AsWeakPtr(), |
| 755 base::Bind(&APIUtil::DownloadFileInternal, | 754 base::Bind(&APIUtil::DownloadFileInternal, |
| 756 AsWeakPtr(), | 755 AsWeakPtr(), |
| 757 local_file_md5, | 756 local_file_md5, |
| 758 base::Passed(&local_file), | 757 base::Passed(&local_file), |
| 759 callback))); | 758 callback))); |
| 760 } | 759 } |
| 761 | 760 |
| 762 void APIUtil::DownloadFileInternal( | 761 void APIUtil::DownloadFileInternal( |
| 763 const std::string& local_file_md5, | 762 const std::string& local_file_md5, |
| 764 scoped_ptr<webkit_blob::ScopedFile> local_file, | 763 scoped_ptr<storage::ScopedFile> local_file, |
| 765 const DownloadFileCallback& callback, | 764 const DownloadFileCallback& callback, |
| 766 google_apis::GDataErrorCode error, | 765 google_apis::GDataErrorCode error, |
| 767 scoped_ptr<google_apis::ResourceEntry> entry) { | 766 scoped_ptr<google_apis::ResourceEntry> entry) { |
| 768 DCHECK(CalledOnValidThread()); | 767 DCHECK(CalledOnValidThread()); |
| 769 | 768 |
| 770 if (error != google_apis::HTTP_SUCCESS) { | 769 if (error != google_apis::HTTP_SUCCESS) { |
| 771 DVLOG(2) << "Error on getting resource entry for download"; | 770 DVLOG(2) << "Error on getting resource entry for download"; |
| 772 callback.Run(error, std::string(), 0, base::Time(), local_file->Pass()); | 771 callback.Run(error, std::string(), 0, base::Time(), local_file->Pass()); |
| 773 return; | 772 return; |
| 774 } | 773 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 793 base::Bind(&APIUtil::DidDownloadFile, | 792 base::Bind(&APIUtil::DidDownloadFile, |
| 794 AsWeakPtr(), | 793 AsWeakPtr(), |
| 795 base::Passed(&entry), | 794 base::Passed(&entry), |
| 796 base::Passed(&local_file), | 795 base::Passed(&local_file), |
| 797 callback), | 796 callback), |
| 798 google_apis::GetContentCallback(), | 797 google_apis::GetContentCallback(), |
| 799 google_apis::ProgressCallback()); | 798 google_apis::ProgressCallback()); |
| 800 } | 799 } |
| 801 | 800 |
| 802 void APIUtil::DidDownloadFile(scoped_ptr<google_apis::ResourceEntry> entry, | 801 void APIUtil::DidDownloadFile(scoped_ptr<google_apis::ResourceEntry> entry, |
| 803 scoped_ptr<webkit_blob::ScopedFile> local_file, | 802 scoped_ptr<storage::ScopedFile> local_file, |
| 804 const DownloadFileCallback& callback, | 803 const DownloadFileCallback& callback, |
| 805 google_apis::GDataErrorCode error, | 804 google_apis::GDataErrorCode error, |
| 806 const base::FilePath& downloaded_file_path) { | 805 const base::FilePath& downloaded_file_path) { |
| 807 DCHECK(CalledOnValidThread()); | 806 DCHECK(CalledOnValidThread()); |
| 808 if (error == google_apis::HTTP_SUCCESS) | 807 if (error == google_apis::HTTP_SUCCESS) |
| 809 DVLOG(2) << "Download completed"; | 808 DVLOG(2) << "Download completed"; |
| 810 else | 809 else |
| 811 DVLOG(2) << "Error on downloading file: " << error; | 810 DVLOG(2) << "Error on downloading file: " << error; |
| 812 | 811 |
| 813 callback.Run( | 812 callback.Run( |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1143 } | 1142 } |
| 1144 | 1143 |
| 1145 std::string APIUtil::GetRootResourceId() const { | 1144 std::string APIUtil::GetRootResourceId() const { |
| 1146 if (IsDriveAPIDisabled()) | 1145 if (IsDriveAPIDisabled()) |
| 1147 return drive_service_->GetRootResourceId(); | 1146 return drive_service_->GetRootResourceId(); |
| 1148 return root_resource_id_; | 1147 return root_resource_id_; |
| 1149 } | 1148 } |
| 1150 | 1149 |
| 1151 } // namespace drive_backend | 1150 } // namespace drive_backend |
| 1152 } // namespace sync_file_system | 1151 } // namespace sync_file_system |
| OLD | NEW |