OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_REQUEST_BUILDER_H_ |
6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_REQUEST_BUILDER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "storage/browser/blob/blob_data_builder.h" | 16 #include "storage/browser/blob/blob_data_builder.h" |
17 #include "storage/browser/storage_browser_export.h" | 17 #include "storage/browser/storage_browser_export.h" |
18 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 18 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
19 #include "storage/common/data_element.h" | 19 #include "storage/common/data_element.h" |
20 | 20 |
21 namespace storage { | 21 namespace storage { |
22 | 22 |
23 // This class computes and stores the strategy for asynchronously transporting | 23 // This class generates the requests needed to asynchronously transport the |
24 // memory from the renderer to the browser. We take memory constraints of our | 24 // given blob items from the renderer to the browser. The main job of this class |
25 // system and the description of a blob, and figure out: | 25 // is to segment the memory being transfered to efficiently use shared memory, |
26 // 1) How to store the blob data in the browser process: in memory or on disk. | 26 // file, and IPC max sizes. |
27 // 2) How to transport the data from the renderer: ipc payload, shared memory, | |
28 // or file handles. | |
29 // We then generate data requests for that blob's memory and seed a | |
30 // BlobDataBuilder for storing that data. | |
31 // | |
32 // Note: This class does not compute requests by using the 'shortcut' method, | 27 // Note: This class does not compute requests by using the 'shortcut' method, |
33 // where the data is already present in the blob description, and will | 28 // where the data is already present in the blob description, and will |
34 // always give the caller the full strategy for requesting all data from | 29 // always give the caller requests for requesting all data from the |
35 // the renderer. | 30 // renderer. |
36 class STORAGE_EXPORT BlobAsyncTransportStrategy { | 31 class STORAGE_EXPORT BlobAsyncTransportRequestBuilder { |
37 public: | 32 public: |
38 enum Error { | |
39 ERROR_NONE = 0, | |
40 ERROR_TOO_LARGE, // This item can't fit in disk or memory | |
41 ERROR_INVALID_PARAMS | |
42 }; | |
43 | |
44 struct RendererMemoryItemRequest { | 33 struct RendererMemoryItemRequest { |
45 RendererMemoryItemRequest(); | 34 RendererMemoryItemRequest(); |
46 // This is the index of the item in the builder on the browser side. | 35 // This is the index of the item in the builder on the browser side. |
47 size_t browser_item_index; | 36 size_t browser_item_index; |
48 // Note: For files this offset should always be 0, as the file offset in | 37 // Note: For files this offset should always be 0, as the file offset in |
49 // segmentation is handled by the handle_offset in the message. This | 38 // segmentation is handled by the handle_offset in the message. This |
50 // offset is used for populating a chunk when the data comes back to | 39 // offset is used for populating a chunk when the data comes back to |
51 // the browser. | 40 // the browser. |
52 size_t browser_item_offset; | 41 size_t browser_item_offset; |
53 BlobItemBytesRequest message; | 42 BlobItemBytesRequest message; |
54 bool received; | |
55 }; | 43 }; |
56 | 44 |
57 BlobAsyncTransportStrategy(); | 45 BlobAsyncTransportRequestBuilder(); |
58 virtual ~BlobAsyncTransportStrategy(); | 46 virtual ~BlobAsyncTransportRequestBuilder(); |
59 | 47 |
60 // This call does the computation to create the requests and builder for the | 48 // Initializes the request builder for file requests. One or more files are |
61 // blob given the memory constraints and blob description. |memory_available| | 49 // created to hold the given data. Each file can hold data from multiple |
62 // is the total amount of memory we can offer for storing blobs. | 50 // items, and the data from each item can be in multiple files. |
63 // This method can only be called once. | 51 // See file_handle_sizes() for the generated file sizes. |
64 void Initialize(size_t max_ipc_memory_size, | 52 // max_file_size: This is the maximum size for a file to back a blob. |
65 size_t max_shared_memory_size, | 53 // blob_total_size: This is the total in-memory size of the blob. |
66 size_t max_file_size, | 54 // elements: These are the descriptions of the blob items being sent from the |
67 uint64_t disk_space_left, | 55 // renderer. |
68 size_t memory_available, | 56 // builder: This is the builder that is populated with the 'future' versions |
69 const std::string& uuid, | 57 // of the data elements. In this case, we call 'AppendFutureData' in |
70 const std::vector<DataElement>& blob_item_infos); | 58 // the items that we expect to be backed by files writen by the |
| 59 // renderer. |
| 60 void InitializeForFileRequests(size_t max_file_size, |
| 61 uint64_t blob_total_size, |
| 62 const std::vector<DataElement>& elements, |
| 63 BlobDataBuilder* builder); |
71 | 64 |
72 // The sizes of the handles being used (by handle index) in the async | 65 // Initializes the request builder for shared memory requests. We try to |
73 // operation. This is used for both files or shared memory, as their use is | 66 // consolidate as much memory as possible in each shared memory segment we |
74 // mutually exclusive. | 67 // use. |
75 const std::vector<size_t>& handle_sizes() const { return handle_sizes_; } | 68 // See shared_memory_handle_sizes() for the shared memory sizes. |
| 69 // max_shared_memory_size: This is the maximum size for a shared memory |
| 70 // segment used to transport the data between renderer |
| 71 // and browser. |
| 72 // blob_total_size: This is the total in-memory size of the blob. |
| 73 // elements: These are the descriptions of the blob items being sent from the |
| 74 // renderer. |
| 75 // builder: This is the builder that is populated with the 'future' versions |
| 76 // of the data elements. In this case, we call 'AppendFutureData' for |
| 77 // the items we expect to be populated later. |
| 78 void InitializeForSharedMemoryRequests( |
| 79 size_t max_shared_memory_size, |
| 80 uint64_t blob_total_size, |
| 81 const std::vector<DataElement>& elements, |
| 82 BlobDataBuilder* builder); |
| 83 |
| 84 // Initializes the request builder for IPC requests. We put as much memory |
| 85 // in a single IPC request as possible. |
| 86 // max_ipc_memory_size: This is the maximum size for an IPC message which will |
| 87 // be used to transport memory from the renderer to the |
| 88 // browser. |
| 89 // blob_total_size: This is the total in-memory size of the blob. |
| 90 // elements: These are the descriptions of the blob items being sent from the |
| 91 // renderer. |
| 92 // builder: This is the builder that is populated with the 'future' versions |
| 93 // of the data elements. In this case, we call 'AppendFutureData' for |
| 94 // the items we expect to be populated later. |
| 95 void InitializeForIPCRequests(size_t max_ipc_memory_size, |
| 96 uint64_t blob_total_size, |
| 97 const std::vector<DataElement>& elements, |
| 98 BlobDataBuilder* builder); |
| 99 |
| 100 // The sizes of the shared memory handles being used (by handle index). |
| 101 const std::vector<size_t>& shared_memory_sizes() const { |
| 102 return shared_memory_sizes_; |
| 103 } |
| 104 |
| 105 // The sizes of the file handles being used (by handle index). |
| 106 const std::vector<size_t>& file_sizes() const { return file_sizes_; } |
76 | 107 |
77 // The requests for memory, segmented as described above, along with their | 108 // The requests for memory, segmented as described above, along with their |
78 // destination browser indexes and offsets. | 109 // destination browser indexes and offsets. |
79 const std::vector<RendererMemoryItemRequest>& requests() const { | 110 const std::vector<RendererMemoryItemRequest>& requests() const { |
80 return requests_; | 111 return requests_; |
81 } | 112 } |
82 | 113 |
83 // Marks the request at the given request number as recieved. | |
84 void MarkRequestAsReceived(size_t request_num) { | |
85 DCHECK_LT(request_num, requests_.size()); | |
86 requests_[request_num].received = true; | |
87 } | |
88 | |
89 // A BlobDataBuilder which can be used to construct the Blob in the | |
90 // BlobStorageContext object after: | |
91 // * The bytes items from AppendFutureData are populated by | |
92 // PopulateFutureData. | |
93 // * The temporary files from AppendFutureFile are populated by | |
94 // PopulateFutureFile. | |
95 BlobDataBuilder* blob_builder() { return builder_.get(); } | |
96 | |
97 // The total bytes size of memory items in the blob. | 114 // The total bytes size of memory items in the blob. |
98 uint64_t total_bytes_size() const { return total_bytes_size_; } | 115 uint64_t total_bytes_size() const { return total_bytes_size_; } |
99 | 116 |
100 Error error() const { return error_; } | |
101 | |
102 static bool ShouldBeShortcut(const std::vector<DataElement>& items, | 117 static bool ShouldBeShortcut(const std::vector<DataElement>& items, |
103 size_t memory_available); | 118 size_t memory_available); |
104 | 119 |
105 private: | 120 private: |
106 static void ComputeHandleSizes(uint64_t total_memory_size, | 121 static void ComputeHandleSizes(uint64_t total_memory_size, |
107 size_t max_segment_size, | 122 size_t max_segment_size, |
108 std::vector<size_t>* segment_sizes); | 123 std::vector<size_t>* segment_sizes); |
109 | 124 |
110 Error error_; | 125 std::vector<size_t> shared_memory_sizes_; |
111 | 126 // The size of the files is capped by the |max_file_size| argument in |
112 // We use the same vector for shared memory handle sizes and file handle sizes | 127 // InitializeForFileRequests, so we can just use size_t. |
113 // because we only use one for any strategy. The size of the handles is capped | 128 std::vector<size_t> file_sizes_; |
114 // by the |max_file_size| argument in Initialize, so we can just use size_t. | |
115 std::vector<size_t> handle_sizes_; | |
116 | 129 |
117 uint64_t total_bytes_size_; | 130 uint64_t total_bytes_size_; |
118 std::vector<RendererMemoryItemRequest> requests_; | 131 std::vector<RendererMemoryItemRequest> requests_; |
119 scoped_ptr<BlobDataBuilder> builder_; | |
120 | 132 |
121 DISALLOW_COPY_AND_ASSIGN(BlobAsyncTransportStrategy); | 133 DISALLOW_COPY_AND_ASSIGN(BlobAsyncTransportRequestBuilder); |
122 }; | 134 }; |
123 | 135 |
124 } // namespace storage | 136 } // namespace storage |
125 | 137 |
126 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ | 138 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_REQUEST_BUILDER_H_ |
OLD | NEW |