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_INTERNAL_BLOB_DATA_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_INTERNAL_BLOB_DATA_H_ |
6 #define STORAGE_BROWSER_BLOB_INTERNAL_BLOB_DATA_H_ | 6 #define STORAGE_BROWSER_BLOB_INTERNAL_BLOB_DATA_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
| 14 #include "base/callback_forward.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "storage/browser/blob/shareable_blob_data_item.h" | 17 #include "base/optional.h" |
| 18 #include "storage/browser/blob/blob_memory_controller.h" |
| 19 #include "storage/browser/storage_browser_export.h" |
17 | 20 |
18 namespace storage { | 21 namespace storage { |
| 22 class BlobDataHandle; |
| 23 class ShareableBlobDataItem; |
19 class ViewBlobInternalsJob; | 24 class ViewBlobInternalsJob; |
20 | 25 |
21 // This class represents a blob in the BlobStorageContext. It is constructed | 26 // This class represents a blob. We export this only for unit tests. |
22 // using the internal Builder class. | 27 class STORAGE_EXPORT InternalBlobData { |
23 class InternalBlobData { | |
24 public: | 28 public: |
| 29 using PopulatationAllowedCallback = |
| 30 base::Callback<void(BlobStatus, |
| 31 std::vector<BlobMemoryController::FileCreationInfo>)>; |
| 32 |
| 33 // This records a copy from a referenced blob. When we finish building our |
| 34 // blob we perform all of these copies. |
| 35 struct STORAGE_EXPORT ItemCopyEntry { |
| 36 ItemCopyEntry(scoped_refptr<ShareableBlobDataItem> source_item, |
| 37 size_t source_item_offset, |
| 38 scoped_refptr<ShareableBlobDataItem> dest_item); |
| 39 ~ItemCopyEntry(); |
| 40 ItemCopyEntry(const ItemCopyEntry&); |
| 41 |
| 42 scoped_refptr<ShareableBlobDataItem> source_item; |
| 43 size_t source_item_offset = 0; |
| 44 scoped_refptr<ShareableBlobDataItem> dest_item; |
| 45 }; |
| 46 |
| 47 // This keeps track of our building state for our blob. While building, four |
| 48 // things can be happening mostly simultaneously: |
| 49 // 1. Waiting for blobs we reference to complete, |
| 50 // 2. Waiting for quota to be reserved for memory needed, and |
| 51 // 3. Waiting for quota to be reserved (and files created) for files, |
| 52 // 4. Waiting for user population of data after quota. |
| 53 struct STORAGE_EXPORT BuildingState { |
| 54 // |user_data_population_callback| is !null when user data needs population. |
| 55 BuildingState(bool user_data_present, |
| 56 PopulatationAllowedCallback user_data_population_callback, |
| 57 bool dependent_building_blobs_present, |
| 58 size_t num_building_dependent_blobs, |
| 59 bool memory_quota_needed, |
| 60 bool file_quota_needed); |
| 61 ~BuildingState(); |
| 62 |
| 63 bool MemoryQuotaComplete() const { |
| 64 return !memory_quota_needed || memory_quota_granted; |
| 65 } |
| 66 |
| 67 bool FileQuotaComplete() const { |
| 68 return !file_quota_needed || file_quota_granted; |
| 69 } |
| 70 |
| 71 bool UserPopulationComplete() const { |
| 72 return !user_data_present || user_data_populated; |
| 73 } |
| 74 |
| 75 bool DependentBlobsComplete() const { |
| 76 return num_building_dependent_blobs == 0; |
| 77 } |
| 78 |
| 79 bool CanFinishBuilding() const { |
| 80 return MemoryQuotaComplete() && FileQuotaComplete() && |
| 81 DependentBlobsComplete() && UserPopulationComplete(); |
| 82 } |
| 83 |
| 84 // If there is file quota needed, then this is all we need for populating |
| 85 // user data. Otherwise we can populate when memory quota is granted. |
| 86 bool CanRequestUserPopulation() const { |
| 87 return !user_data_populated && |
| 88 ((!file_quota_needed && memory_quota_granted) || |
| 89 (file_quota_needed && file_quota_granted)); |
| 90 } |
| 91 |
| 92 const bool user_data_present; |
| 93 // We can have user data that's either populated or unpopulated. If we need |
| 94 // population, this is populated. |
| 95 PopulatationAllowedCallback user_data_population_callback; |
| 96 std::vector<ShareableBlobDataItem*> user_items; |
| 97 bool user_data_populated = false; |
| 98 |
| 99 const bool dependent_building_blobs_present; |
| 100 // Stores all blobs that we're depending on for building. This keeps the |
| 101 // blobs alive while we build our blob. |
| 102 std::vector<std::unique_ptr<BlobDataHandle>> dependent_blobs; |
| 103 size_t num_building_dependent_blobs; |
| 104 |
| 105 const bool memory_quota_needed; |
| 106 BlobMemoryController::PendingMemoryQuotaRequest memory_quota_request; |
| 107 bool memory_quota_granted = false; |
| 108 |
| 109 const bool file_quota_needed; |
| 110 BlobMemoryController::PendingFileQuotaRequest file_quota_request = |
| 111 BlobMemoryController::kInvalidFileQuotaRequest; |
| 112 bool file_quota_granted = false; |
| 113 |
| 114 // These are copies from a referenced blob item to our blob items. Some of |
| 115 // these entries may have changed from bytes to files if they were paged. |
| 116 std::vector<ItemCopyEntry> copies; |
| 117 |
| 118 // When our blob finishes building these callbacks are called. |
| 119 std::vector<BlobStatusCallback> build_completion_callbacks; |
| 120 }; |
| 121 |
| 122 InternalBlobData(const std::string& content_type, |
| 123 const std::string& content_disposition); |
25 ~InternalBlobData(); | 124 ~InternalBlobData(); |
26 | 125 |
27 protected: | 126 // Appends the given shared blob data item to this object. We add |my_uuid| |
28 friend class BlobStorageContext; | 127 // to the shareable item's uuid set. |
29 friend class BlobStorageRegistry; | 128 void AppendSharedBlobItem(const std::string& my_uuid, |
30 friend class ViewBlobInternalsJob; | 129 scoped_refptr<ShareableBlobDataItem> item); |
31 | 130 |
32 // Removes the given blob uuid from the internal ShareableBlobDataItems. | 131 // Removes the given blob uuid from the internal ShareableBlobDataItems. |
33 // This is called when this blob is being destroyed. | 132 // This is called when this blob is being destroyed. |
34 void RemoveBlobFromShareableItems(const std::string& blob_uuid); | 133 void RemoveBlobFromShareableItems(const std::string& blob_uuid); |
35 | 134 |
36 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items() const; | |
37 | |
38 // Gets the memory used by this blob that is not shared by other blobs. This | 135 // Gets the memory used by this blob that is not shared by other blobs. This |
39 // also doesn't count duplicate items. | 136 // also doesn't count duplicate items. |
40 size_t GetUnsharedMemoryUsage() const; | 137 size_t GetUnsharedMemoryUsage() const; |
41 | 138 |
42 // Gets the memory used by this blob. Total memory includes memory of items | 139 // Returns if we're a pending blob that can finish building. |
43 // possibly shared with other blobs, or items that appear multiple times in | 140 bool CanFinishBuilding() const { |
44 // this blob. Unshared memory is memory used by this blob that is not shared | 141 return status_ == BlobStatus::PENDING && |
45 // by other blobs. | 142 building_state_->CanFinishBuilding(); |
46 void GetMemoryUsage(size_t* total_memory, size_t* unshared_memory); | 143 } |
| 144 |
| 145 BlobStatus status() const { return status_; } |
| 146 |
| 147 size_t refcount() const { return refcount_; } |
| 148 |
| 149 const std::string& content_type() const { return content_type_; } |
| 150 |
| 151 const std::string& content_disposition() const { |
| 152 return content_disposition_; |
| 153 } |
| 154 |
| 155 // Total size of this blob in bytes. |
| 156 uint64_t total_size() const { return size_; }; |
| 157 |
| 158 // We record the cumulative offsets of each blob item (except for the first, |
| 159 // which would always be 0) to enable binary searching for an item at a |
| 160 // specific byte offset. |
| 161 const std::vector<uint64_t>& offsets() const { return offsets_; } |
| 162 |
| 163 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items() const; |
47 | 164 |
48 private: | 165 private: |
49 friend class Builder; | 166 friend class BlobStorageContext; |
50 InternalBlobData(); | |
51 | 167 |
| 168 BlobStatus status_ = BlobStatus::PENDING; |
| 169 size_t refcount_ = 0; |
| 170 |
| 171 // Metadata. |
52 std::string content_type_; | 172 std::string content_type_; |
53 std::string content_disposition_; | 173 std::string content_disposition_; |
| 174 |
54 std::vector<scoped_refptr<ShareableBlobDataItem>> items_; | 175 std::vector<scoped_refptr<ShareableBlobDataItem>> items_; |
55 | 176 |
56 class Builder { | 177 // Size in bytes. IFF we're a single file then this can be uint64_max. |
57 public: | 178 uint64_t size_ = 0; |
58 Builder(); | |
59 ~Builder(); | |
60 | 179 |
61 void AppendSharedBlobItem(scoped_refptr<ShareableBlobDataItem> item); | 180 // Only populated if len(items_) > 1. Used for binary search. |
| 181 // Since the offset of the first item is always 0, we exclude this. |
| 182 std::vector<uint64_t> offsets_; |
62 | 183 |
63 // Gets the memory used by this builder that is not shared with other blobs. | 184 // Only populated if our status is PENDING_*. |
64 size_t GetNonsharedMemoryUsage() const; | 185 std::unique_ptr<BuildingState> building_state_; |
65 | |
66 // Removes the given blob uuid from the internal ShareableBlobDataItems. | |
67 // This is called on destruction of the blob if we're still building it. | |
68 void RemoveBlobFromShareableItems(const std::string& blob_uuid); | |
69 | |
70 // The builder is invalid after calling this method. | |
71 std::unique_ptr<::storage::InternalBlobData> Build(); | |
72 | |
73 private: | |
74 std::unique_ptr<::storage::InternalBlobData> data_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(Builder); | |
77 }; | |
78 | 186 |
79 DISALLOW_COPY_AND_ASSIGN(InternalBlobData); | 187 DISALLOW_COPY_AND_ASSIGN(InternalBlobData); |
80 }; | 188 }; |
81 | 189 |
82 } // namespace storage | 190 } // namespace storage |
83 #endif // STORAGE_BROWSER_BLOB_INTERNAL_BLOB_DATA_H_ | 191 #endif // STORAGE_BROWSER_BLOB_INTERNAL_BLOB_DATA_H_ |
OLD | NEW |