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