OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
| 6 #define CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "content/common/content_export.h" |
| 15 #include "storage/common/data_element.h" |
| 16 #include "third_party/WebKit/public/platform/WebBlobData.h" |
| 17 #include "third_party/WebKit/public/platform/WebString.h" |
| 18 #include "third_party/WebKit/public/platform/WebThreadSafeData.h" |
| 19 #include "third_party/WebKit/public/platform/WebURL.h" |
| 20 |
| 21 namespace content { |
| 22 |
| 23 // This class facilitates the consolidation of memory items in blobs. No memory |
| 24 // is copied to store items in this object. Instead, the memory is copied into |
| 25 // the external char* array given to the ReadMemory method. |
| 26 // Features: |
| 27 // * Add_Item methods for building the blob. |
| 28 // * consolidated_items for getting the consolidated items. This is |
| 29 // used to describe the blob to the browser. |
| 30 // * total_memory to get the total memory size of the blob. |
| 31 // * ReadMemory for reading arbitrary memory from any consolidated item. |
| 32 // * RecordMemoryRead for recording that an amount of memory has been read from |
| 33 // a given consolidated item. This allows this class to release memory |
| 34 // resources once an item has been read. |
| 35 // |
| 36 // TODO(dmurph): Keep memory accounting per item instead of globally so we can |
| 37 // free resources while reading instead of all at the end. |
| 38 class CONTENT_EXPORT BlobConsolidation { |
| 39 public: |
| 40 enum class ReadStatus { |
| 41 ERROR = 0, |
| 42 ERROR_WRONG_TYPE, |
| 43 ERROR_OUT_OF_BOUNDS, |
| 44 BLOB_BYTES_PENDING, |
| 45 DONE |
| 46 }; |
| 47 struct ConsolidatedItem { |
| 48 ConsolidatedItem(); |
| 49 ConsolidatedItem(storage::DataElement::Type type, uint64_t offset, |
| 50 uint64_t length); |
| 51 ~ConsolidatedItem(); |
| 52 |
| 53 storage::DataElement::Type type; |
| 54 uint64_t offset; |
| 55 uint64_t length; |
| 56 |
| 57 blink::WebString path; // For TYPE_FILE. |
| 58 blink::WebURL filesystem_url; // For TYPE_FILE_FILESYSTEM. |
| 59 double expected_modification_time; // For TYPE_FILE, TYPE_FILE_FILESYSTEM. |
| 60 blink::WebString blob_uuid; // For TYPE_BLOB. |
| 61 // Only populated if len(items) > 1. Used for binary search. |
| 62 // Since the offset of the first item is always 0, we exclude this. |
| 63 std::vector<size_t> offsets; // For TYPE_BYTES. |
| 64 std::vector<blink::WebThreadSafeData> data; // For TYPE_BYTES. |
| 65 }; |
| 66 |
| 67 BlobConsolidation(); |
| 68 ~BlobConsolidation(); |
| 69 |
| 70 void AddDataItem(const blink::WebThreadSafeData& data); |
| 71 void AddFileItem(const blink::WebString& path, uint64_t offset, |
| 72 uint64_t length, double expected_modification_time); |
| 73 void AddBlobItem(const blink::WebString& uuid, uint64_t offset, |
| 74 uint64_t length); |
| 75 void AddFileSystemItem(const blink::WebURL& url, uint64_t offset, |
| 76 uint64_t length, double expected_modification_time); |
| 77 |
| 78 // This gets the consolidated items constructed from the WebBlobData. This |
| 79 // is used to describe the blob to the browser. |
| 80 const std::vector<ConsolidatedItem>& consolidated_items() const { |
| 81 return consolidated_items_; |
| 82 } |
| 83 |
| 84 const size_t total_memory() const { return total_memory_; } |
| 85 |
| 86 // Reads memory from the given item into the given buffer. Returns: |
| 87 // * ReadStatus::ERROR if the state or arguments are invalid (see error log), |
| 88 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, |
| 89 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid, |
| 90 // * ReadStatus::DONE if the memory has been successfully read. |
| 91 ReadStatus ReadMemory(size_t consolidated_item_index, |
| 92 size_t consolidated_offset, size_t consolidated_size, |
| 93 void* memory_out); |
| 94 |
| 95 // Records an amount of memory has been read from a given item and is no |
| 96 // longer needed. |
| 97 // * ReadStatus::ERROR if we have read more than the blob's total memory. |
| 98 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, |
| 99 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index or size are invalid, |
| 100 // * ReadStatus::BLOB_BYTES_PENDING if there are still bytes to be read from |
| 101 // this blob (not only the given item). |
| 102 // * ReadStatus::DONE if all bytes have been read from this blob. This object |
| 103 // can now be destroyed, as all bytes have been read. |
| 104 // TODO(dmurph): do per-item accounting, and error if we exceed per-item size. |
| 105 ReadStatus RecordMemoryRead(size_t consolidated_item_index, |
| 106 size_t memory_size); |
| 107 |
| 108 private: |
| 109 size_t total_memory_; |
| 110 size_t total_memory_read_; |
| 111 std::vector<ConsolidatedItem> consolidated_items_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); |
| 114 }; |
| 115 |
| 116 } // namespace content |
| 117 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
OLD | NEW |