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_TRANSPORT_STRATEGY_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_TRANSPORT_STRATEGY_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "storage/browser/storage_browser_export.h" |
| 12 #include "storage/common/data_element.h" |
| 13 #include "storage/common/fileapi/blob_item_bytes_request.h" |
| 14 |
| 15 namespace storage { |
| 16 |
| 17 // This class determines and stores the strategy for asynchronously transporting |
| 18 // memory from the renderer to the browser. This involves two transformations: |
| 19 // 1. Transforming the data storage to be efficient in-transport. This can |
| 20 // mean that data is consolidated from cross-blob or cross-file boundaries |
| 21 // in the blob in a common block of shared memory or a file. |
| 22 // 2. Transforming the transport data into the browser storage representation. |
| 23 // This can simply mirror the renderer representation, or it can be instead |
| 24 // pointing to transport representation with offsets and sizes. |
| 25 // |
| 26 // The initial implementation of this is simple, but the protocol allows any |
| 27 // sort of flexibility, as everything has an offset and size specified. |
| 28 class STORAGE_EXPORT BlobTransportStrategy { |
| 29 public: |
| 30 enum Mode { MODE_UNKNOWN = 0, MODE_NO_DISK, MODE_DISK }; |
| 31 |
| 32 enum Error { |
| 33 ERROR_NONE = 0, |
| 34 ERROR_TOO_LARGE, // This item can't fit in disk or memory |
| 35 ERROR_INVALID_PARAMS |
| 36 }; |
| 37 |
| 38 struct MemoryItemRequest { |
| 39 size_t browser_item_index; |
| 40 size_t browser_item_offset; |
| 41 BlobItemBytesRequest message; |
| 42 }; |
| 43 |
| 44 BlobTransportStrategy(); |
| 45 virtual ~BlobTransportStrategy(); |
| 46 |
| 47 // Determines the requests, file handles, and memory handles for the given |
| 48 // system state and blob description. Can be called multiple times to reset |
| 49 // the object. |
| 50 // |
| 51 // mode: The transportation mode, either MODE_NO_DISK or MODE_DISK. NO_DISK |
| 52 // means that we won't transport files over disk. |
| 53 // max_ipc_memory_size: Maximum amount of memory that we can include in our |
| 54 // IPC message. |
| 55 // max_shared_memory_size: Maximum amount of memory we can store |
| 56 // in a shared memory block. |
| 57 // max_file_size: Maximum amount of memory we can store in a file. |
| 58 // max_blob_in_memory_size: Maximum size of a blob before it skips being |
| 59 // stored in memory and goes straight to a file. |
| 60 // disk_space_left: The amount of disk space left. |
| 61 // memory_left: The amount of memory left in our memory cache. |
| 62 void Initialize(Mode mode, |
| 63 size_t max_ipc_memory_size, |
| 64 size_t max_shared_memory_size, |
| 65 uint64_t max_file_size, |
| 66 size_t max_blob_in_memory_size, |
| 67 uint64_t disk_space_left, |
| 68 size_t memory_left, |
| 69 const std::vector<DataElement>& blob_item_infos); |
| 70 |
| 71 const std::vector<uint64_t>& file_handles() const { return file_handles_; } |
| 72 |
| 73 const std::vector<size_t>& shared_memory_handles() const { |
| 74 return shared_memory_handles_; |
| 75 } |
| 76 |
| 77 const std::vector<MemoryItemRequest>& requests() const { return requests_; } |
| 78 |
| 79 Error error() const { return error_; } |
| 80 |
| 81 private: |
| 82 // This iterates of the data elements and segments the 'bytes' data into |
| 83 // the smallest number of segments given the max_segment_size. |
| 84 // The callback describes either: |
| 85 // * A non-memory item |
| 86 // * A partition of a bytes element which will be populated into a given |
| 87 // segment and segment offset. |
| 88 // The function signature of the template functor is: |
| 89 // void(DataElement::Type /*element_type*/, |
| 90 // size_t /*element_index*/, |
| 91 // SizeType /*element_offset*/, |
| 92 // size_t /*segment_index*/, |
| 93 // SizeType /*segment_offset*/, |
| 94 // SizeType /*size*/) |
| 95 // |
| 96 // element: A DataElement from the |items| vector. The callback is called |
| 97 // for every single element in the vector (multiple times |
| 98 // if the element needs to be split up) |
| 99 // segment: A memory segment, with a maximum size of |max_segment_size|. |
| 100 // |
| 101 // Assumptions: All memory items are consolidated. As in, there are no two |
| 102 // 'bytes' items next to eachother. |
| 103 template <typename ForEachFunctor, typename SizeType> |
| 104 static void ForEachWithSegment(const std::vector<DataElement>& items, |
| 105 SizeType max_segment_size, |
| 106 ForEachFunctor function); |
| 107 |
| 108 // This populates the |segment_sizes| vector with the sizes of the segments |
| 109 // that will be used in the transport strategy. |
| 110 template <typename SizeType> |
| 111 static void CreateHandleSizes(SizeType total_memory_size, |
| 112 SizeType max_segment_size, |
| 113 std::vector<SizeType>* segment_sizes); |
| 114 |
| 115 Error error_; |
| 116 |
| 117 std::vector<uint64_t> file_handles_; |
| 118 std::vector<size_t> shared_memory_handles_; |
| 119 |
| 120 std::vector<MemoryItemRequest> requests_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(BlobTransportStrategy); |
| 123 }; |
| 124 |
| 125 } // namespace storage |
| 126 |
| 127 #endif // STORAGE_BROWSER_BLOB_BLOB_TRANSPORT_STRATEGY_H_ |
| 128 |
OLD | NEW |