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 EARLY_ABORT, | |
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 // The visitor is guaranteed to be called before this method returns. |
kinuko
2016/04/15 15:02:32
Now this comment doesn't give enough info about wh
dmurph
2016/04/20 21:15:34
Done.
| |
95 // * ReadStatus::ERROR if the state or arguments are invalid (see error log), | 98 // * ReadStatus::ERROR if the state or arguments are invalid (see error log), |
96 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, | 99 // * 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, | 100 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid, |
101 // * ReadStatus::EARLY_ABORT if the visitor returns false before we're done, | |
102 // * ReadStatus::DONE if the memory has been successfully visited. | |
103 ReadStatus VisitMemory( | |
104 size_t consolidated_item_index, | |
105 size_t consolidated_offset, | |
106 size_t consolidated_size, | |
107 base::Callback<bool(size_t /* total_memory_read */, | |
108 const char* /* memory */, | |
109 size_t /* memory_size */)> visitor) const; | |
110 | |
111 // Reads memory from the given item into the given buffer. This is a simple | |
112 // wrapper of VisitMemory. Returns: | |
113 // * ReadStatus::ERROR if the state or arguments are invalid (see error log), | |
114 // * ReadStatus::ERROR_WRONG_TYPE if the item at the index isn't memory, | |
115 // * ReadStatus::ERROR_OUT_OF_BOUNDS if index, offset, or size are invalid, | |
98 // * ReadStatus::DONE if the memory has been successfully read. | 116 // * ReadStatus::DONE if the memory has been successfully read. |
kinuko
2016/04/15 15:02:32
For some of these results we probably don't need t
dmurph
2016/04/20 21:15:34
Done.
| |
99 // Precondition: memory_out must be a valid pointer to memory with a size of | 117 // Precondition: memory_out must be a valid pointer to memory with a size of |
100 // at least consolidated_size. | 118 // at least consolidated_size. |
101 ReadStatus ReadMemory(size_t consolidated_item_index, | 119 ReadStatus ReadMemory(size_t consolidated_item_index, |
102 size_t consolidated_offset, | 120 size_t consolidated_offset, |
103 size_t consolidated_size, | 121 size_t consolidated_size, |
104 void* memory_out); | 122 void* memory_out) const; |
105 | 123 |
106 private: | 124 private: |
125 friend class base::RefCountedThreadSafe<BlobConsolidation>; | |
126 ~BlobConsolidation(); | |
127 | |
107 size_t total_memory_; | 128 size_t total_memory_; |
108 std::set<std::string> referenced_blobs_; | 129 std::set<std::string> referenced_blobs_; |
109 std::vector<ConsolidatedItem> consolidated_items_; | 130 std::vector<ConsolidatedItem> consolidated_items_; |
110 | 131 |
111 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); | 132 DISALLOW_COPY_AND_ASSIGN(BlobConsolidation); |
112 }; | 133 }; |
113 | 134 |
114 } // namespace content | 135 } // namespace content |
115 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ | 136 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_CONSOLIDATION_H_ |
OLD | NEW |