OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_storage_registry.h" | 5 #include "storage/browser/blob/blob_storage_registry.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | |
13 #include "base/location.h" | 12 #include "base/location.h" |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "storage/browser/blob/internal_blob_data.h" |
17 #include "url/gurl.h" | 16 #include "url/gurl.h" |
18 | 17 |
19 namespace storage { | 18 namespace storage { |
20 using BlobState = BlobStorageRegistry::BlobState; | |
21 | 19 |
22 namespace { | 20 namespace { |
23 // We can't use GURL directly for these hash fragment manipulations | 21 // We can't use GURL directly for these hash fragment manipulations |
24 // since it doesn't have specific knowlege of the BlobURL format. GURL | 22 // since it doesn't have specific knowlege of the BlobURL format. GURL |
25 // treats BlobURLs as if they were PathURLs which don't support hash | 23 // treats BlobURLs as if they were PathURLs which don't support hash |
26 // fragments. | 24 // fragments. |
27 | 25 |
28 bool BlobUrlHasRef(const GURL& url) { | 26 bool BlobUrlHasRef(const GURL& url) { |
29 return url.spec().find('#') != std::string::npos; | 27 return url.spec().find('#') != std::string::npos; |
30 } | 28 } |
31 | 29 |
32 GURL ClearBlobUrlRef(const GURL& url) { | 30 GURL ClearBlobUrlRef(const GURL& url) { |
33 size_t hash_pos = url.spec().find('#'); | 31 size_t hash_pos = url.spec().find('#'); |
34 if (hash_pos == std::string::npos) | 32 if (hash_pos == std::string::npos) |
35 return url; | 33 return url; |
36 return GURL(url.spec().substr(0, hash_pos)); | 34 return GURL(url.spec().substr(0, hash_pos)); |
37 } | 35 } |
38 | 36 |
39 } // namespace | 37 } // namespace |
40 | 38 |
41 BlobStorageRegistry::Entry::Entry(int refcount, BlobState state) | |
42 : refcount(refcount), state(state) {} | |
43 | |
44 BlobStorageRegistry::Entry::~Entry() {} | |
45 | |
46 bool BlobStorageRegistry::Entry::TestAndSetState(BlobState expected, | |
47 BlobState set) { | |
48 if (state != expected) | |
49 return false; | |
50 state = set; | |
51 return true; | |
52 } | |
53 | 39 |
54 BlobStorageRegistry::BlobStorageRegistry() {} | 40 BlobStorageRegistry::BlobStorageRegistry() {} |
55 | 41 |
56 BlobStorageRegistry::~BlobStorageRegistry() { | 42 BlobStorageRegistry::~BlobStorageRegistry() { |
57 // Note: We don't bother calling the construction complete callbacks, as we | 43 // Note: We don't bother calling the construction complete callbacks, as we |
58 // are only being destructed at the end of the life of the browser process. | 44 // are only being destructed at the end of the life of the browser process. |
59 // So it shouldn't matter. | 45 // So it shouldn't matter. |
60 } | 46 } |
61 | 47 |
62 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( | 48 InternalBlobData* BlobStorageRegistry::CreateEntry( |
63 const std::string& uuid, | 49 const std::string& uuid, |
64 const std::string& content_type, | 50 const std::string& content_type, |
65 const std::string& content_disposition) { | 51 const std::string& content_disposition) { |
66 DCHECK(!base::ContainsKey(blob_map_, uuid)); | 52 DCHECK(!ContainsKey(blob_map_, uuid)); |
67 std::unique_ptr<Entry> entry(new Entry(1, BlobState::PENDING)); | 53 std::unique_ptr<InternalBlobData> entry( |
68 entry->content_type = content_type; | 54 new InternalBlobData(content_type, content_disposition)); |
69 entry->content_disposition = content_disposition; | 55 InternalBlobData* entry_ptr = entry.get(); |
70 Entry* entry_ptr = entry.get(); | |
71 blob_map_.add(uuid, std::move(entry)); | 56 blob_map_.add(uuid, std::move(entry)); |
72 return entry_ptr; | 57 return entry_ptr; |
73 } | 58 } |
74 | 59 |
75 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { | 60 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { |
76 return blob_map_.erase(uuid) == 1; | 61 return blob_map_.erase(uuid) == 1; |
77 } | 62 } |
78 | 63 |
79 bool BlobStorageRegistry::HasEntry(const std::string& uuid) const { | 64 bool BlobStorageRegistry::HasEntry(const std::string& uuid) const { |
80 return blob_map_.find(uuid) != blob_map_.end(); | 65 return blob_map_.find(uuid) != blob_map_.end(); |
81 } | 66 } |
82 | 67 |
83 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry( | 68 InternalBlobData* BlobStorageRegistry::GetEntry(const std::string& uuid) { |
84 const std::string& uuid) { | |
85 BlobMap::iterator found = blob_map_.find(uuid); | 69 BlobMap::iterator found = blob_map_.find(uuid); |
86 if (found == blob_map_.end()) | 70 if (found == blob_map_.end()) |
87 return nullptr; | 71 return nullptr; |
88 return found->second; | 72 return found->second; |
89 } | 73 } |
90 | 74 |
91 const BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry( | 75 const InternalBlobData* BlobStorageRegistry::GetEntry( |
92 const std::string& uuid) const { | 76 const std::string& uuid) const { |
93 return const_cast<BlobStorageRegistry*>(this)->GetEntry(uuid); | 77 return const_cast<BlobStorageRegistry*>(this)->GetEntry(uuid); |
94 } | 78 } |
95 | 79 |
96 bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url, | 80 bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url, |
97 const std::string& uuid) { | 81 const std::string& uuid) { |
98 DCHECK(!BlobUrlHasRef(blob_url)); | 82 DCHECK(!BlobUrlHasRef(blob_url)); |
99 if (blob_map_.find(uuid) == blob_map_.end() || IsURLMapped(blob_url)) | 83 if (blob_map_.find(uuid) == blob_map_.end() || IsURLMapped(blob_url)) |
100 return false; | 84 return false; |
101 url_to_uuid_[blob_url] = uuid; | 85 url_to_uuid_[blob_url] = uuid; |
102 return true; | 86 return true; |
103 } | 87 } |
104 | 88 |
105 bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url, | 89 bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url, |
106 std::string* uuid) { | 90 std::string* uuid) { |
107 DCHECK(!BlobUrlHasRef(blob_url)); | 91 DCHECK(!BlobUrlHasRef(blob_url)); |
108 URLMap::iterator found = url_to_uuid_.find(blob_url); | 92 URLMap::iterator found = url_to_uuid_.find(blob_url); |
109 if (found == url_to_uuid_.end()) | 93 if (found == url_to_uuid_.end()) |
110 return false; | 94 return false; |
111 if (uuid) | 95 if (uuid) |
112 uuid->assign(found->second); | 96 uuid->assign(found->second); |
113 url_to_uuid_.erase(found); | 97 url_to_uuid_.erase(found); |
114 return true; | 98 return true; |
115 } | 99 } |
116 | 100 |
117 bool BlobStorageRegistry::IsURLMapped(const GURL& blob_url) const { | 101 bool BlobStorageRegistry::IsURLMapped(const GURL& blob_url) const { |
118 return base::ContainsKey(url_to_uuid_, blob_url); | 102 return base::ContainsKey(url_to_uuid_, blob_url); |
119 } | 103 } |
120 | 104 |
121 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntryFromURL( | 105 InternalBlobData* BlobStorageRegistry::GetEntryFromURL(const GURL& url, |
122 const GURL& url, | 106 std::string* uuid) { |
123 std::string* uuid) { | |
124 URLMap::iterator found = | 107 URLMap::iterator found = |
125 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | 108 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); |
126 if (found == url_to_uuid_.end()) | 109 if (found == url_to_uuid_.end()) |
127 return nullptr; | 110 return nullptr; |
128 Entry* entry = GetEntry(found->second); | 111 InternalBlobData* entry = GetEntry(found->second); |
129 if (entry && uuid) | 112 if (entry && uuid) |
130 uuid->assign(found->second); | 113 uuid->assign(found->second); |
131 return entry; | 114 return entry; |
132 } | 115 } |
133 | 116 |
134 } // namespace storage | 117 } // namespace storage |
OLD | NEW |