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
3 // found in the LICENSE file.
5 #include "storage/browser/blob/blob_url_request_job_factory.h"
7 #include "base/basictypes.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/request_priority.h"
10 #include "net/url_request/url_request_context.h"
11 #include "storage/browser/blob/blob_data_handle.h"
12 #include "storage/browser/blob/blob_storage_context.h"
13 #include "storage/browser/blob/blob_url_request_job.h"
14 #include "storage/browser/fileapi/file_system_context.h"
20 int kUserDataKey
; // The value is not important, the addr is a key.
22 BlobDataHandle
* GetRequestedBlobDataHandle(net::URLRequest
* request
) {
23 return static_cast<BlobDataHandle
*>(request
->GetUserData(&kUserDataKey
));
29 scoped_ptr
<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 scoped_ptr
<net::URLRequest
> request
= request_context
->CreateRequest(
35 kBlobUrl
, net::DEFAULT_PRIORITY
, request_delegate
);
36 SetRequestedBlobDataHandle(request
.get(), blob_data_handle
.Pass());
37 return request
.Pass();
41 void BlobProtocolHandler::SetRequestedBlobDataHandle(
42 net::URLRequest
* request
,
43 scoped_ptr
<BlobDataHandle
> blob_data_handle
) {
44 request
->SetUserData(&kUserDataKey
, blob_data_handle
.release());
47 BlobProtocolHandler::BlobProtocolHandler(
48 BlobStorageContext
* context
,
49 storage::FileSystemContext
* file_system_context
,
50 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
)
51 : file_system_context_(file_system_context
),
52 file_task_runner_(task_runner
) {
54 context_
= context
->AsWeakPtr();
57 BlobProtocolHandler::~BlobProtocolHandler() {
60 net::URLRequestJob
* BlobProtocolHandler::MaybeCreateJob(
61 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) const {
62 return new storage::BlobURLRequestJob(request
,
64 LookupBlobData(request
),
65 file_system_context_
.get(),
66 file_task_runner_
.get());
69 scoped_ptr
<BlobDataSnapshot
> BlobProtocolHandler::LookupBlobData(
70 net::URLRequest
* request
) const {
71 BlobDataHandle
* blob_data_handle
= GetRequestedBlobDataHandle(request
);
73 return blob_data_handle
->CreateSnapshot().Pass();
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
79 // impl that does not depend on urlfetching to perform this function.
80 const std::string
kPrefix("blob:uuid/");
81 if (!base::StartsWith(request
->url().spec(), kPrefix
,
82 base::CompareCase::SENSITIVE
))
84 std::string uuid
= request
->url().spec().substr(kPrefix
.length());
85 scoped_ptr
<BlobDataHandle
> handle
= context_
->GetBlobDataFromUUID(uuid
);
86 scoped_ptr
<BlobDataSnapshot
> snapshot
;
88 snapshot
= handle
->CreateSnapshot().Pass();
89 SetRequestedBlobDataHandle(request
, handle
.Pass());
91 return snapshot
.Pass();
94 } // namespace storage