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 "base/bind.h" | |
6 #include "base/callback.h" | |
7 #include "base/location.h" | |
8 #include "base/logging.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/stl_util.h" | |
11 #include "storage/browser/blob/blob_storage_registry.h" | |
michaeln
2015/08/18 02:25:46
you can put the main include up top followed by a
dmurph
2015/09/15 00:54:34
Whoops, thanks.
| |
12 #include "url/gurl.h" | |
13 | |
14 namespace storage { | |
15 using BlobState = BlobStorageRegistry::BlobState; | |
16 | |
17 namespace { | |
18 // We can't use GURL directly for these hash fragment manipulations | |
19 // since it doesn't have specific knowlege of the BlobURL format. GURL | |
20 // treats BlobURLs as if they were PathURLs which don't support hash | |
21 // fragments. | |
22 | |
23 bool BlobUrlHasRef(const GURL& url) { | |
24 return url.spec().find('#') != std::string::npos; | |
25 } | |
26 | |
27 GURL ClearBlobUrlRef(const GURL& url) { | |
28 size_t hash_pos = url.spec().find('#'); | |
29 if (hash_pos == std::string::npos) | |
30 return url; | |
31 return GURL(url.spec().substr(0, hash_pos)); | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 BlobStorageRegistry::BlobRegistryEntry::BlobRegistryEntry(int refcount, | |
37 BlobState state) | |
38 : refcount(refcount), state(state), flags(0) {} | |
39 | |
40 BlobStorageRegistry::BlobRegistryEntry::~BlobRegistryEntry() {} | |
41 | |
42 bool BlobStorageRegistry::BlobRegistryEntry::IsFlagSet(int flag) { | |
43 return ((flags & flag) != 0); | |
michaeln
2015/08/18 02:25:46
probably can inline the flag getters/settings and
dmurph
2015/09/15 00:54:34
done.
| |
44 } | |
45 | |
46 void BlobStorageRegistry::BlobRegistryEntry::SetFlag(int flag) { | |
47 flags |= flag; | |
48 } | |
49 | |
50 void BlobStorageRegistry::BlobRegistryEntry::UnsetFlag(int flag) { | |
51 flags &= !flag; | |
michaeln
2015/08/18 02:25:46
do you want the bitwise operator here?
dmurph
2015/09/15 00:54:34
Yes. This is the current behavior in the system. I
michaeln
2015/10/09 02:16:14
Why, your changing most all other aspects of the c
dmurph
2015/10/10 00:11:33
Done.
| |
52 } | |
53 | |
54 BlobStorageRegistry::BlobStorageRegistry() {} | |
55 | |
56 BlobStorageRegistry::~BlobStorageRegistry() { | |
57 auto begin = blob_map_.begin(); | |
58 auto end = blob_map_.end(); | |
59 while (begin != end) { | |
michaeln
2015/08/18 02:25:46
this is oddly formed for each loop, would a for (c
dmurph
2015/09/15 00:54:34
Done.
| |
60 auto temp = begin; | |
61 ++begin; | |
62 BlobRegistryEntry* entry = temp->second; | |
kinuko
2015/08/14 10:12:18
scoped_ptr<BlobRegistryEntry> entry(temp->second);
dmurph
2015/09/15 00:54:34
Done.
| |
63 // We need to make sure we cleanup all of the callbacks so we don't leave | |
64 // any dangling URL requests around. | |
65 if (entry && !entry->construction_complete_callbacks.empty()) { | |
michaeln
2015/08/18 02:25:46
Can |entry| ever be null? i don't see how it can?
dmurph
2015/09/15 00:54:34
Nope. Removed.
| |
66 for (const auto& callback : entry->construction_complete_callbacks) { | |
67 base::MessageLoop::current()->PostTask(FROM_HERE, | |
68 base::Bind(callback, false)); | |
69 } | |
70 } | |
71 delete temp->second; | |
72 } | |
73 } | |
74 | |
75 BlobStorageRegistry::BlobRegistryEntry* BlobStorageRegistry::RegisterBlobUUID( | |
76 const std::string& uuid) { | |
77 if (blob_map_.find(uuid) != blob_map_.end()) | |
kinuko
2015/08/14 10:12:18
nit: prefer ContainsKey() in stl_util.h in general
michaeln
2015/08/18 02:25:46
Is attempting to reuse a uuid ever expected? I can
dmurph
2015/09/15 00:54:34
Tests accidentally do this a lot, as I've done mys
michaeln
2015/10/09 02:16:14
Fix the test, it's buggy.
dmurph
2015/10/10 00:11:33
Ok, I made it a dcheck
| |
78 return nullptr; | |
79 auto* entry = new BlobRegistryEntry(1, BlobState::RESERVED); | |
80 blob_map_[uuid] = entry; | |
81 return entry; | |
82 } | |
83 | |
84 bool BlobStorageRegistry::RemoveBlobEntry(const std::string& uuid) { | |
85 BlobMap::iterator found = blob_map_.find(uuid); | |
86 if (found == blob_map_.end()) { | |
87 return false; | |
88 } | |
89 delete found->second; | |
90 blob_map_.erase(found); | |
91 return true; | |
92 } | |
93 | |
94 bool BlobStorageRegistry::IsUUIDRegistered(const std::string& uuid) const { | |
95 return blob_map_.find(uuid) != blob_map_.end(); | |
96 } | |
97 | |
98 BlobStorageRegistry::BlobRegistryEntry* BlobStorageRegistry::GetBlobEntry( | |
99 const std::string& uuid) { | |
100 BlobMap::iterator found = blob_map_.find(uuid); | |
101 if (found == blob_map_.end()) { | |
102 return nullptr; | |
103 } | |
104 return found->second; | |
105 } | |
106 | |
107 BlobState BlobStorageRegistry::GetBlobState(const std::string& uuid) const { | |
108 BlobMap::const_iterator found = blob_map_.find(uuid); | |
109 return found == blob_map_.end() ? BlobState::UNKNOWN : found->second->state; | |
110 } | |
111 | |
112 bool BlobStorageRegistry::TestAndSetState(const std::string& uuid, | |
113 BlobState expected, | |
114 BlobState set) { | |
115 BlobRegistryEntry* entry = GetBlobEntry(uuid); | |
116 if (!entry || entry->state != expected) | |
117 return false; | |
118 entry->state = set; | |
119 return true; | |
120 } | |
121 | |
122 bool BlobStorageRegistry::RegisterURLMapping(const GURL& blob_url, | |
123 const std::string& uuid) { | |
124 if (!IsUUIDRegistered(uuid) || IsURLMapped(blob_url)) | |
125 return false; | |
126 url_to_uuid_[blob_url] = uuid; | |
127 return true; | |
128 } | |
129 | |
130 bool BlobStorageRegistry::RemoveURLMapping(const GURL& blob_url, | |
131 std::string* uuid) { | |
132 DCHECK(!BlobUrlHasRef(blob_url)); | |
133 URLMap::iterator found = url_to_uuid_.find(blob_url); | |
134 if (found == url_to_uuid_.end()) { | |
135 return false; | |
136 } | |
137 if (uuid) { | |
138 uuid->assign(found->second); | |
139 } | |
140 url_to_uuid_.erase(found); | |
141 return true; | |
142 } | |
143 | |
144 bool BlobStorageRegistry::IsURLMapped(const GURL& blob_url) const { | |
145 return url_to_uuid_.find(blob_url) != url_to_uuid_.end(); | |
kinuko
2015/08/14 10:12:18
nit: ContainsKey
dmurph
2015/09/15 00:54:34
Done.
| |
146 } | |
147 | |
148 BlobStorageRegistry::BlobRegistryEntry* | |
149 BlobStorageRegistry::GetBlobEntryFromURL(const GURL& url, std::string* uuid) { | |
150 URLMap::iterator found = | |
151 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | |
152 if (found == url_to_uuid_.end()) { | |
153 return nullptr; | |
154 } | |
155 BlobRegistryEntry* entry = GetBlobEntry(found->second); | |
156 if (entry && uuid) { | |
157 uuid->assign(found->second); | |
158 } | |
159 return entry; | |
160 } | |
161 | |
162 } // namespace storage | |
OLD | NEW |