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 } |
| 57 |
| 58 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( |
| 59 const std::string& uuid) { |
| 60 DCHECK(!ContainsKey(blob_map_, uuid)); |
| 61 Entry* entry = new Entry(1, BlobState::RESERVED); |
| 62 blob_map_.add(uuid, make_scoped_ptr(entry)); |
| 63 return entry; |
| 64 } |
| 65 |
| 66 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { |
| 67 return blob_map_.erase(uuid) == 1; |
| 68 } |
| 69 |
| 70 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntry( |
| 71 const std::string& uuid) { |
| 72 BlobMap::iterator found = blob_map_.find(uuid); |
| 73 if (found == blob_map_.end()) |
| 74 return nullptr; |
| 75 return found->second; |
| 76 } |
| 77 |
| 78 bool BlobStorageRegistry::CreateUrlMapping(const GURL& blob_url, |
| 79 const std::string& uuid) { |
| 80 DCHECK(!BlobUrlHasRef(blob_url)); |
| 81 if (blob_map_.find(uuid) == blob_map_.end() || IsURLMapped(blob_url)) |
| 82 return false; |
| 83 url_to_uuid_[blob_url] = uuid; |
| 84 return true; |
| 85 } |
| 86 |
| 87 bool BlobStorageRegistry::DeleteURLMapping(const GURL& blob_url, |
| 88 std::string* uuid) { |
| 89 DCHECK(!BlobUrlHasRef(blob_url)); |
| 90 URLMap::iterator found = url_to_uuid_.find(blob_url); |
| 91 if (found == url_to_uuid_.end()) |
| 92 return false; |
| 93 if (uuid) |
| 94 uuid->assign(found->second); |
| 95 url_to_uuid_.erase(found); |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool BlobStorageRegistry::IsURLMapped(const GURL& blob_url) const { |
| 100 return ContainsKey(url_to_uuid_, blob_url); |
| 101 } |
| 102 |
| 103 BlobStorageRegistry::Entry* BlobStorageRegistry::GetEntryFromURL( |
| 104 const GURL& url, |
| 105 std::string* uuid) { |
| 106 URLMap::iterator found = |
| 107 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); |
| 108 if (found == url_to_uuid_.end()) |
| 109 return nullptr; |
| 110 Entry* entry = GetEntry(found->second); |
| 111 if (entry && uuid) |
| 112 uuid->assign(found->second); |
| 113 return entry; |
| 114 } |
| 115 |
| 116 } // namespace storage |
OLD | NEW |