Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/ntp_snippets/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 13 #include "google_apis/google_api_keys.h" | 14 #include "google_apis/google_api_keys.h" |
| 14 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 15 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
| 16 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 17 #include "net/http/http_status_code.h" | 18 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
| 19 | 20 |
| 20 using net::URLFetcher; | 21 using net::URLFetcher; |
| 21 using net::URLRequestContextGetter; | 22 using net::URLRequestContextGetter; |
| 22 using net::HttpRequestHeaders; | 23 using net::HttpRequestHeaders; |
| 23 using net::URLRequestStatus; | 24 using net::URLRequestStatus; |
| 24 | 25 |
| 25 namespace ntp_snippets { | 26 namespace ntp_snippets { |
| 26 | 27 |
| 28 namespace { | |
| 29 | |
| 30 const char kStatusMessageURLRequestErrorFormat[] = "URLRequestStatus error %d"; | |
| 31 const char kStatusMessageHTTPErrorFormat[] = "HTTP error %d"; | |
| 32 | |
| 33 } // namespace | |
|
Bernhard Bauer
2016/04/22 12:54:00
If you use an anonymous namespace here, include th
jkrcal
2016/04/22 13:04:37
Done.
| |
| 34 | |
| 27 const char kContentSnippetsServerFormat[] = | 35 const char kContentSnippetsServerFormat[] = |
| 28 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; | 36 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; |
| 29 | 37 |
| 30 const char kRequestParameterFormat[] = | 38 const char kRequestParameterFormat[] = |
| 31 "{" | 39 "{" |
| 32 " \"response_detail_level\": \"STANDARD\"," | 40 " \"response_detail_level\": \"STANDARD\"," |
| 33 " \"advanced_options\": {" | 41 " \"advanced_options\": {" |
| 34 " \"local_scoring_params\": {" | 42 " \"local_scoring_params\": {" |
| 35 " \"content_params\": {" | 43 " \"content_params\": {" |
| 36 " \"only_return_personalized_results\": false" | 44 " \"only_return_personalized_results\": false" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 // Try to make fetching the files bit more robust even with poor connection. | 111 // Try to make fetching the files bit more robust even with poor connection. |
| 104 url_fetcher_->SetMaxRetriesOn5xx(3); | 112 url_fetcher_->SetMaxRetriesOn5xx(3); |
| 105 url_fetcher_->Start(); | 113 url_fetcher_->Start(); |
| 106 } | 114 } |
| 107 | 115 |
| 108 //////////////////////////////////////////////////////////////////////////////// | 116 //////////////////////////////////////////////////////////////////////////////// |
| 109 // URLFetcherDelegate overrides | 117 // URLFetcherDelegate overrides |
| 110 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { | 118 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { |
| 111 DCHECK_EQ(url_fetcher_.get(), source); | 119 DCHECK_EQ(url_fetcher_.get(), source); |
| 112 | 120 |
| 121 std::string message; | |
| 113 const URLRequestStatus& status = source->GetStatus(); | 122 const URLRequestStatus& status = source->GetStatus(); |
| 114 if (!status.is_success()) { | 123 if (!status.is_success()) { |
| 115 DLOG(WARNING) << "URLRequestStatus error " << status.error() | 124 message = base::StringPrintf(kStatusMessageURLRequestErrorFormat, |
| 116 << " while trying to download " << source->GetURL().spec(); | 125 status.error()); |
| 117 return; | 126 } else if (source->GetResponseCode() != net::HTTP_OK) { |
| 118 } | 127 message = base::StringPrintf(kStatusMessageHTTPErrorFormat, |
| 119 | 128 source->GetResponseCode()); |
| 120 int response_code = source->GetResponseCode(); | |
| 121 if (response_code != net::HTTP_OK) { | |
| 122 DLOG(WARNING) << "HTTP error " << response_code | |
| 123 << " while trying to download " << source->GetURL().spec(); | |
| 124 return; | |
| 125 } | 129 } |
| 126 | 130 |
| 127 std::string response; | 131 std::string response; |
| 128 bool stores_result_to_string = source->GetResponseAsString(&response); | 132 if (!message.empty()) { |
| 129 DCHECK(stores_result_to_string); | 133 DLOG(WARNING) << message << " while trying to download " |
| 134 << source->GetURL().spec(); | |
| 130 | 135 |
| 131 callback_list_.Notify(response); | 136 } else { |
| 137 bool stores_result_to_string = source->GetResponseAsString(&response); | |
| 138 DCHECK(stores_result_to_string); | |
| 139 } | |
| 140 | |
| 141 callback_list_.Notify(response, message); | |
| 132 } | 142 } |
| 133 | 143 |
| 134 } // namespace ntp_snippets | 144 } // namespace ntp_snippets |
| OLD | NEW |