| 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 <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 namespace storage { | 19 namespace storage { |
| 20 using BlobState = BlobStorageRegistry::BlobState; | |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 // We can't use GURL directly for these hash fragment manipulations | 22 // We can't use GURL directly for these hash fragment manipulations |
| 24 // since it doesn't have specific knowlege of the BlobURL format. GURL | 23 // since it doesn't have specific knowlege of the BlobURL format. GURL |
| 25 // treats BlobURLs as if they were PathURLs which don't support hash | 24 // treats BlobURLs as if they were PathURLs which don't support hash |
| 26 // fragments. | 25 // fragments. |
| 27 | 26 |
| 28 bool BlobUrlHasRef(const GURL& url) { | 27 bool BlobUrlHasRef(const GURL& url) { |
| 29 return url.spec().find('#') != std::string::npos; | 28 return url.spec().find('#') != std::string::npos; |
| 30 } | 29 } |
| 31 | 30 |
| 32 GURL ClearBlobUrlRef(const GURL& url) { | 31 GURL ClearBlobUrlRef(const GURL& url) { |
| 33 size_t hash_pos = url.spec().find('#'); | 32 size_t hash_pos = url.spec().find('#'); |
| 34 if (hash_pos == std::string::npos) | 33 if (hash_pos == std::string::npos) |
| 35 return url; | 34 return url; |
| 36 return GURL(url.spec().substr(0, hash_pos)); | 35 return GURL(url.spec().substr(0, hash_pos)); |
| 37 } | 36 } |
| 38 | 37 |
| 39 } // namespace | 38 } // namespace |
| 40 | 39 |
| 41 BlobStorageRegistry::Entry::Entry(int refcount, BlobState state) | 40 BlobStorageRegistry::ItemCopyEntry::ItemCopyEntry( |
| 42 : refcount(refcount), state(state) {} | 41 scoped_refptr<ShareableBlobDataItem> source_item, |
| 42 size_t source_item_offset, |
| 43 scoped_refptr<ShareableBlobDataItem> dest_item) |
| 44 : source_item(std::move(source_item)), |
| 45 source_item_offset(source_item_offset), |
| 46 dest_item(std::move(dest_item)) {} |
| 47 |
| 48 BlobStorageRegistry::ItemCopyEntry::ItemCopyEntry(const ItemCopyEntry&) = |
| 49 default; |
| 50 |
| 51 BlobStorageRegistry::ItemCopyEntry::~ItemCopyEntry() {} |
| 52 |
| 53 BlobStorageRegistry::Entry::Entry() {} |
| 43 | 54 |
| 44 BlobStorageRegistry::Entry::~Entry() {} | 55 BlobStorageRegistry::Entry::~Entry() {} |
| 45 | 56 |
| 46 bool BlobStorageRegistry::Entry::TestAndSetState(BlobState expected, | |
| 47 BlobState set) { | |
| 48 if (state != expected) | |
| 49 return false; | |
| 50 state = set; | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 BlobStorageRegistry::BlobStorageRegistry() {} | 57 BlobStorageRegistry::BlobStorageRegistry() {} |
| 55 | 58 |
| 56 BlobStorageRegistry::~BlobStorageRegistry() { | 59 BlobStorageRegistry::~BlobStorageRegistry() { |
| 57 // Note: We don't bother calling the construction complete callbacks, as we | 60 // Note: We don't bother calling the construction complete callbacks, as we |
| 58 // are only being destructed at the end of the life of the browser process. | 61 // are only being destructed at the end of the life of the browser process. |
| 59 // So it shouldn't matter. | 62 // So it shouldn't matter. |
| 60 } | 63 } |
| 61 | 64 |
| 62 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( | 65 BlobStorageRegistry::Entry* BlobStorageRegistry::CreateEntry( |
| 63 const std::string& uuid, | 66 const std::string& uuid, |
| 64 const std::string& content_type, | 67 const std::string& content_type, |
| 65 const std::string& content_disposition) { | 68 const std::string& content_disposition) { |
| 66 DCHECK(!ContainsKey(blob_map_, uuid)); | 69 DCHECK(!ContainsKey(blob_map_, uuid)); |
| 67 std::unique_ptr<Entry> entry(new Entry(1, BlobState::PENDING)); | 70 std::unique_ptr<Entry> entry(new Entry()); |
| 68 entry->content_type = content_type; | 71 entry->content_type = content_type; |
| 69 entry->content_disposition = content_disposition; | 72 entry->content_disposition = content_disposition; |
| 70 Entry* entry_ptr = entry.get(); | 73 Entry* entry_ptr = entry.get(); |
| 71 blob_map_.add(uuid, std::move(entry)); | 74 blob_map_.add(uuid, std::move(entry)); |
| 72 return entry_ptr; | 75 return entry_ptr; |
| 73 } | 76 } |
| 74 | 77 |
| 75 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { | 78 bool BlobStorageRegistry::DeleteEntry(const std::string& uuid) { |
| 76 return blob_map_.erase(uuid) == 1; | 79 return blob_map_.erase(uuid) == 1; |
| 77 } | 80 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | 128 url_to_uuid_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); |
| 126 if (found == url_to_uuid_.end()) | 129 if (found == url_to_uuid_.end()) |
| 127 return nullptr; | 130 return nullptr; |
| 128 Entry* entry = GetEntry(found->second); | 131 Entry* entry = GetEntry(found->second); |
| 129 if (entry && uuid) | 132 if (entry && uuid) |
| 130 uuid->assign(found->second); | 133 uuid->assign(found->second); |
| 131 return entry; | 134 return entry; |
| 132 } | 135 } |
| 133 | 136 |
| 134 } // namespace storage | 137 } // namespace storage |
| OLD | NEW |