| 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 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 CategoryFactory* category_factory, | 83 CategoryFactory* category_factory, |
| 84 const ParseJSONCallback& parse_json_callback, | 84 const ParseJSONCallback& parse_json_callback, |
| 85 bool is_stable_channel); | 85 bool is_stable_channel); |
| 86 ~NTPSnippetsFetcher() override; | 86 ~NTPSnippetsFetcher() override; |
| 87 | 87 |
| 88 // Set a callback that is called when a new set of snippets are downloaded, | 88 // Set a callback that is called when a new set of snippets are downloaded, |
| 89 // overriding any previously set callback. | 89 // overriding any previously set callback. |
| 90 void SetCallback(const SnippetsAvailableCallback& callback); | 90 void SetCallback(const SnippetsAvailableCallback& callback); |
| 91 | 91 |
| 92 // Fetches snippets from the server. |hosts| restricts the results to a set of | 92 // Fetches snippets from the server. |hosts| restricts the results to a set of |
| 93 // hosts, e.g. "www.google.com". An empty host set produces an error. | 93 // hosts, e.g. "www.google.com". If host restrictions are enabled, an empty |
| 94 // host set produces an error without issuing a fetch. |
| 95 // |
| 96 // |excluded_ids| will be reported to the server; the server should not return |
| 97 // suggestions with those IDs. |
| 94 // | 98 // |
| 95 // If an ongoing fetch exists, it will be cancelled and a new one started, | 99 // If an ongoing fetch exists, it will be cancelled and a new one started, |
| 96 // without triggering an additional callback (i.e. not noticeable by | 100 // without triggering an additional callback (i.e. not noticeable by |
| 97 // subscriber of SetCallback()). | 101 // subscriber of SetCallback()). |
| 98 // | 102 // |
| 99 // Fetches snippets only if the daily quota not exceeded, unless | 103 // Fetches snippets only if the daily quota not exceeded, unless |
| 100 // |interactive_request| is set to true (use only for user-initiated fetches). | 104 // |interactive_request| is set to true (use only for user-initiated fetches). |
| 101 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, | 105 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, |
| 102 const std::string& language_code, | 106 const std::string& language_code, |
| 107 const std::set<std::string>& excluded_ids, |
| 103 int count, | 108 int count, |
| 104 bool interactive_request); | 109 bool interactive_request); |
| 105 | 110 |
| 106 // Debug string representing the status/result of the last fetch attempt. | 111 // Debug string representing the status/result of the last fetch attempt. |
| 107 const std::string& last_status() const { return last_status_; } | 112 const std::string& last_status() const { return last_status_; } |
| 108 | 113 |
| 109 // Returns the last JSON fetched from the server. | 114 // Returns the last JSON fetched from the server. |
| 110 const std::string& last_json() const { | 115 const std::string& last_json() const { |
| 111 return last_fetch_json_; | 116 return last_fetch_json_; |
| 112 } | 117 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 127 tick_clock_ = std::move(tick_clock); | 132 tick_clock_ = std::move(tick_clock); |
| 128 } | 133 } |
| 129 | 134 |
| 130 void SetPersonalizationForTesting(Personalization personalization) { | 135 void SetPersonalizationForTesting(Personalization personalization) { |
| 131 personalization_ = personalization; | 136 personalization_ = personalization; |
| 132 } | 137 } |
| 133 | 138 |
| 134 private: | 139 private: |
| 135 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestAuthenticated); | 140 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestAuthenticated); |
| 136 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestUnauthenticated); | 141 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestUnauthenticated); |
| 142 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestExcludedIds); |
| 137 | 143 |
| 138 enum FetchAPI { | 144 enum FetchAPI { |
| 139 CHROME_READER_API, | 145 CHROME_READER_API, |
| 140 CHROME_CONTENT_SUGGESTIONS_API, | 146 CHROME_CONTENT_SUGGESTIONS_API, |
| 141 }; | 147 }; |
| 142 | 148 |
| 143 struct RequestParams { | 149 struct RequestParams { |
| 144 FetchAPI fetch_api; | 150 FetchAPI fetch_api; |
| 145 std::string obfuscated_gaia_id; | 151 std::string obfuscated_gaia_id; |
| 146 bool only_return_personalized_results; | 152 bool only_return_personalized_results; |
| 147 std::string user_locale; | 153 std::string user_locale; |
| 148 std::set<std::string> host_restricts; | 154 std::set<std::string> host_restricts; |
| 155 std::set<std::string> excluded_ids; |
| 149 int count_to_fetch; | 156 int count_to_fetch; |
| 150 | 157 |
| 151 RequestParams(); | 158 RequestParams(); |
| 152 ~RequestParams(); | 159 ~RequestParams(); |
| 153 | 160 |
| 154 std::string BuildRequest(); | 161 std::string BuildRequest(); |
| 155 }; | 162 }; |
| 156 | 163 |
| 157 void FetchSnippetsImpl(const GURL& url, | 164 void FetchSnippetsImpl(const GURL& url, |
| 158 const std::string& auth_header, | 165 const std::string& auth_header, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 201 |
| 195 CategoryFactory* const category_factory_; | 202 CategoryFactory* const category_factory_; |
| 196 const ParseJSONCallback parse_json_callback_; | 203 const ParseJSONCallback parse_json_callback_; |
| 197 base::TimeTicks fetch_start_time_; | 204 base::TimeTicks fetch_start_time_; |
| 198 std::string last_status_; | 205 std::string last_status_; |
| 199 std::string last_fetch_json_; | 206 std::string last_fetch_json_; |
| 200 | 207 |
| 201 // Hosts to restrict the snippets to. | 208 // Hosts to restrict the snippets to. |
| 202 std::set<std::string> hosts_; | 209 std::set<std::string> hosts_; |
| 203 | 210 |
| 211 // Snippets to exclude from the results. |
| 212 std::set<std::string> excluded_ids_; |
| 213 |
| 204 // Count of snippets to fetch. | 214 // Count of snippets to fetch. |
| 205 int count_to_fetch_; | 215 int count_to_fetch_; |
| 206 | 216 |
| 207 // Language code to restrict to for personalized results. | 217 // Language code to restrict to for personalized results. |
| 208 std::string locale_; | 218 std::string locale_; |
| 209 | 219 |
| 210 // API endpoint for fetching snippets. | 220 // API endpoint for fetching snippets. |
| 211 const GURL fetch_url_; | 221 const GURL fetch_url_; |
| 212 // Which API to use | 222 // Which API to use |
| 213 const FetchAPI fetch_api_; | 223 const FetchAPI fetch_api_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 235 // When a token request gets canceled, we want to retry once. | 245 // When a token request gets canceled, we want to retry once. |
| 236 bool oauth_token_retried_; | 246 bool oauth_token_retried_; |
| 237 | 247 |
| 238 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 248 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
| 239 | 249 |
| 240 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 250 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 241 }; | 251 }; |
| 242 } // namespace ntp_snippets | 252 } // namespace ntp_snippets |
| 243 | 253 |
| 244 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 254 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |