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