Index: storage/browser/blob/internal_blob_data.h |
diff --git a/storage/browser/blob/internal_blob_data.h b/storage/browser/blob/internal_blob_data.h |
index 65db2f3cdc322fe85a8c01e275d2c988601b95e9..8633970995b3ab97a42fcc82ed2590cd51a5d00e 100644 |
--- a/storage/browser/blob/internal_blob_data.h |
+++ b/storage/browser/blob/internal_blob_data.h |
@@ -11,70 +11,150 @@ |
#include <string> |
#include <vector> |
+#include "base/callback_forward.h" |
#include "base/macros.h" |
#include "base/memory/ref_counted.h" |
-#include "storage/browser/blob/shareable_blob_data_item.h" |
+#include "base/optional.h" |
+#include "storage/browser/blob/blob_memory_controller.h" |
+#include "storage/browser/blob/blob_storage_context.h" |
+#include "storage/browser/storage_browser_export.h" |
namespace storage { |
+class BlobDataHandle; |
+class ShareableBlobDataItem; |
class ViewBlobInternalsJob; |
-// This class represents a blob in the BlobStorageContext. It is constructed |
-// using the internal Builder class. |
-class InternalBlobData { |
+// This class represents a blob. We export this only for unit tests. |
+class STORAGE_EXPORT InternalBlobData { |
public: |
+ // This records a copy from a referenced blob. When we finish building our |
+ // blob we perform all of these copies. |
+ struct STORAGE_EXPORT ItemCopyEntry { |
+ ItemCopyEntry(scoped_refptr<ShareableBlobDataItem> source_item, |
+ size_t source_item_offset, |
+ scoped_refptr<ShareableBlobDataItem> dest_item); |
+ ~ItemCopyEntry(); |
+ ItemCopyEntry(const ItemCopyEntry&); |
+ |
+ scoped_refptr<ShareableBlobDataItem> source_item; |
+ size_t source_item_offset = 0; |
+ scoped_refptr<ShareableBlobDataItem> dest_item; |
+ }; |
+ |
+ // This keeps track of our building state for our blob. While building, three |
+ // things can be happening at the same time: |
+ // 1. We are waiting for blobs we reference to complete, |
+ // 2. We are waiting for quota to be reserved for our copies from referenced |
+ // blobs, and |
+ // 3. We are waiting for quota to be reserved for the blob data (which can be |
+ // either memory quota or disk quota -- where we're creating files). |
+ struct STORAGE_EXPORT BuildingState { |
+ BuildingState(); |
+ ~BuildingState(); |
+ |
+ // Returns if all three cases above have been satisfied and we can finish |
+ // building our blob. |
+ bool CanFinishBuilding() const { |
+ return user_quota_granted && copy_quota_granted && |
+ dependent_blobs_building == 0 && !data_pending_from_user; |
+ } |
+ |
+ // Stored if the BlobDataBuilder we're given to construct has pending data, |
+ // and we need to tell the user when to start populating this data. |
+ BlobStorageContext::PopulatationAllowedCallback |
+ ready_for_user_population_callback; |
+ // Set when the user has finished populating pending data. |
+ bool data_pending_from_user = false; |
+ |
+ // Stores all blobs that we're depending on for building. This keeps the |
+ // blobs alive while we build our blob. |
+ std::vector<std::unique_ptr<BlobDataHandle>> dependent_blobs; |
+ size_t dependent_blobs_building = 0; |
+ |
+ // These fields keep track of requesting quota for our blob memory that the |
+ // user gives us. |
+ base::Optional<BlobMemoryController::PendingQuotaEntry> user_quota_entry; |
+ bool user_quota_granted = true; |
+ // We keep track of the items we're requesting quota for so we can mark them |
+ // as populated when the user finishes populating them. |
+ std::vector<ShareableBlobDataItem*> transporting_items; |
+ |
+ // These fields keep track of requesting quota for copies from other blobs. |
+ base::Optional<BlobMemoryController::PendingQuotaEntry> copy_quota_entry; |
+ bool copy_quota_granted = true; |
+ |
+ // These are copies from a referenced blob item to our blob items. Some of |
+ // these entries may have changed from bytes to files if they were paged. |
+ std::vector<ItemCopyEntry> copies; |
+ |
+ // When our blob finishes building these callbacks are called. |
+ std::vector<BlobStatusCallback> build_completion_callbacks; |
+ }; |
+ |
+ InternalBlobData(const std::string& content_type, |
+ const std::string& content_disposition); |
~InternalBlobData(); |
- protected: |
- friend class BlobStorageContext; |
- friend class BlobStorageRegistry; |
- friend class ViewBlobInternalsJob; |
+ // Appends the given shared blob data item to this object. We add |my_uuid| |
+ // to the shareable item's uuid set. |
+ void AppendSharedBlobItem(const std::string& my_uuid, |
+ scoped_refptr<ShareableBlobDataItem> item); |
// Removes the given blob uuid from the internal ShareableBlobDataItems. |
// This is called when this blob is being destroyed. |
void RemoveBlobFromShareableItems(const std::string& blob_uuid); |
- const std::vector<scoped_refptr<ShareableBlobDataItem>>& items() const; |
- |
// Gets the memory used by this blob that is not shared by other blobs. This |
// also doesn't count duplicate items. |
size_t GetUnsharedMemoryUsage() const; |
- // Gets the memory used by this blob. Total memory includes memory of items |
- // possibly shared with other blobs, or items that appear multiple times in |
- // this blob. Unshared memory is memory used by this blob that is not shared |
- // by other blobs. |
- void GetMemoryUsage(size_t* total_memory, size_t* unshared_memory); |
+ // Returns if we're a pending blob that can finish building. |
+ bool CanFinishBuilding() const { |
+ return status_ == BlobStatus::PENDING && |
+ building_state_->CanFinishBuilding(); |
+ } |
- private: |
- friend class Builder; |
- InternalBlobData(); |
+ BlobStatus status() const { return status_; } |
- std::string content_type_; |
- std::string content_disposition_; |
- std::vector<scoped_refptr<ShareableBlobDataItem>> items_; |
+ size_t refcount() const { return refcount_; } |
- class Builder { |
- public: |
- Builder(); |
- ~Builder(); |
+ const std::string& content_type() const { return content_type_; } |
- void AppendSharedBlobItem(scoped_refptr<ShareableBlobDataItem> item); |
+ const std::string& content_disposition() const { |
+ return content_disposition_; |
+ } |
- // Gets the memory used by this builder that is not shared with other blobs. |
- size_t GetNonsharedMemoryUsage() const; |
+ // Total size of this blob in bytes. |
+ uint64_t total_size() const { return size_; }; |
- // Removes the given blob uuid from the internal ShareableBlobDataItems. |
- // This is called on destruction of the blob if we're still building it. |
- void RemoveBlobFromShareableItems(const std::string& blob_uuid); |
+ // We record the cumulative offsets of each blob item (except for the first, |
+ // which would always be 0) to enable binary searching for an item at a |
+ // specific byte offset. |
+ const std::vector<uint64_t>& offsets() const { return offsets_; } |
- // The builder is invalid after calling this method. |
- std::unique_ptr<::storage::InternalBlobData> Build(); |
+ const std::vector<scoped_refptr<ShareableBlobDataItem>>& items() const; |
- private: |
- std::unique_ptr<::storage::InternalBlobData> data_; |
+ private: |
+ friend class BlobStorageContext; |
- DISALLOW_COPY_AND_ASSIGN(Builder); |
- }; |
+ BlobStatus status_ = BlobStatus::PENDING; |
+ size_t refcount_ = 0; |
+ |
+ // Metadata. |
+ std::string content_type_; |
+ std::string content_disposition_; |
+ |
+ std::vector<scoped_refptr<ShareableBlobDataItem>> items_; |
+ |
+ // Size in bytes. IFF we're a single file then this can be uint64_max. |
+ uint64_t size_ = 0; |
+ |
+ // Only populated if len(items_) > 1. Used for binary search. |
+ // Since the offset of the first item is always 0, we exclude this. |
+ std::vector<uint64_t> offsets_; |
+ |
+ // Only populated if our status is PENDING_*. |
+ std::unique_ptr<BuildingState> building_state_; |
DISALLOW_COPY_AND_ASSIGN(InternalBlobData); |
}; |