ArtificialFailure.cpp (1200B)
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/. */ 6 7 #include "mozilla/dom/quota/ArtificialFailure.h" 8 9 #include "mozilla/Result.h" 10 #include "mozilla/StaticPrefs_dom.h" 11 12 namespace mozilla::dom::quota { 13 14 Result<Ok, nsresult> ArtificialFailure( 15 nsIQuotaArtificialFailure::Category aCategory) { 16 MOZ_ASSERT(aCategory != 0 && ((aCategory & (aCategory - 1)) == 0)); 17 18 if ((StaticPrefs::dom_quotaManager_artificialFailure_categories() & 19 aCategory) == 0) { 20 return Ok{}; 21 } 22 23 uint32_t probability = 24 StaticPrefs::dom_quotaManager_artificialFailure_probability(); 25 if (probability == 0) { 26 return Ok{}; 27 } 28 29 uint32_t randomValue = std::rand() % 100; 30 if (randomValue >= probability) { 31 return Ok{}; 32 } 33 34 uint32_t errorCode = 35 StaticPrefs::dom_quotaManager_artificialFailure_errorCode(); 36 37 nsresult rv = static_cast<nsresult>(errorCode); 38 return Err(rv); 39 } 40 41 } // namespace mozilla::dom::quota