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