Chromium Code Reviews| Index: storage/browser/blob/blob_async_transport_strategy.h |
| diff --git a/storage/browser/blob/blob_async_transport_strategy.h b/storage/browser/blob/blob_async_transport_strategy.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eaa83b1c9c3308099b4e00798ef9b5bb5e2de552 |
| --- /dev/null |
| +++ b/storage/browser/blob/blob_async_transport_strategy.h |
| @@ -0,0 +1,154 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |
| +#define STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |
| + |
| +#include <map> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "storage/browser/blob/blob_data_builder.h" |
| +#include "storage/browser/storage_browser_export.h" |
| +#include "storage/common/blob_storage/blob_item_bytes_request.h" |
| +#include "storage/common/data_element.h" |
| + |
| +namespace storage { |
| +class FileStorageStrategy; |
| +class SharedMemoryStorageStrategy; |
| + |
| +// This class computes and stores the strategy for asynchronously transporting |
| +// memory from the renderer to the browser. We take memory constraints of our |
| +// system and the description of a blob, and figure out: |
| +// 1) How to store the blob data in the browser process: in memory or on disk. |
| +// 2) How to transport the data from the renderer: ipc payload, shared memory, |
| +// or file handles. |
| +// We then generate data requests for that blob's memory and seed a |
| +// BlobDataBuilder for storing that data. |
| +// |
| +// Note: This class does not compute requests by using the 'shortcut' method, |
| +// where the data is already present in the blob description, and will |
| +// always give the caller the full strategy for requesting all data from |
| +// the renderer. |
|
michaeln
2015/11/24 23:19:40
short-n-sweet... thnx!
dmurph
2015/11/25 21:16:30
np
|
| +class STORAGE_EXPORT BlobAsyncTransportStrategy { |
| + public: |
| + enum Error { |
| + ERROR_NONE = 0, |
| + ERROR_TOO_LARGE, // This item can't fit in disk or memory |
| + ERROR_INVALID_PARAMS |
| + }; |
| + |
| + struct RendererMemoryItemRequest { |
| + RendererMemoryItemRequest(); |
| + // This is the index of the item in the builder on the browser side. |
| + size_t browser_item_index; |
| + // Note: For files this offset should always be 0, as the file offset in |
| + // segmentation is handled by the handle_offset in the message. This |
| + // offset is used for populating a chunk when the data comes back to |
| + // the browser. |
| + size_t browser_item_offset; |
| + BlobItemBytesRequest message; |
| + bool received; |
| + }; |
| + |
| + BlobAsyncTransportStrategy(); |
| + virtual ~BlobAsyncTransportStrategy(); |
| + |
| + // This call does the computation to create the requests and builder for the |
| + // blob given the memory constraints and blob description. |
| + // 'memory_available' is the total amount of memory we can offer for storing |
| + // 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.
|
| + void Initialize(size_t max_ipc_memory_size, |
| + size_t max_shared_memory_size, |
| + uint64_t max_file_size, |
| + uint64_t disk_space_left, |
| + size_t memory_available, |
| + const std::string& uuid, |
| + const std::vector<DataElement>& blob_item_infos); |
| + |
| + // The sizes of the file handles being used (by handle index) in the async |
| + // operation. |
| + 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
|
| + |
| + // The sizes of the shared memory handles being used (by handle index) in the |
| + // async operation. |
| + std::vector<size_t>& shared_memory_handle_sizes() { |
| + return shared_memory_handle_sizes_; |
| + } |
| + |
| + // The requests for memory, segmented as described above, along with their |
| + // destination browser indexes and offsets. |
| + const std::vector<RendererMemoryItemRequest>& requests() const { |
| + return requests_; |
| + } |
| + |
| + // A BlobDataBuilder which can be used to construct the Blob in the |
| + // BlobStorageContext object after: |
| + // * The bytes items from AppendFutureData are populated by |
| + // PopulateFutureData. |
| + // * The temporary files from AppendFutureFile are populated by |
| + // PopulateFutureFile. |
| + BlobDataBuilder* blob_builder() { return builder_.get(); } |
| + |
| + // The total bytes size of memory items in the blob. |
| + uint64_t total_bytes_size() const { return total_bytes_size_; } |
| + |
| + Error error() const { return error_; } |
|
michaeln
2015/11/25 21:23:59
Given how this data member is used, maybe have Ini
|
| + |
| + static bool ShouldBeShortcut(const std::vector<DataElement>& items, |
| + size_t memory_available); |
| + |
| + protected: |
| + friend class FileStorageStrategy; |
| + friend class SharedMemoryStorageStrategy; |
| + |
| + template <typename SizeType> |
| + class BlobSegmentVisitor { |
| + public: |
| + virtual ~BlobSegmentVisitor(){}; |
| + virtual void VisitBytesSegment(size_t element_index, |
| + SizeType element_offset, |
| + size_t segment_index, |
| + SizeType segment_offset, |
| + SizeType size) = 0; |
| + virtual void VisitNonBytesSegment(const DataElement& element, |
| + size_t element_index) = 0; |
| + virtual void Done() = 0; |
| + }; |
| + |
| + // This iterates of the data elements and segments the 'bytes' data into |
| + // the smallest number of segments given the max_segment_size. |
| + // The callback describes either: |
| + // * A non-memory item |
| + // * A partition of a bytes element which will be populated into a given |
| + // segment and segment offset. |
| + // Assumptions: All memory items are consolidated. As in, there are no two |
| + // 'bytes' items next to eachother. |
| + template <typename SizeType> |
| + static void ForEachWithSegment(const std::vector<DataElement>& items, |
| + SizeType max_segment_size, |
| + BlobSegmentVisitor<SizeType>* visitor); |
| + |
| + private: |
| + template <typename SizeType> |
| + static void ComputeHandleSizes(SizeType total_memory_size, |
| + SizeType max_segment_size, |
| + std::vector<SizeType>* segment_sizes); |
| + |
| + Error error_; |
| + |
| + std::vector<uint64_t> file_handle_sizes_; |
| + std::vector<size_t> shared_memory_handle_sizes_; |
| + |
| + uint64_t total_bytes_size_; |
| + std::vector<RendererMemoryItemRequest> requests_; |
| + scoped_ptr<BlobDataBuilder> builder_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlobAsyncTransportStrategy); |
| +}; |
| + |
| +} // namespace storage |
| + |
| +#endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_TRANSPORT_STRATEGY_H_ |