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

Unified Diff: chrome/browser/ui/webui/snippets_internals_message_handler.cc

Issue 2223963002: Revert of Add per-section clearing and dismissed suggestions to snippets-internals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/snippets_internals_message_handler.cc
diff --git a/chrome/browser/ui/webui/snippets_internals_message_handler.cc b/chrome/browser/ui/webui/snippets_internals_message_handler.cc
index f68733ca36a0bc516ece90651f977ce4036d98dc..2b0d34c00893831e0b9575e10c9576d59cf97fe1 100644
--- a/chrome/browser/ui/webui/snippets_internals_message_handler.cc
+++ b/chrome/browser/ui/webui/snippets_internals_message_handler.cc
@@ -32,6 +32,32 @@
using ntp_snippets::KnownCategories;
namespace {
+
+std::unique_ptr<base::DictionaryValue> PrepareSnippet(
+ const ntp_snippets::NTPSnippet& snippet,
+ int index,
+ bool dismissed) {
+ std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
+ entry->SetString("snippetId", snippet.id());
+ entry->SetString("title", snippet.title());
+ entry->SetString("siteTitle", snippet.best_source().publisher_name);
+ entry->SetString("snippet", snippet.snippet());
+ entry->SetString("published",
+ TimeFormatShortDateAndTime(snippet.publish_date()));
+ entry->SetString("expires",
+ TimeFormatShortDateAndTime(snippet.expiry_date()));
+ entry->SetString("url", snippet.best_source().url.spec());
+ entry->SetString("ampUrl", snippet.best_source().amp_url.spec());
+ entry->SetString("salientImageUrl", snippet.salient_image_url().spec());
+ entry->SetDouble("score", snippet.score());
+
+ if (dismissed)
+ entry->SetString("id", "dismissed-snippet-" + base::IntToString(index));
+ else
+ entry->SetString("id", "snippet-" + base::IntToString(index));
+
+ return entry;
+}
std::unique_ptr<base::DictionaryValue> PrepareSuggestion(
const ContentSuggestion& suggestion,
@@ -95,37 +121,6 @@
SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
-void SnippetsInternalsMessageHandler::RegisterMessages() {
- // additional initialization (web_ui() does not work from the constructor)
- Profile* profile = Profile::FromWebUI(web_ui());
-
- content_suggestions_service_ =
- ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
- content_suggestions_service_observer_.Add(content_suggestions_service_);
-
- ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service();
-
- web_ui()->RegisterMessageCallback(
- "refreshContent",
- base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent,
- base::Unretained(this)));
-
- web_ui()->RegisterMessageCallback(
- "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
- base::Unretained(this)));
-
- web_ui()->RegisterMessageCallback(
- "clearCachedSuggestions",
- base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
- base::Unretained(this)));
-
- web_ui()->RegisterMessageCallback(
- "clearDismissedSuggestions",
- base::Bind(
- &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
- base::Unretained(this)));
-}
-
void SnippetsInternalsMessageHandler::OnNewSuggestions() {
if (!dom_loaded_)
return;
@@ -142,6 +137,46 @@
void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
+void SnippetsInternalsMessageHandler::RegisterMessages() {
+ // additional initialization (web_ui() does not work from the constructor)
+ Profile* profile = Profile::FromWebUI(web_ui());
+
+ content_suggestions_service_ =
+ ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
+ content_suggestions_service_observer_.Add(content_suggestions_service_);
+
+ ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service();
+
+ web_ui()->RegisterMessageCallback(
+ "refreshContent",
+ base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent,
+ base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear,
+ base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
+ base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ "clearDismissed",
+ base::Bind(&SnippetsInternalsMessageHandler::HandleClearDismissed,
+ base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ "clearCachedSuggestions",
+ base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
+ base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ "clearDismissedSuggestions",
+ base::Bind(
+ &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
+ base::Unretained(this)));
+}
+
void SnippetsInternalsMessageHandler::HandleRefreshContent(
const base::ListValue* args) {
DCHECK_EQ(0u, args->GetSize());
@@ -151,6 +186,20 @@
SendAllContent();
}
+void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) {
+ DCHECK_EQ(0u, args->GetSize());
+
+ ntp_snippets_service_->ClearCachedSuggestionsForDebugging();
+}
+
+void SnippetsInternalsMessageHandler::HandleClearDismissed(
+ const base::ListValue* args) {
+ DCHECK_EQ(0u, args->GetSize());
+
+ ntp_snippets_service_->ClearDismissedSuggestionsForDebugging();
+ SendDismissedSnippets();
+}
+
void SnippetsInternalsMessageHandler::HandleDownload(
const base::ListValue* args) {
DCHECK_EQ(1u, args->GetSize());
@@ -158,7 +207,7 @@
SendString("hosts-status", std::string());
std::string hosts_string;
- DCHECK(args->GetString(0, &hosts_string));
+ args->GetString(0, &hosts_string);
std::vector<std::string> hosts_vector = base::SplitString(
hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
@@ -169,28 +218,16 @@
void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
const base::ListValue* args) {
- DCHECK_EQ(1u, args->GetSize());
-
- int category_id;
- DCHECK(args->GetInteger(0, &category_id));
- Category category =
- content_suggestions_service_->category_factory()->FromIDValue(
- category_id);
- content_suggestions_service_->ClearCachedSuggestionsForDebugging(category);
- SendContentSuggestions();
+ DCHECK_EQ(0u, args->GetSize());
+
+ content_suggestions_service_->ClearCachedSuggestionsForDebugging();
}
void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions(
const base::ListValue* args) {
- DCHECK_EQ(1u, args->GetSize());
-
- int category_id;
- DCHECK(args->GetInteger(0, &category_id));
- Category category =
- content_suggestions_service_->category_factory()->FromIDValue(
- category_id);
- content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category);
- SendContentSuggestions();
+ DCHECK_EQ(0u, args->GetSize());
+
+ content_suggestions_service_->ClearDismissedSuggestionsForDebugging();
}
void SnippetsInternalsMessageHandler::SendAllContent() {
@@ -228,7 +265,41 @@
base::StringValue(
ntp_snippets_service_->snippets_fetcher()->last_json()));
+ SendSnippets();
+ SendDismissedSnippets();
SendContentSuggestions();
+}
+
+void SnippetsInternalsMessageHandler::SendSnippets() {
+ std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
+
+ int index = 0;
+ for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet :
+ ntp_snippets_service_->snippets())
+ snippets_list->Append(PrepareSnippet(*snippet, index++, false));
+
+ base::DictionaryValue result;
+ result.Set("list", std::move(snippets_list));
+ web_ui()->CallJavascriptFunctionUnsafe(
+ "chrome.SnippetsInternals.receiveSnippets", result);
+
+ const std::string& status =
+ ntp_snippets_service_->snippets_fetcher()->last_status();
+ if (!status.empty())
+ SendString("hosts-status", "Finished: " + status);
+}
+
+void SnippetsInternalsMessageHandler::SendDismissedSnippets() {
+ std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
+
+ int index = 0;
+ for (const auto& snippet : ntp_snippets_service_->dismissed_snippets())
+ snippets_list->Append(PrepareSnippet(*snippet, index++, true));
+
+ base::DictionaryValue result;
+ result.Set("list", std::move(snippets_list));
+ web_ui()->CallJavascriptFunctionUnsafe(
+ "chrome.SnippetsInternals.receiveDismissedSnippets", result);
}
void SnippetsInternalsMessageHandler::SendHosts() {
@@ -258,30 +329,17 @@
content_suggestions_service_->GetCategoryStatus(category);
const std::vector<ContentSuggestion>& suggestions =
content_suggestions_service_->GetSuggestionsForCategory(category);
- std::vector<ContentSuggestion> dismissed_suggestions =
- content_suggestions_service_->GetDismissedSuggestionsForDebugging(
- category);
std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
for (const ContentSuggestion& suggestion : suggestions) {
suggestions_list->Append(PrepareSuggestion(suggestion, index++));
}
- std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue);
- for (const ContentSuggestion& suggestion : dismissed_suggestions) {
- dismissed_list->Append(PrepareSuggestion(suggestion, index++));
- }
-
std::unique_ptr<base::DictionaryValue> category_entry(
new base::DictionaryValue);
- category_entry->SetInteger("categoryId", category.id());
- category_entry->SetString(
- "dismissedContainerId",
- "dismissed-suggestions-" + base::IntToString(category.id()));
category_entry->SetString("title", GetCategoryTitle(category));
category_entry->SetString("status", GetCategoryStatusName(status));
category_entry->Set("suggestions", std::move(suggestions_list));
- category_entry->Set("dismissedSuggestions", std::move(dismissed_list));
categories_list->Append(std::move(category_entry));
}

Powered by Google App Engine
This is Rietveld 408576698