Chromium Code Reviews| 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 |
| 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/drive/drive.pb.h" | 20 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 21 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" | 21 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" |
| 22 #include "chrome/browser/chromeos/drive/drive_system_service.h" | 22 #include "chrome/browser/chromeos/drive/drive_system_service.h" |
| 23 #include "chrome/browser/google_apis/drive_service_interface.h" | 23 #include "chrome/browser/google_apis/drive_service_interface.h" |
| 24 #include "chrome/browser/google_apis/gdata_errorcode.h" | 24 #include "chrome/browser/google_apis/gdata_errorcode.h" |
| 25 #include "chrome/browser/google_apis/time_util.h" | 25 #include "chrome/browser/google_apis/time_util.h" |
| 26 #include "chrome/browser/profiles/profile.h" | |
| 27 #include "chrome/browser/profiles/profile_manager.h" | |
| 28 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 29 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 30 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
| 31 #include "net/base/file_stream.h" | 29 #include "net/base/file_stream.h" |
| 32 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| 33 #include "net/http/http_request_headers.h" | 31 #include "net/http/http_request_headers.h" |
| 34 #include "net/http/http_response_headers.h" | 32 #include "net/http/http_response_headers.h" |
| 35 #include "net/http/http_response_info.h" | 33 #include "net/http/http_response_info.h" |
| 36 #include "net/url_request/url_request.h" | 34 #include "net/url_request/url_request.h" |
| 37 #include "net/url_request/url_request_job.h" | 35 #include "net/url_request/url_request_job.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 path.size() <= drive_schema.size()) { | 83 path.size() <= drive_schema.size()) { |
| 86 return false; | 84 return false; |
| 87 } | 85 } |
| 88 | 86 |
| 89 std::string id = path.substr(drive_schema.size()); | 87 std::string id = path.substr(drive_schema.size()); |
| 90 *resource_id = net::UnescapeURLComponent(id, kUrlPathUnescapeMask); | 88 *resource_id = net::UnescapeURLComponent(id, kUrlPathUnescapeMask); |
| 91 return resource_id->size(); | 89 return resource_id->size(); |
| 92 } | 90 } |
| 93 | 91 |
| 94 // Helper function to get DriveSystemService from Profile. | 92 // Helper function to get DriveSystemService from Profile. |
| 95 DriveSystemService* GetSystemService() { | 93 DriveSystemService* GetSystemService(Profile* profile) { |
|
hashimoto
2013/01/07 08:22:51
How about moving the IsValidProfile check to this
Haruki Sato
2013/01/07 08:54:49
Good catch! Thanks!
Done.
| |
| 96 return DriveSystemServiceFactory::GetForProfile( | 94 return DriveSystemServiceFactory::GetForProfile(profile); |
| 97 ProfileManager::GetDefaultProfile()); | |
| 98 } | 95 } |
| 99 | 96 |
| 100 // Helper function to get DriveFileSystem from Profile on UI thread. | 97 // Helper function to get DriveFileSystem from Profile on UI thread. |
| 101 DriveFileSystemInterface* GetFileSystemOnUIThread() { | 98 DriveFileSystemInterface* GetFileSystemOnUIThread(void* profile_id) { |
| 102 DriveSystemService* system_service = GetSystemService(); | 99 Profile* profile = reinterpret_cast<Profile*>(profile_id); |
| 100 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) | |
| 101 return NULL; | |
| 102 | |
| 103 DriveSystemService* system_service = GetSystemService(profile); | |
| 103 return system_service ? system_service->file_system() : NULL; | 104 return system_service ? system_service->file_system() : NULL; |
| 104 } | 105 } |
| 105 | 106 |
| 106 // Helper function to cancel Drive download operation on UI thread. | 107 // Helper function to cancel Drive download operation on UI thread. |
| 107 void CancelDriveDownloadOnUIThread(const FilePath& drive_file_path) { | 108 void CancelDriveDownloadOnUIThread( |
| 108 DriveSystemService* system_service = GetSystemService(); | 109 void* profile_id, const FilePath& drive_file_path) { |
| 110 Profile* profile = reinterpret_cast<Profile*>(profile_id); | |
| 111 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) | |
| 112 return; | |
| 113 | |
| 114 DriveSystemService* system_service = GetSystemService(profile); | |
| 109 if (system_service) | 115 if (system_service) |
| 110 system_service->drive_service()->CancelForFilePath(drive_file_path); | 116 system_service->drive_service()->CancelForFilePath(drive_file_path); |
| 111 } | 117 } |
| 112 | 118 |
| 113 // DriveURLRequesetJob is the gateway between network-level drive://... | 119 // DriveURLRequesetJob is the gateway between network-level drive://... |
| 114 // requests for drive resources and DriveFileSytem. It exposes content URLs | 120 // requests for drive resources and DriveFileSytem. It exposes content URLs |
| 115 // formatted as drive://<resource-id>. | 121 // formatted as drive://<resource-id>. |
| 116 class DriveURLRequestJob : public net::URLRequestJob { | 122 class DriveURLRequestJob : public net::URLRequestJob { |
| 117 public: | 123 public: |
| 118 DriveURLRequestJob(net::URLRequest* request, | 124 DriveURLRequestJob(void* profile_id, |
| 125 net::URLRequest* request, | |
| 119 net::NetworkDelegate* network_delegate); | 126 net::NetworkDelegate* network_delegate); |
| 120 | 127 |
| 121 // net::URLRequestJob overrides: | 128 // net::URLRequestJob overrides: |
| 122 virtual void Start() OVERRIDE; | 129 virtual void Start() OVERRIDE; |
| 123 virtual void Kill() OVERRIDE; | 130 virtual void Kill() OVERRIDE; |
| 124 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; | 131 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
| 125 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE; | 132 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE; |
| 126 virtual int GetResponseCode() const OVERRIDE; | 133 virtual int GetResponseCode() const OVERRIDE; |
| 127 virtual bool ReadRawData(net::IOBuffer* buf, | 134 virtual bool ReadRawData(net::IOBuffer* buf, |
| 128 int buf_size, | 135 int buf_size, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 | 188 |
| 182 // Helper methods to formulate and notify about response status, info and | 189 // Helper methods to formulate and notify about response status, info and |
| 183 // headers. | 190 // headers. |
| 184 void NotifySuccess(); | 191 void NotifySuccess(); |
| 185 void NotifyFailure(int); | 192 void NotifyFailure(int); |
| 186 void HeadersCompleted(int status_code, const std::string& status_txt); | 193 void HeadersCompleted(int status_code, const std::string& status_txt); |
| 187 | 194 |
| 188 // Helper method to close |stream_|. | 195 // Helper method to close |stream_|. |
| 189 void CloseFileStream(); | 196 void CloseFileStream(); |
| 190 | 197 |
| 198 // The profile for processing Drive accesses. Should not be NULL and needs to | |
| 199 // be checked with ProfileManager::IsValidProfile before using it. | |
| 200 void* profile_id_; | |
| 191 DriveFileSystemInterface* file_system_; | 201 DriveFileSystemInterface* file_system_; |
| 192 | 202 |
| 193 bool error_; // True if we've encountered an error. | 203 bool error_; // True if we've encountered an error. |
| 194 bool headers_set_; // True if headers have been set. | 204 bool headers_set_; // True if headers have been set. |
| 195 | 205 |
| 196 FilePath local_file_path_; | 206 FilePath local_file_path_; |
| 197 FilePath drive_file_path_; | 207 FilePath drive_file_path_; |
| 198 std::string mime_type_; | 208 std::string mime_type_; |
| 199 int64 initial_file_size_; | 209 int64 initial_file_size_; |
| 200 int64 remaining_bytes_; | 210 int64 remaining_bytes_; |
| 201 scoped_ptr<net::FileStream> stream_; | 211 scoped_ptr<net::FileStream> stream_; |
| 202 scoped_refptr<net::DrainableIOBuffer> read_buf_; | 212 scoped_refptr<net::DrainableIOBuffer> read_buf_; |
| 203 scoped_ptr<net::HttpResponseInfo> response_info_; | 213 scoped_ptr<net::HttpResponseInfo> response_info_; |
| 204 bool streaming_download_; | 214 bool streaming_download_; |
| 205 scoped_refptr<net::GrowableIOBuffer> download_growable_buf_; | 215 scoped_refptr<net::GrowableIOBuffer> download_growable_buf_; |
| 206 scoped_refptr<net::DrainableIOBuffer> download_drainable_buf_; | 216 scoped_refptr<net::DrainableIOBuffer> download_drainable_buf_; |
| 207 | 217 |
| 208 // This should remain the last member so it'll be destroyed first and | 218 // This should remain the last member so it'll be destroyed first and |
| 209 // invalidate its weak pointers before other members are destroyed. | 219 // invalidate its weak pointers before other members are destroyed. |
| 210 base::WeakPtrFactory<DriveURLRequestJob> weak_ptr_factory_; | 220 base::WeakPtrFactory<DriveURLRequestJob> weak_ptr_factory_; |
| 211 DISALLOW_COPY_AND_ASSIGN(DriveURLRequestJob); | 221 DISALLOW_COPY_AND_ASSIGN(DriveURLRequestJob); |
| 212 }; | 222 }; |
| 213 | 223 |
| 214 DriveURLRequestJob::DriveURLRequestJob(net::URLRequest* request, | 224 DriveURLRequestJob::DriveURLRequestJob(void* profile_id, |
| 225 net::URLRequest* request, | |
| 215 net::NetworkDelegate* network_delegate) | 226 net::NetworkDelegate* network_delegate) |
| 216 : net::URLRequestJob(request, network_delegate), | 227 : net::URLRequestJob(request, network_delegate), |
| 228 profile_id_(profile_id), | |
| 217 file_system_(NULL), | 229 file_system_(NULL), |
| 218 error_(false), | 230 error_(false), |
| 219 headers_set_(false), | 231 headers_set_(false), |
| 220 initial_file_size_(0), | 232 initial_file_size_(0), |
| 221 remaining_bytes_(0), | 233 remaining_bytes_(0), |
| 222 streaming_download_(false), | 234 streaming_download_(false), |
| 223 download_growable_buf_(new net::GrowableIOBuffer), | 235 download_growable_buf_(new net::GrowableIOBuffer), |
| 224 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 236 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 225 download_growable_buf_->SetCapacity(kInitialDownloadBufferSizeInBytes); | 237 download_growable_buf_->SetCapacity(kInitialDownloadBufferSizeInBytes); |
| 226 download_drainable_buf_ = new net::DrainableIOBuffer( | 238 download_drainable_buf_ = new net::DrainableIOBuffer( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 // - gotten size of physical file if file exists in cache. | 297 // - gotten size of physical file if file exists in cache. |
| 286 | 298 |
| 287 // 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 |
| 288 // profile needs to happen on UI thread, so post GetFileSystemOnUIThread to | 300 // profile needs to happen on UI thread, so post GetFileSystemOnUIThread to |
| 289 // UI thread; StartAsync reply task will proceed with actually starting the | 301 // UI thread; StartAsync reply task will proceed with actually starting the |
| 290 // request. | 302 // request. |
| 291 | 303 |
| 292 BrowserThread::PostTaskAndReplyWithResult( | 304 BrowserThread::PostTaskAndReplyWithResult( |
| 293 BrowserThread::UI, | 305 BrowserThread::UI, |
| 294 FROM_HERE, | 306 FROM_HERE, |
| 295 base::Bind(&GetFileSystemOnUIThread), | 307 base::Bind(&GetFileSystemOnUIThread, profile_id_), |
| 296 base::Bind(&DriveURLRequestJob::StartAsync, | 308 base::Bind(&DriveURLRequestJob::StartAsync, |
| 297 weak_ptr_factory_.GetWeakPtr())); | 309 weak_ptr_factory_.GetWeakPtr())); |
| 298 } | 310 } |
| 299 | 311 |
| 300 void DriveURLRequestJob::Kill() { | 312 void DriveURLRequestJob::Kill() { |
| 301 DVLOG(1) << "Killing request"; | 313 DVLOG(1) << "Killing request"; |
| 302 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 303 | 315 |
| 304 CloseFileStream(); | 316 CloseFileStream(); |
| 305 | 317 |
| 306 // If download operation for drive file (via | 318 // If download operation for drive file (via |
| 307 // DriveFileSystem::GetFileByResourceId) is still in progress, cancel it by | 319 // DriveFileSystem::GetFileByResourceId) is still in progress, cancel it by |
| 308 // posting a task on the UI thread. | 320 // posting a task on the UI thread. |
| 309 // Download operation is still in progress if: | 321 // Download operation is still in progress if: |
| 310 // 1) |local_file_path_| is still empty; it gets filled when callback for | 322 // 1) |local_file_path_| is still empty; it gets filled when callback for |
| 311 // GetFileByResourceId is called, AND | 323 // GetFileByResourceId is called, AND |
| 312 // 2) we're still streaming download data i.e. |remaining_bytes_| > 0; if | 324 // 2) we're still streaming download data i.e. |remaining_bytes_| > 0; if |
| 313 // we've finished streaming data, we want to avoid possibly killing last | 325 // we've finished streaming data, we want to avoid possibly killing last |
| 314 // part of the download process where the last chunk is written to file; | 326 // part of the download process where the last chunk is written to file; |
| 315 // if we're reading directly from cache file, |remaining_bytes_| doesn't | 327 // if we're reading directly from cache file, |remaining_bytes_| doesn't |
| 316 // matter 'cos |local_file_path_| will not be empty. | 328 // matter 'cos |local_file_path_| will not be empty. |
| 317 if (file_system_ && !drive_file_path_.empty() && local_file_path_.empty() && | 329 if (file_system_ && !drive_file_path_.empty() && local_file_path_.empty() && |
| 318 remaining_bytes_ > 0) { | 330 remaining_bytes_ > 0) { |
| 319 DVLOG(1) << "Canceling download operation for " << drive_file_path_.value(); | 331 DVLOG(1) << "Canceling download operation for " << drive_file_path_.value(); |
| 320 BrowserThread::PostTask( | 332 BrowserThread::PostTask( |
| 321 BrowserThread::UI, | 333 BrowserThread::UI, |
| 322 FROM_HERE, | 334 FROM_HERE, |
| 323 base::Bind(&CancelDriveDownloadOnUIThread, | 335 base::Bind(&CancelDriveDownloadOnUIThread, |
| 336 profile_id_, | |
| 324 drive_file_path_)); | 337 drive_file_path_)); |
| 325 } | 338 } |
| 326 | 339 |
| 327 net::URLRequestJob::Kill(); | 340 net::URLRequestJob::Kill(); |
| 328 weak_ptr_factory_.InvalidateWeakPtrs(); | 341 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 329 } | 342 } |
| 330 | 343 |
| 331 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { | 344 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 333 mime_type->assign(FixupMimeType(mime_type_)); | 346 mime_type->assign(FixupMimeType(mime_type_)); |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 902 headers_set_ = true; | 915 headers_set_ = true; |
| 903 | 916 |
| 904 NotifyHeadersComplete(); | 917 NotifyHeadersComplete(); |
| 905 } | 918 } |
| 906 | 919 |
| 907 } // namespace | 920 } // namespace |
| 908 | 921 |
| 909 /////////////////////////////////////////////////////////////////////////////// | 922 /////////////////////////////////////////////////////////////////////////////// |
| 910 // DriveProtocolHandler class | 923 // DriveProtocolHandler class |
| 911 | 924 |
| 912 DriveProtocolHandler::DriveProtocolHandler() { | 925 DriveProtocolHandler::DriveProtocolHandler(void* profile_id) |
| 926 : profile_id_(profile_id) { | |
| 913 } | 927 } |
| 914 | 928 |
| 915 DriveProtocolHandler::~DriveProtocolHandler() { | 929 DriveProtocolHandler::~DriveProtocolHandler() { |
| 916 } | 930 } |
| 917 | 931 |
| 918 net::URLRequestJob* DriveProtocolHandler::MaybeCreateJob( | 932 net::URLRequestJob* DriveProtocolHandler::MaybeCreateJob( |
| 919 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 933 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 920 DVLOG(1) << "Handling url: " << request->url().spec(); | 934 DVLOG(1) << "Handling url: " << request->url().spec(); |
| 921 return new DriveURLRequestJob(request, network_delegate); | 935 return new DriveURLRequestJob(profile_id_, request, network_delegate); |
| 922 } | 936 } |
| 923 | 937 |
| 924 } // namespace drive | 938 } // namespace drive |
| OLD | NEW |