OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "storage/browser/blob/blob_storage_registry.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/location.h" | |
10 #include "base/logging.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/stl_util.h" | |
13 #include "url/gurl.h" | |
14 | |
15 namespace storage { | |
16 using BlobState = BlobStorageRegistry::BlobState; | |
17 | |
18 namespace { | |
19 // We can't use GURL directly for these hash fragment manipulations | |
20 // since it doesn't have specific knowlege of the BlobURL format. GURL | |
21 // treats BlobURLs as if they were PathURLs which don't support hash | |
22 // fragments. | |
23 | |
24 bool BlobUrlHasRef(const GURL& url) { | |
25 return url.spec().find('#') != std::string::npos; | |
26 } | |
27 | |
28 GURL ClearBlobUrlRef(const GURL& url) { | |
29 size_t hash_pos = url.spec().find('#'); | |
30 if (hash_pos == std::string::npos) | |
31 return url; | |
32 return GURL(url.spec().substr(0, hash_pos)); | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 BlobStorageRegistry::Entry::Entry(int refcount, | |
38 BlobState state) | |
michaeln
2015/10/09 02:16:14
indent is (waaaaayyyy :) off
dmurph
2015/10/10 00:11:33
Done.
| |
39 : refcount(refcount), state(state), flags(0) {} | |
40 | |
41 BlobStorageRegistry::Entry::~Entry() {} | |
42 | |
43 BlobStorageRegistry::BlobStorageRegistry() {} | |
44 | |
45 BlobStorageRegistry::~BlobStorageRegistry() { | |
46 for (const auto& pair : blob_map_) { | |
47 scoped_ptr<Entry> entry(pair.second); | |
48 // We need to make sure we cleanup all of the callbacks so we don't leave | |
49 // any dangling URL requests around. | |
michaeln
2015/10/09 02:16:14
Alternatively, just don't callback. The registry c
dmurph
2015/10/10 00:11:33
Done. Left a comment.
| |
50 if (!entry->construction_complete_callbacks.empty()) { | |
51 for (const auto& callback : entry->construction_complete_callbacks) { | |
52 base::MessageLoop::current()->PostTask(FROM_HERE, | |
53 base::Bind(callback, false)); | |
54 } | |
55 } | |
56 } | |
57 } | |
58 | |
59 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( | |
60 const std::string& uuid) { | |
61 if (ContainsKey(blob_map_, uuid)) { | |
62 return nullptr; | |
63 } | |
64 auto* entry = new Entry(1, BlobState::RESERVED); | |
michaeln
2015/10/09 02:16:14
Entry* instead of auto (slightly less magical)
dmurph
2015/10/10 00:11:33
Done.
| |
65 blob_map_[uuid] = entry; | |
66 return entry; | |
67 } | |
68 | |
69 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { | |
70 BlobMap::iterator found = blob_map_.find(uuid); | |
71 if (found == blob_map_.end()) { | |
72 return false; | |
73 } | |
74 delete found->second; | |
75 blob_map_.erase(found); | |
76 return true; | |
77 } | |
78 | |
79 bool BlobStorageRegistry::HasEntry(const std::string& uuid) const { | |
80 return ContainsKey(blob_map_, uuid); | |
81 } | |
82 | |
83 BlobStorageRegistry::Entry* BlobStorageRegistry::GetBlobEntry( | |
84 const std::string& uuid) { | |
85 BlobMap::iterator found = blob_map_.find(uuid); | |
86 if (found == blob_map_.end()) { | |
87 return nullptr; | |
88 } | |
89 return found->second; | |
90 } | |
91 | |
92 BlobState BlobStorageRegistry::GetBlobState(const std::string& uuid) const { | |
93 BlobMap::const_iterator found = blob_map_.find(uuid); | |
94 return found == blob_map_.end() ? BlobState::UNKNOWN : found->second->state; | |
95 } | |
96 | |
97 bool BlobStorageRegistry::TestAndSetState(const std::string& uuid, | |
98 BlobState expected, | |
99 BlobState set) { | |
100 Entry* entry = GetBlobEntry(uuid); | |
101 if (!entry || entry->state != expected) | |
102 return false; | |
103 entry->state = set; | |
104 return true; | |
105 } | |
106 | |
107 bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url, | |
108 const std::string& uuid) { | |
michaeln
2015/10/09 02:16:14
maybe DCHECK(!BlobUrlHasRef(blob_url));
dmurph
2015/10/10 00:11:33
Done.
| |
109 if (!HasEntry(uuid) || IsURLMapped(blob_url)) | |
110 return false; | |
111 url_to_uuid_[blob_url] = uuid; | |
112 return true; | |
113 } | |
114 | |
115 bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url, | |
116 std::string* uuid) { | |
117 DCHECK(!BlobUrlHasRef(blob_url)); | |
118 URLMap::iterator found = url_to_uuid_.find(blob_url); | |
119 if (found == url_to_uuid_.end()) { | |
120 return false; | |
121 } | |
122 if (uuid) { | |
123 uuid->assign(found->second); | |
124 } | |
125 url_to_uuid_.erase(found); | |
126 return true; | |
127 } | |
128 | |
129 bool BlobStorageRegistry::IsURLMapped(const GURL& blob_url) const { | |
130 return ContainsKey(url_to_uuid_, blob_url); | |
131 } | |
132 | |
133 BlobStorageRegistry::Entry* | |
134 BlobStorageRegistry::GetBlobEntryFromURL(const GURL& url, std::string* uuid) { | |
135 URLMap::iterator found = | |
136 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | |
137 if (found == url_to_uuid_.end()) { | |
138 return nullptr; | |
139 } | |
140 Entry* entry = GetBlobEntry(found->second); | |
141 if (entry && uuid) { | |
142 uuid->assign(found->second); | |
143 } | |
144 return entry; | |
145 } | |
146 | |
147 } // namespace storage | |
OLD | NEW |