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 <limits> |
8 #include <memory> | 9 #include <memory> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/supports_user_data.h" | 16 #include "base/supports_user_data.h" |
16 #include "storage/browser/storage_browser_export.h" | 17 #include "storage/browser/storage_browser_export.h" |
17 #include "storage/common/blob_storage/blob_storage_constants.h" | 18 #include "storage/common/blob_storage/blob_storage_constants.h" |
(...skipping 14 matching lines...) Expand all Loading... |
32 // that needs to keep a blob alive needs to store this handle. | 33 // that needs to keep a blob alive needs to store this handle. |
33 // When the blob data itself is needed, clients must call the CreateSnapshot() | 34 // When the blob data itself is needed, clients must call the CreateSnapshot() |
34 // method on the IO thread to create a snapshot of the blob data. This snapshot | 35 // method on the IO thread to create a snapshot of the blob data. This snapshot |
35 // is not intended to be persisted, and serves to ensure that the backing | 36 // is not intended to be persisted, and serves to ensure that the backing |
36 // resources remain around for the duration of reading the blob. This snapshot | 37 // resources remain around for the duration of reading the blob. This snapshot |
37 // can be read on any thread, but it must be destructed on the IO thread. | 38 // can be read on any thread, but it must be destructed on the IO thread. |
38 // This object has delete semantics and may be deleted on any thread. | 39 // This object has delete semantics and may be deleted on any thread. |
39 class STORAGE_EXPORT BlobDataHandle | 40 class STORAGE_EXPORT BlobDataHandle |
40 : public base::SupportsUserData::Data { | 41 : public base::SupportsUserData::Data { |
41 public: | 42 public: |
42 // True means the blob was constructed successfully, and false means that | 43 static constexpr uint64_t kUnknownSize = std::numeric_limits<uint64_t>::max(); |
43 // there was an error, which is reported in the second argument. | |
44 using BlobConstructedCallback = | |
45 base::Callback<void(bool, IPCBlobCreationCancelCode)>; | |
46 | 44 |
47 BlobDataHandle(const BlobDataHandle& other); // May be copied on any thread. | 45 BlobDataHandle(const BlobDataHandle& other); // May be copied on any thread. |
48 ~BlobDataHandle() override; // May be deleted on any thread. | 46 ~BlobDataHandle() override; // May be deleted on any thread. |
49 | 47 |
50 // Returns if this blob is still constructing. If so, one can use the | 48 // Returns if this blob is still constructing. If so, one can use the |
51 // RunOnConstructionComplete to wait. | 49 // RunOnConstructionComplete to wait. |
52 // Must be called on IO thread. | 50 // Must be called on IO thread. |
53 bool IsBeingBuilt() const; | 51 bool IsBeingBuilt() const; |
54 | 52 |
55 // Returns if this blob is broken, and there is no data associated with it. | 53 // Returns if this blob is broken, and there is no data associated with it. |
56 // Must be called on IO thread. | 54 // Must be called on IO thread. |
57 bool IsBroken() const; | 55 bool IsBroken() const; |
58 | 56 |
| 57 // Returns the broken reason if this blob is broken. |
| 58 // Must be called on IO thread. |
| 59 BlobStatus GetBlobStatus() const; |
| 60 |
59 // The callback will be run on the IO thread when construction of the blob | 61 // The callback will be run on the IO thread when construction of the blob |
60 // is complete. If construction is already complete, then the task is run | 62 // is complete. If construction is already complete, then the task is run |
61 // immediately on the current message loop (i.e. IO thread). | 63 // immediately on the current message loop (i.e. IO thread). |
62 // Must be called on IO thread. Returns if construction successful. | 64 // Must be called on IO thread. Returns if construction successful. |
63 // Calling this multiple times results in registering multiple | 65 // Calling this multiple times results in registering multiple |
64 // completion callbacks. | 66 // completion callbacks. |
65 void RunOnConstructionComplete(const BlobConstructedCallback& done); | 67 void RunOnConstructionComplete(const BlobStatusCallback& done); |
66 | 68 |
67 // A BlobReader is used to read the data from the blob. This object is | 69 // A BlobReader is used to read the data from the blob. This object is |
68 // intended to be transient and should not be stored for any extended period | 70 // intended to be transient and should not be stored for any extended period |
69 // of time. | 71 // of time. |
70 std::unique_ptr<BlobReader> CreateReader( | 72 std::unique_ptr<BlobReader> CreateReader( |
71 FileSystemContext* file_system_context, | 73 FileSystemContext* file_system_context, |
72 base::SequencedTaskRunner* file_task_runner) const; | 74 base::SequencedTaskRunner* file_task_runner) const; |
73 | 75 |
74 // May be accessed on any thread. | 76 // May be accessed on any thread. |
75 const std::string& uuid() const; | 77 const std::string& uuid() const; |
76 // May be accessed on any thread. | 78 // May be accessed on any thread. |
77 const std::string& content_type() const; | 79 const std::string& content_type() const; |
78 // May be accessed on any thread. | 80 // May be accessed on any thread. |
79 const std::string& content_disposition() const; | 81 const std::string& content_disposition() const; |
| 82 // May be accessed on any thread. In rare cases where the blob is created |
| 83 // as a file from javascript, this will be kUnknownSize. |
| 84 uint64_t size() const; |
80 | 85 |
81 // This call and the destruction of the returned snapshot must be called | 86 // This call and the destruction of the returned snapshot must be called |
82 // on the IO thread. If the blob is broken, then we return a nullptr here. | 87 // on the IO thread. If the blob is broken, then we return a nullptr here. |
83 // Please do not call this, and use CreateReader instead. It appropriately | 88 // Please do not call this, and use CreateReader instead. It appropriately |
84 // waits until the blob is built before having a size (see CalculateSize). | 89 // waits until the blob is built before having a size (see CalculateSize). |
85 // TODO(dmurph): Make this protected, where only the BlobReader can call it. | 90 // TODO(dmurph): Make this protected, where only the BlobReader can call it. |
86 std::unique_ptr<BlobDataSnapshot> CreateSnapshot() const; | 91 std::unique_ptr<BlobDataSnapshot> CreateSnapshot() const; |
87 | 92 |
88 private: | 93 private: |
89 // Internal class whose destructor is guarenteed to be called on the IO | 94 // Internal class whose destructor is guarenteed to be called on the IO |
90 // thread. | 95 // thread. |
91 class BlobDataHandleShared | 96 class BlobDataHandleShared |
92 : public base::RefCountedThreadSafe<BlobDataHandleShared> { | 97 : public base::RefCountedThreadSafe<BlobDataHandleShared> { |
93 public: | 98 public: |
94 BlobDataHandleShared(const std::string& uuid, | 99 BlobDataHandleShared(const std::string& uuid, |
95 const std::string& content_type, | 100 const std::string& content_type, |
96 const std::string& content_disposition, | 101 const std::string& content_disposition, |
| 102 uint64_t size, |
97 BlobStorageContext* context); | 103 BlobStorageContext* context); |
98 | 104 |
99 private: | 105 private: |
100 friend class base::DeleteHelper<BlobDataHandleShared>; | 106 friend class base::DeleteHelper<BlobDataHandleShared>; |
101 friend class base::RefCountedThreadSafe<BlobDataHandleShared>; | 107 friend class base::RefCountedThreadSafe<BlobDataHandleShared>; |
102 friend class BlobDataHandle; | 108 friend class BlobDataHandle; |
103 | 109 |
104 virtual ~BlobDataHandleShared(); | 110 virtual ~BlobDataHandleShared(); |
105 | 111 |
106 const std::string uuid_; | 112 const std::string uuid_; |
107 const std::string content_type_; | 113 const std::string content_type_; |
108 const std::string content_disposition_; | 114 const std::string content_disposition_; |
| 115 const uint64_t size_; |
109 base::WeakPtr<BlobStorageContext> context_; | 116 base::WeakPtr<BlobStorageContext> context_; |
110 | 117 |
111 DISALLOW_COPY_AND_ASSIGN(BlobDataHandleShared); | 118 DISALLOW_COPY_AND_ASSIGN(BlobDataHandleShared); |
112 }; | 119 }; |
113 | 120 |
114 friend class BlobStorageContext; | 121 friend class BlobStorageContext; |
115 BlobDataHandle(const std::string& uuid, | 122 BlobDataHandle(const std::string& uuid, |
116 const std::string& content_type, | 123 const std::string& content_type, |
117 const std::string& content_disposition, | 124 const std::string& content_disposition, |
| 125 uint64_t size, |
118 BlobStorageContext* context, | 126 BlobStorageContext* context, |
119 base::SequencedTaskRunner* io_task_runner); | 127 base::SequencedTaskRunner* io_task_runner); |
120 | 128 |
121 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 129 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
122 scoped_refptr<BlobDataHandleShared> shared_; | 130 scoped_refptr<BlobDataHandleShared> shared_; |
123 }; | 131 }; |
124 | 132 |
125 } // namespace storage | 133 } // namespace storage |
126 | 134 |
127 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ | 135 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ |
OLD | NEW |