OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/browser/blob/blob_url_request_job_factory.h" | 5 #include "webkit/browser/blob/blob_url_request_job_factory.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request_context.h" |
11 #include "net/url_request/url_request_job_factory.h" | 11 #include "net/url_request/url_request_job_factory.h" |
12 #include "webkit/browser/blob/blob_storage_controller.h" | 12 #include "webkit/browser/blob/blob_data_handle.h" |
13 #include "webkit/browser/blob/blob_url_request_job.h" | 13 #include "webkit/browser/blob/blob_url_request_job.h" |
14 #include "webkit/browser/fileapi/file_system_context.h" | 14 #include "webkit/browser/fileapi/file_system_context.h" |
15 | 15 |
16 namespace webkit_blob { | 16 namespace webkit_blob { |
17 | 17 |
| 18 namespace { |
| 19 |
| 20 int kUserDataKey; // The value is not important, the addr is a key. |
| 21 |
| 22 BlobDataHandle* GetRequestedBlobDataHandle(net::URLRequest* request) { |
| 23 return static_cast<BlobDataHandle*>(request->GetUserData(&kUserDataKey)); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 // static |
| 29 net::URLRequest* BlobProtocolHandler::CreateBlobRequest( |
| 30 scoped_ptr<BlobDataHandle> blob_data_handle, |
| 31 const net::URLRequestContext* request_context, |
| 32 net::URLRequest::Delegate* request_delegate) { |
| 33 const GURL kBlobUrl("blob://see_user_data/"); |
| 34 net::URLRequest* request = request_context->CreateRequest( |
| 35 kBlobUrl, request_delegate); |
| 36 SetRequestedBlobDataHandle(request, blob_data_handle.Pass()); |
| 37 return request; |
| 38 } |
| 39 |
| 40 // static |
| 41 void BlobProtocolHandler::SetRequestedBlobDataHandle( |
| 42 net::URLRequest* request, |
| 43 scoped_ptr<BlobDataHandle> blob_data_handle) { |
| 44 request->SetUserData(&kUserDataKey, blob_data_handle.release()); |
| 45 } |
| 46 |
18 BlobProtocolHandler::BlobProtocolHandler( | 47 BlobProtocolHandler::BlobProtocolHandler( |
19 webkit_blob::BlobStorageController* blob_storage_controller, | |
20 fileapi::FileSystemContext* file_system_context, | 48 fileapi::FileSystemContext* file_system_context, |
21 base::MessageLoopProxy* loop_proxy) | 49 base::MessageLoopProxy* loop_proxy) |
22 : blob_storage_controller_(blob_storage_controller), | 50 : file_system_context_(file_system_context), |
23 file_system_context_(file_system_context), | |
24 file_loop_proxy_(loop_proxy) { | 51 file_loop_proxy_(loop_proxy) { |
25 DCHECK(blob_storage_controller_); | |
26 DCHECK(file_system_context_.get()); | |
27 DCHECK(file_loop_proxy_.get()); | |
28 } | 52 } |
29 | 53 |
30 BlobProtocolHandler::~BlobProtocolHandler() {} | 54 BlobProtocolHandler::~BlobProtocolHandler() { |
| 55 } |
31 | 56 |
32 net::URLRequestJob* BlobProtocolHandler::MaybeCreateJob( | 57 net::URLRequestJob* BlobProtocolHandler::MaybeCreateJob( |
33 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 58 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
34 scoped_refptr<webkit_blob::BlobData> data = LookupBlobData(request); | 59 return new webkit_blob::BlobURLRequestJob( |
35 if (!data.get()) { | 60 request, network_delegate, LookupBlobData(request), |
36 // This request is not coming through resource dispatcher host. | 61 file_system_context_, file_loop_proxy_); |
37 data = blob_storage_controller_->GetBlobDataFromUrl(request->url()); | |
38 } | |
39 return new webkit_blob::BlobURLRequestJob(request, | |
40 network_delegate, | |
41 data.get(), | |
42 file_system_context_.get(), | |
43 file_loop_proxy_.get()); | |
44 } | 62 } |
45 | 63 |
46 scoped_refptr<webkit_blob::BlobData> | 64 scoped_refptr<webkit_blob::BlobData> |
47 BlobProtocolHandler::LookupBlobData(net::URLRequest* request) const { | 65 BlobProtocolHandler::LookupBlobData(net::URLRequest* request) const { |
| 66 BlobDataHandle* blob_data_handle = GetRequestedBlobDataHandle(request); |
| 67 if (blob_data_handle) |
| 68 return blob_data_handle->data(); |
48 return NULL; | 69 return NULL; |
49 } | 70 } |
50 | 71 |
51 } // namespace webkit_blob | 72 } // namespace webkit_blob |
OLD | NEW |