OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/remote/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/remote/ntp_snippets_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 } | 176 } |
177 int num_snippets = snippets->size(); | 177 int num_snippets = snippets->size(); |
178 // Remove snippets that do not have all the info we need to display it to | 178 // Remove snippets that do not have all the info we need to display it to |
179 // the user. | 179 // the user. |
180 snippets->erase( | 180 snippets->erase( |
181 std::remove_if(snippets->begin(), snippets->end(), | 181 std::remove_if(snippets->begin(), snippets->end(), |
182 [](const std::unique_ptr<NTPSnippet>& snippet) { | 182 [](const std::unique_ptr<NTPSnippet>& snippet) { |
183 return !snippet->is_complete(); | 183 return !snippet->is_complete(); |
184 }), | 184 }), |
185 snippets->end()); | 185 snippets->end()); |
186 int num_snippets_dismissed = num_snippets - snippets->size(); | 186 int num_snippets_removed = num_snippets - snippets->size(); |
187 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", | 187 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", |
188 num_snippets_dismissed > 0); | 188 num_snippets_removed > 0); |
189 if (num_snippets_dismissed > 0) { | 189 if (num_snippets_removed > 0) { |
190 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", | 190 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", |
191 num_snippets_dismissed); | 191 num_snippets_removed); |
192 } | 192 } |
193 } | 193 } |
194 | 194 |
195 std::vector<ContentSuggestion> ConvertToContentSuggestions( | 195 std::vector<ContentSuggestion> ConvertToContentSuggestions( |
196 Category category, | 196 Category category, |
197 const NTPSnippet::PtrVector& snippets) { | 197 const NTPSnippet::PtrVector& snippets) { |
198 std::vector<ContentSuggestion> result; | 198 std::vector<ContentSuggestion> result; |
199 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) { | 199 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) { |
200 // TODO(sfiera): if a snippet is not going to be displayed, move it | 200 // TODO(sfiera): if a snippet is not going to be displayed, move it |
201 // directly to content.dismissed on fetch. Otherwise, we might prune | 201 // directly to content.dismissed on fetch. Otherwise, we might prune |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 if (ready()) | 297 if (ready()) |
298 FetchSnippetsFromHosts(std::set<std::string>(), interactive_request); | 298 FetchSnippetsFromHosts(std::set<std::string>(), interactive_request); |
299 else | 299 else |
300 fetch_when_ready_ = true; | 300 fetch_when_ready_ = true; |
301 } | 301 } |
302 | 302 |
303 void NTPSnippetsService::FetchSnippetsFromHosts( | 303 void NTPSnippetsService::FetchSnippetsFromHosts( |
304 const std::set<std::string>& hosts, | 304 const std::set<std::string>& hosts, |
305 bool interactive_request) { | 305 bool interactive_request) { |
306 FetchSnippetsFromHostsImpl(hosts, interactive_request, | 306 FetchSnippetsFromHostsImpl(hosts, interactive_request, |
307 /*fetch_more=*/false, FetchedMoreCallback(), | 307 /*fetch_more=*/false, std::set<std::string>(), |
308 base::Optional<Category>()); | 308 base::Optional<Category>(), FetchingCallback()); |
309 } | 309 } |
310 | 310 |
311 void NTPSnippetsService::FetchMore(const Category& category, | 311 void NTPSnippetsService::Fetch(const Category& category, |
312 FetchedMoreCallback callback) { | 312 std::set<std::string> known_suggestion_ids, |
313 FetchingCallback callback) { | |
313 FetchSnippetsFromHostsImpl(std::set<std::string>(), | 314 FetchSnippetsFromHostsImpl(std::set<std::string>(), |
314 /*interactive_request=*/true, | 315 /*interactive_request=*/true, |
315 /*fetch_more=*/true, callback, | 316 /*fetch_more=*/true, |
316 base::Optional<Category>(category)); | 317 std::move(known_suggestion_ids), |
318 base::Optional<Category>(category), callback); | |
317 } | 319 } |
318 | 320 |
319 void NTPSnippetsService::FetchSnippetsFromHostsImpl( | 321 void NTPSnippetsService::FetchSnippetsFromHostsImpl( |
320 const std::set<std::string>& hosts, | 322 const std::set<std::string>& hosts, |
321 bool interactive_request, | 323 bool interactive_request, |
322 bool fetch_more, | 324 bool fetch_more, |
323 FetchedMoreCallback callback, | 325 std::set<std::string> known_suggestion_ids, |
324 base::Optional<Category> exclusive_category) { | 326 base::Optional<Category> exclusive_category, |
327 FetchingCallback callback) { | |
325 if (!ready()) { | 328 if (!ready()) { |
326 // TODO(fhorschig): Call |callback| if it's non-null. | 329 if (!callback.is_null()) |
330 std::move(callback).Run(std::vector<ContentSuggestion>()); | |
dgn
2016/11/02 11:10:11
why is std::move necessary here?
dgn
2016/11/02 12:04:33
Okay, this is explained in https://chromium.google
fhorschig
2016/11/03 01:53:14
Thank you very much!
You discovered (maybe unwilli
| |
327 return; | 331 return; |
328 } | 332 } |
329 | 333 |
330 MarkEmptyCategoriesAsLoading(); | 334 MarkEmptyCategoriesAsLoading(); |
331 | 335 |
332 NTPSnippetsFetcher::Params params; | 336 NTPSnippetsFetcher::Params params; |
333 params.language_code = application_language_code_; | 337 params.language_code = application_language_code_; |
334 params.excluded_ids = CollectIdsToExclude(fetch_more); | 338 params.excluded_ids = |
339 CollectIdsToExclude(fetch_more, std::move(known_suggestion_ids)); | |
335 params.count_to_fetch = kMaxSnippetCount; | 340 params.count_to_fetch = kMaxSnippetCount; |
336 params.hosts = hosts; | 341 params.hosts = hosts; |
337 params.interactive_request = interactive_request; | 342 params.interactive_request = interactive_request; |
338 params.exclusive_category = std::move(exclusive_category); | 343 params.exclusive_category = std::move(exclusive_category); |
339 | 344 |
340 snippets_fetcher_->FetchSnippets( | 345 snippets_fetcher_->FetchSnippets( |
341 params, base::BindOnce(&NTPSnippetsService::OnFetchFinished, | 346 params, base::BindOnce(&NTPSnippetsService::OnFetchFinished, |
342 base::Unretained(this), fetch_more, callback)); | 347 base::Unretained(this), fetch_more, callback)); |
343 } | 348 } |
344 | 349 |
345 std::set<std::string> NTPSnippetsService::CollectIdsToExclude( | 350 std::set<std::string> NTPSnippetsService::CollectIdsToExclude( |
346 bool fetch_more) const { | 351 bool fetch_more, |
352 std::set<std::string> additional_ids) const { | |
347 std::set<std::string> ids; | 353 std::set<std::string> ids; |
348 for (const auto& item : categories_) { | 354 for (const auto& item : categories_) { |
349 const CategoryContent& content = item.second; | 355 const CategoryContent& content = item.second; |
350 for (const auto& snippet : content.dismissed) | 356 for (const auto& snippet : content.dismissed) |
351 ids.insert(snippet->id()); | 357 ids.insert(snippet->id()); |
352 if (!fetch_more) | 358 if (!fetch_more) |
353 continue; | 359 continue; |
354 for (const auto& snippet : content.archived) | 360 for (const auto& snippet : content.archived) |
355 ids.insert(snippet->id()); | 361 ids.insert(snippet->id()); |
356 for (const auto& snippet : content.snippets) | |
357 ids.insert(snippet->id()); | |
358 } | 362 } |
363 std::move(additional_ids.begin(), additional_ids.end(), | |
364 std::insert_iterator<std::set<std::string>>(ids, ids.end())); | |
359 return ids; | 365 return ids; |
360 } | 366 } |
361 | 367 |
362 void NTPSnippetsService::MarkEmptyCategoriesAsLoading() { | 368 void NTPSnippetsService::MarkEmptyCategoriesAsLoading() { |
363 for (const auto& item : categories_) { | 369 for (const auto& item : categories_) { |
364 Category category = item.first; | 370 Category category = item.first; |
365 const CategoryContent& content = item.second; | 371 const CategoryContent& content = item.second; |
366 if (content.snippets.empty()) | 372 if (content.snippets.empty()) |
367 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE_LOADING); | 373 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE_LOADING); |
368 } | 374 } |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
624 FinishInitialization(); | 630 FinishInitialization(); |
625 } | 631 } |
626 | 632 |
627 void NTPSnippetsService::OnDatabaseError() { | 633 void NTPSnippetsService::OnDatabaseError() { |
628 EnterState(State::ERROR_OCCURRED); | 634 EnterState(State::ERROR_OCCURRED); |
629 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); | 635 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); |
630 } | 636 } |
631 | 637 |
632 void NTPSnippetsService::OnFetchFinished( | 638 void NTPSnippetsService::OnFetchFinished( |
633 bool fetched_more, | 639 bool fetched_more, |
634 FetchedMoreCallback fetched_more_callback, | 640 FetchingCallback fetched_more_callback, |
dgn
2016/11/02 11:10:11
Can this argument be a base::Optional<FetchingCall
fhorschig
2016/11/03 01:53:14
Done.
Yes. This whole set of parameters is planne
| |
635 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) { | 641 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) { |
636 if (!ready()) | 642 if (!ready()) |
637 return; | 643 return; |
638 | 644 |
639 // TODO(fhorschig): Check which of the things here should actually happen for | 645 // TODO(fhorschig): Check which of the things here should actually happen for |
640 // |fetch_more| requests. Maybe it makes sense to have two separate | 646 // |fetch_more| requests. Maybe it makes sense to have two separate |
641 // "Finished" methods? | 647 // "Finished" methods? |
642 | 648 |
643 // Mark all categories as not provided by the server in the latest fetch. The | 649 // Mark all categories as not provided by the server in the latest fetch. The |
644 // ones we got will be marked again below. | 650 // ones we got will be marked again below. |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1065 | 1071 |
1066 std::vector<ContentSuggestion> result = | 1072 std::vector<ContentSuggestion> result = |
1067 ConvertToContentSuggestions(category, content.snippets); | 1073 ConvertToContentSuggestions(category, content.snippets); |
1068 | 1074 |
1069 DVLOG(1) << "NotifyNewSuggestions(): " << result.size() | 1075 DVLOG(1) << "NotifyNewSuggestions(): " << result.size() |
1070 << " items in category " << category; | 1076 << " items in category " << category; |
1071 observer()->OnNewSuggestions(this, category, std::move(result)); | 1077 observer()->OnNewSuggestions(this, category, std::move(result)); |
1072 } | 1078 } |
1073 | 1079 |
1074 void NTPSnippetsService::NotifyMoreSuggestions(Category category, | 1080 void NTPSnippetsService::NotifyMoreSuggestions(Category category, |
1075 FetchedMoreCallback callback) { | 1081 FetchingCallback callback) { |
1076 DCHECK(base::ContainsKey(categories_, category)); | 1082 DCHECK(base::ContainsKey(categories_, category)); |
1077 const CategoryContent& content = categories_[category]; | 1083 const CategoryContent& content = categories_[category]; |
1078 DCHECK(IsCategoryStatusAvailable(content.status)); | 1084 DCHECK(IsCategoryStatusAvailable(content.status)); |
1079 | 1085 |
1080 std::vector<ContentSuggestion> result = | 1086 std::vector<ContentSuggestion> result = |
1081 ConvertToContentSuggestions(category, content.snippets); | 1087 ConvertToContentSuggestions(category, content.snippets); |
1082 | 1088 |
1083 DVLOG(1) << "NotifyMoreSuggestions(): " << result.size() | 1089 DVLOG(1) << "NotifyMoreSuggestions(): " << result.size() |
1084 << " items in category " << category; | 1090 << " items in category " << category; |
1085 DCHECK(!callback.is_null()); | 1091 DCHECK(!callback.is_null()); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1196 } | 1202 } |
1197 | 1203 |
1198 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1204 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
1199 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1205 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
1200 default; | 1206 default; |
1201 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1207 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
1202 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1208 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
1203 operator=(CategoryContent&&) = default; | 1209 operator=(CategoryContent&&) = default; |
1204 | 1210 |
1205 } // namespace ntp_snippets | 1211 } // namespace ntp_snippets |
OLD | NEW |