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