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 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/callback_forward.h" |
| 13 #include "base/containers/scoped_ptr_hash_map.h" |
| 14 #include "base/macros.h" |
| 15 #include "storage/browser/blob/internal_blob_data.h" |
| 16 #include "storage/browser/storage_browser_export.h" |
| 17 |
| 18 class GURL; |
| 19 |
| 20 namespace storage { |
| 21 |
| 22 // This class stores the blob data in the various states of construction, as |
| 23 // well as URL mappings to blob uuids. |
| 24 // Implementation notes: |
| 25 // * There is no implicit refcounting in this class, except for setting the |
| 26 // refcount to 1 on registration. |
| 27 // * When removing a uuid registration, we do not check for URL mappings to that |
| 28 // uuid. The user must keep track of these. |
| 29 class STORAGE_EXPORT BlobStorageRegistry { |
| 30 public: |
| 31 enum class BlobState { |
| 32 // First the renderer reserves the uuid. |
| 33 RESERVED = 1, |
| 34 // Second, we are asynchronously transporting data to the browser. |
| 35 ASYNC_TRANSPORTATION, |
| 36 // Third, we construct the blob when we have all of the data. |
| 37 CONSTRUCTION, |
| 38 // Finally, the blob is built. |
| 39 ACTIVE |
| 40 }; |
| 41 |
| 42 struct STORAGE_EXPORT Entry { |
| 43 size_t refcount; |
| 44 BlobState state; |
| 45 std::vector<base::Callback<void(bool)>> construction_complete_callbacks; |
| 46 |
| 47 // Flags |
| 48 bool exceeded_memory; |
| 49 |
| 50 // data and data_builder are mutually exclusive. |
| 51 scoped_ptr<InternalBlobData> data; |
| 52 scoped_ptr<InternalBlobData::Builder> data_builder; |
| 53 |
| 54 Entry() = delete; |
| 55 Entry(int refcount, BlobState state); |
| 56 ~Entry(); |
| 57 |
| 58 // Performs a test-and-set on the state of the given blob. If the state |
| 59 // isn't as expected, we return false. Otherwise we set the new state and |
| 60 // return true. |
| 61 bool TestAndSetState(BlobState expected, BlobState set); |
| 62 }; |
| 63 |
| 64 BlobStorageRegistry(); |
| 65 ~BlobStorageRegistry(); |
| 66 |
| 67 // Creates the blob entry with a refcount of 1 and a state of RESERVED. If |
| 68 // the blob is already in use, we return null. |
| 69 Entry* CreateEntry(const std::string& uuid); |
| 70 |
| 71 // Removes the blob entry with the given uuid. This does not unmap any |
| 72 // URLs that are pointing to this uuid. Returns if the entry existed. |
| 73 bool DeleteEntry(const std::string& uuid); |
| 74 |
| 75 // Gets the blob entry for the given uuid. Returns nullptr if the entry |
| 76 // does not exist. |
| 77 Entry* GetEntry(const std::string& uuid); |
| 78 |
| 79 // Creates a url mapping from blob uuid to the given url. Returns false if |
| 80 // the uuid isn't mapped to an entry or if there already is a map for the URL. |
| 81 bool CreateUrlMapping(const GURL& url, const std::string& uuid); |
| 82 |
| 83 // Removes the given URL mapping. Optionally populates a uuid string of the |
| 84 // removed entry uuid. Returns false if the url isn't mapped. |
| 85 bool DeleteURLMapping(const GURL& url, std::string* uuid); |
| 86 |
| 87 // Returns if the url is mapped to a blob uuid. |
| 88 bool IsURLMapped(const GURL& blob_url) const; |
| 89 |
| 90 // Returns the entry from the given url, and optionally populates the uuid for |
| 91 // that entry. Returns a nullptr if the mapping or entry doesn't exist. |
| 92 Entry* GetEntryFromURL(const GURL& url, std::string* uuid); |
| 93 |
| 94 size_t blob_count() const { return blob_map_.size(); } |
| 95 size_t url_count() const { return url_to_uuid_.size(); } |
| 96 |
| 97 private: |
| 98 using BlobMap = base::ScopedPtrHashMap<std::string, scoped_ptr<Entry>>; |
| 99 using URLMap = std::map<GURL, std::string>; |
| 100 |
| 101 BlobMap blob_map_; |
| 102 URLMap url_to_uuid_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(BlobStorageRegistry); |
| 105 }; |
| 106 |
| 107 } // namespace storage |
| 108 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ |
OLD | NEW |