Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / storage / browser / blob / blob_data_item.h
blob5b25db4db0f1b60990e47c54c23048250d11f1ea
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef STORAGE_BROWSER_BLOB_BLOB_DATA_ITEM_H_
6 #define STORAGE_BROWSER_BLOB_BLOB_DATA_ITEM_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "storage/browser/storage_browser_export.h"
11 #include "storage/common/data_element.h"
13 namespace disk_cache {
14 class Entry;
17 namespace storage {
18 class BlobDataBuilder;
19 class BlobStorageContext;
21 // Ref counted blob item. This class owns the backing data of the blob item. The
22 // backing data is immutable, and cannot change after creation. The purpose of
23 // this class is to allow the resource to stick around in the snapshot even
24 // after the resource was swapped in the blob (either to disk or to memory) by
25 // the BlobStorageContext.
26 class STORAGE_EXPORT BlobDataItem : public base::RefCounted<BlobDataItem> {
27 public:
28 // The DataHandle class is used to persist resources that are needed for
29 // reading this BlobDataItem. This object will stay around while any reads are
30 // pending. If all blobs with this item are deleted or the item is swapped for
31 // a different backend version (mem-to-disk or the reverse), then the item
32 // will be destructed after all pending reads are complete.
33 class STORAGE_EXPORT DataHandle : public base::RefCounted<DataHandle> {
34 protected:
35 virtual ~DataHandle() = 0;
37 private:
38 friend class base::RefCounted<DataHandle>;
41 DataElement::Type type() const { return item_->type(); }
42 const char* bytes() const { return item_->bytes(); }
43 const base::FilePath& path() const { return item_->path(); }
44 const GURL& filesystem_url() const { return item_->filesystem_url(); }
45 const std::string& blob_uuid() const { return item_->blob_uuid(); }
46 uint64 offset() const { return item_->offset(); }
47 uint64 length() const { return item_->length(); }
48 const base::Time& expected_modification_time() const {
49 return item_->expected_modification_time();
51 const DataElement& data_element() const { return *item_; }
52 const DataElement* data_element_ptr() const { return item_.get(); }
54 disk_cache::Entry* disk_cache_entry() const { return disk_cache_entry_; }
55 int disk_cache_stream_index() const { return disk_cache_stream_index_; }
57 private:
58 friend class BlobDataBuilder;
59 friend class BlobStorageContext;
60 friend class base::RefCounted<BlobDataItem>;
62 BlobDataItem(scoped_ptr<DataElement> item);
63 BlobDataItem(scoped_ptr<DataElement> item,
64 const scoped_refptr<DataHandle>& data_handle);
65 BlobDataItem(scoped_ptr<DataElement> item,
66 const scoped_refptr<DataHandle>& data_handle,
67 disk_cache::Entry* entry,
68 int disk_cache_stream_index_);
69 virtual ~BlobDataItem();
71 scoped_ptr<DataElement> item_;
72 scoped_refptr<DataHandle> data_handle_;
74 // This naked pointer is safe because the scope is protected by the DataHandle
75 // instance for disk cache entries during the lifetime of this BlobDataItem.
76 disk_cache::Entry* disk_cache_entry_;
77 int disk_cache_stream_index_; // For TYPE_DISK_CACHE_ENTRY.
80 #if defined(UNIT_TEST)
81 inline bool operator==(const BlobDataItem& a, const BlobDataItem& b) {
82 return a.disk_cache_entry() == b.disk_cache_entry() &&
83 a.disk_cache_stream_index() == b.disk_cache_stream_index() &&
84 a.data_element() == b.data_element();
87 inline bool operator!=(const BlobDataItem& a, const BlobDataItem& b) {
88 return !(a == b);
90 #endif // defined(UNIT_TEST)
92 } // namespace storage
94 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_ITEM_H_