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

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

Issue 2145563002: Add ContentSuggestionsService to SnippetsInternals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@articleprovider2
Patch Set: Refactor NTPSnippetsService::UpdateStateForStatus switch statement Created 4 years, 5 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 f6e7411baf0ca1ad6ad5cf19a64dda6b3303b449..fdb57a837b076e95f3ebbaf3f8d2c0c29713fbd7 100644
--- a/chrome/browser/ui/webui/snippets_internals_message_handler.cc
+++ b/chrome/browser/ui/webui/snippets_internals_message_handler.cc
@@ -14,16 +14,22 @@
#include "base/feature_list.h"
#include "base/i18n/time_formatting.h"
#include "base/logging.h"
+#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "chrome/browser/android/chrome_feature_list.h"
+#include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h"
#include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/ntp_snippets/ntp_snippet.h"
#include "components/ntp_snippets/switches.h"
#include "content/public/browser/web_ui.h"
+using ntp_snippets::ContentSuggestion;
+using ntp_snippets::ContentSuggestionsCategory;
+using ntp_snippets::ContentSuggestionsCategoryStatus;
+
namespace {
std::unique_ptr<base::DictionaryValue> PrepareSnippet(
@@ -52,12 +58,68 @@ std::unique_ptr<base::DictionaryValue> PrepareSnippet(
return entry;
}
+std::unique_ptr<base::DictionaryValue> PrepareSuggestion(
+ const ContentSuggestion& suggestion,
+ int index) {
+ auto entry = base::MakeUnique<base::DictionaryValue>();
+ entry->SetString("suggestionId", suggestion.id());
+ entry->SetString("url", suggestion.url().spec());
+ entry->SetString("ampUrl", suggestion.amp_url().spec());
+ entry->SetString("title", suggestion.title());
+ entry->SetString("snippetText", suggestion.snippet_text());
+ entry->SetString("publishDate",
+ TimeFormatShortDateAndTime(suggestion.publish_date()));
+ entry->SetString("publisherName", suggestion.publisher_name());
+ entry->SetString("id", "content-suggestion-" + base::IntToString(index));
+ return entry;
+}
+
+std::string MapCategoryName(ContentSuggestionsCategory category) {
+ switch (category) {
+ case ContentSuggestionsCategory::ARTICLES:
+ return "Articles";
+ case ContentSuggestionsCategory::COUNT:
+ NOTREACHED() << "Category::COUNT must not be used as a value";
+ }
+ return std::string();
+}
+
+std::string MapCategoryStatus(ContentSuggestionsCategoryStatus status) {
+ switch (status) {
+ case ContentSuggestionsCategoryStatus::INITIALIZING:
+ return "INITIALIZING";
+ case ContentSuggestionsCategoryStatus::AVAILABLE:
+ return "AVAILABLE";
+ case ContentSuggestionsCategoryStatus::AVAILABLE_LOADING:
+ return "AVAILABLE_LOADING";
+ case ContentSuggestionsCategoryStatus::NOT_PROVIDED:
+ return "NOT_PROVIDED";
+ case ContentSuggestionsCategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED:
+ return "ALL_SUGGESTIONS_EXPLICITLY_DISABLED";
+ case ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED:
+ return "CATEGORY_EXPLICITLY_DISABLED";
+ case ContentSuggestionsCategoryStatus::SIGNED_OUT:
+ return "SIGNED_OUT";
+ case ContentSuggestionsCategoryStatus::SYNC_DISABLED:
+ return "SYNC_DISABLED";
+ case ContentSuggestionsCategoryStatus::PASSPHRASE_ENCRYPTION_ENABLED:
+ return "PASSPHRASE_ENCRYPTION_ENABLED";
+ case ContentSuggestionsCategoryStatus::HISTORY_SYNC_DISABLED:
+ return "HISTORY_SYNC_DISABLED";
+ case ContentSuggestionsCategoryStatus::LOADING_ERROR:
+ return "LOADING_ERROR";
+ }
+ return std::string();
+}
+
} // namespace
SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler()
- : observer_(this),
+ : ntp_snippets_service_observer_(this),
+ content_suggestions_service_observer_(this),
dom_loaded_(false),
- ntp_snippets_service_(nullptr) {}
+ ntp_snippets_service_(nullptr),
+ content_suggestions_service_(nullptr) {}
SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
@@ -77,11 +139,33 @@ void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() {
void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged(
ntp_snippets::DisabledReason disabled_reason) {}
+void SnippetsInternalsMessageHandler::OnNewSuggestions() {
+ if (!dom_loaded_)
+ return;
+ SendContentSuggestions();
+}
+
+void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
+ ContentSuggestionsCategory category,
+ ContentSuggestionsCategoryStatus new_status) {
+ if (!dom_loaded_)
+ return;
+ SendContentSuggestions();
+}
+
+void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
+
void SnippetsInternalsMessageHandler::RegisterMessages() {
// additional initialization (web_ui() does not work from the constructor)
- ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()->
- GetForProfile(Profile::FromWebUI(web_ui()));
- observer_.Add(ntp_snippets_service_);
+ Profile* profile = Profile::FromWebUI(web_ui());
+
+ ntp_snippets_service_ =
+ NTPSnippetsServiceFactory::GetInstance()->GetForProfile(profile);
+ ntp_snippets_service_observer_.Add(ntp_snippets_service_);
+
+ content_suggestions_service_ =
+ ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
+ content_suggestions_service_observer_.Add(content_suggestions_service_);
web_ui()->RegisterMessageCallback(
"loaded",
@@ -100,6 +184,17 @@ void SnippetsInternalsMessageHandler::RegisterMessages() {
"clearDiscarded",
base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded,
base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ "clearCachedSuggestions",
+ base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
+ base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ "clearDiscardedSuggestions",
+ base::Bind(
+ &SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions,
+ base::Unretained(this)));
}
void SnippetsInternalsMessageHandler::HandleLoaded(
@@ -141,6 +236,20 @@ void SnippetsInternalsMessageHandler::HandleDownload(
ntp_snippets_service_->FetchSnippetsFromHosts(hosts);
}
+void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
+ const base::ListValue* args) {
+ DCHECK_EQ(0u, args->GetSize());
+
+ content_suggestions_service_->ClearCachedSuggestionsForDebugging();
+}
+
+void SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions(
+ const base::ListValue* args) {
+ DCHECK_EQ(0u, args->GetSize());
+
+ content_suggestions_service_->ClearDiscardedSuggestionsForDebugging();
+}
+
void SnippetsInternalsMessageHandler::SendInitialData() {
SendHosts();
@@ -170,6 +279,7 @@ void SnippetsInternalsMessageHandler::SendInitialData() {
SendSnippets();
SendDiscardedSnippets();
+ SendContentSuggestions();
}
void SnippetsInternalsMessageHandler::SendSnippets() {
@@ -222,6 +332,36 @@ void SnippetsInternalsMessageHandler::SendHosts() {
"chrome.SnippetsInternals.receiveHosts", result);
}
+void SnippetsInternalsMessageHandler::SendContentSuggestions() {
+ std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
+
+ int index = 0;
+ for (ContentSuggestionsCategory category :
+ content_suggestions_service_->GetCategories()) {
+ ContentSuggestionsCategoryStatus status =
+ content_suggestions_service_->GetCategoryStatus(category);
+ const std::vector<ContentSuggestion>& suggestions =
+ content_suggestions_service_->GetSuggestionsForCategory(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::DictionaryValue> category_entry(
+ new base::DictionaryValue);
+ category_entry->SetString("name", MapCategoryName(category));
+ category_entry->SetString("status", MapCategoryStatus(status));
+ category_entry->Set("suggestions", std::move(suggestions_list));
+ categories_list->Append(std::move(category_entry));
+ }
+
+ base::DictionaryValue result;
+ result.Set("list", std::move(categories_list));
+ web_ui()->CallJavascriptFunctionUnsafe(
+ "chrome.SnippetsInternals.receiveContentSuggestions", result);
+}
+
void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
bool value) {
SendString(name, value ? "True" : "False");
« no previous file with comments | « chrome/browser/ui/webui/snippets_internals_message_handler.h ('k') | components/ntp_snippets/ntp_snippets_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698