Chromium Code Reviews| 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, BlobState state) | |
| 38 : refcount(refcount), state(state), exceeded_memory(false) {} | |
| 39 | |
| 40 BlobStorageRegistry::Entry::~Entry() {} | |
| 41 | |
| 42 bool BlobStorageRegistry::Entry::TestAndSetState(BlobState expected, | |
| 43 BlobState set) { | |
| 44 if (state != expected) | |
| 45 return false; | |
| 46 state = set; | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 BlobStorageRegistry::BlobStorageRegistry() {} | |
| 51 | |
| 52 BlobStorageRegistry::~BlobStorageRegistry() { | |
| 53 // Note: We don't bother calling the construction complete callbacks, as we | |
| 54 // are only being destructed at the end of the life of the browser process. | |
| 55 // So it shouldn't matter. | |
| 56 STLDeleteContainerPairSecondPointers(blob_map_.begin(), blob_map_.end()); | |
| 57 } | |
| 58 | |
| 59 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( | |
| 60 const std::string& uuid) { | |
| 61 DCHECK(!ContainsKey(blob_map_, uuid)); | |
| 62 Entry* entry = new Entry(1, BlobState::RESERVED); | |
| 63 blob_map_[uuid] = entry; | |
| 64 return entry; | |
| 65 } | |
| 66 | |
| 67 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { | |
| 68 BlobMap::iterator found = blob_map_.find(uuid); | |
| 69 if (found == blob_map_.end()) { | |
| 70 return false; | |
| 71 } | |
| 72 delete found->second; | |
| 73 blob_map_.erase(found); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 bool BlobStorageRegistry::HasEntry(const std::string& uuid) const { | |
| 78 return ContainsKey(blob_map_, uuid); | |
| 79 } | |
| 80 | |
| 81 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry( | |
| 82 const std::string& uuid) const { | |
| 83 BlobMap::const_iterator found = blob_map_.find(uuid); | |
| 84 if (found == blob_map_.end()) { | |
| 85 return nullptr; | |
| 86 } | |
| 87 return found->second; | |
| 88 } | |
| 89 | |
| 90 bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url, | |
| 91 const std::string& uuid) { | |
| 92 DCHECK(!BlobUrlHasRef(blob_url)); | |
| 93 if (!HasEntry(uuid) || IsURLMapped(blob_url)) | |
| 94 return false; | |
| 95 url_to_uuid_[blob_url] = uuid; | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url, | |
| 100 std::string* uuid) { | |
| 101 DCHECK(!BlobUrlHasRef(blob_url)); | |
| 102 URLMap::iterator found = url_to_uuid_.find(blob_url); | |
| 103 if (found == url_to_uuid_.end()) { | |
| 104 return false; | |
| 105 } | |
|
kinuko
2015/10/12 14:34:58
nit: some one-line block has {} while some do not,
dmurph
2015/10/12 20:08:28
Fixed.
| |
| 106 if (uuid) { | |
| 107 uuid->assign(found->second); | |
| 108 } | |
| 109 url_to_uuid_.erase(found); | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 bool BlobStorageRegistry::IsURLMapped(const GURL& blob_url) const { | |
| 114 return ContainsKey(url_to_uuid_, blob_url); | |
| 115 } | |
| 116 | |
| 117 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntryFromURL( | |
| 118 const GURL& url, | |
| 119 std::string* uuid) const { | |
| 120 URLMap::const_iterator found = | |
| 121 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | |
| 122 if (found == url_to_uuid_.end()) { | |
| 123 return nullptr; | |
| 124 } | |
| 125 Entry* entry = GetEntry(found->second); | |
| 126 if (entry && uuid) { | |
| 127 uuid->assign(found->second); | |
| 128 } | |
| 129 return entry; | |
| 130 } | |
| 131 | |
| 132 } // namespace storage | |
| OLD | NEW |