1 // Copyright (c) 2013 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_HANDLE_H_
6 #define STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/supports_user_data.h"
13 #include "storage/browser/storage_browser_export.h"
16 class SequencedTaskRunner
;
21 class BlobDataSnapshot
;
22 class BlobStorageContext
;
24 // BlobDataHandle ensures that the underlying blob (keyed by the uuid) remains
25 // in the BlobStorageContext's collection while this object is alive. Anything
26 // that needs to keep a blob alive needs to store this handle.
27 // When the blob data itself is needed, clients must call the CreateSnapshot()
28 // method on the IO thread to create a snapshot of the blob data. This snapshot
29 // is not intended to be persisted, and serves to ensure that the backing
30 // resources remain around for the duration of reading the blob. This snapshot
31 // can be read on any thread, but it must be destructed on the IO thread.
32 // This object has delete semantics and may be deleted on any thread.
33 class STORAGE_EXPORT BlobDataHandle
34 : public base::SupportsUserData::Data
{
36 BlobDataHandle(const BlobDataHandle
& other
); // May be copied on any thread.
37 ~BlobDataHandle() override
; // May be deleted on any thread.
39 // A BlobDataSnapshot is used to read the data from the blob. This object is
40 // intended to be transient and should not be stored for any extended period
42 // This call and the destruction of the returned snapshot must be called
44 scoped_ptr
<BlobDataSnapshot
> CreateSnapshot() const;
46 const std::string
& uuid() const; // May be accessed on any thread.
49 // Internal class whose destructor is guarenteed to be called on the IO
51 class BlobDataHandleShared
52 : public base::RefCountedThreadSafe
<BlobDataHandleShared
> {
54 BlobDataHandleShared(const std::string
& uuid
,
55 BlobStorageContext
* context
,
56 base::SequencedTaskRunner
* task_runner
);
58 scoped_ptr
<BlobDataSnapshot
> CreateSnapshot() const;
59 const std::string
& uuid() const;
62 friend class base::DeleteHelper
<BlobDataHandleShared
>;
63 friend class base::RefCountedThreadSafe
<BlobDataHandleShared
>;
64 friend class BlobDataHandle
;
66 virtual ~BlobDataHandleShared();
68 const std::string uuid_
;
69 base::WeakPtr
<BlobStorageContext
> context_
;
71 DISALLOW_COPY_AND_ASSIGN(BlobDataHandleShared
);
74 friend class BlobStorageContext
;
75 BlobDataHandle(const std::string
& uuid
,
76 BlobStorageContext
* context
,
77 base::SequencedTaskRunner
* task_runner
);
79 scoped_refptr
<base::SequencedTaskRunner
> io_task_runner_
;
80 scoped_refptr
<BlobDataHandleShared
> shared_
;
83 } // namespace storage
85 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_