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