DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d6844fe546ad)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/* -*- 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 mozilla_dom_quota_persistencetype_h__
#define mozilla_dom_quota_persistencetype_h__

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

#include "mozilla/dom/StorageTypeBinding.h"

BEGIN_QUOTA_NAMESPACE

enum PersistenceType {
  PERSISTENCE_TYPE_PERSISTENT = 0,
  PERSISTENCE_TYPE_TEMPORARY,
  PERSISTENCE_TYPE_DEFAULT,

  // Only needed for IPC serialization helper, should never be used in code.
  PERSISTENCE_TYPE_INVALID
};

static const PersistenceType kBestEffortPersistenceTypes[] = {
    PERSISTENCE_TYPE_TEMPORARY, PERSISTENCE_TYPE_DEFAULT};

static const PersistenceType kAllPersistenceTypes[] = {
    PERSISTENCE_TYPE_PERSISTENT, PERSISTENCE_TYPE_TEMPORARY,
    PERSISTENCE_TYPE_DEFAULT};

inline void PersistenceTypeToText(PersistenceType aPersistenceType,
                                  nsACString& aText) {
  switch (aPersistenceType) {
    case PERSISTENCE_TYPE_PERSISTENT:
      aText.AssignLiteral("persistent");
      return;
    case PERSISTENCE_TYPE_TEMPORARY:
      aText.AssignLiteral("temporary");
      return;
    case PERSISTENCE_TYPE_DEFAULT:
      aText.AssignLiteral("default");
      return;

    case PERSISTENCE_TYPE_INVALID:
    default:
      MOZ_CRASH("Bad persistence type value!");
  }
}

class PersistenceTypeString : public nsCString {
 public:
  explicit PersistenceTypeString(PersistenceType aPersistenceType) {
    PersistenceTypeToText(aPersistenceType, *this);
  }
};

inline PersistenceType PersistenceTypeFromText(const nsACString& aText) {
  if (aText.EqualsLiteral("persistent")) {
    return PERSISTENCE_TYPE_PERSISTENT;
  }

  if (aText.EqualsLiteral("temporary")) {
    return PERSISTENCE_TYPE_TEMPORARY;
  }

  if (aText.EqualsLiteral("default")) {
    return PERSISTENCE_TYPE_DEFAULT;
  }

  MOZ_CRASH("Should never get here!");
}

inline nsresult PersistenceTypeFromInt32(int32_t aInt,
                                         PersistenceType& aPersistenceType) {
  static_assert(
      PERSISTENCE_TYPE_PERSISTENT == 0 && PERSISTENCE_TYPE_TEMPORARY == 1 &&
          PERSISTENCE_TYPE_DEFAULT == 2 && PERSISTENCE_TYPE_INVALID == 3,
      "Incorrect enum values!");

  if (aInt < PERSISTENCE_TYPE_PERSISTENT || aInt > PERSISTENCE_TYPE_DEFAULT) {
    return NS_ERROR_FAILURE;
  }

  aPersistenceType = static_cast<PersistenceType>(aInt);
  return NS_OK;
}

inline nsresult NullablePersistenceTypeFromText(
    const nsACString& aText, Nullable<PersistenceType>* aPersistenceType) {
  if (aText.IsVoid()) {
    *aPersistenceType = Nullable<PersistenceType>();
    return NS_OK;
  }

  if (aText.EqualsLiteral("persistent")) {
    *aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_PERSISTENT);
    return NS_OK;
  }

  if (aText.EqualsLiteral("temporary")) {
    *aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_TEMPORARY);
    return NS_OK;
  }

  if (aText.EqualsLiteral("default")) {
    *aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_DEFAULT);
    return NS_OK;
  }

  return NS_ERROR_FAILURE;
}

inline mozilla::dom::StorageType PersistenceTypeToStorage(
    PersistenceType aPersistenceType) {
  return mozilla::dom::StorageType(static_cast<int>(aPersistenceType));
}

inline PersistenceType PersistenceTypeFromStorage(
    const Optional<mozilla::dom::StorageType>& aStorage) {
  if (aStorage.WasPassed()) {
    return PersistenceType(static_cast<int>(aStorage.Value()));
  }

  return PERSISTENCE_TYPE_DEFAULT;
}

inline PersistenceType ComplementaryPersistenceType(
    PersistenceType aPersistenceType) {
  MOZ_ASSERT(aPersistenceType == PERSISTENCE_TYPE_DEFAULT ||
             aPersistenceType == PERSISTENCE_TYPE_TEMPORARY);

  if (aPersistenceType == PERSISTENCE_TYPE_DEFAULT) {
    return PERSISTENCE_TYPE_TEMPORARY;
  }

  return PERSISTENCE_TYPE_DEFAULT;
}

END_QUOTA_NAMESPACE

#endif  // mozilla_dom_quota_persistencetype_h__