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 #ifndef mozilla_dom_quota_InitializationTypes_h
8 #define mozilla_dom_quota_InitializationTypes_h
11 #include "ErrorList.h"
12 #include "mozilla/Assertions.h"
13 #include "mozilla/TypedEnumBits.h"
15 #include "nsLiteralString.h"
16 #include "nsStringFwd.h"
18 // XXX Avoid these includes by moving ReportFirstInitializationAttempt to cpp
20 #include "mozilla/Telemetry.h"
21 #include "mozilla/TelemetryHistogramEnums.h"
23 // Undo X11/X.h's definition of None
30 enum class Initialization
{
33 TemporaryStorage
= 1 << 1,
34 DefaultRepository
= 1 << 2,
35 TemporaryRepository
= 1 << 3,
36 UpgradeStorageFrom0_0To1_0
= 1 << 4,
37 UpgradeStorageFrom1_0To2_0
= 1 << 5,
38 UpgradeStorageFrom2_0To2_1
= 1 << 6,
39 UpgradeStorageFrom2_1To2_2
= 1 << 7,
40 UpgradeStorageFrom2_2To2_3
= 1 << 8,
41 UpgradeFromIndexedDBDirectory
= 1 << 9,
42 UpgradeFromPersistentStorageDirectory
= 1 << 10,
45 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Initialization
)
47 class InitializationInfo final
{
48 Initialization mInitializationAttempts
= Initialization::None
;
51 template <typename SuccessFunction
>
52 class AutoInitializationAttempt
{
53 InitializationInfo
& mOwner
;
54 const Initialization mInitialization
;
55 const SuccessFunction mSuccessFunction
;
58 AutoInitializationAttempt(InitializationInfo
& aOwner
,
59 const Initialization aInitialization
,
60 SuccessFunction
&& aSuccessFunction
)
62 mInitialization(aInitialization
),
63 mSuccessFunction(std::move(aSuccessFunction
)) {}
65 bool IsFirstInitializationAttempt() const {
66 return !mOwner
.InitializationAttempted(mInitialization
);
69 ~AutoInitializationAttempt() {
70 if (mOwner
.InitializationAttempted(mInitialization
)) {
74 mOwner
.ReportFirstInitializationAttempt(mInitialization
,
79 template <typename SuccessFunction
>
80 AutoInitializationAttempt
<SuccessFunction
> RecordFirstInitializationAttempt(
81 const Initialization aInitialization
,
82 SuccessFunction
&& aSuccessFunction
) {
83 return AutoInitializationAttempt
<SuccessFunction
>(
84 *this, aInitialization
, std::move(aSuccessFunction
));
87 void RecordFirstInitializationAttempt(const Initialization aInitialization
,
89 if (InitializationAttempted(aInitialization
)) {
93 ReportFirstInitializationAttempt(aInitialization
, NS_SUCCEEDED(aRv
));
96 void AssertInitializationAttempted(const Initialization aInitialization
) {
97 MOZ_ASSERT(InitializationAttempted(aInitialization
));
100 void ResetInitializationAttempts() {
101 mInitializationAttempts
= Initialization::None
;
105 // TODO: Use constexpr here once bug 1594094 is addressed.
106 static nsLiteralCString
GetInitializationString(
107 const Initialization aInitialization
) {
108 switch (aInitialization
) {
109 case Initialization::Storage
:
111 case Initialization::TemporaryStorage
:
112 return "TemporaryStorage"_ns
;
113 case Initialization::DefaultRepository
:
114 return "DefaultRepository"_ns
;
115 case Initialization::TemporaryRepository
:
116 return "TemporaryRepository"_ns
;
117 case Initialization::UpgradeStorageFrom0_0To1_0
:
118 return "UpgradeStorageFrom0_0To1_0"_ns
;
119 case Initialization::UpgradeStorageFrom1_0To2_0
:
120 return "UpgradeStorageFrom1_0To2_0"_ns
;
121 case Initialization::UpgradeStorageFrom2_0To2_1
:
122 return "UpgradeStorageFrom2_0To2_1"_ns
;
123 case Initialization::UpgradeStorageFrom2_1To2_2
:
124 return "UpgradeStorageFrom2_1To2_2"_ns
;
125 case Initialization::UpgradeStorageFrom2_2To2_3
:
126 return "UpgradeStorageFrom2_2To2_3"_ns
;
127 case Initialization::UpgradeFromIndexedDBDirectory
:
128 return "UpgradeFromIndexedDBDirectory"_ns
;
129 case Initialization::UpgradeFromPersistentStorageDirectory
:
130 return "UpgradeFromPersistentStorageDirectory"_ns
;
133 MOZ_CRASH("Bad initialization value!");
137 bool InitializationAttempted(const Initialization aInitialization
) const {
138 return static_cast<bool>(mInitializationAttempts
& aInitialization
);
141 void ReportFirstInitializationAttempt(const Initialization aInitialization
,
142 const bool aSuccess
) {
143 MOZ_ASSERT(!InitializationAttempted(aInitialization
));
145 mInitializationAttempts
|= aInitialization
;
146 Telemetry::Accumulate(Telemetry::QM_FIRST_INITIALIZATION_ATTEMPT
,
147 GetInitializationString(aInitialization
),
148 static_cast<uint32_t>(aSuccess
));
154 } // namespace mozilla
156 #endif // mozilla_dom_quota_InitializationTypes_h