Line data Source code
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 : #ifndef mozilla_dom_quota_persistencetype_h__
8 : #define mozilla_dom_quota_persistencetype_h__
9 :
10 : #include "mozilla/dom/quota/QuotaCommon.h"
11 :
12 : #include "mozilla/dom/StorageTypeBinding.h"
13 :
14 : BEGIN_QUOTA_NAMESPACE
15 :
16 : enum PersistenceType
17 : {
18 : PERSISTENCE_TYPE_PERSISTENT = 0,
19 : PERSISTENCE_TYPE_TEMPORARY,
20 : PERSISTENCE_TYPE_DEFAULT,
21 :
22 : // Only needed for IPC serialization helper, should never be used in code.
23 : PERSISTENCE_TYPE_INVALID
24 : };
25 :
26 : static const PersistenceType kAllPersistenceTypes[] = {
27 : PERSISTENCE_TYPE_PERSISTENT,
28 : PERSISTENCE_TYPE_TEMPORARY,
29 : PERSISTENCE_TYPE_DEFAULT
30 : };
31 :
32 : inline void
33 0 : PersistenceTypeToText(PersistenceType aPersistenceType, nsACString& aText)
34 : {
35 0 : switch (aPersistenceType) {
36 : case PERSISTENCE_TYPE_PERSISTENT:
37 0 : aText.AssignLiteral("persistent");
38 0 : return;
39 : case PERSISTENCE_TYPE_TEMPORARY:
40 0 : aText.AssignLiteral("temporary");
41 0 : return;
42 : case PERSISTENCE_TYPE_DEFAULT:
43 0 : aText.AssignLiteral("default");
44 0 : return;
45 :
46 : case PERSISTENCE_TYPE_INVALID:
47 : default:
48 0 : MOZ_CRASH("Bad persistence type value!");
49 : }
50 : }
51 :
52 : inline PersistenceType
53 0 : PersistenceTypeFromText(const nsACString& aText)
54 : {
55 0 : if (aText.EqualsLiteral("persistent")) {
56 0 : return PERSISTENCE_TYPE_PERSISTENT;
57 : }
58 :
59 0 : if (aText.EqualsLiteral("temporary")) {
60 0 : return PERSISTENCE_TYPE_TEMPORARY;
61 : }
62 :
63 0 : if (aText.EqualsLiteral("default")) {
64 0 : return PERSISTENCE_TYPE_DEFAULT;
65 : }
66 :
67 0 : MOZ_CRASH("Should never get here!");
68 : }
69 :
70 : inline nsresult
71 0 : NullablePersistenceTypeFromText(const nsACString& aText,
72 : Nullable<PersistenceType>* aPersistenceType)
73 : {
74 0 : if (aText.IsVoid()) {
75 0 : *aPersistenceType = Nullable<PersistenceType>();
76 0 : return NS_OK;
77 : }
78 :
79 0 : if (aText.EqualsLiteral("persistent")) {
80 0 : *aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_PERSISTENT);
81 0 : return NS_OK;
82 : }
83 :
84 0 : if (aText.EqualsLiteral("temporary")) {
85 0 : *aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_TEMPORARY);
86 0 : return NS_OK;
87 : }
88 :
89 0 : if (aText.EqualsLiteral("default")) {
90 0 : *aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_DEFAULT);
91 0 : return NS_OK;
92 : }
93 :
94 0 : return NS_ERROR_FAILURE;
95 : }
96 :
97 : inline mozilla::dom::StorageType
98 0 : PersistenceTypeToStorage(PersistenceType aPersistenceType)
99 : {
100 0 : return mozilla::dom::StorageType(static_cast<int>(aPersistenceType));
101 : }
102 :
103 : inline PersistenceType
104 0 : PersistenceTypeFromStorage(const Optional<mozilla::dom::StorageType>& aStorage)
105 : {
106 0 : if (aStorage.WasPassed()) {
107 0 : return PersistenceType(static_cast<int>(aStorage.Value()));
108 : }
109 :
110 0 : return PERSISTENCE_TYPE_DEFAULT;
111 : }
112 :
113 : inline PersistenceType
114 0 : ComplementaryPersistenceType(PersistenceType aPersistenceType)
115 : {
116 0 : MOZ_ASSERT(aPersistenceType == PERSISTENCE_TYPE_DEFAULT ||
117 : aPersistenceType == PERSISTENCE_TYPE_TEMPORARY);
118 :
119 0 : if (aPersistenceType == PERSISTENCE_TYPE_DEFAULT) {
120 0 : return PERSISTENCE_TYPE_TEMPORARY;
121 : }
122 :
123 0 : return PERSISTENCE_TYPE_DEFAULT;
124 : }
125 :
126 : END_QUOTA_NAMESPACE
127 :
128 : #endif // mozilla_dom_quota_persistencetype_h__
|