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 STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "storage/browser/blob/blob_data_builder.h" |
| 14 #include "storage/browser/storage_browser_export.h" |
| 15 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
| 16 #include "storage/common/data_element.h" |
| 17 |
| 18 namespace storage { |
| 19 |
| 20 // This class determines and stores the strategy for asynchronously transporting |
| 21 // memory from the renderer to the browser. This does not involve using the |
| 22 // 'shortcut' method, where the data is already present, and will always give |
| 23 // the caller the full strategy for requesting all data from the renderer. The |
| 24 // returned builder will always have TYPE_BYTES_DESCRIPTION items for the future |
| 25 // memory locations. |
| 26 // This involves two transformations: |
| 27 // 1. Transforming the data storage to be efficient in-transport. This can |
| 28 // mean that data is consolidated from cross-blob or cross-file boundaries |
| 29 // in the blob in a common block of shared memory or a file. |
| 30 // 2. Transforming the transport data into the browser storage representation. |
| 31 // This can simply mirror the renderer representation, or it can be instead |
| 32 // pointing to transport representation with offsets and sizes. |
| 33 // |
| 34 // The following information is generated: |
| 35 // 1. The total bytes size of memory items. |
| 36 // 2. The requests for memory, segmented as described above, along with their |
| 37 // destination browser indexes and offsets |
| 38 // 3. The sizes of the shared memory and file handles being used (by handle |
| 39 // index) in the async operation. |
| 40 // 4. A BlobDataBuilder which can be used to construct the Blob in the |
| 41 // BlobStorageContext object, after the TYPE_BYTES_DESCRIPTION entries are |
| 42 // turned into TYPE_BYTES entries. |
| 43 // The initial implementation of this is simple, but the protocol allows any |
| 44 // sort of flexibility, as everything has an offset and size specified. |
| 45 class STORAGE_EXPORT BlobAsyncTransportStrategy { |
| 46 public: |
| 47 enum Mode { MODE_NO_DISK, MODE_DISK }; |
| 48 |
| 49 enum Error { |
| 50 ERROR_NONE = 0, |
| 51 ERROR_TOO_LARGE, // This item can't fit in disk or memory |
| 52 ERROR_INVALID_PARAMS |
| 53 }; |
| 54 |
| 55 struct MemoryItemRequest { |
| 56 MemoryItemRequest(); |
| 57 size_t browser_item_index; |
| 58 // Note: For files this offset should always be 0, as the file offset in |
| 59 // segmentation is handled by the handle_offset in the message. This |
| 60 // offset is used for populating a chunk when the data comes back to |
| 61 // the browser. |
| 62 size_t browser_item_offset; |
| 63 BlobItemBytesRequest message; |
| 64 bool received; |
| 65 }; |
| 66 |
| 67 template <typename SizeType> |
| 68 class BlobSegmentVisitor { |
| 69 public: |
| 70 virtual ~BlobSegmentVisitor(){}; |
| 71 virtual void VisitSegment(const DataElement& element, |
| 72 size_t element_index, |
| 73 SizeType element_offset, |
| 74 size_t segment_index, |
| 75 SizeType segment_offset, |
| 76 SizeType size) = 0; |
| 77 virtual void Done() = 0; |
| 78 }; |
| 79 |
| 80 BlobAsyncTransportStrategy(); |
| 81 virtual ~BlobAsyncTransportStrategy(); |
| 82 |
| 83 void Initialize(Mode mode, |
| 84 size_t max_ipc_memory_size, |
| 85 size_t max_shared_memory_size, |
| 86 uint64_t max_file_size, |
| 87 size_t max_blob_in_memory_size, |
| 88 uint64_t disk_space_left, |
| 89 size_t memory_left, |
| 90 const std::string& uuid, |
| 91 const std::vector<DataElement>& blob_item_infos); |
| 92 |
| 93 std::vector<uint64_t>& file_handles() { return file_handles_; } |
| 94 |
| 95 // The sizes will always be in a descending order (so if you create a block |
| 96 // with the first size, it will always be greater than the later sizes) |
| 97 std::vector<size_t>& shared_memory_handles() { |
| 98 return shared_memory_handles_; |
| 99 } |
| 100 |
| 101 const std::vector<MemoryItemRequest>& requests() const { return requests_; } |
| 102 |
| 103 BlobDataBuilder* blob_builder() { return builder_.get(); } |
| 104 |
| 105 uint64_t total_bytes_size() const { return total_bytes_size_; } |
| 106 |
| 107 Error error() const { return error_; } |
| 108 |
| 109 static bool ShouldBeShortcut(const std::vector<DataElement>& items, |
| 110 size_t memory_available); |
| 111 |
| 112 // This iterates of the data elements and segments the 'bytes' data into |
| 113 // the smallest number of segments given the max_segment_size. |
| 114 // The callback describes either: |
| 115 // * A non-memory item |
| 116 // * A partition of a bytes element which will be populated into a given |
| 117 // segment and segment offset. |
| 118 // Assumptions: All memory items are consolidated. As in, there are no two |
| 119 // 'bytes' items next to eachother. |
| 120 template <typename SizeType> |
| 121 static void ForEachWithSegment(const std::vector<DataElement>& items, |
| 122 SizeType max_segment_size, |
| 123 BlobSegmentVisitor<SizeType>* visitor); |
| 124 |
| 125 private: |
| 126 // This simply fills the segment_sizes vector with the sizes of segments that |
| 127 // would be used to partition a block of total_memory_size. |
| 128 template <typename SizeType> |
| 129 static void CreateHandleSizes(SizeType total_memory_size, |
| 130 SizeType max_segment_size, |
| 131 std::vector<SizeType>* segment_sizes); |
| 132 |
| 133 Error error_; |
| 134 |
| 135 std::vector<uint64_t> file_handles_; |
| 136 // can be size_t, but same as file handles so we can use the same methods. |
| 137 std::vector<size_t> shared_memory_handles_; |
| 138 |
| 139 uint64_t total_bytes_size_; |
| 140 std::vector<MemoryItemRequest> requests_; |
| 141 scoped_ptr<BlobDataBuilder> builder_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(BlobAsyncTransportStrategy); |
| 144 }; |
| 145 |
| 146 } // namespace storage |
| 147 |
| 148 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |
OLD | NEW |