File: status.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 3,324 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/indexed_db/status.h"

#include <string>

#include "base/metrics/histogram_functions.h"
#include "third_party/leveldatabase/env_chromium.h"

namespace content::indexed_db {

Status::Status() : type_(Type::kOk), msg_("OK") {}

Status::Status(const Status& rhs) = default;

Status::Status(Status&& rhs) noexcept = default;

Status::Status(leveldb::Status&& status) noexcept
    : type_(Type::kDatabaseEngine), leveldb_status_(std::move(status)) {}

Status::Status(Status::Type type, std::string_view msg)
    : type_(type), msg_(msg) {
  CHECK(!msg_.empty());
}

Status::~Status() = default;

Status& Status::operator=(const Status& rhs) = default;

Status& Status::operator=(Status&& rhs) noexcept = default;

Status& Status::operator=(leveldb::Status&& rhs) noexcept {
  msg_.clear();
  leveldb_status_ = std::move(rhs);
  type_ = Type::kDatabaseEngine;
  return *this;
}

// static
Status Status::OK() {
  return Status();
}

// static
Status Status::NotFound(std::string_view msg) {
  return Status(Type::kNotFound, msg);
}

// static
Status Status::Corruption(std::string_view msg) {
  return Status(Type::kCorruption, msg);
}

// static
Status Status::InvalidArgument(std::string_view msg) {
  return Status(Type::kInvalidArgument, msg);
}

// static
Status Status::IOError(std::string_view msg) {
  return Status(Type::kIoError, msg.empty() ? "IO Error" : msg);
}

bool Status::ok() const {
  return type_ == Type::kOk || (leveldb_status_ && leveldb_status_->ok());
}

bool Status::IsNotFound() const {
  return type_ == Type::kNotFound ||
         (leveldb_status_ && leveldb_status_->IsNotFound());
}

bool Status::IsCorruption() const {
  return type_ == Type::kCorruption ||
         (leveldb_status_ && leveldb_status_->IsCorruption());
}

bool Status::IsIOError() const {
  return type_ == Type::kIoError ||
         (leveldb_status_ && leveldb_status_->IsIOError());
}

bool Status::IsInvalidArgument() const {
  return type_ == Type::kInvalidArgument;
}

std::string Status::ToString() const {
  if (leveldb_status_) {
    return leveldb_status_->ToString();
  }
  return msg_;
}

bool Status::IndicatesDiskFull() const {
  return leveldb_status_ && leveldb_env::IndicatesDiskFull(*leveldb_status_);
}

void Status::Log(std::string_view histogram_name) const {
  base::UmaHistogramEnumeration(
      histogram_name,
      static_cast<leveldb_env::LevelDBStatusValue>(GetTypeForLegacyLogging()),
      leveldb_env::LEVELDB_STATUS_MAX);
}

int Status::GetTypeForLegacyLogging() const {
  switch (type_) {
    case Type::kOk:
      return leveldb_env::LEVELDB_STATUS_OK;
    case Type::kNotFound:
      return leveldb_env::LEVELDB_STATUS_NOT_FOUND;
    case Type::kCorruption:
      return leveldb_env::LEVELDB_STATUS_CORRUPTION;
    case Type::kNotSupported:
      return leveldb_env::LEVELDB_STATUS_NOT_SUPPORTED;
    case Type::kInvalidArgument:
      return leveldb_env::LEVELDB_STATUS_INVALID_ARGUMENT;
    case Type::kIoError:
      return leveldb_env::LEVELDB_STATUS_IO_ERROR;
    case Type::kDatabaseEngine:
      return leveldb_env::GetLevelDBStatusUMAValue(*leveldb_status_);
  }
  NOTREACHED();
}

}  // namespace content::indexed_db