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 | |
26 } // namespace | 22 } // namespace |
27 | 23 |
28 // static | 24 // static |
29 scoped_ptr<net::URLRequest> BlobProtocolHandler::CreateBlobRequest( | 25 scoped_ptr<net::URLRequest> BlobProtocolHandler::CreateBlobRequest( |
30 scoped_ptr<BlobDataHandle> blob_data_handle, | 26 scoped_ptr<BlobDataHandle> blob_data_handle, |
31 const net::URLRequestContext* request_context, | 27 const net::URLRequestContext* request_context, |
32 net::URLRequest::Delegate* request_delegate) { | 28 net::URLRequest::Delegate* request_delegate) { |
33 const GURL kBlobUrl("blob://see_user_data/"); | 29 const GURL kBlobUrl("blob://see_user_data/"); |
34 scoped_ptr<net::URLRequest> request = request_context->CreateRequest( | 30 scoped_ptr<net::URLRequest> request = request_context->CreateRequest( |
35 kBlobUrl, net::DEFAULT_PRIORITY, request_delegate); | 31 kBlobUrl, net::DEFAULT_PRIORITY, request_delegate); |
36 SetRequestedBlobDataHandle(request.get(), blob_data_handle.Pass()); | 32 SetRequestedBlobDataHandle(request.get(), blob_data_handle.Pass()); |
37 return request.Pass(); | 33 return request.Pass(); |
38 } | 34 } |
39 | 35 |
40 // static | 36 // static |
41 void BlobProtocolHandler::SetRequestedBlobDataHandle( | 37 void BlobProtocolHandler::SetRequestedBlobDataHandle( |
42 net::URLRequest* request, | 38 net::URLRequest* request, |
43 scoped_ptr<BlobDataHandle> blob_data_handle) { | 39 scoped_ptr<BlobDataHandle> blob_data_handle) { |
44 request->SetUserData(&kUserDataKey, blob_data_handle.release()); | 40 request->SetUserData(&kUserDataKey, blob_data_handle.release()); |
45 } | 41 } |
46 | 42 |
| 43 // static |
| 44 BlobDataHandle* BlobProtocolHandler::GetRequestBlobDataHandle( |
| 45 net::URLRequest* request) { |
| 46 return static_cast<BlobDataHandle*>(request->GetUserData(&kUserDataKey)); |
| 47 } |
| 48 |
47 BlobProtocolHandler::BlobProtocolHandler( | 49 BlobProtocolHandler::BlobProtocolHandler( |
48 BlobStorageContext* context, | 50 BlobStorageContext* context, |
49 storage::FileSystemContext* file_system_context, | 51 storage::FileSystemContext* file_system_context, |
50 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 52 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
51 : file_system_context_(file_system_context), | 53 : file_system_context_(file_system_context), |
52 file_task_runner_(task_runner) { | 54 file_task_runner_(task_runner) { |
53 if (context) | 55 if (context) |
54 context_ = context->AsWeakPtr(); | 56 context_ = context->AsWeakPtr(); |
55 } | 57 } |
56 | 58 |
57 BlobProtocolHandler::~BlobProtocolHandler() { | 59 BlobProtocolHandler::~BlobProtocolHandler() { |
58 } | 60 } |
59 | 61 |
60 net::URLRequestJob* BlobProtocolHandler::MaybeCreateJob( | 62 net::URLRequestJob* BlobProtocolHandler::MaybeCreateJob( |
61 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 63 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
62 return new storage::BlobURLRequestJob(request, | 64 return new storage::BlobURLRequestJob( |
63 network_delegate, | 65 request, network_delegate, LookupBlobHandle(request), |
64 LookupBlobData(request), | 66 file_system_context_.get(), file_task_runner_.get()); |
65 file_system_context_.get(), | |
66 file_task_runner_.get()); | |
67 } | 67 } |
68 | 68 |
69 scoped_ptr<BlobDataSnapshot> BlobProtocolHandler::LookupBlobData( | 69 BlobDataHandle* BlobProtocolHandler::LookupBlobHandle( |
70 net::URLRequest* request) const { | 70 net::URLRequest* request) const { |
71 BlobDataHandle* blob_data_handle = GetRequestedBlobDataHandle(request); | 71 BlobDataHandle* blob_data_handle = GetRequestBlobDataHandle(request); |
72 if (blob_data_handle) | 72 if (blob_data_handle) |
73 return blob_data_handle->CreateSnapshot().Pass(); | 73 return blob_data_handle; |
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 scoped_ptr<BlobDataSnapshot> snapshot; | 86 BlobDataHandle* handle_ptr = handle.get(); |
87 if (handle) { | 87 if (handle) { |
88 snapshot = handle->CreateSnapshot().Pass(); | |
89 SetRequestedBlobDataHandle(request, handle.Pass()); | 88 SetRequestedBlobDataHandle(request, handle.Pass()); |
90 } | 89 } |
91 return snapshot.Pass(); | 90 return handle_ptr; |
92 } | 91 } |
93 | 92 |
94 } // namespace storage | 93 } // namespace storage |
OLD | NEW |