| 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 // Assignment operator matching copy constructor. | 48 // Assignment operator matching copy constructor. |
| 51 BlobDataHandle& operator=(const BlobDataHandle& other); | 49 BlobDataHandle& operator=(const BlobDataHandle& other); |
| 52 | 50 |
| 53 // Returns if this blob is still constructing. If so, one can use the | 51 // Returns if this blob is still constructing. If so, one can use the |
| 54 // RunOnConstructionComplete to wait. | 52 // RunOnConstructionComplete to wait. |
| 55 // Must be called on IO thread. | 53 // Must be called on IO thread. |
| 56 bool IsBeingBuilt() const; | 54 bool IsBeingBuilt() const; |
| 57 | 55 |
| 58 // Returns if this blob is broken, and there is no data associated with it. | 56 // Returns if this blob is broken, and there is no data associated with it. |
| 59 // Must be called on IO thread. | 57 // Must be called on IO thread. |
| 60 bool IsBroken() const; | 58 bool IsBroken() const; |
| 61 | 59 |
| 60 // Returns the broken reason if this blob is broken. |
| 61 // Must be called on IO thread. |
| 62 BlobStatus GetBlobStatus() const; |
| 63 |
| 62 // The callback will be run on the IO thread when construction of the blob | 64 // The callback will be run on the IO thread when construction of the blob |
| 63 // is complete. If construction is already complete, then the task is run | 65 // is complete. If construction is already complete, then the task is run |
| 64 // immediately on the current message loop (i.e. IO thread). | 66 // immediately on the current message loop (i.e. IO thread). |
| 65 // Must be called on IO thread. Returns if construction successful. | 67 // Must be called on IO thread. Returns if construction successful. |
| 66 // Calling this multiple times results in registering multiple | 68 // Calling this multiple times results in registering multiple |
| 67 // completion callbacks. | 69 // completion callbacks. |
| 68 void RunOnConstructionComplete(const BlobConstructedCallback& done); | 70 void RunOnConstructionComplete(const BlobStatusCallback& done); |
| 69 | 71 |
| 70 // A BlobReader is used to read the data from the blob. This object is | 72 // A BlobReader is used to read the data from the blob. This object is |
| 71 // intended to be transient and should not be stored for any extended period | 73 // intended to be transient and should not be stored for any extended period |
| 72 // of time. | 74 // of time. |
| 73 std::unique_ptr<BlobReader> CreateReader( | 75 std::unique_ptr<BlobReader> CreateReader( |
| 74 FileSystemContext* file_system_context, | 76 FileSystemContext* file_system_context, |
| 75 base::SequencedTaskRunner* file_task_runner) const; | 77 base::SequencedTaskRunner* file_task_runner) const; |
| 76 | 78 |
| 77 // May be accessed on any thread. | 79 // May be accessed on any thread. |
| 78 const std::string& uuid() const; | 80 const std::string& uuid() const; |
| 79 // May be accessed on any thread. | 81 // May be accessed on any thread. |
| 80 const std::string& content_type() const; | 82 const std::string& content_type() const; |
| 81 // May be accessed on any thread. | 83 // May be accessed on any thread. |
| 82 const std::string& content_disposition() const; | 84 const std::string& content_disposition() const; |
| 85 // May be accessed on any thread. In rare cases where the blob is created |
| 86 // as a file from javascript, this will be kUnknownSize. |
| 87 uint64_t size() const; |
| 83 | 88 |
| 84 // This call and the destruction of the returned snapshot must be called | 89 // This call and the destruction of the returned snapshot must be called |
| 85 // on the IO thread. If the blob is broken, then we return a nullptr here. | 90 // on the IO thread. If the blob is broken, then we return a nullptr here. |
| 86 // Please do not call this, and use CreateReader instead. It appropriately | 91 // Please do not call this, and use CreateReader instead. It appropriately |
| 87 // waits until the blob is built before having a size (see CalculateSize). | 92 // waits until the blob is built before having a size (see CalculateSize). |
| 88 // TODO(dmurph): Make this protected, where only the BlobReader can call it. | 93 // TODO(dmurph): Make this protected, where only the BlobReader can call it. |
| 89 std::unique_ptr<BlobDataSnapshot> CreateSnapshot() const; | 94 std::unique_ptr<BlobDataSnapshot> CreateSnapshot() const; |
| 90 | 95 |
| 91 private: | 96 private: |
| 92 // Internal class whose destructor is guarenteed to be called on the IO | 97 // Internal class whose destructor is guarenteed to be called on the IO |
| 93 // thread. | 98 // thread. |
| 94 class BlobDataHandleShared | 99 class BlobDataHandleShared |
| 95 : public base::RefCountedThreadSafe<BlobDataHandleShared> { | 100 : public base::RefCountedThreadSafe<BlobDataHandleShared> { |
| 96 public: | 101 public: |
| 97 BlobDataHandleShared(const std::string& uuid, | 102 BlobDataHandleShared(const std::string& uuid, |
| 98 const std::string& content_type, | 103 const std::string& content_type, |
| 99 const std::string& content_disposition, | 104 const std::string& content_disposition, |
| 105 uint64_t size, |
| 100 BlobStorageContext* context); | 106 BlobStorageContext* context); |
| 101 | 107 |
| 102 private: | 108 private: |
| 103 friend class base::DeleteHelper<BlobDataHandleShared>; | 109 friend class base::DeleteHelper<BlobDataHandleShared>; |
| 104 friend class base::RefCountedThreadSafe<BlobDataHandleShared>; | 110 friend class base::RefCountedThreadSafe<BlobDataHandleShared>; |
| 105 friend class BlobDataHandle; | 111 friend class BlobDataHandle; |
| 106 | 112 |
| 107 virtual ~BlobDataHandleShared(); | 113 virtual ~BlobDataHandleShared(); |
| 108 | 114 |
| 109 const std::string uuid_; | 115 const std::string uuid_; |
| 110 const std::string content_type_; | 116 const std::string content_type_; |
| 111 const std::string content_disposition_; | 117 const std::string content_disposition_; |
| 118 const uint64_t size_; |
| 112 base::WeakPtr<BlobStorageContext> context_; | 119 base::WeakPtr<BlobStorageContext> context_; |
| 113 | 120 |
| 114 DISALLOW_COPY_AND_ASSIGN(BlobDataHandleShared); | 121 DISALLOW_COPY_AND_ASSIGN(BlobDataHandleShared); |
| 115 }; | 122 }; |
| 116 | 123 |
| 117 friend class BlobStorageContext; | 124 friend class BlobStorageContext; |
| 118 BlobDataHandle(const std::string& uuid, | 125 BlobDataHandle(const std::string& uuid, |
| 119 const std::string& content_type, | 126 const std::string& content_type, |
| 120 const std::string& content_disposition, | 127 const std::string& content_disposition, |
| 128 uint64_t size, |
| 121 BlobStorageContext* context, | 129 BlobStorageContext* context, |
| 122 base::SequencedTaskRunner* io_task_runner); | 130 base::SequencedTaskRunner* io_task_runner); |
| 123 | 131 |
| 124 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 132 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 125 scoped_refptr<BlobDataHandleShared> shared_; | 133 scoped_refptr<BlobDataHandleShared> shared_; |
| 126 }; | 134 }; |
| 127 | 135 |
| 128 } // namespace storage | 136 } // namespace storage |
| 129 | 137 |
| 130 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ | 138 #endif // STORAGE_BROWSER_BLOB_BLOB_DATA_HANDLE_H_ |
| OLD | NEW |