Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / child / blob_storage / blob_consolidation.h
blobb47352217402dea0fa0b4354d082d8253d95505a
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.
5 #ifndef CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_
6 #define CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_
8 #include <string>
9 #include <vector>
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "content/common/content_export.h"
15 #include "storage/common/data_element.h"
16 #include "third_party/WebKit/public/platform/WebThreadSafeData.h"
18 namespace content {
20 // This class facilitates the consolidation of memory items in blobs. No memory
21 // is copied to store items in this object. Instead, the memory is copied into
22 // the external char* array given to the ReadMemory method.
23 // Features:
24 // * Add_Item methods for building the blob.
25 // * consolidated_items for getting the consolidated items. This is
26 // used to describe the blob to the browser.
27 // * total_memory to get the total memory size of the blob.
28 // * ReadMemory for reading arbitrary memory from any consolidated item.
30 // NOTE: this class does not do memory accounting or garbage collecting. The
31 // memory for the blob sticks around until this class is destructed.
32 class CONTENT_EXPORT BlobConsolidation {
33 public:
34 enum class ReadStatus {
35 ERROR_UNKNOWN,
36 ERROR_WRONG_TYPE,
37 ERROR_OUT_OF_BOUNDS,
40 struct ConsolidatedItem {
41 ConsolidatedItem();
42 ConsolidatedItem(storage::DataElement::Type type,
43 uint64_t offset,
44 uint64_t length);
45 ~ConsolidatedItem();
47 storage::DataElement::Type type;
48 uint64_t offset;
49 uint64_t length;
51 base::FilePath path; // For TYPE_FILE.
52 GURL filesystem_url; // For TYPE_FILE_FILESYSTEM.
53 double expected_modification_time; // For TYPE_FILE, TYPE_FILE_FILESYSTEM.
54 std::string blob_uuid; // For TYPE_BLOB.
55 // Only populated if len(items) > 1. Used for binary search.
56 // Since the offset of the first item is always 0, we exclude this.
57 std::vector<size_t> offsets; // For TYPE_BYTES.
58 std::vector<blink::WebThreadSafeData> data; // For TYPE_BYTES.
61 BlobConsolidation();
62 ~BlobConsolidation();
64 void AddDataItem(const blink::WebThreadSafeData& data);
65 void AddFileItem(const base::FilePath& path,
66 uint64_t offset,
67 uint64_t length,
68 double expected_modification_time);
69 void AddBlobItem(const std::string& uuid, uint64_t offset, uint64_t length);
70 void AddFileSystemItem(const GURL& url,
71 uint64_t offset,
72 uint64_t length,
73 double expected_modification_time);
75 // These are the resulting consolidated items, constructed from the Add*
76 // methods. This configuration is used to describe the data to the browser,
77 // even though one consolidated memory items can contain multiple data parts.
78 const std::vector<ConsolidatedItem>& consolidated_items() const {
79 return consolidated_items_;
82 size_t total_memory() const { return total_memory_; }
84 // Reads memory from the given item into the given buffer. Returns:
85 // * ReadStatus::ERROR if the state or arguments are invalid (see error log),
86 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory,
87 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid,
88 // * ReadStatus::DONE if the memory has been successfully read.
89 // Precondition: memory_out must be a valid pointer to memory with a size of
90 // at least consolidated_size.
91 ReadStatus ReadMemory(size_t consolidated_item_index,
92 size_t consolidated_offset,
93 size_t consolidated_size,
94 void* memory_out);
96 private:
97 size_t total_memory_;
98 std::vector<ConsolidatedItem> consolidated_items_;
100 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation);
103 } // namespace content
104 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_