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. We take memory constraints of our |
| 22 // system and the description of a blob, and figure out: |
| 23 // 1) How to store the blob data in the browser process: in memory or on disk. |
| 24 // 2) How to transport the data from the renderer: ipc payload, shared memory, |
| 25 // or file handles. |
| 26 // We then generate data requests for that blob's memory and seed a |
| 27 // BlobDataBuilder for storing that data. |
| 28 // |
| 29 // Note: This class does not compute requests by using the 'shortcut' method, |
| 30 // where the data is already present in the blob description, and will |
| 31 // always give the caller the full strategy for requesting all data from |
| 32 // the renderer. |
| 33 class STORAGE_EXPORT BlobAsyncTransportStrategy { |
| 34 public: |
| 35 enum Error { |
| 36 ERROR_NONE = 0, |
| 37 ERROR_TOO_LARGE, // This item can't fit in disk or memory |
| 38 ERROR_INVALID_PARAMS |
| 39 }; |
| 40 |
| 41 struct RendererMemoryItemRequest { |
| 42 RendererMemoryItemRequest(); |
| 43 // This is the index of the item in the builder on the browser side. |
| 44 size_t browser_item_index; |
| 45 // Note: For files this offset should always be 0, as the file offset in |
| 46 // segmentation is handled by the handle_offset in the message. This |
| 47 // offset is used for populating a chunk when the data comes back to |
| 48 // the browser. |
| 49 size_t browser_item_offset; |
| 50 BlobItemBytesRequest message; |
| 51 bool received; |
| 52 }; |
| 53 |
| 54 BlobAsyncTransportStrategy(); |
| 55 virtual ~BlobAsyncTransportStrategy(); |
| 56 |
| 57 // This call does the computation to create the requests and builder for the |
| 58 // blob given the memory constraints and blob description. |memory_available| |
| 59 // is the total amount of memory we can offer for storing blobs. |
| 60 // This method can only be called once. |
| 61 void Initialize(size_t max_ipc_memory_size, |
| 62 size_t max_shared_memory_size, |
| 63 size_t max_file_size, |
| 64 uint64_t disk_space_left, |
| 65 size_t memory_available, |
| 66 const std::string& uuid, |
| 67 const std::vector<DataElement>& blob_item_infos); |
| 68 |
| 69 // The sizes of the handles being used (by handle index) in the async |
| 70 // operation. This is used for both files or shared memory, as their use is |
| 71 // mutually exclusive. |
| 72 const std::vector<size_t>& handle_sizes() const { return handle_sizes_; } |
| 73 |
| 74 // The requests for memory, segmented as described above, along with their |
| 75 // destination browser indexes and offsets. |
| 76 const std::vector<RendererMemoryItemRequest>& requests() const { |
| 77 return requests_; |
| 78 } |
| 79 |
| 80 // Marks the request at the given request number as recieved. |
| 81 void MarkRequestAsReceived(size_t request_num) { |
| 82 DCHECK_LT(request_num, requests_.size()); |
| 83 requests_[request_num].received = true; |
| 84 } |
| 85 |
| 86 // A BlobDataBuilder which can be used to construct the Blob in the |
| 87 // BlobStorageContext object after: |
| 88 // * The bytes items from AppendFutureData are populated by |
| 89 // PopulateFutureData. |
| 90 // * The temporary files from AppendFutureFile are populated by |
| 91 // PopulateFutureFile. |
| 92 BlobDataBuilder* blob_builder() { return builder_.get(); } |
| 93 |
| 94 // The total bytes size of memory items in the blob. |
| 95 uint64_t total_bytes_size() const { return total_bytes_size_; } |
| 96 |
| 97 Error error() const { return error_; } |
| 98 |
| 99 static bool ShouldBeShortcut(const std::vector<DataElement>& items, |
| 100 size_t memory_available); |
| 101 |
| 102 private: |
| 103 static void ComputeHandleSizes(uint64_t total_memory_size, |
| 104 size_t max_segment_size, |
| 105 std::vector<size_t>* segment_sizes); |
| 106 |
| 107 Error error_; |
| 108 |
| 109 // We use the same vector for shared memory handle sizes and file handle sizes |
| 110 // because we only use one for any strategy. The size of the handles is capped |
| 111 // by the |max_file_size| argument in Initialize, so we can just use size_t. |
| 112 std::vector<size_t> handle_sizes_; |
| 113 |
| 114 uint64_t total_bytes_size_; |
| 115 std::vector<RendererMemoryItemRequest> requests_; |
| 116 scoped_ptr<BlobDataBuilder> builder_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(BlobAsyncTransportStrategy); |
| 119 }; |
| 120 |
| 121 } // namespace storage |
| 122 |
| 123 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |
OLD | NEW |