| 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 |
| 27 const char kContentSnippetsServerFormat[] = | 33 const char kContentSnippetsServerFormat[] = |
| 28 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; | 34 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; |
| 29 | 35 |
| 30 const char kRequestParameterFormat[] = | 36 const char kRequestParameterFormat[] = |
| 31 "{" | 37 "{" |
| 32 " \"response_detail_level\": \"STANDARD\"," | 38 " \"response_detail_level\": \"STANDARD\"," |
| 33 " \"advanced_options\": {" | 39 " \"advanced_options\": {" |
| 34 " \"local_scoring_params\": {" | 40 " \"local_scoring_params\": {" |
| 35 " \"content_params\": {" | 41 " \"content_params\": {" |
| 36 " \"only_return_personalized_results\": false" | 42 " \"only_return_personalized_results\": false" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 54 " }" | 60 " }" |
| 55 " }" | 61 " }" |
| 56 "}"; | 62 "}"; |
| 57 | 63 |
| 58 const char kHostRestrictFormat[] = | 64 const char kHostRestrictFormat[] = |
| 59 " ,\"content_selectors\": {" | 65 " ,\"content_selectors\": {" |
| 60 " \"type\": \"HOST_RESTRICT\"," | 66 " \"type\": \"HOST_RESTRICT\"," |
| 61 " \"value\": \"%s\"" | 67 " \"value\": \"%s\"" |
| 62 " }"; | 68 " }"; |
| 63 | 69 |
| 70 } // namespace |
| 71 |
| 64 NTPSnippetsFetcher::NTPSnippetsFetcher( | 72 NTPSnippetsFetcher::NTPSnippetsFetcher( |
| 65 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 73 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 66 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 74 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
| 67 bool is_stable_channel) | 75 bool is_stable_channel) |
| 68 : file_task_runner_(file_task_runner), | 76 : file_task_runner_(file_task_runner), |
| 69 url_request_context_getter_(url_request_context_getter), | 77 url_request_context_getter_(url_request_context_getter), |
| 70 is_stable_channel_(is_stable_channel) {} | 78 is_stable_channel_(is_stable_channel) {} |
| 71 | 79 |
| 72 NTPSnippetsFetcher::~NTPSnippetsFetcher() { | 80 NTPSnippetsFetcher::~NTPSnippetsFetcher() { |
| 73 } | 81 } |
| (...skipping 29 matching lines...) Expand all 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 |