| 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_number_conversions.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 " \"type\": \"METADATA\"," | 49 " \"type\": \"METADATA\"," |
| 50 " \"value\": \"SNIPPET\"" | 50 " \"value\": \"SNIPPET\"" |
| 51 " }," | 51 " }," |
| 52 " \"content_restricts\": {" | 52 " \"content_restricts\": {" |
| 53 " \"type\": \"METADATA\"," | 53 " \"type\": \"METADATA\"," |
| 54 " \"value\": \"THUMBNAIL\"" | 54 " \"value\": \"THUMBNAIL\"" |
| 55 " }" | 55 " }" |
| 56 "%s" | 56 "%s" |
| 57 " }," | 57 " }," |
| 58 " \"global_scoring_params\": {" | 58 " \"global_scoring_params\": {" |
| 59 " \"num_to_return\": 10" | 59 " \"num_to_return\": %i" |
| 60 " }" | 60 " }" |
| 61 " }" | 61 " }" |
| 62 "}"; | 62 "}"; |
| 63 | 63 |
| 64 const char kHostRestrictFormat[] = | 64 const char kHostRestrictFormat[] = |
| 65 " ,\"content_selectors\": {" | 65 " ,\"content_selectors\": {" |
| 66 " \"type\": \"HOST_RESTRICT\"," | 66 " \"type\": \"HOST_RESTRICT\"," |
| 67 " \"value\": \"%s\"" | 67 " \"value\": \"%s\"" |
| 68 " }"; | 68 " }"; |
| 69 | 69 |
| 70 } // namespace | 70 } // namespace |
| 71 | 71 |
| 72 NTPSnippetsFetcher::NTPSnippetsFetcher( | 72 NTPSnippetsFetcher::NTPSnippetsFetcher( |
| 73 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 73 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 74 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 74 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
| 75 bool is_stable_channel) | 75 bool is_stable_channel) |
| 76 : file_task_runner_(file_task_runner), | 76 : file_task_runner_(file_task_runner), |
| 77 url_request_context_getter_(url_request_context_getter), | 77 url_request_context_getter_(url_request_context_getter), |
| 78 is_stable_channel_(is_stable_channel) {} | 78 is_stable_channel_(is_stable_channel) {} |
| 79 | 79 |
| 80 NTPSnippetsFetcher::~NTPSnippetsFetcher() { | 80 NTPSnippetsFetcher::~NTPSnippetsFetcher() {} |
| 81 } | |
| 82 | 81 |
| 83 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | 82 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> |
| 84 NTPSnippetsFetcher::AddCallback(const SnippetsAvailableCallback& callback) { | 83 NTPSnippetsFetcher::AddCallback(const SnippetsAvailableCallback& callback) { |
| 85 return callback_list_.Add(callback); | 84 return callback_list_.Add(callback); |
| 86 } | 85 } |
| 87 | 86 |
| 88 void NTPSnippetsFetcher::FetchSnippets(const std::set<std::string>& hosts) { | 87 void NTPSnippetsFetcher::FetchSnippets(const std::set<std::string>& hosts, |
| 88 int count) { |
| 89 // TODO(treib): What to do if there's already a pending request? | 89 // TODO(treib): What to do if there's already a pending request? |
| 90 const std::string& key = is_stable_channel_ | 90 const std::string& key = is_stable_channel_ |
| 91 ? google_apis::GetAPIKey() | 91 ? google_apis::GetAPIKey() |
| 92 : google_apis::GetNonStableAPIKey(); | 92 : google_apis::GetNonStableAPIKey(); |
| 93 std::string url = | 93 std::string url = |
| 94 base::StringPrintf(kContentSnippetsServerFormat, key.c_str()); | 94 base::StringPrintf(kContentSnippetsServerFormat, key.c_str()); |
| 95 url_fetcher_ = URLFetcher::Create(GURL(url), URLFetcher::POST, this); | 95 url_fetcher_ = URLFetcher::Create(GURL(url), URLFetcher::POST, this); |
| 96 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); | 96 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); |
| 97 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 97 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 98 net::LOAD_DO_NOT_SAVE_COOKIES); | 98 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 99 HttpRequestHeaders headers; | 99 HttpRequestHeaders headers; |
| 100 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); | 100 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); |
| 101 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); | 101 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); |
| 102 std::string host_restricts; | 102 std::string host_restricts; |
| 103 for (const std::string& host : hosts) | 103 for (const std::string& host : hosts) |
| 104 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); | 104 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); |
| 105 url_fetcher_->SetUploadData("application/json", | 105 url_fetcher_->SetUploadData("application/json", |
| 106 base::StringPrintf(kRequestParameterFormat, | 106 base::StringPrintf(kRequestParameterFormat, |
| 107 host_restricts.c_str())); | 107 host_restricts.c_str(), |
| 108 count)); |
| 108 | 109 |
| 109 // Fetchers are sometimes cancelled because a network change was detected. | 110 // Fetchers are sometimes cancelled because a network change was detected. |
| 110 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | 111 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); |
| 111 // Try to make fetching the files bit more robust even with poor connection. | 112 // Try to make fetching the files bit more robust even with poor connection. |
| 112 url_fetcher_->SetMaxRetriesOn5xx(3); | 113 url_fetcher_->SetMaxRetriesOn5xx(3); |
| 113 url_fetcher_->Start(); | 114 url_fetcher_->Start(); |
| 114 } | 115 } |
| 115 | 116 |
| 116 //////////////////////////////////////////////////////////////////////////////// | 117 //////////////////////////////////////////////////////////////////////////////// |
| 117 // URLFetcherDelegate overrides | 118 // URLFetcherDelegate overrides |
| (...skipping 17 matching lines...) Expand all Loading... |
| 135 | 136 |
| 136 } else { | 137 } else { |
| 137 bool stores_result_to_string = source->GetResponseAsString(&response); | 138 bool stores_result_to_string = source->GetResponseAsString(&response); |
| 138 DCHECK(stores_result_to_string); | 139 DCHECK(stores_result_to_string); |
| 139 } | 140 } |
| 140 | 141 |
| 141 callback_list_.Notify(response, message); | 142 callback_list_.Notify(response, message); |
| 142 } | 143 } |
| 143 | 144 |
| 144 } // namespace ntp_snippets | 145 } // namespace ntp_snippets |
| OLD | NEW |