Chromium Code Reviews| 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 c93e230c4dda7ab2ca8aa6f07fcf4f06a2914214..41df13818baee7c6f61bd1f87aeb752ac320f8bb 100644 |
| --- a/chrome/browser/ui/webui/snippets_internals_message_handler.cc |
| +++ b/chrome/browser/ui/webui/snippets_internals_message_handler.cc |
| @@ -15,6 +15,7 @@ |
| #include "base/i18n/time_formatting.h" |
| #include "base/logging.h" |
| #include "base/memory/ptr_util.h" |
| +#include "base/memory/weak_ptr.h" |
|
Marc Treib
2016/08/19 12:36:15
-> header
Philipp Keck
2016/08/19 14:20:40
Done.
|
| #include "base/optional.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_split.h" |
| @@ -79,7 +80,8 @@ SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() |
| : content_suggestions_service_observer_(this), |
| dom_loaded_(false), |
| ntp_snippets_service_(nullptr), |
| - content_suggestions_service_(nullptr) {} |
| + content_suggestions_service_(nullptr), |
| + weak_ptr_factory_(this) {} |
| SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| @@ -112,6 +114,12 @@ void SnippetsInternalsMessageHandler::RegisterMessages() { |
| base::Bind( |
| &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, |
| base::Unretained(this))); |
| + |
| + web_ui()->RegisterMessageCallback( |
| + "toggleDismissedSuggestions", |
| + base::Bind( |
| + &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions, |
| + base::Unretained(this))); |
| } |
| void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { |
| @@ -144,6 +152,18 @@ void SnippetsInternalsMessageHandler::HandleRefreshContent( |
| dom_loaded_ = true; |
| + for (std::pair<const Category, DismissedState>& category_state_pair : |
| + dismissed_state_) { |
| + if (category_state_pair.second == DismissedState::VISIBLE) { |
| + category_state_pair.second = DismissedState::LOADING; |
| + content_suggestions_service_->GetDismissedSuggestionsForDebugging( |
| + category_state_pair.first, |
| + base::Bind( |
| + &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + } |
| + |
| SendAllContent(); |
| } |
| @@ -192,6 +212,38 @@ void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions( |
| category_id); |
| content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category); |
| SendContentSuggestions(); |
| + dismissed_state_[category] = DismissedState::LOADING; |
| + content_suggestions_service_->GetDismissedSuggestionsForDebugging( |
| + category, |
| + base::Bind(&SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions( |
| + const base::ListValue* args) { |
| + DCHECK_EQ(2u, args->GetSize()); |
| + |
| + int category_id; |
| + if (!args->GetInteger(0, &category_id)) |
| + return; |
| + bool dismissed_visible; |
| + if (!args->GetBoolean(1, &dismissed_visible)) |
| + return; |
| + |
| + Category category = |
| + content_suggestions_service_->category_factory()->FromIDValue( |
| + category_id); |
| + if (dismissed_visible) { |
| + dismissed_state_[category] = DismissedState::LOADING; |
| + content_suggestions_service_->GetDismissedSuggestionsForDebugging( |
| + category, |
| + base::Bind( |
| + &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } else { |
| + dismissed_state_[category] = DismissedState::HIDDEN; |
| + dismissed_suggestions_[category].clear(); |
| + } |
| } |
| void SnippetsInternalsMessageHandler::SendAllContent() { |
| @@ -271,9 +323,6 @@ void SnippetsInternalsMessageHandler::SendContentSuggestions() { |
| DCHECK(info); |
| 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) { |
| @@ -281,7 +330,8 @@ void SnippetsInternalsMessageHandler::SendContentSuggestions() { |
| } |
| std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue); |
| - for (const ContentSuggestion& suggestion : dismissed_suggestions) { |
| + for (const ContentSuggestion& suggestion : |
| + dismissed_suggestions_[category]) { |
| dismissed_list->Append(PrepareSuggestion(suggestion, index++)); |
| } |
| @@ -317,3 +367,13 @@ void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| web_ui()->CallJavascriptFunctionUnsafe( |
| "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
| } |
| + |
| +void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( |
| + Category category, |
| + std::vector<ContentSuggestion> dismissed_suggestions) { |
| + if (dismissed_state_[category] == DismissedState::HIDDEN) |
| + return; |
| + dismissed_suggestions_[category] = std::move(dismissed_suggestions); |
| + dismissed_state_[category] = DismissedState::VISIBLE; |
| + SendContentSuggestions(); |
| +} |