1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/common/net/gaia/google_service_auth_error.h" 6 7 #include <string> 8 9 #include "base/logging.h" 10 #include "base/values.h" 11 #include "net/base/net_errors.h" 12 13 GoogleServiceAuthError::Captcha::Captcha( 14 const std::string& t, const GURL& img, const GURL& unlock) 15 : token(t), image_url(img), unlock_url(unlock) {} 16 17 bool GoogleServiceAuthError::operator==( 18 const GoogleServiceAuthError &b) const { 19 return (state_ == b.state_ && 20 network_error_ == b.network_error_ && 21 captcha_.token == b.captcha_.token && 22 captcha_.image_url == b.captcha_.image_url && 23 captcha_.unlock_url == b.captcha_.unlock_url); 24 } 25 26 GoogleServiceAuthError::GoogleServiceAuthError(State s) 27 : state_(s), 28 captcha_("", GURL(), GURL()), 29 network_error_(0) { 30 // If the caller has no idea, then we just set it to a generic failure. 31 if (s == CONNECTION_FAILED) { 32 network_error_ = net::ERR_FAILED; 33 } 34 } 35 36 GoogleServiceAuthError 37 GoogleServiceAuthError::FromConnectionError(int error) { 38 return GoogleServiceAuthError(CONNECTION_FAILED, error); 39 } 40 41 GoogleServiceAuthError GoogleServiceAuthError::FromCaptchaChallenge( 42 const std::string& captcha_token, 43 const GURL& captcha_image_url, 44 const GURL& captcha_unlock_url) { 45 return GoogleServiceAuthError(CAPTCHA_REQUIRED, captcha_token, 46 captcha_image_url, captcha_unlock_url); 47 } 48 49 GoogleServiceAuthError GoogleServiceAuthError::None() { 50 return GoogleServiceAuthError(NONE); 51 } 52 53 const GoogleServiceAuthError::State& GoogleServiceAuthError::state() const { 54 return state_; 55 } 56 57 const GoogleServiceAuthError::Captcha& GoogleServiceAuthError::captcha() const { 58 return captcha_; 59 } 60 61 int GoogleServiceAuthError::network_error() const { 62 return network_error_; 63 } 64 65 DictionaryValue* GoogleServiceAuthError::ToValue() const { 66 DictionaryValue* value = new DictionaryValue(); 67 std::string state_str; 68 switch (state_) { 69 #define STATE_CASE(x) case x: state_str = #x; break 70 STATE_CASE(NONE); 71 STATE_CASE(INVALID_GAIA_CREDENTIALS); 72 STATE_CASE(USER_NOT_SIGNED_UP); 73 STATE_CASE(CONNECTION_FAILED); 74 STATE_CASE(CAPTCHA_REQUIRED); 75 STATE_CASE(ACCOUNT_DELETED); 76 STATE_CASE(ACCOUNT_DISABLED); 77 STATE_CASE(SERVICE_UNAVAILABLE); 78 STATE_CASE(TWO_FACTOR); 79 STATE_CASE(REQUEST_CANCELED); 80 STATE_CASE(HOSTED_NOT_ALLOWED); 81 #undef STATE_CASE 82 default: 83 NOTREACHED(); 84 break; 85 } 86 value->SetString("state", state_str); 87 if (state_ == CAPTCHA_REQUIRED) { 88 DictionaryValue* captcha_value = new DictionaryValue(); 89 value->Set("captcha", captcha_value); 90 captcha_value->SetString("token", captcha_.token); 91 captcha_value->SetString("imageUrl", captcha_.image_url.spec()); 92 captcha_value->SetString("unlockUrl", captcha_.unlock_url.spec()); 93 } else if (state_ == CONNECTION_FAILED) { 94 value->SetString("networkError", net::ErrorToString(network_error_)); 95 } 96 return value; 97 } 98 99 GoogleServiceAuthError::GoogleServiceAuthError(State s, int error) 100 : state_(s), 101 captcha_("", GURL(), GURL()), 102 network_error_(error) {} 103 104 GoogleServiceAuthError::GoogleServiceAuthError( 105 State s, const std::string& captcha_token, 106 const GURL& captcha_image_url, 107 const GURL& captcha_unlock_url) 108 : state_(s), 109 captcha_(captcha_token, captcha_image_url, captcha_unlock_url), 110 network_error_(0) {} 111