OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/service/cloud_print/cloud_print_url_fetcher.h" | 5 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h" |
6 | 6 |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/common/net/http_return.h" | 9 #include "chrome/common/net/http_return.h" |
10 #include "chrome/service/cloud_print/cloud_print_consts.h" | 10 #include "chrome/service/cloud_print/cloud_print_consts.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 content::URLFetcher::POST, | 45 content::URLFetcher::POST, |
46 delegate, | 46 delegate, |
47 max_retries, | 47 max_retries, |
48 post_data_mime_type, | 48 post_data_mime_type, |
49 post_data, | 49 post_data, |
50 additional_headers); | 50 additional_headers); |
51 } | 51 } |
52 | 52 |
53 void CloudPrintURLFetcher::OnURLFetchComplete( | 53 void CloudPrintURLFetcher::OnURLFetchComplete( |
54 const content::URLFetcher* source) { | 54 const content::URLFetcher* source) { |
55 VLOG(1) << "CP_PROXY: OnURLFetchComplete, url: " << source->GetUrl() | 55 VLOG(1) << "CP_PROXY: OnURLFetchComplete, url: " << source->GetURL() |
56 << ", response code: " << source->GetResponseCode(); | 56 << ", response code: " << source->GetResponseCode(); |
57 // Make sure we stay alive through the body of this function. | 57 // Make sure we stay alive through the body of this function. |
58 scoped_refptr<CloudPrintURLFetcher> keep_alive(this); | 58 scoped_refptr<CloudPrintURLFetcher> keep_alive(this); |
59 std::string data; | 59 std::string data; |
60 source->GetResponseAsString(&data); | 60 source->GetResponseAsString(&data); |
61 ResponseAction action = delegate_->HandleRawResponse( | 61 ResponseAction action = delegate_->HandleRawResponse( |
62 source, | 62 source, |
63 source->GetUrl(), | 63 source->GetURL(), |
64 source->GetStatus(), | 64 source->GetStatus(), |
65 source->GetResponseCode(), | 65 source->GetResponseCode(), |
66 source->GetCookies(), | 66 source->GetCookies(), |
67 data); | 67 data); |
68 if (action == CONTINUE_PROCESSING) { | 68 if (action == CONTINUE_PROCESSING) { |
69 // If we are not using an OAuth token, and we got an auth error, we are | 69 // If we are not using an OAuth token, and we got an auth error, we are |
70 // done. Else, the token may have been refreshed. Let us try again. | 70 // done. Else, the token may have been refreshed. Let us try again. |
71 if ((RC_FORBIDDEN == source->GetResponseCode()) && | 71 if ((RC_FORBIDDEN == source->GetResponseCode()) && |
72 (!CloudPrintTokenStore::current() || | 72 (!CloudPrintTokenStore::current() || |
73 !CloudPrintTokenStore::current()->token_is_oauth())) { | 73 !CloudPrintTokenStore::current()->token_is_oauth())) { |
74 delegate_->OnRequestAuthError(); | 74 delegate_->OnRequestAuthError(); |
75 return; | 75 return; |
76 } | 76 } |
77 // We need to retry on all network errors. | 77 // We need to retry on all network errors. |
78 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) | 78 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) |
79 action = RETRY_REQUEST; | 79 action = RETRY_REQUEST; |
80 else | 80 else |
81 action = delegate_->HandleRawData(source, source->GetUrl(), data); | 81 action = delegate_->HandleRawData(source, source->GetURL(), data); |
82 | 82 |
83 if (action == CONTINUE_PROCESSING) { | 83 if (action == CONTINUE_PROCESSING) { |
84 // If the delegate is not interested in handling the raw response data, | 84 // If the delegate is not interested in handling the raw response data, |
85 // we assume that a JSON response is expected. If we do not get a JSON | 85 // we assume that a JSON response is expected. If we do not get a JSON |
86 // response, we will retry (to handle the case where we got redirected | 86 // response, we will retry (to handle the case where we got redirected |
87 // to a non-cloudprint-server URL eg. for authentication). | 87 // to a non-cloudprint-server URL eg. for authentication). |
88 bool succeeded = false; | 88 bool succeeded = false; |
89 DictionaryValue* response_dict = NULL; | 89 DictionaryValue* response_dict = NULL; |
90 CloudPrintHelpers::ParseResponseJSON(data, &succeeded, &response_dict); | 90 CloudPrintHelpers::ParseResponseJSON(data, &succeeded, &response_dict); |
91 if (response_dict) | 91 if (response_dict) |
92 action = delegate_->HandleJSONData(source, | 92 action = delegate_->HandleJSONData(source, |
93 source->GetUrl(), | 93 source->GetURL(), |
94 response_dict, | 94 response_dict, |
95 succeeded); | 95 succeeded); |
96 else | 96 else |
97 action = RETRY_REQUEST; | 97 action = RETRY_REQUEST; |
98 } | 98 } |
99 } | 99 } |
100 // Retry the request if needed. | 100 // Retry the request if needed. |
101 if (action == RETRY_REQUEST) { | 101 if (action == RETRY_REQUEST) { |
102 // Explicitly call ReceivedContentWasMalformed() to ensure the current | 102 // Explicitly call ReceivedContentWasMalformed() to ensure the current |
103 // request gets counted as a failure for calculation of the back-off | 103 // request gets counted as a failure for calculation of the back-off |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 166 |
167 net::URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { | 167 net::URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() { |
168 ServiceURLRequestContextGetter* getter = | 168 ServiceURLRequestContextGetter* getter = |
169 g_service_process->GetServiceURLRequestContextGetter(); | 169 g_service_process->GetServiceURLRequestContextGetter(); |
170 // Now set up the user agent for cloudprint. | 170 // Now set up the user agent for cloudprint. |
171 std::string user_agent = getter->user_agent(); | 171 std::string user_agent = getter->user_agent(); |
172 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); | 172 base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent); |
173 getter->set_user_agent(user_agent); | 173 getter->set_user_agent(user_agent); |
174 return getter; | 174 return getter; |
175 } | 175 } |
OLD | NEW |