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/blob/blob_storage_context.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 // This records a copy from a referenced blob. When we finish building our |
| 31 // blob we perform all of these copies. |
| 32 struct STORAGE_EXPORT ItemCopyEntry { |
| 33 ItemCopyEntry(scoped_refptr<ShareableBlobDataItem> source_item, |
| 34 size_t source_item_offset, |
| 35 scoped_refptr<ShareableBlobDataItem> dest_item); |
| 36 ~ItemCopyEntry(); |
| 37 ItemCopyEntry(const ItemCopyEntry&); |
| 38 |
| 39 scoped_refptr<ShareableBlobDataItem> source_item; |
| 40 size_t source_item_offset = 0; |
| 41 scoped_refptr<ShareableBlobDataItem> dest_item; |
| 42 }; |
| 43 |
| 44 // This keeps track of our building state for our blob. While building, three |
| 45 // things can be happening at the same time: |
| 46 // 1. We are waiting for blobs we reference to complete, |
| 47 // 2. We are waiting for quota to be reserved for our copies from referenced |
| 48 // blobs, and |
| 49 // 3. We are waiting for quota to be reserved for the blob data (which can be |
| 50 // either memory quota or disk quota -- where we're creating files). |
| 51 struct STORAGE_EXPORT BuildingState { |
| 52 BuildingState(); |
| 53 ~BuildingState(); |
| 54 |
| 55 // Returns if all three cases above have been satisfied and we can finish |
| 56 // building our blob. |
| 57 bool CanFinishBuilding() const { |
| 58 return user_quota_granted && copy_quota_granted && |
| 59 dependent_blobs_building == 0 && !data_pending_from_user; |
| 60 } |
| 61 |
| 62 // Stored if the BlobDataBuilder we're given to construct has pending data, |
| 63 // and we need to tell the user when to start populating this data. |
| 64 BlobStorageContext::PopulatationAllowedCallback |
| 65 ready_for_user_population_callback; |
| 66 // Set when the user has finished populating pending data. |
| 67 bool data_pending_from_user = false; |
| 68 |
| 69 // Stores all blobs that we're depending on for building. This keeps the |
| 70 // blobs alive while we build our blob. |
| 71 std::vector<std::unique_ptr<BlobDataHandle>> dependent_blobs; |
| 72 size_t dependent_blobs_building = 0; |
| 73 |
| 74 // These fields keep track of requesting quota for our blob memory that the |
| 75 // user gives us. |
| 76 base::Optional<BlobMemoryController::PendingQuotaEntry> user_quota_entry; |
| 77 bool user_quota_granted = true; |
| 78 // We keep track of the items we're requesting quota for so we can mark them |
| 79 // as populated when the user finishes populating them. |
| 80 std::vector<ShareableBlobDataItem*> transporting_items; |
| 81 |
| 82 // These fields keep track of requesting quota for copies from other blobs. |
| 83 base::Optional<BlobMemoryController::PendingQuotaEntry> copy_quota_entry; |
| 84 bool copy_quota_granted = true; |
| 85 |
| 86 // These are copies from a referenced blob item to our blob items. Some of |
| 87 // these entries may have changed from bytes to files if they were paged. |
| 88 std::vector<ItemCopyEntry> copies; |
| 89 |
| 90 // When our blob finishes building these callbacks are called. |
| 91 std::vector<BlobStatusCallback> build_completion_callbacks; |
| 92 }; |
| 93 |
| 94 InternalBlobData(const std::string& content_type, |
| 95 const std::string& content_disposition); |
25 ~InternalBlobData(); | 96 ~InternalBlobData(); |
26 | 97 |
27 protected: | 98 // Appends the given shared blob data item to this object. We add |my_uuid| |
28 friend class BlobStorageContext; | 99 // to the shareable item's uuid set. |
29 friend class BlobStorageRegistry; | 100 void AppendSharedBlobItem(const std::string& my_uuid, |
30 friend class ViewBlobInternalsJob; | 101 scoped_refptr<ShareableBlobDataItem> item); |
31 | 102 |
32 // Removes the given blob uuid from the internal ShareableBlobDataItems. | 103 // Removes the given blob uuid from the internal ShareableBlobDataItems. |
33 // This is called when this blob is being destroyed. | 104 // This is called when this blob is being destroyed. |
34 void RemoveBlobFromShareableItems(const std::string& blob_uuid); | 105 void RemoveBlobFromShareableItems(const std::string& blob_uuid); |
35 | 106 |
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 | 107 // Gets the memory used by this blob that is not shared by other blobs. This |
39 // also doesn't count duplicate items. | 108 // also doesn't count duplicate items. |
40 size_t GetUnsharedMemoryUsage() const; | 109 size_t GetUnsharedMemoryUsage() const; |
41 | 110 |
42 // Gets the memory used by this blob. Total memory includes memory of items | 111 // Returns if we're a pending blob that can finish building. |
43 // possibly shared with other blobs, or items that appear multiple times in | 112 bool CanFinishBuilding() const { |
44 // this blob. Unshared memory is memory used by this blob that is not shared | 113 return status_ == BlobStatus::PENDING && |
45 // by other blobs. | 114 building_state_->CanFinishBuilding(); |
46 void GetMemoryUsage(size_t* total_memory, size_t* unshared_memory); | 115 } |
| 116 |
| 117 BlobStatus status() const { return status_; } |
| 118 |
| 119 size_t refcount() const { return refcount_; } |
| 120 |
| 121 const std::string& content_type() const { return content_type_; } |
| 122 |
| 123 const std::string& content_disposition() const { |
| 124 return content_disposition_; |
| 125 } |
| 126 |
| 127 // Total size of this blob in bytes. |
| 128 uint64_t total_size() const { return size_; }; |
| 129 |
| 130 // We record the cumulative offsets of each blob item (except for the first, |
| 131 // which would always be 0) to enable binary searching for an item at a |
| 132 // specific byte offset. |
| 133 const std::vector<uint64_t>& offsets() const { return offsets_; } |
| 134 |
| 135 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items() const; |
47 | 136 |
48 private: | 137 private: |
49 friend class Builder; | 138 friend class BlobStorageContext; |
50 InternalBlobData(); | |
51 | 139 |
| 140 BlobStatus status_ = BlobStatus::PENDING; |
| 141 size_t refcount_ = 0; |
| 142 |
| 143 // Metadata. |
52 std::string content_type_; | 144 std::string content_type_; |
53 std::string content_disposition_; | 145 std::string content_disposition_; |
| 146 |
54 std::vector<scoped_refptr<ShareableBlobDataItem>> items_; | 147 std::vector<scoped_refptr<ShareableBlobDataItem>> items_; |
55 | 148 |
56 class Builder { | 149 // Size in bytes. IFF we're a single file then this can be uint64_max. |
57 public: | 150 uint64_t size_ = 0; |
58 Builder(); | |
59 ~Builder(); | |
60 | 151 |
61 void AppendSharedBlobItem(scoped_refptr<ShareableBlobDataItem> item); | 152 // Only populated if len(items_) > 1. Used for binary search. |
| 153 // Since the offset of the first item is always 0, we exclude this. |
| 154 std::vector<uint64_t> offsets_; |
62 | 155 |
63 // Gets the memory used by this builder that is not shared with other blobs. | 156 // Only populated if our status is PENDING_*. |
64 size_t GetNonsharedMemoryUsage() const; | 157 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 | 158 |
79 DISALLOW_COPY_AND_ASSIGN(InternalBlobData); | 159 DISALLOW_COPY_AND_ASSIGN(InternalBlobData); |
80 }; | 160 }; |
81 | 161 |
82 } // namespace storage | 162 } // namespace storage |
83 #endif // STORAGE_BROWSER_BLOB_INTERNAL_BLOB_DATA_H_ | 163 #endif // STORAGE_BROWSER_BLOB_INTERNAL_BLOB_DATA_H_ |
OLD | NEW |