summaryrefslogtreecommitdiffstats
path: root/third_party/rust/webext-storage/src/ffi.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/webext-storage/src/ffi.rs')
-rw-r--r--third_party/rust/webext-storage/src/ffi.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/third_party/rust/webext-storage/src/ffi.rs b/third_party/rust/webext-storage/src/ffi.rs
new file mode 100644
index 0000000000..dc4af8ed6e
--- /dev/null
+++ b/third_party/rust/webext-storage/src/ffi.rs
@@ -0,0 +1,48 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/// This module contains glue for the FFI, including error codes and a
+/// `From<Error>` implementation for `ffi_support::ExternError`. These must be
+/// implemented in this crate, and not `webext_storage_ffi`, because of Rust's
+/// orphan rule for implementing traits (`webext_storage_ffi` can't implement
+/// a trait in `ffi_support` for a type in `webext_storage`).
+use ffi_support::{ErrorCode, ExternError};
+
+use crate::error::{Error, ErrorKind, QuotaReason};
+
+mod error_codes {
+ /// An unexpected error occurred which likely cannot be meaningfully handled
+ /// by the application.
+ pub const UNEXPECTED: i32 = 1;
+
+ /// The application passed an invalid JSON string for a storage key or value.
+ pub const INVALID_JSON: i32 = 2;
+
+ /// The total number of bytes stored in the database for this extension,
+ /// counting all key-value pairs serialized to JSON, exceeds the allowed limit.
+ pub const QUOTA_TOTAL_BYTES_EXCEEDED: i32 = 32;
+
+ /// A single key-value pair exceeds the allowed byte limit when serialized
+ /// to JSON.
+ pub const QUOTA_ITEM_BYTES_EXCEEDED: i32 = 32 + 1;
+
+ /// The total number of key-value pairs stored for this extension exceeded the
+ /// allowed limit.
+ pub const QUOTA_MAX_ITEMS_EXCEEDED: i32 = 32 + 2;
+}
+
+impl From<Error> for ExternError {
+ fn from(err: Error) -> ExternError {
+ let code = ErrorCode::new(match err.kind() {
+ ErrorKind::JsonError(_) => error_codes::INVALID_JSON,
+ ErrorKind::QuotaError(QuotaReason::TotalBytes) => {
+ error_codes::QUOTA_TOTAL_BYTES_EXCEEDED
+ }
+ ErrorKind::QuotaError(QuotaReason::ItemBytes) => error_codes::QUOTA_ITEM_BYTES_EXCEEDED,
+ ErrorKind::QuotaError(QuotaReason::MaxItems) => error_codes::QUOTA_MAX_ITEMS_EXCEEDED,
+ _ => error_codes::UNEXPECTED,
+ });
+ ExternError::new_error(code, err.to_string())
+ }
+}