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 }; |
44 struct ConsolidatedItem { | 48 struct ConsolidatedItem { |
45 ConsolidatedItem(); | 49 ConsolidatedItem(); |
46 ConsolidatedItem(storage::DataElement::Type type, | 50 ConsolidatedItem(storage::DataElement::Type type, |
47 uint64_t offset, | 51 uint64_t offset, |
48 uint64_t length); | 52 uint64_t length); |
49 ConsolidatedItem(const ConsolidatedItem& other); | 53 ConsolidatedItem(const ConsolidatedItem& other); |
50 ~ConsolidatedItem(); | 54 ~ConsolidatedItem(); |
51 | 55 |
52 storage::DataElement::Type type; | 56 storage::DataElement::Type type; |
53 uint64_t offset; | 57 uint64_t offset; |
54 uint64_t length; | 58 uint64_t length; |
55 | 59 |
56 base::FilePath path; // For TYPE_FILE. | 60 base::FilePath path; // For TYPE_FILE. |
57 GURL filesystem_url; // For TYPE_FILE_FILESYSTEM. | 61 GURL filesystem_url; // For TYPE_FILE_FILESYSTEM. |
58 double expected_modification_time; // For TYPE_FILE, TYPE_FILE_FILESYSTEM. | 62 double expected_modification_time; // For TYPE_FILE, TYPE_FILE_FILESYSTEM. |
59 std::string blob_uuid; // For TYPE_BLOB. | 63 std::string blob_uuid; // For TYPE_BLOB. |
60 // Only populated if len(items) > 1. Used for binary search. | 64 // Only populated if len(items) > 1. Used for binary search. |
61 // Since the offset of the first item is always 0, we exclude this. | 65 // Since the offset of the first item is always 0, we exclude this. |
62 std::vector<size_t> offsets; // For TYPE_BYTES. | 66 std::vector<size_t> offsets; // For TYPE_BYTES. |
63 std::vector<blink::WebThreadSafeData> data; // For TYPE_BYTES. | 67 std::vector<blink::WebThreadSafeData> data; // For TYPE_BYTES. |
64 }; | 68 }; |
65 | 69 |
66 BlobConsolidation(); | 70 BlobConsolidation(); |
67 ~BlobConsolidation(); | |
68 | 71 |
69 void AddDataItem(const blink::WebThreadSafeData& data); | 72 void AddDataItem(const blink::WebThreadSafeData& data); |
70 void AddFileItem(const base::FilePath& path, | 73 void AddFileItem(const base::FilePath& path, |
71 uint64_t offset, | 74 uint64_t offset, |
72 uint64_t length, | 75 uint64_t length, |
73 double expected_modification_time); | 76 double expected_modification_time); |
74 void AddBlobItem(const std::string& uuid, uint64_t offset, uint64_t length); | 77 void AddBlobItem(const std::string& uuid, uint64_t offset, uint64_t length); |
75 void AddFileSystemItem(const GURL& url, | 78 void AddFileSystemItem(const GURL& url, |
76 uint64_t offset, | 79 uint64_t offset, |
77 uint64_t length, | 80 uint64_t length, |
78 double expected_modification_time); | 81 double expected_modification_time); |
79 | 82 |
80 // These are the resulting consolidated items, constructed from the Add* | 83 // These are the resulting consolidated items, constructed from the Add* |
81 // methods. This configuration is used to describe the data to the browser, | 84 // methods. This configuration is used to describe the data to the browser, |
82 // even though one consolidated memory items can contain multiple data parts. | 85 // even though one consolidated memory items can contain multiple data parts. |
83 const std::vector<ConsolidatedItem>& consolidated_items() const { | 86 const std::vector<ConsolidatedItem>& consolidated_items() const { |
84 return consolidated_items_; | 87 return consolidated_items_; |
85 } | 88 } |
86 | 89 |
87 // These are all of the blobs referenced in the construction of this blob. | 90 // These are all of the blobs referenced in the construction of this blob. |
88 const std::set<std::string> referenced_blobs() const { | 91 const std::set<std::string> referenced_blobs() const { |
89 return referenced_blobs_; | 92 return referenced_blobs_; |
90 } | 93 } |
91 | 94 |
92 size_t total_memory() const { return total_memory_; } | 95 size_t total_memory() const { return total_memory_; } |
93 | 96 |
94 // Reads memory from the given item into the given buffer. Returns: | 97 // This method calls the |visitor| callback with the given memory item, |
95 // * ReadStatus::ERROR if the state or arguments are invalid (see error log), | 98 // offset, and size. Since items are consolidated, |visitor| can be called |
99 // multiple times. The return value of |visitor| determines if we should | |
100 // continue reading memory, where 'false' triggers an early return and we | |
101 // return EARLY_ABORT. |visitor| is guaranteed to be called only during this | |
102 // method call. | |
103 // Returns: | |
104 // * ReadStatus::ERROR_UNKNOWN if the state or arguments are invalid, | |
96 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, | 105 // * 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, | 106 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid, |
98 // * ReadStatus::DONE if the memory has been successfully read. | 107 // * ReadStatus::CANCELLED_BY_VISITOR if the visitor returns false before |
108 // completion, and | |
109 // * ReadStatus::DONE if the memory has been successfully visited. | |
110 ReadStatus VisitMemory( | |
111 size_t consolidated_item_index, | |
112 size_t consolidated_offset, | |
113 size_t consolidated_size, | |
114 base::Callback<bool(const char* /* memory */, size_t /* memory_size */)> | |
michaeln
2016/04/28 00:16:56
maybe spring for a VisitorFunction using/typedef
dmurph
2016/05/09 19:55:47
Done.
| |
115 visitor) const; | |
116 | |
117 // Reads memory from the given item into the given buffer. This is a simple | |
118 // wrapper of VisitMemory. Returns the same values as VisitMemory, except we | |
119 // don't return CANCELLED_BY_VISITOR. | |
99 // Precondition: memory_out must be a valid pointer to memory with a size of | 120 // Precondition: memory_out must be a valid pointer to memory with a size of |
100 // at least consolidated_size. | 121 // at least consolidated_size. |
101 ReadStatus ReadMemory(size_t consolidated_item_index, | 122 ReadStatus ReadMemory(size_t consolidated_item_index, |
102 size_t consolidated_offset, | 123 size_t consolidated_offset, |
103 size_t consolidated_size, | 124 size_t consolidated_size, |
104 void* memory_out); | 125 void* memory_out) const; |
105 | 126 |
106 private: | 127 private: |
128 friend class base::RefCountedThreadSafe<BlobConsolidation>; | |
129 ~BlobConsolidation(); | |
130 | |
107 size_t total_memory_; | 131 size_t total_memory_; |
108 std::set<std::string> referenced_blobs_; | 132 std::set<std::string> referenced_blobs_; |
109 std::vector<ConsolidatedItem> consolidated_items_; | 133 std::vector<ConsolidatedItem> consolidated_items_; |
110 | 134 |
111 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); | 135 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); |
112 }; | 136 }; |
113 | 137 |
114 } // namespace content | 138 } // namespace content |
115 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ | 139 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
OLD | NEW |