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 CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ | 5 #ifndef CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
6 #define CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ | 6 #define CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <set> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
| 16 #include "base/callback_forward.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/ref_counted.h" |
18 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
19 #include "storage/common/data_element.h" | 21 #include "storage/common/data_element.h" |
20 #include "third_party/WebKit/public/platform/WebThreadSafeData.h" | 22 #include "third_party/WebKit/public/platform/WebThreadSafeData.h" |
21 | 23 |
22 namespace content { | 24 namespace content { |
23 | 25 |
24 // This class facilitates the consolidation of memory items in blobs. No memory | 26 // This class facilitates the consolidation of memory items in blobs. No memory |
25 // is copied to store items in this object. Instead, the memory is copied into | 27 // is copied to store items in this object. Instead, the memory is copied into |
26 // the external char* array given to the ReadMemory method. | 28 // the external char* array given to the ReadMemory method. |
27 // Features: | 29 // Features: |
28 // * Add_Item methods for building the blob. | 30 // * Add_Item methods for building the blob. |
29 // * consolidated_items for getting the consolidated items. This is | 31 // * consolidated_items for getting the consolidated items. This is |
30 // used to describe the blob to the browser. | 32 // used to describe the blob to the browser. |
31 // * total_memory to get the total memory size of the blob. | 33 // * total_memory to get the total memory size of the blob. |
32 // * ReadMemory for reading arbitrary memory from any consolidated item. | 34 // * ReadMemory for reading arbitrary memory from any consolidated item. |
33 // | 35 // |
34 // NOTE: this class does not do memory accounting or garbage collecting. The | 36 // NOTE: this class does not do memory accounting or garbage collecting. The |
35 // memory for the blob sticks around until this class is destructed. | 37 // memory for the blob sticks around until this class is destructed. |
36 class CONTENT_EXPORT BlobConsolidation { | 38 class CONTENT_EXPORT BlobConsolidation |
| 39 : public base::RefCountedThreadSafe<BlobConsolidation> { |
37 public: | 40 public: |
38 enum class ReadStatus { | 41 enum class ReadStatus { |
39 ERROR_UNKNOWN, | 42 ERROR_UNKNOWN, |
40 ERROR_WRONG_TYPE, | 43 ERROR_WRONG_TYPE, |
41 ERROR_OUT_OF_BOUNDS, | 44 ERROR_OUT_OF_BOUNDS, |
| 45 CANCELLED_BY_VISITOR, |
42 OK | 46 OK |
43 }; | 47 }; |
| 48 |
44 struct ConsolidatedItem { | 49 struct ConsolidatedItem { |
45 ConsolidatedItem(); | 50 ConsolidatedItem(); |
46 ConsolidatedItem(storage::DataElement::Type type, | 51 ConsolidatedItem(storage::DataElement::Type type, |
47 uint64_t offset, | 52 uint64_t offset, |
48 uint64_t length); | 53 uint64_t length); |
49 ConsolidatedItem(const ConsolidatedItem& other); | 54 ConsolidatedItem(const ConsolidatedItem& other); |
50 ~ConsolidatedItem(); | 55 ~ConsolidatedItem(); |
51 | 56 |
52 storage::DataElement::Type type; | 57 storage::DataElement::Type type; |
53 uint64_t offset; | 58 uint64_t offset; |
54 uint64_t length; | 59 uint64_t length; |
55 | 60 |
56 base::FilePath path; // For TYPE_FILE. | 61 base::FilePath path; // For TYPE_FILE. |
57 GURL filesystem_url; // For TYPE_FILE_FILESYSTEM. | 62 GURL filesystem_url; // For TYPE_FILE_FILESYSTEM. |
58 double expected_modification_time; // For TYPE_FILE, TYPE_FILE_FILESYSTEM. | 63 double expected_modification_time; // For TYPE_FILE, TYPE_FILE_FILESYSTEM. |
59 std::string blob_uuid; // For TYPE_BLOB. | 64 std::string blob_uuid; // For TYPE_BLOB. |
60 // Only populated if len(items) > 1. Used for binary search. | 65 // Only populated if len(items) > 1. Used for binary search. |
61 // Since the offset of the first item is always 0, we exclude this. | 66 // Since the offset of the first item is always 0, we exclude this. |
62 std::vector<size_t> offsets; // For TYPE_BYTES. | 67 std::vector<size_t> offsets; // For TYPE_BYTES. |
63 std::vector<blink::WebThreadSafeData> data; // For TYPE_BYTES. | 68 std::vector<blink::WebThreadSafeData> data; // For TYPE_BYTES. |
64 }; | 69 }; |
65 | 70 |
| 71 using MemoryVisitor = |
| 72 base::Callback<bool(const char* memory, size_t memory_size)>; |
| 73 |
66 BlobConsolidation(); | 74 BlobConsolidation(); |
67 ~BlobConsolidation(); | |
68 | 75 |
69 void AddDataItem(const blink::WebThreadSafeData& data); | 76 void AddDataItem(const blink::WebThreadSafeData& data); |
70 void AddFileItem(const base::FilePath& path, | 77 void AddFileItem(const base::FilePath& path, |
71 uint64_t offset, | 78 uint64_t offset, |
72 uint64_t length, | 79 uint64_t length, |
73 double expected_modification_time); | 80 double expected_modification_time); |
74 void AddBlobItem(const std::string& uuid, uint64_t offset, uint64_t length); | 81 void AddBlobItem(const std::string& uuid, uint64_t offset, uint64_t length); |
75 void AddFileSystemItem(const GURL& url, | 82 void AddFileSystemItem(const GURL& url, |
76 uint64_t offset, | 83 uint64_t offset, |
77 uint64_t length, | 84 uint64_t length, |
78 double expected_modification_time); | 85 double expected_modification_time); |
79 | 86 |
80 // These are the resulting consolidated items, constructed from the Add* | 87 // These are the resulting consolidated items, constructed from the Add* |
81 // methods. This configuration is used to describe the data to the browser, | 88 // methods. This configuration is used to describe the data to the browser, |
82 // even though one consolidated memory items can contain multiple data parts. | 89 // even though one consolidated memory items can contain multiple data parts. |
83 const std::vector<ConsolidatedItem>& consolidated_items() const { | 90 const std::vector<ConsolidatedItem>& consolidated_items() const { |
84 return consolidated_items_; | 91 return consolidated_items_; |
85 } | 92 } |
86 | 93 |
87 // These are all of the blobs referenced in the construction of this blob. | 94 // These are all of the blobs referenced in the construction of this blob. |
88 const std::set<std::string> referenced_blobs() const { | 95 const std::set<std::string> referenced_blobs() const { |
89 return referenced_blobs_; | 96 return referenced_blobs_; |
90 } | 97 } |
91 | 98 |
92 size_t total_memory() const { return total_memory_; } | 99 size_t total_memory() const { return total_memory_; } |
93 | 100 |
94 // Reads memory from the given item into the given buffer. Returns: | 101 // This method calls the |visitor| callback with the given memory item, |
95 // * ReadStatus::ERROR if the state or arguments are invalid (see error log), | 102 // offset, and size. Since items are consolidated, |visitor| can be called |
| 103 // multiple times. The return value of |visitor| determines if we should |
| 104 // continue reading memory, where 'false' triggers an early return and we |
| 105 // return EARLY_ABORT. |visitor| is guaranteed to be called only during this |
| 106 // method call. |
| 107 // Returns: |
| 108 // * ReadStatus::ERROR_UNKNOWN if the state or arguments are invalid, |
96 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, | 109 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, |
97 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid, | 110 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid, |
98 // * ReadStatus::DONE if the memory has been successfully read. | 111 // * ReadStatus::CANCELLED_BY_VISITOR if the visitor returns false before |
| 112 // completion, and |
| 113 // * ReadStatus::DONE if the memory has been successfully visited. |
| 114 ReadStatus VisitMemory(size_t consolidated_item_index, |
| 115 size_t consolidated_offset, |
| 116 size_t consolidated_size, |
| 117 const MemoryVisitor& visitor) const; |
| 118 |
| 119 // Reads memory from the given item into the given buffer. This is a simple |
| 120 // wrapper of VisitMemory. Returns the same values as VisitMemory, except we |
| 121 // don't return CANCELLED_BY_VISITOR. |
99 // Precondition: memory_out must be a valid pointer to memory with a size of | 122 // Precondition: memory_out must be a valid pointer to memory with a size of |
100 // at least consolidated_size. | 123 // at least consolidated_size. |
101 ReadStatus ReadMemory(size_t consolidated_item_index, | 124 ReadStatus ReadMemory(size_t consolidated_item_index, |
102 size_t consolidated_offset, | 125 size_t consolidated_offset, |
103 size_t consolidated_size, | 126 size_t consolidated_size, |
104 void* memory_out); | 127 void* memory_out) const; |
105 | 128 |
106 private: | 129 private: |
| 130 friend class base::RefCountedThreadSafe<BlobConsolidation>; |
| 131 ~BlobConsolidation(); |
| 132 |
107 size_t total_memory_; | 133 size_t total_memory_; |
108 std::set<std::string> referenced_blobs_; | 134 std::set<std::string> referenced_blobs_; |
109 std::vector<ConsolidatedItem> consolidated_items_; | 135 std::vector<ConsolidatedItem> consolidated_items_; |
110 | 136 |
111 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); | 137 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); |
112 }; | 138 }; |
113 | 139 |
114 } // namespace content | 140 } // namespace content |
115 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ | 141 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
OLD | NEW |