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 "storage/browser/blob/blob_url_request_job_factory.h" | 5 #include "storage/browser/blob/blob_url_request_job_factory.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "net/base/request_priority.h" | 9 #include "net/base/request_priority.h" |
10 #include "net/url_request/url_request_context.h" | 10 #include "net/url_request/url_request_context.h" |
11 #include "storage/browser/blob/blob_data_handle.h" | 11 #include "storage/browser/blob/blob_data_handle.h" |
12 #include "storage/browser/blob/blob_storage_context.h" | 12 #include "storage/browser/blob/blob_storage_context.h" |
13 #include "storage/browser/blob/blob_url_request_job.h" | 13 #include "storage/browser/blob/blob_url_request_job.h" |
14 #include "storage/browser/fileapi/file_system_context.h" | 14 #include "storage/browser/fileapi/file_system_context.h" |
15 | 15 |
16 namespace storage { | 16 namespace storage { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 int kUserDataKey; // The value is not important, the addr is a key. | 20 int kUserDataKey; // The value is not important, the addr is a key. |
21 | 21 |
| 22 BlobDataHandle* GetRequestedBlobDataHandle(net::URLRequest* request) { |
| 23 return static_cast<BlobDataHandle*>(request->GetUserData(&kUserDataKey)); |
| 24 } |
| 25 |
22 } // namespace | 26 } // namespace |
23 | 27 |
24 // static | 28 // static |
25 scoped_ptr<net::URLRequest> BlobProtocolHandler::CreateBlobRequest( | 29 scoped_ptr<net::URLRequest> BlobProtocolHandler::CreateBlobRequest( |
26 scoped_ptr<BlobDataHandle> blob_data_handle, | 30 scoped_ptr<BlobDataHandle> blob_data_handle, |
27 const net::URLRequestContext* request_context, | 31 const net::URLRequestContext* request_context, |
28 net::URLRequest::Delegate* request_delegate) { | 32 net::URLRequest::Delegate* request_delegate) { |
29 const GURL kBlobUrl("blob://see_user_data/"); | 33 const GURL kBlobUrl("blob://see_user_data/"); |
30 scoped_ptr<net::URLRequest> request = request_context->CreateRequest( | 34 scoped_ptr<net::URLRequest> request = request_context->CreateRequest( |
31 kBlobUrl, net::DEFAULT_PRIORITY, request_delegate); | 35 kBlobUrl, net::DEFAULT_PRIORITY, request_delegate); |
32 SetRequestedBlobDataHandle(request.get(), blob_data_handle.Pass()); | 36 SetRequestedBlobDataHandle(request.get(), blob_data_handle.Pass()); |
33 return request.Pass(); | 37 return request.Pass(); |
34 } | 38 } |
35 | 39 |
36 // static | 40 // static |
37 void BlobProtocolHandler::SetRequestedBlobDataHandle( | 41 void BlobProtocolHandler::SetRequestedBlobDataHandle( |
38 net::URLRequest* request, | 42 net::URLRequest* request, |
39 scoped_ptr<BlobDataHandle> blob_data_handle) { | 43 scoped_ptr<BlobDataHandle> blob_data_handle) { |
40 request->SetUserData(&kUserDataKey, blob_data_handle.release()); | 44 request->SetUserData(&kUserDataKey, blob_data_handle.release()); |
41 } | 45 } |
42 | 46 |
43 // static | |
44 BlobDataHandle* BlobProtocolHandler::GetRequestBlobDataHandle( | |
45 net::URLRequest* request) { | |
46 return static_cast<BlobDataHandle*>(request->GetUserData(&kUserDataKey)); | |
47 } | |
48 | |
49 BlobProtocolHandler::BlobProtocolHandler( | 47 BlobProtocolHandler::BlobProtocolHandler( |
50 BlobStorageContext* context, | 48 BlobStorageContext* context, |
51 storage::FileSystemContext* file_system_context, | 49 storage::FileSystemContext* file_system_context, |
52 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 50 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
53 : file_system_context_(file_system_context), | 51 : file_system_context_(file_system_context), |
54 file_task_runner_(task_runner) { | 52 file_task_runner_(task_runner) { |
55 if (context) | 53 if (context) |
56 context_ = context->AsWeakPtr(); | 54 context_ = context->AsWeakPtr(); |
57 } | 55 } |
58 | 56 |
59 BlobProtocolHandler::~BlobProtocolHandler() { | 57 BlobProtocolHandler::~BlobProtocolHandler() { |
60 } | 58 } |
61 | 59 |
62 net::URLRequestJob* BlobProtocolHandler::MaybeCreateJob( | 60 net::URLRequestJob* BlobProtocolHandler::MaybeCreateJob( |
63 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 61 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
64 return new storage::BlobURLRequestJob( | 62 return new storage::BlobURLRequestJob(request, |
65 request, network_delegate, LookupBlobHandle(request), | 63 network_delegate, |
66 file_system_context_.get(), file_task_runner_.get()); | 64 LookupBlobData(request), |
| 65 file_system_context_.get(), |
| 66 file_task_runner_.get()); |
67 } | 67 } |
68 | 68 |
69 BlobDataHandle* BlobProtocolHandler::LookupBlobHandle( | 69 scoped_ptr<BlobDataSnapshot> BlobProtocolHandler::LookupBlobData( |
70 net::URLRequest* request) const { | 70 net::URLRequest* request) const { |
71 BlobDataHandle* blob_data_handle = GetRequestBlobDataHandle(request); | 71 BlobDataHandle* blob_data_handle = GetRequestedBlobDataHandle(request); |
72 if (blob_data_handle) | 72 if (blob_data_handle) |
73 return blob_data_handle; | 73 return blob_data_handle->CreateSnapshot().Pass(); |
74 if (!context_.get()) | 74 if (!context_.get()) |
75 return NULL; | 75 return NULL; |
76 | 76 |
77 // Support looking up based on uuid, the FeedbackExtensionAPI relies on this. | 77 // Support looking up based on uuid, the FeedbackExtensionAPI relies on this. |
78 // TODO(michaeln): Replace this use case and others like it with a BlobReader | 78 // TODO(michaeln): Replace this use case and others like it with a BlobReader |
79 // impl that does not depend on urlfetching to perform this function. | 79 // impl that does not depend on urlfetching to perform this function. |
80 const std::string kPrefix("blob:uuid/"); | 80 const std::string kPrefix("blob:uuid/"); |
81 if (!base::StartsWith(request->url().spec(), kPrefix, | 81 if (!base::StartsWith(request->url().spec(), kPrefix, |
82 base::CompareCase::SENSITIVE)) | 82 base::CompareCase::SENSITIVE)) |
83 return NULL; | 83 return NULL; |
84 std::string uuid = request->url().spec().substr(kPrefix.length()); | 84 std::string uuid = request->url().spec().substr(kPrefix.length()); |
85 scoped_ptr<BlobDataHandle> handle = context_->GetBlobDataFromUUID(uuid); | 85 scoped_ptr<BlobDataHandle> handle = context_->GetBlobDataFromUUID(uuid); |
86 BlobDataHandle* handle_ptr = handle.get(); | 86 scoped_ptr<BlobDataSnapshot> snapshot; |
87 if (handle) { | 87 if (handle) { |
| 88 snapshot = handle->CreateSnapshot().Pass(); |
88 SetRequestedBlobDataHandle(request, handle.Pass()); | 89 SetRequestedBlobDataHandle(request, handle.Pass()); |
89 } | 90 } |
90 return handle_ptr; | 91 return snapshot.Pass(); |
91 } | 92 } |
92 | 93 |
93 } // namespace storage | 94 } // namespace storage |
OLD | NEW |