Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / dom / quota / ArtificialFailure.cpp
blob135ff631c363d6643fba12cb22c14a02cbc5c475
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/quota/ArtificialFailure.h"
9 #include "mozilla/Result.h"
10 #include "mozilla/StaticPrefs_dom.h"
12 namespace mozilla::dom::quota {
14 Result<Ok, nsresult> ArtificialFailure(
15 nsIQuotaArtificialFailure::Category aCategory) {
16 MOZ_ASSERT(aCategory != 0 && ((aCategory & (aCategory - 1)) == 0));
18 if ((StaticPrefs::dom_quotaManager_artificialFailure_categories() &
19 aCategory) == 0) {
20 return Ok{};
23 uint32_t probability =
24 StaticPrefs::dom_quotaManager_artificialFailure_probability();
25 if (probability == 0) {
26 return Ok{};
29 uint32_t randomValue = std::rand() % 100;
30 if (randomValue >= probability) {
31 return Ok{};
34 uint32_t errorCode =
35 StaticPrefs::dom_quotaManager_artificialFailure_errorCode();
37 nsresult rv = static_cast<nsresult>(errorCode);
38 return Err(rv);
41 } // namespace mozilla::dom::quota