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..707cb06c8524f81f6727146588bcd5556a4c31a6 100644 |
--- a/chrome/browser/ui/webui/snippets_internals_message_handler.cc |
+++ b/chrome/browser/ui/webui/snippets_internals_message_handler.cc |
@@ -18,12 +18,17 @@ |
#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 +57,68 @@ std::unique_ptr<base::DictionaryValue> PrepareSnippet( |
return entry; |
} |
+std::unique_ptr<base::DictionaryValue> PrepareSuggestion( |
+ const ntp_snippets::ContentSuggestion& suggestion, |
Marc Treib
2016/07/12 13:04:52
nit: ntp_snippets:: not needed, you have a "using"
Philipp Keck
2016/07/12 13:45:02
Done.
|
+ int index) { |
+ std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
Bernhard Bauer
2016/07/12 13:09:45
You might be able to do `auto entry = base::MakeUn
Philipp Keck
2016/07/12 13:45:02
Done.
|
+ 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"; |
+ default: |
Bernhard Bauer
2016/07/12 13:09:45
If you remove the default case from the switch sta
Philipp Keck
2016/07/12 13:45:02
Done. Oh great. I didn't know it would warn and th
Bernhard Bauer
2016/07/12 14:44:51
Indeed!
Philipp Keck
2016/07/12 15:19:56
Done. (other CL)
Bernhard Bauer
2016/07/12 15:50:31
You could have an int (not an enum) that represent
Philipp Keck
2016/07/12 16:05:36
You'd always have to remember to update that int,
Bernhard Bauer
2016/07/12 16:16:27
Yes, precisely.
|
+ return "Unknown category " + base::IntToString(int(category)); |
+ } |
+} |
+ |
+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"; |
+ default: |
Bernhard Bauer
2016/07/12 13:09:45
Same here.
Philipp Keck
2016/07/12 13:45:02
Done.
|
+ return "Unknown status: " + base::IntToString(int(status)); |
Bernhard Bauer
2016/07/12 13:09:45
Should this be a static_cast<int>? The style guide
Philipp Keck
2016/07/12 13:45:02
I delete this line anyway, but I use `int(...)` an
Bernhard Bauer
2016/07/12 14:44:51
Hm. I do prefer static_cast<>, but I don't want to
Philipp Keck
2016/07/12 15:19:56
Done. Actually the style guide is quite clear on t
|
+ } |
+} |
+ |
} // 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 +138,31 @@ void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { |
void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged( |
ntp_snippets::DisabledReason disabled_reason) {} |
+void SnippetsInternalsMessageHandler::OnNewSuggestions() { |
+ if (!dom_loaded_) |
+ return; |
+ SendContentSuggestions(); |
+} |
+void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( |
Marc Treib
2016/07/12 13:04:53
nit: newlines between methods please
Philipp Keck
2016/07/12 13:45:02
Done.
|
+ 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 +181,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 +233,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 +276,7 @@ void SnippetsInternalsMessageHandler::SendInitialData() { |
SendSnippets(); |
SendDiscardedSnippets(); |
+ SendContentSuggestions(); |
} |
void SnippetsInternalsMessageHandler::SendSnippets() { |
@@ -222,6 +329,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"); |