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 | |
30 // refcount to 1 on registration. | |
31 // * 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 |
32 // uuid. The user must keep track of these. | 32 // uuid. The user must keep track of these. |
33 class STORAGE_EXPORT BlobStorageRegistry { | 33 class STORAGE_EXPORT BlobStorageRegistry { |
34 public: | 34 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(); | 35 BlobStorageRegistry(); |
78 ~BlobStorageRegistry(); | 36 ~BlobStorageRegistry(); |
79 | 37 |
80 // Creates the blob entry with a refcount of 1 and a state of PENDING. If | 38 // 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. | 39 // the blob is already in use, we return null. |
82 Entry* CreateEntry(const std::string& uuid, | 40 BlobEntry* CreateEntry(const std::string& uuid, |
83 const std::string& content_type, | 41 const std::string& content_type, |
84 const std::string& content_disposition); | 42 const std::string& content_disposition); |
85 | 43 |
86 // Removes the blob entry with the given uuid. This does not unmap any | 44 // 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. | 45 // URLs that are pointing to this uuid. Returns if the entry existed. |
88 bool DeleteEntry(const std::string& uuid); | 46 bool DeleteEntry(const std::string& uuid); |
89 | 47 |
90 bool HasEntry(const std::string& uuid) const; | 48 bool HasEntry(const std::string& uuid) const; |
91 | 49 |
92 // Gets the blob entry for the given uuid. Returns nullptr if the entry | 50 // Gets the blob entry for the given uuid. Returns nullptr if the entry |
93 // does not exist. | 51 // does not exist. |
94 Entry* GetEntry(const std::string& uuid); | 52 BlobEntry* GetEntry(const std::string& uuid); |
95 const Entry* GetEntry(const std::string& uuid) const; | 53 const BlobEntry* GetEntry(const std::string& uuid) const; |
96 | 54 |
97 // Creates a url mapping from blob uuid to the given url. Returns false if | 55 // 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. | 56 // 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); | 57 bool CreateUrlMapping(const GURL& url, const std::string& uuid); |
100 | 58 |
101 // Removes the given URL mapping. Optionally populates a uuid string of the | 59 // Removes the given URL mapping. Optionally populates a uuid string of the |
102 // removed entry uuid. Returns false if the url isn't mapped. | 60 // removed entry uuid. Returns false if the url isn't mapped. |
103 bool DeleteURLMapping(const GURL& url, std::string* uuid); | 61 bool DeleteURLMapping(const GURL& url, std::string* uuid); |
104 | 62 |
105 // Returns if the url is mapped to a blob uuid. | 63 // Returns if the url is mapped to a blob uuid. |
106 bool IsURLMapped(const GURL& blob_url) const; | 64 bool IsURLMapped(const GURL& blob_url) const; |
107 | 65 |
108 // Returns the entry from the given url, and optionally populates the uuid for | 66 // 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. | 67 // that entry. Returns a nullptr if the mapping or entry doesn't exist. |
110 Entry* GetEntryFromURL(const GURL& url, std::string* uuid); | 68 BlobEntry* GetEntryFromURL(const GURL& url, std::string* uuid); |
111 | 69 |
112 size_t blob_count() const { return blob_map_.size(); } | 70 size_t blob_count() const { return blob_map_.size(); } |
113 size_t url_count() const { return url_to_uuid_.size(); } | 71 size_t url_count() const { return url_to_uuid_.size(); } |
114 | 72 |
115 private: | 73 private: |
116 friend class ViewBlobInternalsJob; | 74 friend class ViewBlobInternalsJob; |
117 using BlobMap = base::ScopedPtrHashMap<std::string, std::unique_ptr<Entry>>; | 75 using BlobMap = |
| 76 base::ScopedPtrHashMap<std::string, std::unique_ptr<BlobEntry>>; |
118 using URLMap = std::map<GURL, std::string>; | 77 using URLMap = std::map<GURL, std::string>; |
119 | 78 |
120 BlobMap blob_map_; | 79 BlobMap blob_map_; |
121 URLMap url_to_uuid_; | 80 URLMap url_to_uuid_; |
122 | 81 |
123 DISALLOW_COPY_AND_ASSIGN(BlobStorageRegistry); | 82 DISALLOW_COPY_AND_ASSIGN(BlobStorageRegistry); |
124 }; | 83 }; |
125 | 84 |
126 } // namespace storage | 85 } // namespace storage |
127 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ | 86 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_REGISTRY_H_ |
OLD | NEW |