123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- // 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;
- GoogleServiceAuthError& GoogleServiceAuthError::operator=(
- 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::FromServiceUnavailable(
- const std::string& error_message) {
- return GoogleServiceAuthError(SERVICE_UNAVAILABLE, error_message);
- }
- // static
- GoogleServiceAuthError
- GoogleServiceAuthError::FromScopeLimitedUnrecoverableError(
- const std::string& error_message) {
- return GoogleServiceAuthError(SCOPE_LIMITED_UNRECOVERABLE_ERROR,
- error_message);
- }
- // 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:
- case SCOPE_LIMITED_UNRECOVERABLE_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 SCOPE_LIMITED_UNRECOVERABLE_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::IsScopePersistentError() const {
- return state_ == GoogleServiceAuthError::SCOPE_LIMITED_UNRECOVERABLE_ERROR;
- }
- 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) {}
|