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