| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "google_apis/gaia/oauth2_api_call_flow.h" | 5 #include "google_apis/gaia/oauth2_api_call_flow.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 OAuth2ApiCallFlow::OAuth2ApiCallFlow() : state_(INITIAL) { | 35 OAuth2ApiCallFlow::OAuth2ApiCallFlow() : state_(INITIAL) { |
| 36 } | 36 } |
| 37 | 37 |
| 38 OAuth2ApiCallFlow::~OAuth2ApiCallFlow() {} | 38 OAuth2ApiCallFlow::~OAuth2ApiCallFlow() {} |
| 39 | 39 |
| 40 void OAuth2ApiCallFlow::Start(net::URLRequestContextGetter* context, | 40 void OAuth2ApiCallFlow::Start(net::URLRequestContextGetter* context, |
| 41 const std::string& access_token) { | 41 const std::string& access_token) { |
| 42 CHECK(state_ == INITIAL); | 42 CHECK(state_ == INITIAL); |
| 43 state_ = API_CALL_STARTED; | 43 state_ = API_CALL_STARTED; |
| 44 | 44 |
| 45 url_fetcher_.reset(CreateURLFetcher(context, access_token)); | 45 url_fetcher_ = CreateURLFetcher(context, access_token); |
| 46 url_fetcher_->Start(); // OnURLFetchComplete will be called. | 46 url_fetcher_->Start(); // OnURLFetchComplete will be called. |
| 47 } | 47 } |
| 48 | 48 |
| 49 void OAuth2ApiCallFlow::EndApiCall(const net::URLFetcher* source) { | 49 void OAuth2ApiCallFlow::EndApiCall(const net::URLFetcher* source) { |
| 50 CHECK_EQ(API_CALL_STARTED, state_); | 50 CHECK_EQ(API_CALL_STARTED, state_); |
| 51 | 51 |
| 52 URLRequestStatus status = source->GetStatus(); | 52 URLRequestStatus status = source->GetStatus(); |
| 53 int status_code = source->GetResponseCode(); | 53 int status_code = source->GetResponseCode(); |
| 54 if (!status.is_success() || | 54 if (!status.is_success() || |
| 55 (status_code != net::HTTP_OK && status_code != net::HTTP_NO_CONTENT)) { | 55 (status_code != net::HTTP_OK && status_code != net::HTTP_NO_CONTENT)) { |
| 56 state_ = ERROR_STATE; | 56 state_ = ERROR_STATE; |
| 57 ProcessApiCallFailure(source); | 57 ProcessApiCallFailure(source); |
| 58 } else { | 58 } else { |
| 59 state_ = API_CALL_DONE; | 59 state_ = API_CALL_DONE; |
| 60 ProcessApiCallSuccess(source); | 60 ProcessApiCallSuccess(source); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 std::string OAuth2ApiCallFlow::CreateApiCallBodyContentType() { | 64 std::string OAuth2ApiCallFlow::CreateApiCallBodyContentType() { |
| 65 return "application/x-www-form-urlencoded"; | 65 return "application/x-www-form-urlencoded"; |
| 66 } | 66 } |
| 67 | 67 |
| 68 void OAuth2ApiCallFlow::OnURLFetchComplete(const net::URLFetcher* source) { | 68 void OAuth2ApiCallFlow::OnURLFetchComplete(const net::URLFetcher* source) { |
| 69 CHECK(source); | 69 CHECK(source); |
| 70 CHECK_EQ(API_CALL_STARTED, state_); | 70 CHECK_EQ(API_CALL_STARTED, state_); |
| 71 EndApiCall(source); | 71 EndApiCall(source); |
| 72 } | 72 } |
| 73 | 73 |
| 74 URLFetcher* OAuth2ApiCallFlow::CreateURLFetcher( | 74 scoped_ptr<URLFetcher> OAuth2ApiCallFlow::CreateURLFetcher( |
| 75 net::URLRequestContextGetter* context, | 75 net::URLRequestContextGetter* context, |
| 76 const std::string& access_token) { | 76 const std::string& access_token) { |
| 77 std::string body = CreateApiCallBody(); | 77 std::string body = CreateApiCallBody(); |
| 78 bool empty_body = body.empty(); | 78 bool empty_body = body.empty(); |
| 79 URLFetcher* result = net::URLFetcher::Create( | 79 scoped_ptr<URLFetcher> result = net::URLFetcher::Create( |
| 80 0, | 80 0, CreateApiCallUrl(), empty_body ? URLFetcher::GET : URLFetcher::POST, |
| 81 CreateApiCallUrl(), | |
| 82 empty_body ? URLFetcher::GET : URLFetcher::POST, | |
| 83 this); | 81 this); |
| 84 | 82 |
| 85 result->SetRequestContext(context); | 83 result->SetRequestContext(context); |
| 86 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 84 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 87 net::LOAD_DO_NOT_SAVE_COOKIES); | 85 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 88 result->AddExtraRequestHeader(MakeAuthorizationHeader(access_token)); | 86 result->AddExtraRequestHeader(MakeAuthorizationHeader(access_token)); |
| 89 // Fetchers are sometimes cancelled because a network change was detected, | 87 // Fetchers are sometimes cancelled because a network change was detected, |
| 90 // especially at startup and after sign-in on ChromeOS. Retrying once should | 88 // especially at startup and after sign-in on ChromeOS. Retrying once should |
| 91 // be enough in those cases; let the fetcher retry up to 3 times just in case. | 89 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
| 92 // http://crbug.com/163710 | 90 // http://crbug.com/163710 |
| 93 result->SetAutomaticallyRetryOnNetworkChanges(3); | 91 result->SetAutomaticallyRetryOnNetworkChanges(3); |
| 94 | 92 |
| 95 if (!empty_body) | 93 if (!empty_body) |
| 96 result->SetUploadData(CreateApiCallBodyContentType(), body); | 94 result->SetUploadData(CreateApiCallBodyContentType(), body); |
| 97 | 95 |
| 98 return result; | 96 return result; |
| 99 } | 97 } |
| OLD | NEW |