Chromium Code Reviews| 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. | |
|
michaeln
2015/11/17 21:55:29
The comment is verbose, but honestly i'm not sure
| |
| 45 class STORAGE_EXPORT BlobAsyncTransportStrategy { | |
| 46 public: | |
| 47 enum Mode { MODE_UNKNOWN = 0, 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 { | |
|
michaeln
2015/11/17 21:55:28
Using 'Memory' in this struct name threw me off a
dmurph
2015/11/19 02:06:18
I mean, a request for an item that's memory in the
| |
| 56 MemoryItemRequest(); | |
| 57 size_t browser_item_index; | |
|
michaeln
2015/11/17 21:55:29
it's not real clear what this is an index into, bu
dmurph
2015/11/19 02:06:18
Done.
| |
| 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 { | |
|
michaeln
2015/11/17 21:55:28
can this be private?
dmurph
2015/11/19 02:06:18
Sure.
| |
| 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, | |
|
michaeln
2015/11/17 21:55:28
About the packaging of this logic, feels like more
dmurph
2015/11/19 02:06:18
Well, the main idea of this class is that it's the
| |
| 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_; } | |
|
michaeln
2015/11/17 21:55:28
The comments and member names make it sound like t
dmurph
2015/11/19 02:06:18
Done.
| |
| 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) | |
|
michaeln
2015/11/17 21:55:28
comment could be simpler, sizes are sorted from la
dmurph
2015/11/19 02:06:18
Not intentionally. These are the sizes of all of t
| |
| 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, | |
|
michaeln
2015/11/17 21:55:28
naming suggestion: ComputeHandleSizes to avoid hav
dmurph
2015/11/19 02:06:18
Done.
| |
| 130 SizeType max_segment_size, | |
| 131 std::vector<SizeType>* segment_sizes); | |
| 132 | |
| 133 Error error_; | |
| 134 | |
| 135 std::vector<uint64_t> file_handles_; | |
|
michaeln
2015/11/17 21:55:28
naming suggestions: append sizes_ to these names
dmurph
2015/11/19 02:06:18
Done.
| |
| 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 |