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 "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 GURL ClearBlobUrlRef(const GURL& url) { | 30 GURL ClearBlobUrlRef(const GURL& url) { |
31 size_t hash_pos = url.spec().find('#'); | 31 size_t hash_pos = url.spec().find('#'); |
32 if (hash_pos == std::string::npos) | 32 if (hash_pos == std::string::npos) |
33 return url; | 33 return url; |
34 return GURL(url.spec().substr(0, hash_pos)); | 34 return GURL(url.spec().substr(0, hash_pos)); |
35 } | 35 } |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 BlobStorageRegistry::Entry::Entry(int refcount, BlobState state) | 39 BlobStorageRegistry::Entry::Entry(int refcount, BlobState state) |
40 : refcount(refcount), state(state), exceeded_memory(false) {} | 40 : refcount(refcount), state(state) {} |
41 | 41 |
42 BlobStorageRegistry::Entry::~Entry() {} | 42 BlobStorageRegistry::Entry::~Entry() {} |
43 | 43 |
44 bool BlobStorageRegistry::Entry::TestAndSetState(BlobState expected, | 44 bool BlobStorageRegistry::Entry::TestAndSetState(BlobState expected, |
45 BlobState set) { | 45 BlobState set) { |
46 if (state != expected) | 46 if (state != expected) |
47 return false; | 47 return false; |
48 state = set; | 48 state = set; |
49 return true; | 49 return true; |
50 } | 50 } |
51 | 51 |
52 BlobStorageRegistry::BlobStorageRegistry() {} | 52 BlobStorageRegistry::BlobStorageRegistry() {} |
53 | 53 |
54 BlobStorageRegistry::~BlobStorageRegistry() { | 54 BlobStorageRegistry::~BlobStorageRegistry() { |
55 // Note: We don't bother calling the construction complete callbacks, as we | 55 // Note: We don't bother calling the construction complete callbacks, as we |
56 // are only being destructed at the end of the life of the browser process. | 56 // are only being destructed at the end of the life of the browser process. |
57 // So it shouldn't matter. | 57 // So it shouldn't matter. |
58 } | 58 } |
59 | 59 |
60 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( | 60 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( |
61 const std::string& uuid) { | 61 const std::string& uuid) { |
62 DCHECK(!ContainsKey(blob_map_, uuid)); | 62 DCHECK(!ContainsKey(blob_map_, uuid)); |
63 Entry* entry = new Entry(1, BlobState::RESERVED); | 63 Entry* entry = new Entry(1, BlobState::PENDING); |
64 blob_map_.add(uuid, make_scoped_ptr(entry)); | 64 blob_map_.add(uuid, make_scoped_ptr(entry)); |
65 return entry; | 65 return entry; |
66 } | 66 } |
67 | 67 |
68 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { | 68 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { |
69 return blob_map_.erase(uuid) == 1; | 69 return blob_map_.erase(uuid) == 1; |
70 } | 70 } |
71 | 71 |
| 72 bool BlobStorageRegistry::HasEntry(const std::string& uuid) const { |
| 73 return blob_map_.find(uuid) != blob_map_.end(); |
| 74 } |
| 75 |
72 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry( | 76 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry( |
73 const std::string& uuid) { | 77 const std::string& uuid) { |
74 BlobMap::iterator found = blob_map_.find(uuid); | 78 BlobMap::iterator found = blob_map_.find(uuid); |
75 if (found == blob_map_.end()) | 79 if (found == blob_map_.end()) |
76 return nullptr; | 80 return nullptr; |
77 return found->second; | 81 return found->second; |
78 } | 82 } |
79 | 83 |
| 84 const BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry( |
| 85 const std::string& uuid) const { |
| 86 return const_cast<BlobStorageRegistry*>(this)->GetEntry(uuid); |
| 87 } |
| 88 |
80 bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url, | 89 bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url, |
81 const std::string& uuid) { | 90 const std::string& uuid) { |
82 DCHECK(!BlobUrlHasRef(blob_url)); | 91 DCHECK(!BlobUrlHasRef(blob_url)); |
83 if (blob_map_.find(uuid) == blob_map_.end() || IsURLMapped(blob_url)) | 92 if (blob_map_.find(uuid) == blob_map_.end() || IsURLMapped(blob_url)) |
84 return false; | 93 return false; |
85 url_to_uuid_[blob_url] = uuid; | 94 url_to_uuid_[blob_url] = uuid; |
86 return true; | 95 return true; |
87 } | 96 } |
88 | 97 |
89 bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url, | 98 bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url, |
(...skipping 19 matching lines...) Expand all Loading... |
109 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | 118 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); |
110 if (found == url_to_uuid_.end()) | 119 if (found == url_to_uuid_.end()) |
111 return nullptr; | 120 return nullptr; |
112 Entry* entry = GetEntry(found->second); | 121 Entry* entry = GetEntry(found->second); |
113 if (entry && uuid) | 122 if (entry && uuid) |
114 uuid->assign(found->second); | 123 uuid->assign(found->second); |
115 return entry; | 124 return entry; |
116 } | 125 } |
117 | 126 |
118 } // namespace storage | 127 } // namespace storage |
OLD | NEW |