OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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_BLOB_DATA_HANDLE_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ |
6 #define STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/supports_user_data.h" | 12 #include "base/supports_user_data.h" |
13 #include "storage/browser/storage_browser_export.h" | 13 #include "storage/browser/storage_browser_export.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 class SequencedTaskRunner; | 16 class SequencedTaskRunner; |
17 } | 17 } |
18 | 18 |
19 namespace storage { | 19 namespace storage { |
20 | 20 |
21 class BlobData; | 21 class BlobDataSnapshot; |
22 class BlobStorageContext; | 22 class BlobStorageContext; |
23 | 23 |
24 // A scoper object for use in chrome's main browser process, ensures | 24 // BlobDataHandle ensures that the underlying blob (keyed by the uuid) remains |
25 // the underlying BlobData and its uuid remain in BlobStorageContext's | 25 // in the BlobStorageContext's collection while this object is alive. Anything |
26 // collection for the duration. This object has delete semantics and | 26 // that needs to keep a blob alive needs to store this handle. |
27 // maybe deleted on any thread. | 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. |
28 class STORAGE_EXPORT BlobDataHandle | 33 class STORAGE_EXPORT BlobDataHandle |
29 : public base::SupportsUserData::Data { | 34 : public base::SupportsUserData::Data { |
30 public: | 35 public: |
31 BlobDataHandle(const BlobDataHandle& other); // May be copied on any thread. | 36 BlobDataHandle(const BlobDataHandle& other); // May be copied on any thread. |
32 ~BlobDataHandle() override; // Maybe be deleted on any thread. | 37 ~BlobDataHandle() override; // May be deleted on any thread. |
33 BlobData* data() const; // May only be accessed on the IO thread. | |
34 | 38 |
35 std::string uuid() const; // May be accessed 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 |
| 41 // of time. |
| 42 // This call and the destruction of the returned snapshot must be called |
| 43 // on the IO thread. |
| 44 scoped_ptr<BlobDataSnapshot> CreateSnapshot() const; |
| 45 |
| 46 const std::string& uuid() const; // May be accessed on any thread. |
36 | 47 |
37 private: | 48 private: |
| 49 // Internal class whose destructor is guarenteed to be called on the IO |
| 50 // thread. |
38 class BlobDataHandleShared | 51 class BlobDataHandleShared |
39 : public base::RefCountedThreadSafe<BlobDataHandleShared> { | 52 : public base::RefCountedThreadSafe<BlobDataHandleShared> { |
40 public: | 53 public: |
41 BlobDataHandleShared(BlobData* blob_data, | 54 BlobDataHandleShared(const std::string& uuid, |
42 BlobStorageContext* context, | 55 BlobStorageContext* context, |
43 base::SequencedTaskRunner* task_runner); | 56 base::SequencedTaskRunner* task_runner); |
44 | 57 |
45 BlobData* data() const; | 58 scoped_ptr<BlobDataSnapshot> CreateSnapshot() const; |
46 const std::string& uuid() const; | 59 const std::string& uuid() const; |
47 | 60 |
48 private: | 61 private: |
49 friend class base::DeleteHelper<BlobDataHandleShared>; | 62 friend class base::DeleteHelper<BlobDataHandleShared>; |
50 friend class base::RefCountedThreadSafe<BlobDataHandleShared>; | 63 friend class base::RefCountedThreadSafe<BlobDataHandleShared>; |
51 friend class BlobDataHandle; | 64 friend class BlobDataHandle; |
52 | 65 |
53 virtual ~BlobDataHandleShared(); | 66 virtual ~BlobDataHandleShared(); |
54 | 67 |
55 scoped_refptr<BlobData> blob_data_; | 68 const std::string uuid_; |
56 base::WeakPtr<BlobStorageContext> context_; | 69 base::WeakPtr<BlobStorageContext> context_; |
57 | 70 |
58 DISALLOW_COPY_AND_ASSIGN(BlobDataHandleShared); | 71 DISALLOW_COPY_AND_ASSIGN(BlobDataHandleShared); |
59 }; | 72 }; |
60 | 73 |
61 friend class BlobStorageContext; | 74 friend class BlobStorageContext; |
62 BlobDataHandle(BlobData* blob_data, BlobStorageContext* context, | 75 BlobDataHandle(const std::string& uuid, |
| 76 BlobStorageContext* context, |
63 base::SequencedTaskRunner* task_runner); | 77 base::SequencedTaskRunner* task_runner); |
64 | 78 |
65 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 79 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
66 scoped_refptr<BlobDataHandleShared> shared_; | 80 scoped_refptr<BlobDataHandleShared> shared_; |
67 }; | 81 }; |
68 | 82 |
69 } // namespace storage | 83 } // namespace storage |
70 | 84 |
71 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ | 85 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ |
OLD | NEW |