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