Skip to content

Commit

Permalink
Bug 1841012 - Ensure OPFS transactions are started with a factory met…
Browse files Browse the repository at this point in the history
…hod. r=dom-storage-reviewers,janv

Differential Revision: https://phabricator.services.mozilla.com/D182489
  • Loading branch information
jjjalkanen committed Jun 30, 2023
1 parent 75c7895 commit 165460d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
35 changes: 35 additions & 0 deletions dom/fs/parent/StartedTransaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "StartedTransaction.h"

#include "ResultConnection.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/dom/quota/ResultExtensions.h"

namespace mozilla::dom::fs {

/* static */
Result<StartedTransaction, QMResult> StartedTransaction::Create(
const ResultConnection& aConn) {
auto transaction = MakeUnique<mozStorageTransaction>(
aConn.get(), /* aCommitOnComplete */ false,
mozIStorageConnection::TRANSACTION_IMMEDIATE);

QM_TRY(QM_TO_RESULT(transaction->Start()));

return StartedTransaction(std::move(transaction));
}

nsresult StartedTransaction::Commit() { return mTransaction->Commit(); }

nsresult StartedTransaction::Rollback() { return mTransaction->Rollback(); }

StartedTransaction::StartedTransaction(
UniquePtr<mozStorageTransaction>&& aTransaction)
: mTransaction(std::move(aTransaction)) {}

} // namespace mozilla::dom::fs
39 changes: 39 additions & 0 deletions dom/fs/parent/StartedTransaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#ifndef DOM_FS_PARENT_STARTEDTRANSACTION_H_
#define DOM_FS_PARENT_STARTEDTRANSACTION_H_

#include "ResultConnection.h"
#include "mozStorageHelper.h"
#include "mozilla/dom/QMResult.h"

namespace mozilla::dom::fs {

class StartedTransaction {
public:
static Result<StartedTransaction, QMResult> Create(
const ResultConnection& aConn);

StartedTransaction(StartedTransaction&& aOther) = default;

StartedTransaction(const StartedTransaction& aOther) = delete;

nsresult Commit();

nsresult Rollback();

~StartedTransaction() = default;

private:
explicit StartedTransaction(UniquePtr<mozStorageTransaction>&& aTransaction);

UniquePtr<mozStorageTransaction> mTransaction;
};

} // namespace mozilla::dom::fs

#endif // DOM_FS_PARENT_STARTEDTRANSACTION_H_
1 change: 1 addition & 0 deletions dom/fs/parent/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ UNIFIED_SOURCES += [
"FileSystemStreamCallbacks.cpp",
"FileSystemWritableFileStreamParent.cpp",
"ResultStatement.cpp",
"StartedTransaction.cpp",
]

LOCAL_INCLUDES += [
Expand Down

0 comments on commit 165460d

Please sign in to comment.