Chromium Code Reviews| 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 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ | 
| 6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ | 
| 7 | 7 | 
| 8 #include <stddef.h> | 8 #include <stddef.h> | 
| 9 | 9 | 
| 10 #include <map> | 10 #include <map> | 
| 11 #include <memory> | 11 #include <memory> | 
| 12 #include <string> | 12 #include <string> | 
| 13 #include <vector> | 13 #include <vector> | 
| 14 | 14 | 
| 15 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" | 
| 16 #include "base/containers/scoped_ptr_hash_map.h" | 16 #include "base/containers/scoped_ptr_hash_map.h" | 
| 17 #include "base/macros.h" | 17 #include "base/macros.h" | 
| 18 #include "storage/browser/blob/internal_blob_data.h" | |
| 19 #include "storage/browser/storage_browser_export.h" | 18 #include "storage/browser/storage_browser_export.h" | 
| 20 #include "storage/common/blob_storage/blob_storage_constants.h" | 19 #include "storage/common/blob_storage/blob_storage_constants.h" | 
| 21 | 20 | 
| 22 class GURL; | 21 class GURL; | 
| 23 | 22 | 
| 24 namespace storage { | 23 namespace storage { | 
| 24 class BlobDataHandle; | |
| 25 class BlobEntry; | |
| 26 class ShareableBlobDataItem; | |
| 25 | 27 | 
| 26 // This class stores the blob data in the various states of construction, as | 28 // This class stores the blob data in the various states of construction, as | 
| 27 // well as URL mappings to blob uuids. | 29 // well as URL mappings to blob uuids. | 
| 28 // Implementation notes: | 30 // Implementation notes: | 
| 29 // * There is no implicit refcounting in this class, except for setting the | 31 // * There is no implicit refcounting in this class, except for setting the | 
| 30 // refcount to 1 on registration. | 32 // refcount to 1 on registration. | 
| 
 
kinuko
2016/11/10 05:16:37
Are we still doing this?  Looks like stale comment
 
dmurph
2016/11/10 19:53:20
Done.
 
 | |
| 31 // * When removing a uuid registration, we do not check for URL mappings to that | 33 // * When removing a uuid registration, we do not check for URL mappings to that | 
| 32 // uuid. The user must keep track of these. | 34 // uuid. The user must keep track of these. | 
| 33 class STORAGE_EXPORT BlobStorageRegistry { | 35 class STORAGE_EXPORT BlobStorageRegistry { | 
| 34 public: | 36 public: | 
| 35 // True means the blob was constructed successfully, and false means that | |
| 36 // there was an error, which is reported in the second argument. | |
| 37 using BlobConstructedCallback = | |
| 38 base::Callback<void(bool, IPCBlobCreationCancelCode)>; | |
| 39 | |
| 40 enum class BlobState { | |
| 41 // The blob is pending transportation from the renderer. This is the default | |
| 42 // state on entry construction. | |
| 43 PENDING, | |
| 44 // The blob is complete and can be read from. | |
| 45 COMPLETE, | |
| 46 // The blob is broken and no longer holds data. This happens when there was | |
| 47 // a problem constructing the blob, or we've run out of memory. | |
| 48 BROKEN | |
| 49 }; | |
| 50 | |
| 51 struct STORAGE_EXPORT Entry { | |
| 52 size_t refcount; | |
| 53 BlobState state; | |
| 54 std::vector<BlobConstructedCallback> build_completion_callbacks; | |
| 55 | |
| 56 // Only applicable if the state == BROKEN. | |
| 57 IPCBlobCreationCancelCode broken_reason = | |
| 58 IPCBlobCreationCancelCode::UNKNOWN; | |
| 59 | |
| 60 // data and data_builder are mutually exclusive. | |
| 61 std::unique_ptr<InternalBlobData> data; | |
| 62 std::unique_ptr<InternalBlobData::Builder> data_builder; | |
| 63 | |
| 64 std::string content_type; | |
| 65 std::string content_disposition; | |
| 66 | |
| 67 Entry() = delete; | |
| 68 Entry(int refcount, BlobState state); | |
| 69 ~Entry(); | |
| 70 | |
| 71 // Performs a test-and-set on the state of the given blob. If the state | |
| 72 // isn't as expected, we return false. Otherwise we set the new state and | |
| 73 // return true. | |
| 74 bool TestAndSetState(BlobState expected, BlobState set); | |
| 75 }; | |
| 76 | |
| 77 BlobStorageRegistry(); | 37 BlobStorageRegistry(); | 
| 78 ~BlobStorageRegistry(); | 38 ~BlobStorageRegistry(); | 
| 79 | 39 | 
| 80 // Creates the blob entry with a refcount of 1 and a state of PENDING. If | 40 // Creates the blob entry with a refcount of 1 and a state of PENDING. If | 
| 81 // the blob is already in use, we return null. | 41 // the blob is already in use, we return null. | 
| 82 Entry* CreateEntry(const std::string& uuid, | 42 BlobEntry* CreateEntry(const std::string& uuid, | 
| 83 const std::string& content_type, | 43 const std::string& content_type, | 
| 84 const std::string& content_disposition); | 44 const std::string& content_disposition); | 
| 85 | 45 | 
| 86 // Removes the blob entry with the given uuid. This does not unmap any | 46 // Removes the blob entry with the given uuid. This does not unmap any | 
| 87 // URLs that are pointing to this uuid. Returns if the entry existed. | 47 // URLs that are pointing to this uuid. Returns if the entry existed. | 
| 88 bool DeleteEntry(const std::string& uuid); | 48 bool DeleteEntry(const std::string& uuid); | 
| 89 | 49 | 
| 90 bool HasEntry(const std::string& uuid) const; | 50 bool HasEntry(const std::string& uuid) const; | 
| 91 | 51 | 
| 92 // Gets the blob entry for the given uuid. Returns nullptr if the entry | 52 // Gets the blob entry for the given uuid. Returns nullptr if the entry | 
| 93 // does not exist. | 53 // does not exist. | 
| 94 Entry* GetEntry(const std::string& uuid); | 54 BlobEntry* GetEntry(const std::string& uuid); | 
| 95 const Entry* GetEntry(const std::string& uuid) const; | 55 const BlobEntry* GetEntry(const std::string& uuid) const; | 
| 96 | 56 | 
| 97 // Creates a url mapping from blob uuid to the given url. Returns false if | 57 // Creates a url mapping from blob uuid to the given url. Returns false if | 
| 98 // the uuid isn't mapped to an entry or if there already is a map for the URL. | 58 // the uuid isn't mapped to an entry or if there already is a map for the URL. | 
| 99 bool CreateUrlMapping(const GURL& url, const std::string& uuid); | 59 bool CreateUrlMapping(const GURL& url, const std::string& uuid); | 
| 100 | 60 | 
| 101 // Removes the given URL mapping. Optionally populates a uuid string of the | 61 // Removes the given URL mapping. Optionally populates a uuid string of the | 
| 102 // removed entry uuid. Returns false if the url isn't mapped. | 62 // removed entry uuid. Returns false if the url isn't mapped. | 
| 103 bool DeleteURLMapping(const GURL& url, std::string* uuid); | 63 bool DeleteURLMapping(const GURL& url, std::string* uuid); | 
| 104 | 64 | 
| 105 // Returns if the url is mapped to a blob uuid. | 65 // Returns if the url is mapped to a blob uuid. | 
| 106 bool IsURLMapped(const GURL& blob_url) const; | 66 bool IsURLMapped(const GURL& blob_url) const; | 
| 107 | 67 | 
| 108 // Returns the entry from the given url, and optionally populates the uuid for | 68 // Returns the entry from the given url, and optionally populates the uuid for | 
| 109 // that entry. Returns a nullptr if the mapping or entry doesn't exist. | 69 // that entry. Returns a nullptr if the mapping or entry doesn't exist. | 
| 110 Entry* GetEntryFromURL(const GURL& url, std::string* uuid); | 70 BlobEntry* GetEntryFromURL(const GURL& url, std::string* uuid); | 
| 111 | 71 | 
| 112 size_t blob_count() const { return blob_map_.size(); } | 72 size_t blob_count() const { return blob_map_.size(); } | 
| 113 size_t url_count() const { return url_to_uuid_.size(); } | 73 size_t url_count() const { return url_to_uuid_.size(); } | 
| 114 | 74 | 
| 115 private: | 75 private: | 
| 116 friend class ViewBlobInternalsJob; | 76 friend class ViewBlobInternalsJob; | 
| 117 using BlobMap = base::ScopedPtrHashMap<std::string, std::unique_ptr<Entry>>; | 77 using BlobMap = | 
| 78 base::ScopedPtrHashMap<std::string, std::unique_ptr<BlobEntry>>; | |
| 118 using URLMap = std::map<GURL, std::string>; | 79 using URLMap = std::map<GURL, std::string>; | 
| 119 | 80 | 
| 120 BlobMap blob_map_; | 81 BlobMap blob_map_; | 
| 121 URLMap url_to_uuid_; | 82 URLMap url_to_uuid_; | 
| 122 | 83 | 
| 123 DISALLOW_COPY_AND_ASSIGN(BlobStorageRegistry); | 84 DISALLOW_COPY_AND_ASSIGN(BlobStorageRegistry); | 
| 124 }; | 85 }; | 
| 125 | 86 | 
| 126 } // namespace storage | 87 } // namespace storage | 
| 127 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ | 88 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ | 
| OLD | NEW |