Evan Stade | 416f46f1 | 2025-06-18 15:42:39 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SQL_STREAMING_BLOB_HANDLE_H_ |
| 6 | #define SQL_STREAMING_BLOB_HANDLE_H_ |
| 7 | |
| 8 | #include"base/component_export.h" |
| 9 | #include"base/containers/span.h" |
| 10 | #include"base/functional/callback.h" |
| 11 | #include"base/memory/raw_ptr.h" |
| 12 | #include"base/types/pass_key.h" |
| 13 | #include"sql/sqlite_result_code.h" |
| 14 | |
| 15 | struct sqlite3_blob; |
| 16 | |
| 17 | namespace sql{ |
| 18 | |
| 19 | classDatabase; |
| 20 | |
| 21 | // Wraps a blob handle opened for streaming. |
| 22 | // See https://www.sqlite.org/c3ref/blob_open.html |
| 23 | // The handle will be closed when the instance is destroyed, or when an error |
| 24 | // occurs. |
| 25 | // |
| 26 | // Use `sql::Database::GetStreamingBlob()` to get an instance of this class. |
| 27 | // Callers are responsible for deleting this instance before attempting to |
| 28 | // close, poison, or raze the database. Note that even deleting an instance may |
| 29 | // run into an error which would cause the database's error callback to run. |
| 30 | class COMPONENT_EXPORT(SQL)StreamingBlobHandle{ |
| 31 | public: |
| 32 | StreamingBlobHandle( |
| 33 | base::PassKey<sql::Database>, |
| 34 | sqlite3_blob* blob, |
| 35 | base::OnceCallback<void(SqliteResultCode,constchar*)> done_callback); |
| 36 | ~StreamingBlobHandle(); |
| 37 | |
| 38 | // Move ctor is allowed to facilitate use with optional. |
| 39 | StreamingBlobHandle(StreamingBlobHandle&&); |
| 40 | |
| 41 | StreamingBlobHandle&operator=(StreamingBlobHandle&&)=delete; |
| 42 | StreamingBlobHandle(constStreamingBlobHandle&)=delete; |
| 43 | StreamingBlobHandle&operator=(constStreamingBlobHandle&)=delete; |
| 44 | |
| 45 | // These return true for success. If they fail once, calling them again |
| 46 | // will CHECK. |
| 47 | [[nodiscard]]boolRead(int offset,base::span<uint8_t>into); |
| 48 | [[nodiscard]]boolWrite(int offset,base::span<constuint8_t>from); |
| 49 | |
| 50 | private: |
| 51 | // This handle is owned. |
| 52 | raw_ptr<sqlite3_blob> blob_handle_; |
| 53 | |
| 54 | // This callback is invoked when the blob is closed, either due to an error or |
| 55 | // when `this` is destroyed normally. See `sql::Database::OnSqliteError()` for |
| 56 | // documentation of the parameters. |
| 57 | base::OnceCallback<void(SqliteResultCode,constchar*)> done_callback_; |
| 58 | }; |
| 59 | |
| 60 | }// namespace sql |
| 61 | |
| 62 | #endif// SQL_STREAMING_BLOB_HANDLE_H_ |