Chromium Code Reviews| Index: content/child/blob_storage/blob_consolidation.h |
| diff --git a/content/child/blob_storage/blob_consolidation.h b/content/child/blob_storage/blob_consolidation.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7063958c750d99edf0dcf8d57bbfe27ead1c83b3 |
| --- /dev/null |
| +++ b/content/child/blob_storage/blob_consolidation.h |
| @@ -0,0 +1,117 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
| +#define CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "content/common/content_export.h" |
| +#include "storage/common/data_element.h" |
| +#include "third_party/WebKit/public/platform/WebBlobData.h" |
| +#include "third_party/WebKit/public/platform/WebString.h" |
| +#include "third_party/WebKit/public/platform/WebThreadSafeData.h" |
| +#include "third_party/WebKit/public/platform/WebURL.h" |
| + |
| +namespace content { |
| + |
| +// This class facilitates the consolidation of memory items in blobs. No memory |
| +// is copied to store items in this object. Instead, the memory is copied into |
| +// the external char* array given to the ReadMemory method. |
| +// Features: |
| +// * Add_Item methods for building the blob. |
| +// * consolidated_items for getting the consolidated items. This is |
| +// used to describe the blob to the browser. |
| +// * total_memory to get the total memory size of the blob. |
| +// * ReadMemory for reading arbitrary memory from any consolidated item. |
| +// * RecordMemoryRead for recording that an amount of memory has been read from |
| +// a given consolidated item. This allows this class to release memory |
| +// resources once an item has been read. |
| +// |
| +// TODO(dmurph): Keep memory accounting per item instead of globally so we can |
| +// free resources while reading instead of all at the end. |
| +class CONTENT_EXPORT BlobConsolidation { |
|
michaeln
2015/06/17 03:17:55
Seeing the heavy use of blink:WebTypes in here, i
dmurph
2015/06/17 18:29:50
This class is going to be used elsewhere in later
michaeln
2015/06/18 00:10:48
Got it, makes sense to leave it as a separate clas
dmurph
2015/06/22 18:17:46
Whoops, missed this comment.
As discussed in pers
|
| + public: |
| + enum class ReadStatus { |
| + ERROR = 0, |
| + ERROR_WRONG_TYPE, |
| + ERROR_OUT_OF_BOUNDS, |
| + BLOB_BYTES_PENDING, |
| + DONE |
| + }; |
| + struct ConsolidatedItem { |
| + ConsolidatedItem(); |
| + ConsolidatedItem(storage::DataElement::Type type, uint64_t offset, |
| + uint64_t length); |
| + ~ConsolidatedItem(); |
| + |
| + storage::DataElement::Type type; |
| + uint64_t offset; |
| + uint64_t length; |
| + |
| + blink::WebString path; // For TYPE_FILE. |
| + blink::WebURL filesystem_url; // For TYPE_FILE_FILESYSTEM. |
| + double expected_modification_time; // For TYPE_FILE, TYPE_FILE_FILESYSTEM. |
| + blink::WebString blob_uuid; // For TYPE_BLOB. |
| + // 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<size_t> offsets; // For TYPE_BYTES. |
| + std::vector<blink::WebThreadSafeData> data; // For TYPE_BYTES. |
| + }; |
| + |
| + BlobConsolidation(); |
| + ~BlobConsolidation(); |
| + |
| + void AddDataItem(const blink::WebThreadSafeData& data); |
| + void AddFileItem(const blink::WebString& path, uint64_t offset, |
| + uint64_t length, double expected_modification_time); |
| + void AddBlobItem(const blink::WebString& uuid, uint64_t offset, |
| + uint64_t length); |
| + void AddFileSystemItem(const blink::WebURL& url, uint64_t offset, |
| + uint64_t length, double expected_modification_time); |
| + |
| + // This gets the consolidated items constructed from the WebBlobData. This |
| + // is used to describe the blob to the browser. |
| + const std::vector<ConsolidatedItem>& consolidated_items() const { |
| + return consolidated_items_; |
| + } |
| + |
| + const size_t total_memory() const { return total_memory_; } |
| + |
| + // Reads memory from the given item into the given buffer. Returns: |
| + // * ReadStatus::ERROR if the state or arguments are invalid (see error log), |
| + // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, |
| + // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid, |
| + // * ReadStatus::DONE if the memory has been successfully read. |
| + ReadStatus ReadMemory(size_t consolidated_item_index, |
| + size_t consolidated_offset, size_t consolidated_size, |
| + char* memory_out); |
|
michaeln
2015/06/17 03:17:55
maybe void* memory_out to avoid casting at callsit
dmurph
2015/06/17 18:29:50
Done. This has the side effect of me needing to c
michaeln
2015/06/18 00:10:48
That's a fair trade, better to make life easier fo
|
| + |
| + // Records an amount of memory has been read from a given item and is no |
| + // longer needed. |
| + // * ReadStatus::ERROR if we have read more than the blob's total memory. |
| + // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, |
| + // * ReadStatus::ERROR_OUT_OF_BOUNDS if index or size are invalid, |
| + // * ReadStatus::BLOB_BYTES_PENDING if there are still bytes to be read from |
| + // this blob (not only the given item). |
| + // * ReadStatus::DONE if all bytes have been read from this blob. This object |
| + // can now be destroyed, as all bytes have been read. |
| + // TODO(dmurph): do per-item accounting, and error if we exceed per-item size. |
|
michaeln
2015/06/17 03:17:55
How do you anticipate using the accounting? I'm wo
dmurph
2015/06/17 18:29:50
There are two cases where the memory is read:
1. o
michaeln
2015/06/18 00:10:48
Incremental cleanup might be "nice", but I'd vote
|
| + ReadStatus RecordMemoryRead(size_t consolidated_item_index, |
| + size_t memory_size); |
| + |
| + private: |
| + size_t total_memory_; |
| + size_t total_memory_read_; |
| + std::vector<ConsolidatedItem> consolidated_items_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); |
| +}; |
| + |
| +} // namespace content |
| +#endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |