OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/fileapi/chrome_blob_storage_context.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/guid.h" | |
11 #include "content/public/browser/blob_handle.h" | |
12 #include "content/public/browser/browser_context.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "storage/browser/blob/blob_data_builder.h" | |
15 #include "storage/browser/blob/blob_data_handle.h" | |
16 #include "storage/browser/blob/blob_storage_context.h" | |
17 | |
18 using base::UserDataAdapter; | |
19 using storage::BlobStorageContext; | |
20 | |
21 namespace content { | |
22 | |
23 namespace { | |
24 | |
25 const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; | |
26 | |
27 class BlobHandleImpl : public BlobHandle { | |
28 public: | |
29 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) | |
30 : handle_(std::move(handle)) {} | |
31 | |
32 ~BlobHandleImpl() override {} | |
33 | |
34 std::string GetUUID() override { return handle_->uuid(); } | |
35 | |
36 private: | |
37 std::unique_ptr<storage::BlobDataHandle> handle_; | |
38 }; | |
39 | |
40 } // namespace | |
41 | |
42 ChromeBlobStorageContext::ChromeBlobStorageContext() {} | |
43 | |
44 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( | |
45 BrowserContext* context) { | |
46 if (!context->GetUserData(kBlobStorageContextKeyName)) { | |
47 scoped_refptr<ChromeBlobStorageContext> blob = | |
48 new ChromeBlobStorageContext(); | |
49 context->SetUserData( | |
50 kBlobStorageContextKeyName, | |
51 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); | |
52 // Check first to avoid memory leak in unittests. | |
53 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | |
54 BrowserThread::PostTask( | |
55 BrowserThread::IO, FROM_HERE, | |
56 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); | |
57 } | |
58 } | |
59 | |
60 return UserDataAdapter<ChromeBlobStorageContext>::Get( | |
61 context, kBlobStorageContextKeyName); | |
62 } | |
63 | |
64 void ChromeBlobStorageContext::InitializeOnIOThread() { | |
65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
66 context_.reset(new BlobStorageContext()); | |
67 } | |
68 | |
69 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( | |
70 const char* data, | |
71 size_t length) { | |
72 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
73 | |
74 std::string uuid(base::GenerateGUID()); | |
75 storage::BlobDataBuilder blob_data_builder(uuid); | |
76 blob_data_builder.AppendData(data, length); | |
77 | |
78 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | |
79 context_->AddFinishedBlob(&blob_data_builder); | |
80 if (!blob_data_handle) | |
81 return std::unique_ptr<BlobHandle>(); | |
82 | |
83 std::unique_ptr<BlobHandle> blob_handle( | |
84 new BlobHandleImpl(std::move(blob_data_handle))); | |
85 return blob_handle; | |
86 } | |
87 | |
88 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateFileBackedBlob( | |
89 const base::FilePath& path, | |
90 int64_t offset, | |
91 int64_t size, | |
92 const base::Time& expected_modification_time) { | |
93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
94 | |
95 std::string uuid(base::GenerateGUID()); | |
96 storage::BlobDataBuilder blob_data_builder(uuid); | |
97 blob_data_builder.AppendFile(path, offset, size, expected_modification_time); | |
98 | |
99 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | |
100 context_->AddFinishedBlob(&blob_data_builder); | |
101 if (!blob_data_handle) | |
102 return std::unique_ptr<BlobHandle>(); | |
103 | |
104 std::unique_ptr<BlobHandle> blob_handle( | |
105 new BlobHandleImpl(std::move(blob_data_handle))); | |
106 return blob_handle; | |
107 } | |
108 | |
109 ChromeBlobStorageContext::~ChromeBlobStorageContext() {} | |
110 | |
111 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { | |
112 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && | |
113 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
114 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); | |
115 return; | |
116 } | |
117 delete this; | |
118 } | |
119 | |
120 } // namespace content | |
OLD | NEW |