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 STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_ |
6 #define STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_ | 6 #define STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/hash.h" | 12 #include "base/hash.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "storage/browser/blob/blob_memory_controller.h" | 15 #include "storage/browser/blob/blob_memory_controller.h" |
16 #include "storage/browser/storage_browser_export.h" | 16 #include "storage/browser/storage_browser_export.h" |
17 #include "storage/common/data_element.h" | 17 #include "storage/common/data_element.h" |
18 | 18 |
19 namespace storage { | 19 namespace storage { |
20 class BlobDataItem; | 20 class BlobDataItem; |
21 class InternalBlobData; | |
22 | 21 |
23 // This class allows blob items to be shared between blobs. This class contains | 22 // This class allows blob items to be shared between blobs. This class contains |
24 // both the blob data item and the uuids of all the blobs using this item. | 23 // both the blob data item and the uuids of all the blobs using this item. |
25 // The data in this class (the item) is immutable, but the item itself can be | 24 // The data in this class (the item) is immutable, but the item itself can be |
26 // swapped out with an item with the same data but a different backing (think | 25 // swapped out with an item with the same data but a different backing (think |
27 // RAM vs file backed). | 26 // RAM vs file backed). |
28 // We also allow the storage of a deletion closure which is used for memory | 27 // We also allow the storage of a memory quota allocation object which is used |
29 // quota reclamation. | 28 // for memory quota reclamation. |
30 class STORAGE_EXPORT ShareableBlobDataItem | 29 class STORAGE_EXPORT ShareableBlobDataItem |
31 : public base::RefCounted<ShareableBlobDataItem> { | 30 : public base::RefCounted<ShareableBlobDataItem> { |
32 public: | 31 public: |
33 enum State { | 32 enum State { |
34 // We're an item that needs quota (either disk or memory). | 33 // We're an item that needs quota (either disk or memory). |
35 QUOTA_NEEDED, | 34 QUOTA_NEEDED, |
36 // We have requested quota from the BlobMemoryController. | 35 // We have requested quota from the BlobMemoryController. |
37 QUOTA_REQUESTED, | 36 QUOTA_REQUESTED, |
38 // Space has been allocated for this item in the BlobMemoryController, but | 37 // Space has been allocated for this item in the BlobMemoryController, but |
39 // it may not yet be populated. | 38 // it may not yet be populated. |
40 QUOTA_GRANTED, | 39 QUOTA_GRANTED, |
41 // We're a populated item that needed quota. | 40 // We're a populated item that needed quota. |
42 POPULATED_WITH_QUOTA, | 41 POPULATED_WITH_QUOTA, |
43 // We're a populated item that didn't need quota. | 42 // We're a populated item that didn't need quota. |
44 POPULATED_WITHOUT_QUOTA | 43 POPULATED_WITHOUT_QUOTA |
45 }; | 44 }; |
46 | 45 |
47 ShareableBlobDataItem(const std::string& referencing_blob_uuid, | 46 ShareableBlobDataItem(scoped_refptr<BlobDataItem> item, State state); |
48 scoped_refptr<BlobDataItem> item, | |
49 State state); | |
50 | 47 |
51 const scoped_refptr<BlobDataItem>& item() const { return item_; } | 48 const scoped_refptr<BlobDataItem>& item() const { return item_; } |
| 49 |
52 void set_item(scoped_refptr<BlobDataItem> item); | 50 void set_item(scoped_refptr<BlobDataItem> item); |
53 | 51 |
54 // This is a unique auto-incrementing id assigned to this item on | 52 // This is a unique auto-incrementing id assigned to this item on |
55 // construction. It is used to keep track of this item in an LRU data | 53 // construction. It is used to keep track of this item in an LRU data |
56 // structure for eviction to disk. | 54 // structure for eviction to disk. |
57 uint64_t item_id() const { return item_id_; } | 55 uint64_t item_id() const { return item_id_; } |
58 | 56 |
59 const base::hash_set<std::string>& referencing_blobs() const { | |
60 return referencing_blobs_; | |
61 } | |
62 base::hash_set<std::string>* referencing_blobs_mutable() { | |
63 return &referencing_blobs_; | |
64 } | |
65 | |
66 State state() const { return state_; } | 57 State state() const { return state_; } |
67 void set_state(State state) { state_ = state; } | 58 void set_state(State state) { state_ = state; } |
68 | 59 |
69 bool IsPopulated() const { | 60 bool IsPopulated() const { |
70 return state_ == POPULATED_WITH_QUOTA || state_ == POPULATED_WITHOUT_QUOTA; | 61 return state_ == POPULATED_WITH_QUOTA || state_ == POPULATED_WITHOUT_QUOTA; |
71 } | 62 } |
72 | 63 |
73 bool HasGrantedQuota() const { | 64 bool HasGrantedQuota() const { |
74 return state_ == POPULATED_WITH_QUOTA || state_ == QUOTA_GRANTED; | 65 return state_ == POPULATED_WITH_QUOTA || state_ == QUOTA_GRANTED; |
75 } | 66 } |
76 | 67 |
77 private: | 68 private: |
78 friend class BlobMemoryController; | 69 friend class BlobMemoryController; |
79 friend class BlobMemoryControllerTest; | 70 friend class BlobMemoryControllerTest; |
| 71 friend class BlobStorageContext; |
80 friend class base::RefCounted<ShareableBlobDataItem>; | 72 friend class base::RefCounted<ShareableBlobDataItem>; |
81 friend STORAGE_EXPORT void PrintTo(const ShareableBlobDataItem& x, | 73 friend STORAGE_EXPORT void PrintTo(const ShareableBlobDataItem& x, |
82 ::std::ostream* os); | 74 ::std::ostream* os); |
83 | 75 |
84 ~ShareableBlobDataItem(); | 76 ~ShareableBlobDataItem(); |
85 | 77 |
86 void set_memory_allocation( | 78 void set_memory_allocation( |
87 std::unique_ptr<BlobMemoryController::MemoryAllocation> allocation) { | 79 std::unique_ptr<BlobMemoryController::MemoryAllocation> allocation) { |
88 memory_allocation_ = std::move(allocation); | 80 memory_allocation_ = std::move(allocation); |
89 } | 81 } |
90 | 82 |
| 83 bool has_memory_allocation() { return static_cast<bool>(memory_allocation_); } |
| 84 |
91 // This is a unique identifier for this ShareableBlobDataItem. | 85 // This is a unique identifier for this ShareableBlobDataItem. |
92 const uint64_t item_id_; | 86 const uint64_t item_id_; |
93 State state_; | 87 State state_; |
94 scoped_refptr<BlobDataItem> item_; | 88 scoped_refptr<BlobDataItem> item_; |
95 base::hash_set<std::string> referencing_blobs_; | |
96 std::unique_ptr<BlobMemoryController::MemoryAllocation> memory_allocation_; | 89 std::unique_ptr<BlobMemoryController::MemoryAllocation> memory_allocation_; |
97 | 90 |
98 DISALLOW_COPY_AND_ASSIGN(ShareableBlobDataItem); | 91 DISALLOW_COPY_AND_ASSIGN(ShareableBlobDataItem); |
99 }; | 92 }; |
100 | 93 |
101 STORAGE_EXPORT bool operator==(const ShareableBlobDataItem& a, | 94 STORAGE_EXPORT bool operator==(const ShareableBlobDataItem& a, |
102 const ShareableBlobDataItem& b); | 95 const ShareableBlobDataItem& b); |
103 STORAGE_EXPORT bool operator!=(const ShareableBlobDataItem& a, | 96 STORAGE_EXPORT bool operator!=(const ShareableBlobDataItem& a, |
104 const ShareableBlobDataItem& b); | 97 const ShareableBlobDataItem& b); |
105 | 98 |
106 } // namespace storage | 99 } // namespace storage |
107 #endif // STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_ | 100 #endif // STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_ |
OLD | NEW |