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 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. We take memory constraints of our | |
| 24 // system and the description of a blob, and figure out: | |
| 25 // 1) How to store the blob data in the browser process: in memory or on disk. | |
| 26 // 2) How to transport the data from the renderer: ipc payload, shared memory, | |
| 27 // or file handles. | |
| 28 // We then generate data requests for that blob's memory and seed a | |
| 29 // BlobDataBuilder for storing that data. | |
| 30 // | |
| 31 // Note: This class does not compute requests by using the 'shortcut' method, | |
| 32 // where the data is already present in the blob description, and will | |
| 33 // always give the caller the full strategy for requesting all data from | |
| 34 // the renderer. | |
|
michaeln
2015/11/24 23:19:40
short-n-sweet... thnx!
dmurph
2015/11/25 21:16:30
np
| |
| 35 class STORAGE_EXPORT BlobAsyncTransportStrategy { | |
| 36 public: | |
| 37 enum Error { | |
| 38 ERROR_NONE = 0, | |
| 39 ERROR_TOO_LARGE, // This item can't fit in disk or memory | |
| 40 ERROR_INVALID_PARAMS | |
| 41 }; | |
| 42 | |
| 43 struct RendererMemoryItemRequest { | |
| 44 RendererMemoryItemRequest(); | |
| 45 // This is the index of the item in the builder on the browser side. | |
| 46 size_t browser_item_index; | |
| 47 // Note: For files this offset should always be 0, as the file offset in | |
| 48 // segmentation is handled by the handle_offset in the message. This | |
| 49 // offset is used for populating a chunk when the data comes back to | |
| 50 // the browser. | |
| 51 size_t browser_item_offset; | |
| 52 BlobItemBytesRequest message; | |
| 53 bool received; | |
| 54 }; | |
| 55 | |
| 56 BlobAsyncTransportStrategy(); | |
| 57 virtual ~BlobAsyncTransportStrategy(); | |
| 58 | |
| 59 // This call does the computation to create the requests and builder for the | |
| 60 // blob given the memory constraints and blob description. | |
| 61 // 'memory_available' is the total amount of memory we can offer for storing | |
| 62 // blobs. | |
|
kinuko
2015/11/25 16:08:17
nit: maybe also note that it is not valid to call
dmurph
2015/11/25 21:16:30
Done.
| |
| 63 void Initialize(size_t max_ipc_memory_size, | |
| 64 size_t max_shared_memory_size, | |
| 65 uint64_t max_file_size, | |
| 66 uint64_t disk_space_left, | |
| 67 size_t memory_available, | |
| 68 const std::string& uuid, | |
| 69 const std::vector<DataElement>& blob_item_infos); | |
| 70 | |
| 71 // The sizes of the file handles being used (by handle index) in the async | |
| 72 // operation. | |
| 73 std::vector<uint64_t>& file_handle_sizes() { return file_handle_sizes_; } | |
|
kinuko
2015/11/25 16:08:17
nit: do we really want/need to return non-const ve
dmurph
2015/11/25 21:16:30
Good catch. We don't need to anymore because we a
michaeln
2015/11/25 21:23:59
This gets back to an earlier comment about how he
| |
| 74 | |
| 75 // The sizes of the shared memory handles being used (by handle index) in the | |
| 76 // async operation. | |
| 77 std::vector<size_t>& shared_memory_handle_sizes() { | |
| 78 return shared_memory_handle_sizes_; | |
| 79 } | |
| 80 | |
| 81 // The requests for memory, segmented as described above, along with their | |
| 82 // destination browser indexes and offsets. | |
| 83 const std::vector<RendererMemoryItemRequest>& requests() const { | |
| 84 return requests_; | |
| 85 } | |
| 86 | |
| 87 // A BlobDataBuilder which can be used to construct the Blob in the | |
| 88 // BlobStorageContext object after: | |
| 89 // * The bytes items from AppendFutureData are populated by | |
| 90 // PopulateFutureData. | |
| 91 // * The temporary files from AppendFutureFile are populated by | |
| 92 // PopulateFutureFile. | |
| 93 BlobDataBuilder* blob_builder() { return builder_.get(); } | |
| 94 | |
| 95 // The total bytes size of memory items in the blob. | |
| 96 uint64_t total_bytes_size() const { return total_bytes_size_; } | |
| 97 | |
| 98 Error error() const { return error_; } | |
|
michaeln
2015/11/25 21:23:59
Given how this data member is used, maybe have Ini
| |
| 99 | |
| 100 static bool ShouldBeShortcut(const std::vector<DataElement>& items, | |
| 101 size_t memory_available); | |
| 102 | |
| 103 protected: | |
| 104 friend class FileStorageStrategy; | |
| 105 friend class SharedMemoryStorageStrategy; | |
| 106 | |
| 107 template <typename SizeType> | |
| 108 class BlobSegmentVisitor { | |
| 109 public: | |
| 110 virtual ~BlobSegmentVisitor(){}; | |
| 111 virtual void VisitBytesSegment(size_t element_index, | |
| 112 SizeType element_offset, | |
| 113 size_t segment_index, | |
| 114 SizeType segment_offset, | |
| 115 SizeType size) = 0; | |
| 116 virtual void VisitNonBytesSegment(const DataElement& element, | |
| 117 size_t element_index) = 0; | |
| 118 virtual void Done() = 0; | |
| 119 }; | |
| 120 | |
| 121 // This iterates of the data elements and segments the 'bytes' data into | |
| 122 // the smallest number of segments given the max_segment_size. | |
| 123 // The callback describes either: | |
| 124 // * A non-memory item | |
| 125 // * A partition of a bytes element which will be populated into a given | |
| 126 // segment and segment offset. | |
| 127 // Assumptions: All memory items are consolidated. As in, there are no two | |
| 128 // 'bytes' items next to eachother. | |
| 129 template <typename SizeType> | |
| 130 static void ForEachWithSegment(const std::vector<DataElement>& items, | |
| 131 SizeType max_segment_size, | |
| 132 BlobSegmentVisitor<SizeType>* visitor); | |
| 133 | |
| 134 private: | |
| 135 template <typename SizeType> | |
| 136 static void ComputeHandleSizes(SizeType total_memory_size, | |
| 137 SizeType max_segment_size, | |
| 138 std::vector<SizeType>* segment_sizes); | |
| 139 | |
| 140 Error error_; | |
| 141 | |
| 142 std::vector<uint64_t> file_handle_sizes_; | |
| 143 std::vector<size_t> shared_memory_handle_sizes_; | |
| 144 | |
| 145 uint64_t total_bytes_size_; | |
| 146 std::vector<RendererMemoryItemRequest> requests_; | |
| 147 scoped_ptr<BlobDataBuilder> builder_; | |
| 148 | |
| 149 DISALLOW_COPY_AND_ASSIGN(BlobAsyncTransportStrategy); | |
| 150 }; | |
| 151 | |
| 152 } // namespace storage | |
| 153 | |
| 154 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ | |
| OLD | NEW |