forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_service_auth_error.cc
184 lines (159 loc) · 5.58 KB
/
google_service_auth_error.cc
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "google_apis/gaia/google_service_auth_error.h"
#include <memory>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "net/base/net_errors.h"
namespace {
const char* InvalidCredentialsReasonToString(
GoogleServiceAuthError::InvalidGaiaCredentialsReason reason) {
using InvalidGaiaCredentialsReason =
GoogleServiceAuthError::InvalidGaiaCredentialsReason;
switch (reason) {
case InvalidGaiaCredentialsReason::UNKNOWN:
return "unknown";
case InvalidGaiaCredentialsReason::CREDENTIALS_REJECTED_BY_SERVER:
return "credentials rejected by server";
case InvalidGaiaCredentialsReason::CREDENTIALS_REJECTED_BY_CLIENT:
return "credentials rejected by client";
case InvalidGaiaCredentialsReason::CREDENTIALS_MISSING:
return "credentials missing";
case InvalidGaiaCredentialsReason::NUM_REASONS:
NOTREACHED();
return "";
}
}
} // namespace
bool GoogleServiceAuthError::operator==(
const GoogleServiceAuthError& b) const {
return (state_ == b.state_) && (network_error_ == b.network_error_) &&
(error_message_ == b.error_message_) &&
(invalid_gaia_credentials_reason_ ==
b.invalid_gaia_credentials_reason_);
}
bool GoogleServiceAuthError::operator!=(
const GoogleServiceAuthError& b) const {
return !(*this == b);
}
GoogleServiceAuthError::GoogleServiceAuthError()
: GoogleServiceAuthError(NONE) {}
GoogleServiceAuthError::GoogleServiceAuthError(State s)
: GoogleServiceAuthError(s, std::string()) {}
GoogleServiceAuthError::GoogleServiceAuthError(State state,
const std::string& error_message)
: GoogleServiceAuthError(
state,
(state == CONNECTION_FAILED) ? net::ERR_FAILED : 0) {
error_message_ = error_message;
}
GoogleServiceAuthError::GoogleServiceAuthError(
const GoogleServiceAuthError& other) = default;
// static
GoogleServiceAuthError
GoogleServiceAuthError::FromConnectionError(int error) {
return GoogleServiceAuthError(CONNECTION_FAILED, error);
}
// static
GoogleServiceAuthError GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
InvalidGaiaCredentialsReason reason) {
GoogleServiceAuthError error(INVALID_GAIA_CREDENTIALS);
error.invalid_gaia_credentials_reason_ = reason;
return error;
}
// static
GoogleServiceAuthError GoogleServiceAuthError::FromServiceError(
const std::string& error_message) {
return GoogleServiceAuthError(SERVICE_ERROR, error_message);
}
// static
GoogleServiceAuthError GoogleServiceAuthError::FromUnexpectedServiceResponse(
const std::string& error_message) {
return GoogleServiceAuthError(UNEXPECTED_SERVICE_RESPONSE, error_message);
}
// static
GoogleServiceAuthError GoogleServiceAuthError::AuthErrorNone() {
return GoogleServiceAuthError(NONE);
}
// static
bool GoogleServiceAuthError::IsValid(State state) {
switch (state) {
case NONE:
case INVALID_GAIA_CREDENTIALS:
case USER_NOT_SIGNED_UP:
case CONNECTION_FAILED:
case SERVICE_UNAVAILABLE:
case REQUEST_CANCELED:
case UNEXPECTED_SERVICE_RESPONSE:
case SERVICE_ERROR:
return true;
case NUM_STATES:
return false;
}
return false;
}
GoogleServiceAuthError::State GoogleServiceAuthError::state() const {
return state_;
}
int GoogleServiceAuthError::network_error() const {
return network_error_;
}
const std::string& GoogleServiceAuthError::error_message() const {
return error_message_;
}
GoogleServiceAuthError::InvalidGaiaCredentialsReason
GoogleServiceAuthError::GetInvalidGaiaCredentialsReason() const {
DCHECK_EQ(INVALID_GAIA_CREDENTIALS, state());
return invalid_gaia_credentials_reason_;
}
std::string GoogleServiceAuthError::ToString() const {
switch (state_) {
case NONE:
return std::string();
case INVALID_GAIA_CREDENTIALS:
return base::StringPrintf(
"Invalid credentials (%s).",
InvalidCredentialsReasonToString(invalid_gaia_credentials_reason_));
case USER_NOT_SIGNED_UP:
return "Not authorized.";
case CONNECTION_FAILED:
return base::StringPrintf("Connection failed (%d).", network_error_);
case SERVICE_UNAVAILABLE:
return "Service unavailable; try again later.";
case REQUEST_CANCELED:
return "Request canceled.";
case UNEXPECTED_SERVICE_RESPONSE:
return base::StringPrintf("Unexpected service response (%s)",
error_message_.c_str());
case SERVICE_ERROR:
return base::StringPrintf("Service responded with error: '%s'",
error_message_.c_str());
case NUM_STATES:
NOTREACHED();
return std::string();
}
}
bool GoogleServiceAuthError::IsPersistentError() const {
if (state_ == GoogleServiceAuthError::NONE) return false;
return !IsTransientError();
}
bool GoogleServiceAuthError::IsTransientError() const {
switch (state_) {
// These are failures that are likely to succeed if tried again.
case GoogleServiceAuthError::CONNECTION_FAILED:
case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
case GoogleServiceAuthError::REQUEST_CANCELED:
return true;
// Everything else will have the same result.
default:
return false;
}
}
GoogleServiceAuthError::GoogleServiceAuthError(State s, int error)
: state_(s),
network_error_(error),
invalid_gaia_credentials_reason_(InvalidGaiaCredentialsReason::UNKNOWN) {}