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