| 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 30 matching lines...) Expand all 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 const base::ListValue* args) { | 261 const base::ListValue* args) { |
| 259 DCHECK_EQ(0u, args->GetSize()); | 262 DCHECK_EQ(0u, args->GetSize()); |
| 260 content_suggestions_service_->user_classifier() | 263 content_suggestions_service_->user_classifier() |
| 261 ->ClearClassificationForDebugging(); | 264 ->ClearClassificationForDebugging(); |
| 262 SendClassification(); | 265 SendClassification(); |
| 263 } | 266 } |
| 264 | 267 |
| 265 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground( | 268 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground( |
| 266 const base::ListValue* args) { | 269 const base::ListValue* args) { |
| 267 DCHECK_EQ(0u, args->GetSize()); | 270 DCHECK_EQ(0u, args->GetSize()); |
| 268 remote_suggestions_provider_->FetchSnippetsInTheBackground(); | 271 remote_suggestions_provider_->RefetchInTheBackground(nullptr); |
| 269 } | 272 } |
| 270 | 273 |
| 271 void SnippetsInternalsMessageHandler::SendAllContent() { | 274 void SnippetsInternalsMessageHandler::SendAllContent() { |
| 272 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 275 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 273 ntp_snippets::kContentSuggestionsFeature)); | 276 ntp_snippets::kContentSuggestionsFeature)); |
| 274 SendBoolean( | 277 SendBoolean( |
| 275 "flag-article-suggestions", | 278 "flag-article-suggestions", |
| 276 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)); | 279 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)); |
| 277 SendBoolean("flag-recent-offline-tab-suggestions", | 280 SendBoolean("flag-recent-offline-tab-suggestions", |
| 278 base::FeatureList::IsEnabled( | 281 base::FeatureList::IsEnabled( |
| 279 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); | 282 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); |
| 280 SendBoolean( | 283 SendBoolean( |
| 281 "flag-asset-download-suggestions", | 284 "flag-asset-download-suggestions", |
| 282 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature)); | 285 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature)); |
| 283 SendBoolean("flag-offline-page-download-suggestions", | 286 SendBoolean("flag-offline-page-download-suggestions", |
| 284 base::FeatureList::IsEnabled( | 287 base::FeatureList::IsEnabled( |
| 285 features::kOfflinePageDownloadSuggestionsFeature)); | 288 features::kOfflinePageDownloadSuggestionsFeature)); |
| 286 SendBoolean( | 289 SendBoolean( |
| 287 "flag-bookmark-suggestions", | 290 "flag-bookmark-suggestions", |
| 288 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); | 291 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); |
| 289 | 292 |
| 290 SendBoolean("flag-physical-web-page-suggestions", | 293 SendBoolean("flag-physical-web-page-suggestions", |
| 291 base::FeatureList::IsEnabled( | 294 base::FeatureList::IsEnabled( |
| 292 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); | 295 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); |
| 293 | 296 |
| 294 SendClassification(); | 297 SendClassification(); |
| 295 SendLastRemoteSuggestionsBackgroundFetchTime(); | 298 SendLastRemoteSuggestionsBackgroundFetchTime(); |
| 296 | 299 |
| 297 if (remote_suggestions_provider_) { | 300 if (remote_suggestions_provider_) { |
| 301 const ntp_snippets::NTPSnippetsFetcher* fetcher = |
| 302 remote_suggestions_provider_ |
| 303 ->snippets_fetcher_for_testing_and_debugging(); |
| 298 // TODO(fhorschig): Read this string from variations directly. | 304 // TODO(fhorschig): Read this string from variations directly. |
| 299 SendString("switch-personalized", | 305 SendString("switch-personalized", fetcher->PersonalizationModeString()); |
| 300 remote_suggestions_provider_->snippets_fetcher() | |
| 301 ->PersonalizationModeString()); | |
| 302 | 306 |
| 303 SendString( | 307 SendString("switch-fetch-url", fetcher->fetch_url().spec()); |
| 304 "switch-fetch-url", | |
| 305 remote_suggestions_provider_->snippets_fetcher()->fetch_url().spec()); | |
| 306 web_ui()->CallJavascriptFunctionUnsafe( | 308 web_ui()->CallJavascriptFunctionUnsafe( |
| 307 "chrome.SnippetsInternals.receiveJson", | 309 "chrome.SnippetsInternals.receiveJson", |
| 308 base::StringValue( | 310 base::StringValue(fetcher->last_json())); |
| 309 remote_suggestions_provider_->snippets_fetcher()->last_json())); | |
| 310 } | 311 } |
| 311 | 312 |
| 312 SendContentSuggestions(); | 313 SendContentSuggestions(); |
| 313 } | 314 } |
| 314 | 315 |
| 315 void SnippetsInternalsMessageHandler::SendClassification() { | 316 void SnippetsInternalsMessageHandler::SendClassification() { |
| 316 web_ui()->CallJavascriptFunctionUnsafe( | 317 web_ui()->CallJavascriptFunctionUnsafe( |
| 317 "chrome.SnippetsInternals.receiveClassification", | 318 "chrome.SnippetsInternals.receiveClassification", |
| 318 base::StringValue(content_suggestions_service_->user_classifier() | 319 base::StringValue(content_suggestions_service_->user_classifier() |
| 319 ->GetUserClassDescriptionForDebugging()), | 320 ->GetUserClassDescriptionForDebugging()), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 "dismissed-suggestions-" + base::IntToString(category.id())); | 371 "dismissed-suggestions-" + base::IntToString(category.id())); |
| 371 category_entry->SetString("title", info->title()); | 372 category_entry->SetString("title", info->title()); |
| 372 category_entry->SetString("status", GetCategoryStatusName(status)); | 373 category_entry->SetString("status", GetCategoryStatusName(status)); |
| 373 category_entry->Set("suggestions", std::move(suggestions_list)); | 374 category_entry->Set("suggestions", std::move(suggestions_list)); |
| 374 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); | 375 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); |
| 375 categories_list->Append(std::move(category_entry)); | 376 categories_list->Append(std::move(category_entry)); |
| 376 } | 377 } |
| 377 | 378 |
| 378 if (remote_suggestions_provider_) { | 379 if (remote_suggestions_provider_) { |
| 379 const std::string& status = | 380 const std::string& status = |
| 380 remote_suggestions_provider_->snippets_fetcher()->last_status(); | 381 remote_suggestions_provider_ |
| 382 ->snippets_fetcher_for_testing_and_debugging() |
| 383 ->last_status(); |
| 381 if (!status.empty()) | 384 if (!status.empty()) |
| 382 SendString("remote-status", "Finished: " + status); | 385 SendString("remote-status", "Finished: " + status); |
| 383 } | 386 } |
| 384 | 387 |
| 385 base::DictionaryValue result; | 388 base::DictionaryValue result; |
| 386 result.Set("list", std::move(categories_list)); | 389 result.Set("list", std::move(categories_list)); |
| 387 web_ui()->CallJavascriptFunctionUnsafe( | 390 web_ui()->CallJavascriptFunctionUnsafe( |
| 388 "chrome.SnippetsInternals.receiveContentSuggestions", result); | 391 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
| 389 } | 392 } |
| 390 | 393 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 404 | 407 |
| 405 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( | 408 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( |
| 406 Category category, | 409 Category category, |
| 407 std::vector<ContentSuggestion> dismissed_suggestions) { | 410 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 408 if (dismissed_state_[category] == DismissedState::HIDDEN) | 411 if (dismissed_state_[category] == DismissedState::HIDDEN) |
| 409 return; | 412 return; |
| 410 dismissed_suggestions_[category] = std::move(dismissed_suggestions); | 413 dismissed_suggestions_[category] = std::move(dismissed_suggestions); |
| 411 dismissed_state_[category] = DismissedState::VISIBLE; | 414 dismissed_state_[category] = DismissedState::VISIBLE; |
| 412 SendContentSuggestions(); | 415 SendContentSuggestions(); |
| 413 } | 416 } |
| OLD | NEW |