Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Side by Side Diff: chrome/browser/ui/webui/snippets_internals_message_handler.cc

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Tim's comments + fix ios compilation Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/ui/webui/snippets_internals_message_handler.h" 5 #include "chrome/browser/ui/webui/snippets_internals_message_handler.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 10 matching lines...) Expand all
21 #include "base/strings/string_split.h" 21 #include "base/strings/string_split.h"
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "base/values.h" 23 #include "base/values.h"
24 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" 24 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h"
25 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/common/chrome_features.h" 26 #include "chrome/common/chrome_features.h"
27 #include "components/ntp_snippets/category.h" 27 #include "components/ntp_snippets/category.h"
28 #include "components/ntp_snippets/category_info.h" 28 #include "components/ntp_snippets/category_info.h"
29 #include "components/ntp_snippets/features.h" 29 #include "components/ntp_snippets/features.h"
30 #include "components/ntp_snippets/pref_names.h" 30 #include "components/ntp_snippets/pref_names.h"
31 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h"
31 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" 32 #include "components/ntp_snippets/remote/remote_suggestions_provider.h"
32 #include "components/ntp_snippets/switches.h" 33 #include "components/ntp_snippets/switches.h"
33 #include "components/prefs/pref_service.h" 34 #include "components/prefs/pref_service.h"
34 #include "content/public/browser/web_ui.h" 35 #include "content/public/browser/web_ui.h"
35 36
36 using ntp_snippets::ContentSuggestion; 37 using ntp_snippets::ContentSuggestion;
37 using ntp_snippets::Category; 38 using ntp_snippets::Category;
38 using ntp_snippets::CategoryInfo; 39 using ntp_snippets::CategoryInfo;
39 using ntp_snippets::CategoryStatus; 40 using ntp_snippets::CategoryStatus;
40 using ntp_snippets::KnownCategories; 41 using ntp_snippets::KnownCategories;
42 using ntp_snippets::RemoteSuggestionsProvider;
41 using ntp_snippets::UserClassifier; 43 using ntp_snippets::UserClassifier;
42 44
43 namespace { 45 namespace {
44 46
45 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( 47 std::unique_ptr<base::DictionaryValue> PrepareSuggestion(
46 const ContentSuggestion& suggestion, 48 const ContentSuggestion& suggestion,
47 int index) { 49 int index) {
48 auto entry = base::MakeUnique<base::DictionaryValue>(); 50 auto entry = base::MakeUnique<base::DictionaryValue>();
49 entry->SetString("idWithinCategory", suggestion.id().id_within_category()); 51 entry->SetString("idWithinCategory", suggestion.id().id_within_category());
50 entry->SetString("url", suggestion.url().spec()); 52 entry->SetString("url", suggestion.url().spec());
(...skipping 30 matching lines...) Expand all
81 83
82 } // namespace 84 } // namespace
83 85
84 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler( 86 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler(
85 ntp_snippets::ContentSuggestionsService* content_suggestions_service, 87 ntp_snippets::ContentSuggestionsService* content_suggestions_service,
86 PrefService* pref_service) 88 PrefService* pref_service)
87 : content_suggestions_service_observer_(this), 89 : content_suggestions_service_observer_(this),
88 dom_loaded_(false), 90 dom_loaded_(false),
89 content_suggestions_service_(content_suggestions_service), 91 content_suggestions_service_(content_suggestions_service),
90 remote_suggestions_provider_( 92 remote_suggestions_provider_(
91 content_suggestions_service_->ntp_snippets_service()), 93 content_suggestions_service_
94 ->remote_suggestions_provider_for_debugging()),
92 pref_service_(pref_service), 95 pref_service_(pref_service),
93 weak_ptr_factory_(this) {} 96 weak_ptr_factory_(this) {}
94 97
95 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 98 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
96 99
97 void SnippetsInternalsMessageHandler::RegisterMessages() { 100 void SnippetsInternalsMessageHandler::RegisterMessages() {
98 web_ui()->RegisterMessageCallback( 101 web_ui()->RegisterMessageCallback(
99 "clearCachedSuggestions", 102 "clearCachedSuggestions",
100 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, 103 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
101 base::Unretained(this))); 104 base::Unretained(this)));
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 191
189 void SnippetsInternalsMessageHandler::HandleDownload( 192 void SnippetsInternalsMessageHandler::HandleDownload(
190 const base::ListValue* args) { 193 const base::ListValue* args) {
191 DCHECK_EQ(0u, args->GetSize()); 194 DCHECK_EQ(0u, args->GetSize());
192 195
193 SendString("remote-status", std::string()); 196 SendString("remote-status", std::string());
194 197
195 if (!remote_suggestions_provider_) 198 if (!remote_suggestions_provider_)
196 return; 199 return;
197 200
198 remote_suggestions_provider_->FetchSnippetsForAllCategories(); 201 remote_suggestions_provider_->ReloadSuggestions();
199 } 202 }
200 203
201 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( 204 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
202 const base::ListValue* args) { 205 const base::ListValue* args) {
203 DCHECK_EQ(1u, args->GetSize()); 206 DCHECK_EQ(1u, args->GetSize());
204 207
205 int category_id; 208 int category_id;
206 if (!args->GetInteger(0, &category_id)) 209 if (!args->GetInteger(0, &category_id))
207 return; 210 return;
208 211
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 const base::ListValue* args) { 261 const base::ListValue* args) {
259 DCHECK_EQ(0u, args->GetSize()); 262 DCHECK_EQ(0u, args->GetSize());
260 content_suggestions_service_->user_classifier() 263 content_suggestions_service_->user_classifier()
261 ->ClearClassificationForDebugging(); 264 ->ClearClassificationForDebugging();
262 SendClassification(); 265 SendClassification();
263 } 266 }
264 267
265 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground( 268 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground(
266 const base::ListValue* args) { 269 const base::ListValue* args) {
267 DCHECK_EQ(0u, args->GetSize()); 270 DCHECK_EQ(0u, args->GetSize());
268 remote_suggestions_provider_->FetchSnippetsInTheBackground(); 271 remote_suggestions_provider_->RefetchInTheBackground(
272 std::unique_ptr<RemoteSuggestionsProvider::FetchStatusCallback>(nullptr));
Bernhard Bauer 2016/12/21 10:38:42 Can you see whether just passing nullptr directly
jkrcal 2016/12/21 11:54:40 Much better, thanks!
269 } 273 }
270 274
271 void SnippetsInternalsMessageHandler::SendAllContent() { 275 void SnippetsInternalsMessageHandler::SendAllContent() {
272 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 276 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
273 ntp_snippets::kContentSuggestionsFeature)); 277 ntp_snippets::kContentSuggestionsFeature));
274 SendBoolean( 278 SendBoolean(
275 "flag-article-suggestions", 279 "flag-article-suggestions",
276 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)); 280 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature));
277 SendBoolean("flag-recent-offline-tab-suggestions", 281 SendBoolean("flag-recent-offline-tab-suggestions",
278 base::FeatureList::IsEnabled( 282 base::FeatureList::IsEnabled(
(...skipping 11 matching lines...) Expand all
290 SendBoolean("flag-physical-web-page-suggestions", 294 SendBoolean("flag-physical-web-page-suggestions",
291 base::FeatureList::IsEnabled( 295 base::FeatureList::IsEnabled(
292 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); 296 ntp_snippets::kPhysicalWebPageSuggestionsFeature));
293 297
294 SendClassification(); 298 SendClassification();
295 SendLastRemoteSuggestionsBackgroundFetchTime(); 299 SendLastRemoteSuggestionsBackgroundFetchTime();
296 300
297 if (remote_suggestions_provider_) { 301 if (remote_suggestions_provider_) {
298 // TODO(fhorschig): Read this string from variations directly. 302 // TODO(fhorschig): Read this string from variations directly.
299 SendString("switch-personalized", 303 SendString("switch-personalized",
300 remote_suggestions_provider_->snippets_fetcher() 304 remote_suggestions_provider_
305 ->snippets_fetcher_for_testing_and_debugging()
Bernhard Bauer 2016/12/21 10:38:42 Maybe pull this out into a local variable?
jkrcal 2016/12/21 11:54:40 Done.
301 ->PersonalizationModeString()); 306 ->PersonalizationModeString());
302 307
303 SendString( 308 SendString("switch-fetch-url",
304 "switch-fetch-url", 309 remote_suggestions_provider_
305 remote_suggestions_provider_->snippets_fetcher()->fetch_url().spec()); 310 ->snippets_fetcher_for_testing_and_debugging()
311 ->fetch_url()
312 .spec());
306 web_ui()->CallJavascriptFunctionUnsafe( 313 web_ui()->CallJavascriptFunctionUnsafe(
307 "chrome.SnippetsInternals.receiveJson", 314 "chrome.SnippetsInternals.receiveJson",
308 base::StringValue( 315 base::StringValue(remote_suggestions_provider_
309 remote_suggestions_provider_->snippets_fetcher()->last_json())); 316 ->snippets_fetcher_for_testing_and_debugging()
317 ->last_json()));
310 } 318 }
311 319
312 SendContentSuggestions(); 320 SendContentSuggestions();
313 } 321 }
314 322
315 void SnippetsInternalsMessageHandler::SendClassification() { 323 void SnippetsInternalsMessageHandler::SendClassification() {
316 web_ui()->CallJavascriptFunctionUnsafe( 324 web_ui()->CallJavascriptFunctionUnsafe(
317 "chrome.SnippetsInternals.receiveClassification", 325 "chrome.SnippetsInternals.receiveClassification",
318 base::StringValue(content_suggestions_service_->user_classifier() 326 base::StringValue(content_suggestions_service_->user_classifier()
319 ->GetUserClassDescriptionForDebugging()), 327 ->GetUserClassDescriptionForDebugging()),
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 "dismissed-suggestions-" + base::IntToString(category.id())); 378 "dismissed-suggestions-" + base::IntToString(category.id()));
371 category_entry->SetString("title", info->title()); 379 category_entry->SetString("title", info->title());
372 category_entry->SetString("status", GetCategoryStatusName(status)); 380 category_entry->SetString("status", GetCategoryStatusName(status));
373 category_entry->Set("suggestions", std::move(suggestions_list)); 381 category_entry->Set("suggestions", std::move(suggestions_list));
374 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); 382 category_entry->Set("dismissedSuggestions", std::move(dismissed_list));
375 categories_list->Append(std::move(category_entry)); 383 categories_list->Append(std::move(category_entry));
376 } 384 }
377 385
378 if (remote_suggestions_provider_) { 386 if (remote_suggestions_provider_) {
379 const std::string& status = 387 const std::string& status =
380 remote_suggestions_provider_->snippets_fetcher()->last_status(); 388 remote_suggestions_provider_
389 ->snippets_fetcher_for_testing_and_debugging()
390 ->last_status();
381 if (!status.empty()) 391 if (!status.empty())
382 SendString("remote-status", "Finished: " + status); 392 SendString("remote-status", "Finished: " + status);
383 } 393 }
384 394
385 base::DictionaryValue result; 395 base::DictionaryValue result;
386 result.Set("list", std::move(categories_list)); 396 result.Set("list", std::move(categories_list));
387 web_ui()->CallJavascriptFunctionUnsafe( 397 web_ui()->CallJavascriptFunctionUnsafe(
388 "chrome.SnippetsInternals.receiveContentSuggestions", result); 398 "chrome.SnippetsInternals.receiveContentSuggestions", result);
389 } 399 }
390 400
(...skipping 13 matching lines...) Expand all
404 414
405 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( 415 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded(
406 Category category, 416 Category category,
407 std::vector<ContentSuggestion> dismissed_suggestions) { 417 std::vector<ContentSuggestion> dismissed_suggestions) {
408 if (dismissed_state_[category] == DismissedState::HIDDEN) 418 if (dismissed_state_[category] == DismissedState::HIDDEN)
409 return; 419 return;
410 dismissed_suggestions_[category] = std::move(dismissed_suggestions); 420 dismissed_suggestions_[category] = std::move(dismissed_suggestions);
411 dismissed_state_[category] = DismissedState::VISIBLE; 421 dismissed_state_[category] = DismissedState::VISIBLE;
412 SendContentSuggestions(); 422 SendContentSuggestions();
413 } 423 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698