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 class FileStorageStrategy; |
| 20 class SharedMemoryStorageStrategy; |
| 21 |
| 22 // This class computes and stores the strategy for asynchronously transporting |
| 23 // memory from the renderer to the browser. |
| 24 // |
| 25 // tl;dr: |
| 26 // We take memory constraints of our system and the description of a blob, and |
| 27 // generate data requests for that blob's memory and a seed a BlobDataBuilder |
| 28 // for storing that data. |
| 29 // |
| 30 // This class does not compute requests by using the 'shortcut' method, where |
| 31 // the data is already present in the blob description, and will always give |
| 32 // the caller the full strategy for requesting all data from the renderer. The |
| 33 // returned builder will always have TYPE_BYTES_DESCRIPTION items for the future |
| 34 // memory locations. |
| 35 // This involves two transformations: |
| 36 // 1. Transforming the data storage to be efficient in-transport. This can |
| 37 // mean that data is consolidated from cross-blob or cross-file boundaries |
| 38 // in the blob in a common block of shared memory or a file. |
| 39 // 2. Transforming the transport data into the browser storage representation. |
| 40 // This can simply mirror the renderer representation, or it can be instead |
| 41 // pointing to transport representation with offsets and sizes. |
| 42 // |
| 43 // The following information is generated: |
| 44 // 1. The total bytes size of memory items. |
| 45 // 2. The requests for memory, segmented as described above, along with their |
| 46 // destination browser indexes and offsets |
| 47 // 3. The sizes of the shared memory and file handles being used (by handle |
| 48 // index) in the async operation. |
| 49 // 4. A BlobDataBuilder which can be used to construct the Blob in the |
| 50 // BlobStorageContext object, after the TYPE_BYTES_DESCRIPTION entries are |
| 51 // turned into TYPE_BYTES entries. |
| 52 // The initial implementation of this is simple, but the protocol allows any |
| 53 // sort of flexibility, as everything has an offset and size specified. |
| 54 class STORAGE_EXPORT BlobAsyncTransportStrategy { |
| 55 public: |
| 56 enum Error { |
| 57 ERROR_NONE = 0, |
| 58 ERROR_TOO_LARGE, // This item can't fit in disk or memory |
| 59 ERROR_INVALID_PARAMS |
| 60 }; |
| 61 |
| 62 struct RendererMemoryItemRequest { |
| 63 RendererMemoryItemRequest(); |
| 64 // This is the index of the item in the builder on the browser side. |
| 65 size_t browser_item_index; |
| 66 // Note: For files this offset should always be 0, as the file offset in |
| 67 // segmentation is handled by the handle_offset in the message. This |
| 68 // offset is used for populating a chunk when the data comes back to |
| 69 // the browser. |
| 70 size_t browser_item_offset; |
| 71 BlobItemBytesRequest message; |
| 72 bool received; |
| 73 }; |
| 74 |
| 75 BlobAsyncTransportStrategy(); |
| 76 virtual ~BlobAsyncTransportStrategy(); |
| 77 |
| 78 // This call does the computation to create the requests and builder for the |
| 79 // blob given the memory constraints and blob description. |
| 80 // 'memory_available' is the total amount of memory we can offer for storing |
| 81 // blobs. |
| 82 void Initialize(size_t max_ipc_memory_size, |
| 83 size_t max_shared_memory_size, |
| 84 uint64_t max_file_size, |
| 85 uint64_t disk_space_left, |
| 86 size_t memory_available, |
| 87 const std::string& uuid, |
| 88 const std::vector<DataElement>& blob_item_infos); |
| 89 |
| 90 // In the current algorithm, the file sizes will always be sorted from largest |
| 91 // to smallest. |
| 92 std::vector<uint64_t>& file_handle_sizes() { return file_handle_sizes_; } |
| 93 |
| 94 // In the current algorithm, the shared memory sizes will always be sorted |
| 95 // from largest to smallest. |
| 96 std::vector<size_t>& shared_memory_handle_sizes() { |
| 97 return shared_memory_handle_sizes_; |
| 98 } |
| 99 |
| 100 const std::vector<RendererMemoryItemRequest>& requests() const { |
| 101 return requests_; |
| 102 } |
| 103 |
| 104 BlobDataBuilder* blob_builder() { return builder_.get(); } |
| 105 |
| 106 uint64_t total_bytes_size() const { return total_bytes_size_; } |
| 107 |
| 108 Error error() const { return error_; } |
| 109 |
| 110 static bool ShouldBeShortcut(const std::vector<DataElement>& items, |
| 111 size_t memory_available); |
| 112 |
| 113 protected: |
| 114 friend class FileStorageStrategy; |
| 115 friend class SharedMemoryStorageStrategy; |
| 116 |
| 117 template <typename SizeType> |
| 118 class BlobSegmentVisitor { |
| 119 public: |
| 120 virtual ~BlobSegmentVisitor(){}; |
| 121 virtual void VisitBytesSegment(size_t element_index, |
| 122 SizeType element_offset, |
| 123 size_t segment_index, |
| 124 SizeType segment_offset, |
| 125 SizeType size) = 0; |
| 126 virtual void VisitNonBytesSegment(const DataElement& element, |
| 127 size_t element_index) = 0; |
| 128 virtual void Done() = 0; |
| 129 }; |
| 130 |
| 131 // This iterates of the data elements and segments the 'bytes' data into |
| 132 // the smallest number of segments given the max_segment_size. |
| 133 // The callback describes either: |
| 134 // * A non-memory item |
| 135 // * A partition of a bytes element which will be populated into a given |
| 136 // segment and segment offset. |
| 137 // Assumptions: All memory items are consolidated. As in, there are no two |
| 138 // 'bytes' items next to eachother. |
| 139 template <typename SizeType> |
| 140 static void ForEachWithSegment(const std::vector<DataElement>& items, |
| 141 SizeType max_segment_size, |
| 142 BlobSegmentVisitor<SizeType>* visitor); |
| 143 |
| 144 private: |
| 145 // This simply fills the segment_sizes vector with the sizes of segments that |
| 146 // would be used to partition a block of total_memory_size. |
| 147 template <typename SizeType> |
| 148 static void ComputeHandleSizes(SizeType total_memory_size, |
| 149 SizeType max_segment_size, |
| 150 std::vector<SizeType>* segment_sizes); |
| 151 |
| 152 Error error_; |
| 153 |
| 154 std::vector<uint64_t> file_handle_sizes_; |
| 155 // can be size_t, but same as file handles so we can use the same methods. |
| 156 std::vector<size_t> shared_memory_handle_sizes_; |
| 157 |
| 158 uint64_t total_bytes_size_; |
| 159 std::vector<RendererMemoryItemRequest> requests_; |
| 160 scoped_ptr<BlobDataBuilder> builder_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(BlobAsyncTransportStrategy); |
| 163 }; |
| 164 |
| 165 } // namespace storage |
| 166 |
| 167 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |
OLD | NEW |