| 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/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/values.h" | 23 #include "base/values.h" |
| 24 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 24 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/common/chrome_features.h" | 26 #include "chrome/common/chrome_features.h" |
| 27 #include "components/ntp_snippets/category_info.h" | 27 #include "components/ntp_snippets/category_info.h" |
| 28 #include "components/ntp_snippets/features.h" | 28 #include "components/ntp_snippets/features.h" |
| 29 #include "components/ntp_snippets/pref_names.h" | 29 #include "components/ntp_snippets/pref_names.h" |
| 30 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 30 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" | 31 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 31 #include "components/ntp_snippets/switches.h" | 32 #include "components/ntp_snippets/switches.h" |
| 32 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 33 #include "content/public/browser/web_ui.h" | 34 #include "content/public/browser/web_ui.h" |
| 34 | 35 |
| 35 using ntp_snippets::ContentSuggestion; | 36 using ntp_snippets::ContentSuggestion; |
| 36 using ntp_snippets::Category; | 37 using ntp_snippets::Category; |
| 37 using ntp_snippets::CategoryInfo; | 38 using ntp_snippets::CategoryInfo; |
| 38 using ntp_snippets::CategoryStatus; | 39 using ntp_snippets::CategoryStatus; |
| 39 using ntp_snippets::KnownCategories; | 40 using ntp_snippets::KnownCategories; |
| 41 using ntp_snippets::RemoteSuggestionsProvider; |
| 40 using ntp_snippets::UserClassifier; | 42 using ntp_snippets::UserClassifier; |
| 41 | 43 |
| 42 namespace { | 44 namespace { |
| 43 | 45 |
| 44 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( | 46 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( |
| 45 const ContentSuggestion& suggestion, | 47 const ContentSuggestion& suggestion, |
| 46 int index) { | 48 int index) { |
| 47 auto entry = base::MakeUnique<base::DictionaryValue>(); | 49 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| 48 entry->SetString("idWithinCategory", suggestion.id().id_within_category()); | 50 entry->SetString("idWithinCategory", suggestion.id().id_within_category()); |
| 49 entry->SetString("url", suggestion.url().spec()); | 51 entry->SetString("url", suggestion.url().spec()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 83 |
| 82 } // namespace | 84 } // namespace |
| 83 | 85 |
| 84 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler( | 86 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler( |
| 85 ntp_snippets::ContentSuggestionsService* content_suggestions_service, | 87 ntp_snippets::ContentSuggestionsService* content_suggestions_service, |
| 86 PrefService* pref_service) | 88 PrefService* pref_service) |
| 87 : content_suggestions_service_observer_(this), | 89 : content_suggestions_service_observer_(this), |
| 88 dom_loaded_(false), | 90 dom_loaded_(false), |
| 89 content_suggestions_service_(content_suggestions_service), | 91 content_suggestions_service_(content_suggestions_service), |
| 90 remote_suggestions_provider_( | 92 remote_suggestions_provider_( |
| 91 content_suggestions_service_->ntp_snippets_service()), | 93 content_suggestions_service_ |
| 94 ->remote_suggestions_provider_for_debugging()), |
| 92 pref_service_(pref_service), | 95 pref_service_(pref_service), |
| 93 weak_ptr_factory_(this) {} | 96 weak_ptr_factory_(this) {} |
| 94 | 97 |
| 95 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 98 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 96 | 99 |
| 97 void SnippetsInternalsMessageHandler::RegisterMessages() { | 100 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 98 web_ui()->RegisterMessageCallback( | 101 web_ui()->RegisterMessageCallback( |
| 99 "clearCachedSuggestions", | 102 "clearCachedSuggestions", |
| 100 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, | 103 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, |
| 101 base::Unretained(this))); | 104 base::Unretained(this))); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 191 |
| 189 void SnippetsInternalsMessageHandler::HandleDownload( | 192 void SnippetsInternalsMessageHandler::HandleDownload( |
| 190 const base::ListValue* args) { | 193 const base::ListValue* args) { |
| 191 DCHECK_EQ(0u, args->GetSize()); | 194 DCHECK_EQ(0u, args->GetSize()); |
| 192 | 195 |
| 193 SendString("remote-status", std::string()); | 196 SendString("remote-status", std::string()); |
| 194 | 197 |
| 195 if (!remote_suggestions_provider_) | 198 if (!remote_suggestions_provider_) |
| 196 return; | 199 return; |
| 197 | 200 |
| 198 remote_suggestions_provider_->FetchSnippetsForAllCategories(); | 201 remote_suggestions_provider_->ReloadSuggestions(); |
| 199 } | 202 } |
| 200 | 203 |
| 201 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( | 204 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( |
| 202 const base::ListValue* args) { | 205 const base::ListValue* args) { |
| 203 DCHECK_EQ(1u, args->GetSize()); | 206 DCHECK_EQ(1u, args->GetSize()); |
| 204 | 207 |
| 205 int category_id; | 208 int category_id; |
| 206 if (!args->GetInteger(0, &category_id)) | 209 if (!args->GetInteger(0, &category_id)) |
| 207 return; | 210 return; |
| 208 | 211 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 const base::ListValue* args) { | 267 const base::ListValue* args) { |
| 265 DCHECK_EQ(0u, args->GetSize()); | 268 DCHECK_EQ(0u, args->GetSize()); |
| 266 content_suggestions_service_->user_classifier() | 269 content_suggestions_service_->user_classifier() |
| 267 ->ClearClassificationForDebugging(); | 270 ->ClearClassificationForDebugging(); |
| 268 SendClassification(); | 271 SendClassification(); |
| 269 } | 272 } |
| 270 | 273 |
| 271 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground( | 274 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground( |
| 272 const base::ListValue* args) { | 275 const base::ListValue* args) { |
| 273 DCHECK_EQ(0u, args->GetSize()); | 276 DCHECK_EQ(0u, args->GetSize()); |
| 274 remote_suggestions_provider_->FetchSnippetsInTheBackground(); | 277 remote_suggestions_provider_->RefetchInTheBackground( |
| 278 RemoteSuggestionsProvider::FetchStatusCallback()); |
| 275 } | 279 } |
| 276 | 280 |
| 277 void SnippetsInternalsMessageHandler::SendAllContent() { | 281 void SnippetsInternalsMessageHandler::SendAllContent() { |
| 278 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 282 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 279 ntp_snippets::kContentSuggestionsFeature)); | 283 ntp_snippets::kContentSuggestionsFeature)); |
| 280 SendBoolean( | 284 SendBoolean( |
| 281 "flag-article-suggestions", | 285 "flag-article-suggestions", |
| 282 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)); | 286 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)); |
| 283 SendBoolean("flag-recent-offline-tab-suggestions", | 287 SendBoolean("flag-recent-offline-tab-suggestions", |
| 284 base::FeatureList::IsEnabled( | 288 base::FeatureList::IsEnabled( |
| 285 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); | 289 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); |
| 286 SendBoolean( | 290 SendBoolean( |
| 287 "flag-asset-download-suggestions", | 291 "flag-asset-download-suggestions", |
| 288 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature)); | 292 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature)); |
| 289 SendBoolean("flag-offline-page-download-suggestions", | 293 SendBoolean("flag-offline-page-download-suggestions", |
| 290 base::FeatureList::IsEnabled( | 294 base::FeatureList::IsEnabled( |
| 291 features::kOfflinePageDownloadSuggestionsFeature)); | 295 features::kOfflinePageDownloadSuggestionsFeature)); |
| 292 SendBoolean( | 296 SendBoolean( |
| 293 "flag-bookmark-suggestions", | 297 "flag-bookmark-suggestions", |
| 294 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); | 298 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); |
| 295 | 299 |
| 296 SendBoolean("flag-physical-web-page-suggestions", | 300 SendBoolean("flag-physical-web-page-suggestions", |
| 297 base::FeatureList::IsEnabled( | 301 base::FeatureList::IsEnabled( |
| 298 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); | 302 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); |
| 299 | 303 |
| 300 SendClassification(); | 304 SendClassification(); |
| 301 SendLastRemoteSuggestionsBackgroundFetchTime(); | 305 SendLastRemoteSuggestionsBackgroundFetchTime(); |
| 302 | 306 |
| 303 if (remote_suggestions_provider_) { | 307 if (remote_suggestions_provider_) { |
| 304 switch ( | 308 switch (remote_suggestions_provider_ |
| 305 remote_suggestions_provider_->snippets_fetcher()->personalization()) { | 309 ->snippets_fetcher_for_testing_and_debugging() |
| 310 ->personalization()) { |
| 306 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: | 311 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: |
| 307 SendString("switch-personalized", "Only personalized"); | 312 SendString("switch-personalized", "Only personalized"); |
| 308 break; | 313 break; |
| 309 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: | 314 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: |
| 310 SendString("switch-personalized", | 315 SendString("switch-personalized", |
| 311 "Both personalized and non-personalized"); | 316 "Both personalized and non-personalized"); |
| 312 break; | 317 break; |
| 313 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: | 318 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: |
| 314 SendString("switch-personalized", "Only non-personalized"); | 319 SendString("switch-personalized", "Only non-personalized"); |
| 315 break; | 320 break; |
| 316 } | 321 } |
| 317 | 322 |
| 318 SendString( | 323 SendString("switch-fetch-url", |
| 319 "switch-fetch-url", | 324 remote_suggestions_provider_ |
| 320 remote_suggestions_provider_->snippets_fetcher()->fetch_url().spec()); | 325 ->snippets_fetcher_for_testing_and_debugging() |
| 326 ->fetch_url() |
| 327 .spec()); |
| 321 web_ui()->CallJavascriptFunctionUnsafe( | 328 web_ui()->CallJavascriptFunctionUnsafe( |
| 322 "chrome.SnippetsInternals.receiveJson", | 329 "chrome.SnippetsInternals.receiveJson", |
| 323 base::StringValue( | 330 base::StringValue(remote_suggestions_provider_ |
| 324 remote_suggestions_provider_->snippets_fetcher()->last_json())); | 331 ->snippets_fetcher_for_testing_and_debugging() |
| 332 ->last_json())); |
| 325 } | 333 } |
| 326 | 334 |
| 327 SendContentSuggestions(); | 335 SendContentSuggestions(); |
| 328 } | 336 } |
| 329 | 337 |
| 330 void SnippetsInternalsMessageHandler::SendClassification() { | 338 void SnippetsInternalsMessageHandler::SendClassification() { |
| 331 web_ui()->CallJavascriptFunctionUnsafe( | 339 web_ui()->CallJavascriptFunctionUnsafe( |
| 332 "chrome.SnippetsInternals.receiveClassification", | 340 "chrome.SnippetsInternals.receiveClassification", |
| 333 base::StringValue(content_suggestions_service_->user_classifier() | 341 base::StringValue(content_suggestions_service_->user_classifier() |
| 334 ->GetUserClassDescriptionForDebugging()), | 342 ->GetUserClassDescriptionForDebugging()), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 "dismissed-suggestions-" + base::IntToString(category.id())); | 393 "dismissed-suggestions-" + base::IntToString(category.id())); |
| 386 category_entry->SetString("title", info->title()); | 394 category_entry->SetString("title", info->title()); |
| 387 category_entry->SetString("status", GetCategoryStatusName(status)); | 395 category_entry->SetString("status", GetCategoryStatusName(status)); |
| 388 category_entry->Set("suggestions", std::move(suggestions_list)); | 396 category_entry->Set("suggestions", std::move(suggestions_list)); |
| 389 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); | 397 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); |
| 390 categories_list->Append(std::move(category_entry)); | 398 categories_list->Append(std::move(category_entry)); |
| 391 } | 399 } |
| 392 | 400 |
| 393 if (remote_suggestions_provider_) { | 401 if (remote_suggestions_provider_) { |
| 394 const std::string& status = | 402 const std::string& status = |
| 395 remote_suggestions_provider_->snippets_fetcher()->last_status(); | 403 remote_suggestions_provider_ |
| 404 ->snippets_fetcher_for_testing_and_debugging() |
| 405 ->last_status(); |
| 396 if (!status.empty()) | 406 if (!status.empty()) |
| 397 SendString("remote-status", "Finished: " + status); | 407 SendString("remote-status", "Finished: " + status); |
| 398 } | 408 } |
| 399 | 409 |
| 400 base::DictionaryValue result; | 410 base::DictionaryValue result; |
| 401 result.Set("list", std::move(categories_list)); | 411 result.Set("list", std::move(categories_list)); |
| 402 web_ui()->CallJavascriptFunctionUnsafe( | 412 web_ui()->CallJavascriptFunctionUnsafe( |
| 403 "chrome.SnippetsInternals.receiveContentSuggestions", result); | 413 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
| 404 } | 414 } |
| 405 | 415 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 419 | 429 |
| 420 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( | 430 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( |
| 421 Category category, | 431 Category category, |
| 422 std::vector<ContentSuggestion> dismissed_suggestions) { | 432 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 423 if (dismissed_state_[category] == DismissedState::HIDDEN) | 433 if (dismissed_state_[category] == DismissedState::HIDDEN) |
| 424 return; | 434 return; |
| 425 dismissed_suggestions_[category] = std::move(dismissed_suggestions); | 435 dismissed_suggestions_[category] = std::move(dismissed_suggestions); |
| 426 dismissed_state_[category] = DismissedState::VISIBLE; | 436 dismissed_state_[category] = DismissedState::VISIBLE; |
| 427 SendContentSuggestions(); | 437 SendContentSuggestions(); |
| 428 } | 438 } |
| OLD | NEW |