| OLD | NEW |
| 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> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/feature_list.h" | 15 #include "base/feature_list.h" |
| 16 #include "base/i18n/time_formatting.h" | 16 #include "base/i18n/time_formatting.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 19 #include "base/optional.h" | 19 #include "base/optional.h" |
| 20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 22 #include "base/values.h" | 22 #include "base/values.h" |
| 23 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 23 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/common/chrome_features.h" | 25 #include "chrome/common/chrome_features.h" |
| 26 #include "components/ntp_snippets/category.h" |
| 26 #include "components/ntp_snippets/category_info.h" | 27 #include "components/ntp_snippets/category_info.h" |
| 27 #include "components/ntp_snippets/features.h" | 28 #include "components/ntp_snippets/features.h" |
| 28 #include "components/ntp_snippets/switches.h" | 29 #include "components/ntp_snippets/switches.h" |
| 29 #include "content/public/browser/web_ui.h" | 30 #include "content/public/browser/web_ui.h" |
| 30 | 31 |
| 31 using ntp_snippets::ContentSuggestion; | 32 using ntp_snippets::ContentSuggestion; |
| 32 using ntp_snippets::Category; | 33 using ntp_snippets::Category; |
| 33 using ntp_snippets::CategoryInfo; | 34 using ntp_snippets::CategoryInfo; |
| 34 using ntp_snippets::CategoryStatus; | 35 using ntp_snippets::CategoryStatus; |
| 35 using ntp_snippets::KnownCategories; | 36 using ntp_snippets::KnownCategories; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 203 } |
| 203 | 204 |
| 204 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( | 205 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( |
| 205 const base::ListValue* args) { | 206 const base::ListValue* args) { |
| 206 DCHECK_EQ(1u, args->GetSize()); | 207 DCHECK_EQ(1u, args->GetSize()); |
| 207 | 208 |
| 208 int category_id; | 209 int category_id; |
| 209 if (!args->GetInteger(0, &category_id)) | 210 if (!args->GetInteger(0, &category_id)) |
| 210 return; | 211 return; |
| 211 | 212 |
| 212 Category category = | 213 content_suggestions_service_->ClearCachedSuggestions( |
| 213 content_suggestions_service_->category_factory()->FromIDValue( | 214 Category::FromIDValue(category_id)); |
| 214 category_id); | |
| 215 content_suggestions_service_->ClearCachedSuggestions(category); | |
| 216 SendContentSuggestions(); | 215 SendContentSuggestions(); |
| 217 } | 216 } |
| 218 | 217 |
| 219 void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions( | 218 void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions( |
| 220 const base::ListValue* args) { | 219 const base::ListValue* args) { |
| 221 DCHECK_EQ(1u, args->GetSize()); | 220 DCHECK_EQ(1u, args->GetSize()); |
| 222 | 221 |
| 223 int category_id; | 222 int category_id; |
| 224 if (!args->GetInteger(0, &category_id)) | 223 if (!args->GetInteger(0, &category_id)) |
| 225 return; | 224 return; |
| 226 | 225 |
| 227 Category category = | 226 Category category = Category::FromIDValue(category_id); |
| 228 content_suggestions_service_->category_factory()->FromIDValue( | |
| 229 category_id); | |
| 230 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category); | 227 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category); |
| 231 SendContentSuggestions(); | 228 SendContentSuggestions(); |
| 232 dismissed_state_[category] = DismissedState::LOADING; | 229 dismissed_state_[category] = DismissedState::LOADING; |
| 233 content_suggestions_service_->GetDismissedSuggestionsForDebugging( | 230 content_suggestions_service_->GetDismissedSuggestionsForDebugging( |
| 234 category, | 231 category, |
| 235 base::Bind(&SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, | 232 base::Bind(&SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, |
| 236 weak_ptr_factory_.GetWeakPtr(), category)); | 233 weak_ptr_factory_.GetWeakPtr(), category)); |
| 237 } | 234 } |
| 238 | 235 |
| 239 void SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions( | 236 void SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions( |
| 240 const base::ListValue* args) { | 237 const base::ListValue* args) { |
| 241 DCHECK_EQ(2u, args->GetSize()); | 238 DCHECK_EQ(2u, args->GetSize()); |
| 242 | 239 |
| 243 int category_id; | 240 int category_id; |
| 244 if (!args->GetInteger(0, &category_id)) | 241 if (!args->GetInteger(0, &category_id)) |
| 245 return; | 242 return; |
| 246 bool dismissed_visible; | 243 bool dismissed_visible; |
| 247 if (!args->GetBoolean(1, &dismissed_visible)) | 244 if (!args->GetBoolean(1, &dismissed_visible)) |
| 248 return; | 245 return; |
| 249 | 246 |
| 250 Category category = | 247 Category category = Category::FromIDValue(category_id); |
| 251 content_suggestions_service_->category_factory()->FromIDValue( | |
| 252 category_id); | |
| 253 if (dismissed_visible) { | 248 if (dismissed_visible) { |
| 254 dismissed_state_[category] = DismissedState::LOADING; | 249 dismissed_state_[category] = DismissedState::LOADING; |
| 255 content_suggestions_service_->GetDismissedSuggestionsForDebugging( | 250 content_suggestions_service_->GetDismissedSuggestionsForDebugging( |
| 256 category, | 251 category, |
| 257 base::Bind( | 252 base::Bind( |
| 258 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, | 253 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, |
| 259 weak_ptr_factory_.GetWeakPtr(), category)); | 254 weak_ptr_factory_.GetWeakPtr(), category)); |
| 260 } else { | 255 } else { |
| 261 dismissed_state_[category] = DismissedState::HIDDEN; | 256 dismissed_state_[category] = DismissedState::HIDDEN; |
| 262 dismissed_suggestions_[category].clear(); | 257 dismissed_suggestions_[category].clear(); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 | 398 |
| 404 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( | 399 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( |
| 405 Category category, | 400 Category category, |
| 406 std::vector<ContentSuggestion> dismissed_suggestions) { | 401 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 407 if (dismissed_state_[category] == DismissedState::HIDDEN) | 402 if (dismissed_state_[category] == DismissedState::HIDDEN) |
| 408 return; | 403 return; |
| 409 dismissed_suggestions_[category] = std::move(dismissed_suggestions); | 404 dismissed_suggestions_[category] = std::move(dismissed_suggestions); |
| 410 dismissed_state_[category] = DismissedState::VISIBLE; | 405 dismissed_state_[category] = DismissedState::VISIBLE; |
| 411 SendContentSuggestions(); | 406 SendContentSuggestions(); |
| 412 } | 407 } |
| OLD | NEW |