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/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 static std::string MakeAuthorizationHeader(const std::string& auth_token) { | 28 static std::string MakeAuthorizationHeader(const std::string& auth_token) { |
29 return base::StringPrintf(kAuthorizationHeaderFormat, auth_token.c_str()); | 29 return base::StringPrintf(kAuthorizationHeaderFormat, auth_token.c_str()); |
30 } | 30 } |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 OAuth2ApiCallFlow::OAuth2ApiCallFlow() : state_(INITIAL) { | 33 OAuth2ApiCallFlow::OAuth2ApiCallFlow() : state_(INITIAL) { |
34 } | 34 } |
35 | 35 |
36 OAuth2ApiCallFlow::~OAuth2ApiCallFlow() {} | 36 OAuth2ApiCallFlow::~OAuth2ApiCallFlow() {} |
37 | 37 |
38 void OAuth2ApiCallFlow::Start(net::URLRequestContextGetter* context, | 38 void OAuth2ApiCallFlow::Start( |
39 const std::string& access_token) { | 39 net::URLRequestContextGetter* context, |
| 40 const std::string& access_token, |
| 41 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
40 CHECK(state_ == INITIAL); | 42 CHECK(state_ == INITIAL); |
41 state_ = API_CALL_STARTED; | 43 state_ = API_CALL_STARTED; |
42 | 44 |
43 url_fetcher_ = CreateURLFetcher(context, access_token); | 45 url_fetcher_ = CreateURLFetcher(context, access_token, traffic_annotation); |
44 url_fetcher_->Start(); // OnURLFetchComplete will be called. | 46 url_fetcher_->Start(); // OnURLFetchComplete will be called. |
45 } | 47 } |
46 | 48 |
47 void OAuth2ApiCallFlow::EndApiCall(const net::URLFetcher* source) { | 49 void OAuth2ApiCallFlow::EndApiCall(const net::URLFetcher* source) { |
48 CHECK_EQ(API_CALL_STARTED, state_); | 50 CHECK_EQ(API_CALL_STARTED, state_); |
49 | 51 |
50 URLRequestStatus status = source->GetStatus(); | 52 URLRequestStatus status = source->GetStatus(); |
51 int status_code = source->GetResponseCode(); | 53 int status_code = source->GetResponseCode(); |
52 if (!status.is_success() || | 54 if (!status.is_success() || |
53 (status_code != net::HTTP_OK && status_code != net::HTTP_NO_CONTENT)) { | 55 (status_code != net::HTTP_OK && status_code != net::HTTP_NO_CONTENT)) { |
(...skipping 15 matching lines...) Expand all Loading... |
69 } | 71 } |
70 | 72 |
71 void OAuth2ApiCallFlow::OnURLFetchComplete(const net::URLFetcher* source) { | 73 void OAuth2ApiCallFlow::OnURLFetchComplete(const net::URLFetcher* source) { |
72 CHECK(source); | 74 CHECK(source); |
73 CHECK_EQ(API_CALL_STARTED, state_); | 75 CHECK_EQ(API_CALL_STARTED, state_); |
74 EndApiCall(source); | 76 EndApiCall(source); |
75 } | 77 } |
76 | 78 |
77 std::unique_ptr<URLFetcher> OAuth2ApiCallFlow::CreateURLFetcher( | 79 std::unique_ptr<URLFetcher> OAuth2ApiCallFlow::CreateURLFetcher( |
78 net::URLRequestContextGetter* context, | 80 net::URLRequestContextGetter* context, |
79 const std::string& access_token) { | 81 const std::string& access_token, |
| 82 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
80 std::string body = CreateApiCallBody(); | 83 std::string body = CreateApiCallBody(); |
81 net::URLFetcher::RequestType request_type = GetRequestTypeForBody(body); | 84 net::URLFetcher::RequestType request_type = GetRequestTypeForBody(body); |
82 std::unique_ptr<URLFetcher> result = | 85 std::unique_ptr<URLFetcher> result = net::URLFetcher::Create( |
83 net::URLFetcher::Create(0, CreateApiCallUrl(), request_type, this); | 86 0, CreateApiCallUrl(), request_type, this, traffic_annotation); |
84 | 87 |
85 gaia::MarkURLFetcherAsGaia(result.get()); | 88 gaia::MarkURLFetcherAsGaia(result.get()); |
86 result->SetRequestContext(context); | 89 result->SetRequestContext(context); |
87 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 90 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
88 net::LOAD_DO_NOT_SAVE_COOKIES); | 91 net::LOAD_DO_NOT_SAVE_COOKIES); |
89 result->AddExtraRequestHeader(MakeAuthorizationHeader(access_token)); | 92 result->AddExtraRequestHeader(MakeAuthorizationHeader(access_token)); |
90 // Fetchers are sometimes cancelled because a network change was detected, | 93 // Fetchers are sometimes cancelled because a network change was detected, |
91 // especially at startup and after sign-in on ChromeOS. Retrying once should | 94 // especially at startup and after sign-in on ChromeOS. Retrying once should |
92 // be enough in those cases; let the fetcher retry up to 3 times just in case. | 95 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
93 // http://crbug.com/163710 | 96 // http://crbug.com/163710 |
94 result->SetAutomaticallyRetryOnNetworkChanges(3); | 97 result->SetAutomaticallyRetryOnNetworkChanges(3); |
95 | 98 |
96 // Even if the the body is empty, we still set the Content-Type because an | 99 // Even if the the body is empty, we still set the Content-Type because an |
97 // empty string may be a meaningful value. For example, a Protocol Buffer | 100 // empty string may be a meaningful value. For example, a Protocol Buffer |
98 // message with only default values will be serialized as an empty string. | 101 // message with only default values will be serialized as an empty string. |
99 if (request_type != net::URLFetcher::GET) | 102 if (request_type != net::URLFetcher::GET) |
100 result->SetUploadData(CreateApiCallBodyContentType(), body); | 103 result->SetUploadData(CreateApiCallBodyContentType(), body); |
101 | 104 |
102 return result; | 105 return result; |
103 } | 106 } |
OLD | NEW |