OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/mock_blob_url_request_context.h" | 5 #include "webkit/browser/blob/mock_blob_url_request_context.h" |
6 | 6 |
7 #include "webkit/browser/blob/blob_storage_controller.h" | 7 #include "webkit/browser/blob/blob_storage_context.h" |
8 #include "webkit/browser/blob/blob_url_request_job.h" | 8 #include "webkit/browser/blob/blob_url_request_job.h" |
| 9 #include "webkit/browser/blob/blob_url_request_job_factory.h" |
9 #include "webkit/common/blob/blob_data.h" | 10 #include "webkit/common/blob/blob_data.h" |
10 | 11 |
| 12 |
11 namespace webkit_blob { | 13 namespace webkit_blob { |
12 | 14 |
13 namespace { | |
14 | |
15 class MockBlobProtocolHandler | |
16 : public net::URLRequestJobFactory::ProtocolHandler { | |
17 public: | |
18 explicit MockBlobProtocolHandler( | |
19 BlobStorageController* blob_storage_controller, | |
20 fileapi::FileSystemContext* file_system_context) | |
21 : blob_storage_controller_(blob_storage_controller), | |
22 file_system_context_(file_system_context) {} | |
23 | |
24 virtual ~MockBlobProtocolHandler() {} | |
25 | |
26 virtual net::URLRequestJob* MaybeCreateJob( | |
27 net::URLRequest* request, | |
28 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
29 return new BlobURLRequestJob( | |
30 request, | |
31 network_delegate, | |
32 blob_storage_controller_->GetBlobDataFromUrl(request->url()), | |
33 file_system_context_, | |
34 base::MessageLoopProxy::current().get()); | |
35 } | |
36 | |
37 private: | |
38 webkit_blob::BlobStorageController* const blob_storage_controller_; | |
39 fileapi::FileSystemContext* const file_system_context_; | |
40 | |
41 DISALLOW_COPY_AND_ASSIGN(MockBlobProtocolHandler); | |
42 }; | |
43 | |
44 } // namespace | |
45 | |
46 MockBlobURLRequestContext::MockBlobURLRequestContext( | 15 MockBlobURLRequestContext::MockBlobURLRequestContext( |
47 fileapi::FileSystemContext* file_system_context) | 16 fileapi::FileSystemContext* file_system_context) |
48 : blob_storage_controller_(new BlobStorageController) { | 17 : blob_storage_context_(new BlobStorageContext) { |
49 // Job factory owns the protocol handler. | 18 // Job factory owns the protocol handler. |
50 job_factory_.SetProtocolHandler( | 19 job_factory_.SetProtocolHandler( |
51 "blob", new MockBlobProtocolHandler(blob_storage_controller_.get(), | 20 "blob", new BlobProtocolHandler(file_system_context, |
52 file_system_context)); | 21 base::MessageLoopProxy::current())); |
53 set_job_factory(&job_factory_); | 22 set_job_factory(&job_factory_); |
54 } | 23 } |
55 | 24 |
56 MockBlobURLRequestContext::~MockBlobURLRequestContext() {} | 25 MockBlobURLRequestContext::~MockBlobURLRequestContext() { |
| 26 } |
57 | 27 |
58 ScopedTextBlob::ScopedTextBlob( | 28 ScopedTextBlob::ScopedTextBlob( |
59 const MockBlobURLRequestContext& request_context, | 29 const MockBlobURLRequestContext& request_context, |
60 const GURL& blob_url, | 30 const std::string& blob_id, |
61 const std::string& data) | 31 const std::string& data) |
62 : blob_url_(blob_url), | 32 : blob_id_(blob_id), |
63 blob_storage_controller_(request_context.blob_storage_controller()) { | 33 context_(request_context.blob_storage_context()) { |
64 DCHECK(blob_storage_controller_); | 34 DCHECK(context_); |
65 scoped_refptr<BlobData> blob_data(new BlobData()); | 35 scoped_refptr<BlobData> blob_data(new BlobData(blob_id_)); |
66 blob_data->AppendData(data); | 36 if (!data.empty()) |
67 blob_storage_controller_->AddFinishedBlob(blob_url_, blob_data.get()); | 37 blob_data->AppendData(data); |
| 38 handle_ = context_->AddFinishedBlob(blob_data); |
68 } | 39 } |
69 | 40 |
70 ScopedTextBlob::~ScopedTextBlob() { | 41 ScopedTextBlob::~ScopedTextBlob() { |
71 blob_storage_controller_->RemoveBlob(blob_url_); | 42 } |
| 43 |
| 44 scoped_ptr<BlobDataHandle> ScopedTextBlob::GetBlobDataHandle() { |
| 45 return context_->GetBlobDataFromUUID(blob_id_); |
72 } | 46 } |
73 | 47 |
74 } // namespace webkit_blob | 48 } // namespace webkit_blob |
OLD | NEW |