File: shareable_blob_data_item.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,561 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_
#define STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_

#include <string>

#include "base/component_export.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/hash.h"
#include "base/memory/ref_counted.h"
#include "storage/browser/blob/blob_memory_controller.h"

namespace storage {
class BlobDataItem;

// This class allows blob items to be shared between blobs.  This class contains
// both the blob data item and the uuids of all the blobs using this item.
// The data in this class (the item) is immutable, but the item itself can be
// swapped out with an item with the same data but a different backing (think
// RAM vs file backed).
// We also allow the storage of a memory quota allocation object which is used
// for memory quota reclamation.
class COMPONENT_EXPORT(STORAGE_BROWSER) ShareableBlobDataItem
    : public base::RefCounted<ShareableBlobDataItem> {
 public:
  enum State {
    // We're an item that needs quota (either disk or memory).
    QUOTA_NEEDED,
    // We have requested quota from the BlobMemoryController.
    QUOTA_REQUESTED,
    // Space has been allocated for this item in the BlobMemoryController, but
    // it may not yet be populated.
    QUOTA_GRANTED,
    // We're a populated item that needed quota.
    POPULATED_WITH_QUOTA,
    // We're a populated item that didn't need quota.
    POPULATED_WITHOUT_QUOTA
  };

  ShareableBlobDataItem(scoped_refptr<BlobDataItem> item, State state);

  ShareableBlobDataItem(const ShareableBlobDataItem&) = delete;
  ShareableBlobDataItem& operator=(const ShareableBlobDataItem&) = delete;

  const scoped_refptr<BlobDataItem>& item() const { return item_; }

  void set_item(scoped_refptr<BlobDataItem> item);

  // This is a unique auto-incrementing id assigned to this item on
  // construction. It is used to keep track of this item in an LRU data
  // structure for eviction to disk.
  uint64_t item_id() const { return item_id_; }

  State state() const { return state_; }
  void set_state(State state) { state_ = state; }

  bool IsPopulated() const {
    return state_ == POPULATED_WITH_QUOTA || state_ == POPULATED_WITHOUT_QUOTA;
  }

  bool HasGrantedQuota() const {
    return state_ == POPULATED_WITH_QUOTA || state_ == QUOTA_GRANTED;
  }

 private:
  friend class BlobMemoryController;
  friend class BlobMemoryControllerTest;
  friend class BlobStorageContext;
  friend class base::RefCounted<ShareableBlobDataItem>;
  friend COMPONENT_EXPORT(STORAGE_BROWSER) void PrintTo(
      const ShareableBlobDataItem& x,
      ::std::ostream* os);

  ~ShareableBlobDataItem();

  void set_memory_allocation(
      std::unique_ptr<BlobMemoryController::MemoryAllocation> allocation) {
    memory_allocation_ = std::move(allocation);
  }

  bool has_memory_allocation() { return static_cast<bool>(memory_allocation_); }

  // This is a unique identifier for this ShareableBlobDataItem.
  const uint64_t item_id_;
  State state_;
  scoped_refptr<BlobDataItem> item_;
  std::unique_ptr<BlobMemoryController::MemoryAllocation> memory_allocation_;
};

COMPONENT_EXPORT(STORAGE_BROWSER)
bool operator==(const ShareableBlobDataItem& a, const ShareableBlobDataItem& b);
COMPONENT_EXPORT(STORAGE_BROWSER)
bool operator!=(const ShareableBlobDataItem& a, const ShareableBlobDataItem& b);

}  // namespace storage
#endif  // STORAGE_BROWSER_BLOB_SHAREABLE_BLOB_DATA_ITEM_H_