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