OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_CONTEXT_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
| 15 #include "base/callback_forward.h" |
| 16 #include "base/gtest_prod_util.h" |
15 #include "base/macros.h" | 17 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
17 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
18 #include "storage/browser/blob/blob_data_handle.h" | 20 #include "storage/browser/blob/blob_data_handle.h" |
19 #include "storage/browser/blob/blob_data_item.h" | 21 #include "storage/browser/blob/blob_storage_registry.h" |
20 #include "storage/browser/blob/blob_data_snapshot.h" | |
21 #include "storage/browser/blob/internal_blob_data.h" | 22 #include "storage/browser/blob/internal_blob_data.h" |
22 #include "storage/browser/blob/shareable_blob_data_item.h" | |
23 #include "storage/browser/storage_browser_export.h" | 23 #include "storage/browser/storage_browser_export.h" |
| 24 #include "storage/common/blob_storage/blob_storage_constants.h" |
24 #include "storage/common/data_element.h" | 25 #include "storage/common/data_element.h" |
25 | 26 |
26 class GURL; | 27 class GURL; |
27 | 28 |
28 namespace base { | 29 namespace base { |
29 class FilePath; | 30 class FilePath; |
30 class Time; | 31 class Time; |
31 } | 32 } |
32 | 33 |
33 namespace content { | 34 namespace content { |
34 class BlobStorageHost; | 35 class BlobDispatcherHost; |
| 36 class BlobDispatcherHostTest; |
35 } | 37 } |
36 | 38 |
37 namespace storage { | 39 namespace storage { |
38 | 40 |
39 class BlobDataBuilder; | 41 class BlobDataBuilder; |
40 class InternalBlobData; | 42 class BlobDataItem; |
| 43 class BlobDataSnapshot; |
| 44 class ShareableBlobDataItem; |
41 | 45 |
42 // This class handles the logistics of blob Storage within the browser process, | 46 // This class handles the logistics of blob Storage within the browser process, |
43 // and maintains a mapping from blob uuid to the data. The class is single | 47 // and maintains a mapping from blob uuid to the data. The class is single |
44 // threaded and should only be used on the IO thread. | 48 // threaded and should only be used on the IO thread. |
45 // In chromium, there is one instance per profile. | 49 // In chromium, there is one instance per profile. |
46 class STORAGE_EXPORT BlobStorageContext | 50 class STORAGE_EXPORT BlobStorageContext |
47 : public base::SupportsWeakPtr<BlobStorageContext> { | 51 : public base::SupportsWeakPtr<BlobStorageContext> { |
48 public: | 52 public: |
49 BlobStorageContext(); | 53 BlobStorageContext(); |
50 ~BlobStorageContext(); | 54 ~BlobStorageContext(); |
(...skipping 11 matching lines...) Expand all Loading... |
62 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& builder); | 66 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& builder); |
63 | 67 |
64 // Deprecated, use const ref version above. | 68 // Deprecated, use const ref version above. |
65 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder* builder); | 69 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder* builder); |
66 | 70 |
67 // Useful for coining blob urls from within the browser process. | 71 // Useful for coining blob urls from within the browser process. |
68 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); | 72 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
69 void RevokePublicBlobURL(const GURL& url); | 73 void RevokePublicBlobURL(const GURL& url); |
70 | 74 |
71 size_t memory_usage() const { return memory_usage_; } | 75 size_t memory_usage() const { return memory_usage_; } |
72 size_t blob_count() const { return blob_map_.size(); } | 76 size_t blob_count() const { return registry_.blob_count(); } |
| 77 size_t memory_available() const { |
| 78 return kBlobStorageMaxMemoryUsage - memory_usage_; |
| 79 } |
| 80 |
| 81 const BlobStorageRegistry& registry() { return registry_; } |
73 | 82 |
74 private: | 83 private: |
75 friend class content::BlobStorageHost; | 84 using BlobRegistryEntry = BlobStorageRegistry::Entry; |
| 85 friend class content::BlobDispatcherHost; |
| 86 friend class BlobAsyncBuilderHost; |
| 87 friend class BlobAsyncBuilderHostTest; |
| 88 friend class BlobDataHandle; |
76 friend class BlobDataHandle::BlobDataHandleShared; | 89 friend class BlobDataHandle::BlobDataHandleShared; |
| 90 friend class BlobReaderTest; |
| 91 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel); |
| 92 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, ReadFromIncompleteBlob); |
| 93 friend class BlobStorageContextTest; |
| 94 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, IncrementDecrementRef); |
| 95 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, OnCancelBuildingBlob); |
| 96 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, PublicBlobUrls); |
| 97 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, |
| 98 TestUnknownBrokenAndBuildingBlobReference); |
77 friend class ViewBlobInternalsJob; | 99 friend class ViewBlobInternalsJob; |
78 | 100 |
79 enum EntryFlags { | 101 // CompletePendingBlob or CancelPendingBlob should be called after this. |
80 EXCEEDED_MEMORY = 1 << 1, | 102 void CreatePendingBlob(const std::string& uuid, |
81 }; | 103 const std::string& content_type, |
| 104 const std::string& content_disposition); |
82 | 105 |
83 struct BlobMapEntry { | 106 // This includes resolving blob references in the builder. This will run the |
84 int refcount; | 107 // callbacks given in RunOnConstructionComplete. |
85 int flags; | 108 void CompletePendingBlob(const BlobDataBuilder& external_builder); |
86 // data and data_builder are mutually exclusive. | |
87 scoped_ptr<InternalBlobData> data; | |
88 scoped_ptr<InternalBlobData::Builder> data_builder; | |
89 | 109 |
90 BlobMapEntry(); | 110 // This will run the callbacks given in RunOnConstructionComplete. |
91 BlobMapEntry(int refcount, InternalBlobData::Builder* data); | 111 void CancelPendingBlob(const std::string& uuid, |
92 ~BlobMapEntry(); | 112 IPCBlobCreationCancelCode reason); |
93 | 113 |
94 bool IsBeingBuilt(); | |
95 }; | |
96 | |
97 typedef std::map<std::string, BlobMapEntry*> BlobMap; | |
98 typedef std::map<GURL, std::string> BlobURLMap; | |
99 | |
100 // Called by BlobDataHandle. | |
101 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); | |
102 | |
103 // ### Methods called by BlobStorageHost ### | |
104 void StartBuildingBlob(const std::string& uuid); | |
105 void AppendBlobDataItem(const std::string& uuid, | |
106 const DataElement& data_item); | |
107 void FinishBuildingBlob(const std::string& uuid, const std::string& type); | |
108 void CancelBuildingBlob(const std::string& uuid); | |
109 void IncrementBlobRefCount(const std::string& uuid); | 114 void IncrementBlobRefCount(const std::string& uuid); |
110 void DecrementBlobRefCount(const std::string& uuid); | 115 void DecrementBlobRefCount(const std::string& uuid); |
111 // ######################################### | |
112 | 116 |
113 // Flags the entry for exceeding memory, and resets the builder. | 117 // Methods called by BlobDataHandle: |
114 void BlobEntryExceededMemory(BlobMapEntry* entry); | 118 // This will return an empty snapshot until the blob is complete. |
115 | 119 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then |
116 // Allocates memory to hold the given data element and copies the data over. | 120 // make this DCHECK on the blob not being complete. |
117 scoped_refptr<BlobDataItem> AllocateBlobItem(const std::string& uuid, | 121 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); |
118 const DataElement& data_item); | 122 bool IsBroken(const std::string& uuid) const; |
| 123 bool IsBeingBuilt(const std::string& uuid) const; |
| 124 // Runs |done| when construction completes, with true if it was successful. |
| 125 void RunOnConstructionComplete(const std::string& uuid, |
| 126 const base::Callback<void(bool)>& done); |
119 | 127 |
120 // Appends the given blob item to the blob builder. The new blob | 128 // Appends the given blob item to the blob builder. The new blob |
121 // retains ownership of data_item if applicable, and returns false if there | 129 // retains ownership of data_item if applicable, and returns false if there |
122 // wasn't enough memory to hold the item. | 130 // was an error and pouplates the error_code. We can either have an error of: |
| 131 // OUT_OF_MEMORY: We are out of memory to store this blob. |
| 132 // REFERENCED_BLOB_BROKEN: One of the referenced blobs is broken. |
123 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, | 133 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, |
124 scoped_refptr<BlobDataItem> data_item, | 134 scoped_refptr<BlobDataItem> data_item, |
125 InternalBlobData::Builder* target_blob_data); | 135 InternalBlobData::Builder* target_blob_data, |
| 136 IPCBlobCreationCancelCode* error_code); |
| 137 |
| 138 // Allocates a shareable blob data item, with blob references resolved. If |
| 139 // there isn't enough memory, then a nullptr is returned. |
| 140 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( |
| 141 const std::string& target_blob_uuid, |
| 142 scoped_refptr<BlobDataItem> data_item); |
126 | 143 |
127 // Deconstructs the blob and appends it's contents to the target blob. Items | 144 // Deconstructs the blob and appends it's contents to the target blob. Items |
128 // are shared if possible, and copied if the given offset and length | 145 // are shared if possible, and copied if the given offset and length |
129 // have to split an item. | 146 // have to split an item. |
130 bool AppendBlob(const std::string& target_blob_uuid, | 147 bool AppendBlob(const std::string& target_blob_uuid, |
131 const InternalBlobData& blob, | 148 const InternalBlobData& blob, |
132 uint64_t offset, | 149 uint64_t offset, |
133 uint64_t length, | 150 uint64_t length, |
134 InternalBlobData::Builder* target_blob_data); | 151 InternalBlobData::Builder* target_blob_data); |
135 | 152 |
136 bool IsInUse(const std::string& uuid); | 153 BlobStorageRegistry registry_; |
137 bool IsBeingBuilt(const std::string& uuid); | |
138 bool IsUrlRegistered(const GURL& blob_url); | |
139 | |
140 BlobMap blob_map_; | |
141 BlobURLMap public_blob_urls_; | |
142 | 154 |
143 // Used to keep track of how much memory is being utilized for blob data, | 155 // Used to keep track of how much memory is being utilized for blob data, |
144 // we count only the items of TYPE_DATA which are held in memory and not | 156 // we count only the items of TYPE_DATA which are held in memory and not |
145 // items of TYPE_FILE. | 157 // items of TYPE_FILE. |
146 size_t memory_usage_; | 158 size_t memory_usage_; |
147 | 159 |
148 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); | 160 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
149 }; | 161 }; |
150 | 162 |
151 } // namespace storage | 163 } // namespace storage |
152 | 164 |
153 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 165 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
OLD | NEW |