| 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 <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 13 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 14 #include "base/feature_list.h" | 15 #include "base/feature_list.h" |
| 15 #include "base/i18n/time_formatting.h" | 16 #include "base/i18n/time_formatting.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/optional.h" | 19 #include "base/optional.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 21 #include "base/values.h" | 22 #include "base/values.h" |
| 22 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 23 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 24 #include "components/ntp_snippets/category_info.h" | 25 #include "components/ntp_snippets/category_info.h" |
| 25 #include "components/ntp_snippets/features.h" | 26 #include "components/ntp_snippets/features.h" |
| 26 #include "components/ntp_snippets/ntp_snippet.h" | 27 #include "components/ntp_snippets/ntp_snippet.h" |
| 27 #include "components/ntp_snippets/switches.h" | 28 #include "components/ntp_snippets/switches.h" |
| 28 #include "content/public/browser/web_ui.h" | 29 #include "content/public/browser/web_ui.h" |
| 29 | 30 |
| 30 using ntp_snippets::ContentSuggestion; | 31 using ntp_snippets::ContentSuggestion; |
| 31 using ntp_snippets::Category; | 32 using ntp_snippets::Category; |
| 32 using ntp_snippets::CategoryInfo; | 33 using ntp_snippets::CategoryInfo; |
| 33 using ntp_snippets::CategoryStatus; | 34 using ntp_snippets::CategoryStatus; |
| 34 using ntp_snippets::KnownCategories; | 35 using ntp_snippets::KnownCategories; |
| 36 using ntp_snippets::UserClassifier; |
| 35 | 37 |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 38 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( | 40 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( |
| 39 const ContentSuggestion& suggestion, | 41 const ContentSuggestion& suggestion, |
| 40 int index) { | 42 int index) { |
| 41 auto entry = base::MakeUnique<base::DictionaryValue>(); | 43 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| 42 entry->SetString("suggestionId", suggestion.id()); | 44 entry->SetString("suggestionId", suggestion.id()); |
| 43 entry->SetString("url", suggestion.url().spec()); | 45 entry->SetString("url", suggestion.url().spec()); |
| 44 entry->SetString("ampUrl", suggestion.amp_url().spec()); | 46 entry->SetString("ampUrl", suggestion.amp_url().spec()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 66 case CategoryStatus::CATEGORY_EXPLICITLY_DISABLED: | 68 case CategoryStatus::CATEGORY_EXPLICITLY_DISABLED: |
| 67 return "CATEGORY_EXPLICITLY_DISABLED"; | 69 return "CATEGORY_EXPLICITLY_DISABLED"; |
| 68 case CategoryStatus::SIGNED_OUT: | 70 case CategoryStatus::SIGNED_OUT: |
| 69 return "SIGNED_OUT"; | 71 return "SIGNED_OUT"; |
| 70 case CategoryStatus::LOADING_ERROR: | 72 case CategoryStatus::LOADING_ERROR: |
| 71 return "LOADING_ERROR"; | 73 return "LOADING_ERROR"; |
| 72 } | 74 } |
| 73 return std::string(); | 75 return std::string(); |
| 74 } | 76 } |
| 75 | 77 |
| 76 } // namespace | 78 } // namespace |
| 77 | 79 |
| 78 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() | 80 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() |
| 79 : content_suggestions_service_observer_(this), | 81 : content_suggestions_service_observer_(this), |
| 80 dom_loaded_(false), | 82 dom_loaded_(false), |
| 81 ntp_snippets_service_(nullptr), | 83 ntp_snippets_service_(nullptr), |
| 82 content_suggestions_service_(nullptr), | 84 content_suggestions_service_(nullptr), |
| 83 weak_ptr_factory_(this) {} | 85 weak_ptr_factory_(this) {} |
| 84 | 86 |
| 85 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 87 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 86 | 88 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 112 "clearDismissedSuggestions", | 114 "clearDismissedSuggestions", |
| 113 base::Bind( | 115 base::Bind( |
| 114 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, | 116 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, |
| 115 base::Unretained(this))); | 117 base::Unretained(this))); |
| 116 | 118 |
| 117 web_ui()->RegisterMessageCallback( | 119 web_ui()->RegisterMessageCallback( |
| 118 "toggleDismissedSuggestions", | 120 "toggleDismissedSuggestions", |
| 119 base::Bind( | 121 base::Bind( |
| 120 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions, | 122 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions, |
| 121 base::Unretained(this))); | 123 base::Unretained(this))); |
| 124 |
| 125 web_ui()->RegisterMessageCallback( |
| 126 "clearClassification", |
| 127 base::Bind( |
| 128 &SnippetsInternalsMessageHandler::ClearClassification, |
| 129 base::Unretained(this))); |
| 122 } | 130 } |
| 123 | 131 |
| 124 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { | 132 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { |
| 125 if (!dom_loaded_) | 133 if (!dom_loaded_) |
| 126 return; | 134 return; |
| 127 SendContentSuggestions(); | 135 SendContentSuggestions(); |
| 128 } | 136 } |
| 129 | 137 |
| 130 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( | 138 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( |
| 131 Category category, | 139 Category category, |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 category, | 246 category, |
| 239 base::Bind( | 247 base::Bind( |
| 240 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, | 248 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, |
| 241 weak_ptr_factory_.GetWeakPtr(), category)); | 249 weak_ptr_factory_.GetWeakPtr(), category)); |
| 242 } else { | 250 } else { |
| 243 dismissed_state_[category] = DismissedState::HIDDEN; | 251 dismissed_state_[category] = DismissedState::HIDDEN; |
| 244 dismissed_suggestions_[category].clear(); | 252 dismissed_suggestions_[category].clear(); |
| 245 } | 253 } |
| 246 } | 254 } |
| 247 | 255 |
| 256 void SnippetsInternalsMessageHandler::ClearClassification( |
| 257 const base::ListValue* args) { |
| 258 DCHECK_EQ(0u, args->GetSize()); |
| 259 content_suggestions_service_->user_classifier() |
| 260 ->ClearClassificationForDebugging(); |
| 261 SendClassification(); |
| 262 } |
| 263 |
| 248 void SnippetsInternalsMessageHandler::SendAllContent() { | 264 void SnippetsInternalsMessageHandler::SendAllContent() { |
| 249 SendHosts(); | 265 SendHosts(); |
| 250 | 266 |
| 251 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 267 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 252 ntp_snippets::kContentSuggestionsFeature)); | 268 ntp_snippets::kContentSuggestionsFeature)); |
| 253 SendBoolean("flag-recent-offline-tab-suggestions", | 269 SendBoolean("flag-recent-offline-tab-suggestions", |
| 254 base::FeatureList::IsEnabled( | 270 base::FeatureList::IsEnabled( |
| 255 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); | 271 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); |
| 256 SendBoolean( | 272 SendBoolean( |
| 257 "flag-download-suggestions", | 273 "flag-download-suggestions", |
| 258 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature)); | 274 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature)); |
| 259 SendBoolean( | 275 SendBoolean( |
| 260 "flag-bookmark-suggestions", | 276 "flag-bookmark-suggestions", |
| 261 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); | 277 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); |
| 262 | 278 |
| 263 SendBoolean("flag-physical-web-page-suggestions", | 279 SendBoolean("flag-physical-web-page-suggestions", |
| 264 base::FeatureList::IsEnabled( | 280 base::FeatureList::IsEnabled( |
| 265 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); | 281 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); |
| 266 | 282 |
| 283 SendClassification(); |
| 284 |
| 267 web_ui()->CallJavascriptFunctionUnsafe( | 285 web_ui()->CallJavascriptFunctionUnsafe( |
| 268 "chrome.SnippetsInternals.setHostRestricted", | 286 "chrome.SnippetsInternals.setHostRestricted", |
| 269 base::FundamentalValue( | 287 base::FundamentalValue( |
| 270 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); | 288 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); |
| 271 | 289 |
| 272 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { | 290 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { |
| 273 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: | 291 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: |
| 274 SendString("switch-personalized", "Only personalized"); | 292 SendString("switch-personalized", "Only personalized"); |
| 275 break; | 293 break; |
| 276 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: | 294 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: |
| 277 SendString("switch-personalized", | 295 SendString("switch-personalized", |
| 278 "Both personalized and non-personalized"); | 296 "Both personalized and non-personalized"); |
| 279 break; | 297 break; |
| 280 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: | 298 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: |
| 281 SendString("switch-personalized", "Only non-personalized"); | 299 SendString("switch-personalized", "Only non-personalized"); |
| 282 break; | 300 break; |
| 283 } | 301 } |
| 284 | 302 |
| 285 SendString("switch-fetch-url", | 303 SendString("switch-fetch-url", |
| 286 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); | 304 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); |
| 287 web_ui()->CallJavascriptFunctionUnsafe( | 305 web_ui()->CallJavascriptFunctionUnsafe( |
| 288 "chrome.SnippetsInternals.receiveJson", | 306 "chrome.SnippetsInternals.receiveJson", |
| 289 base::StringValue( | 307 base::StringValue( |
| 290 ntp_snippets_service_->snippets_fetcher()->last_json())); | 308 ntp_snippets_service_->snippets_fetcher()->last_json())); |
| 291 | 309 |
| 292 SendContentSuggestions(); | 310 SendContentSuggestions(); |
| 293 } | 311 } |
| 294 | 312 |
| 313 void SnippetsInternalsMessageHandler::SendClassification() { |
| 314 web_ui()->CallJavascriptFunctionUnsafe( |
| 315 "chrome.SnippetsInternals.receiveClassification", |
| 316 base::StringValue(content_suggestions_service_->user_classifier() |
| 317 ->GetUserClassDescriptionForDebugging()), |
| 318 base::FundamentalValue( |
| 319 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( |
| 320 UserClassifier::Metric::NTP_OPENED)), |
| 321 base::FundamentalValue( |
| 322 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( |
| 323 UserClassifier::Metric::SUGGESTIONS_SHOWN)), |
| 324 base::FundamentalValue( |
| 325 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( |
| 326 UserClassifier::Metric::SUGGESTIONS_USED))); |
| 327 } |
| 328 |
| 295 void SnippetsInternalsMessageHandler::SendHosts() { | 329 void SnippetsInternalsMessageHandler::SendHosts() { |
| 296 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); | 330 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); |
| 297 | 331 |
| 298 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); | 332 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); |
| 299 | 333 |
| 300 for (const std::string& host : hosts) { | 334 for (const std::string& host : hosts) { |
| 301 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 335 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 302 entry->SetString("url", host); | 336 entry->SetString("url", host); |
| 303 | 337 |
| 304 hosts_list->Append(std::move(entry)); | 338 hosts_list->Append(std::move(entry)); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 | 408 |
| 375 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( | 409 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( |
| 376 Category category, | 410 Category category, |
| 377 std::vector<ContentSuggestion> dismissed_suggestions) { | 411 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 378 if (dismissed_state_[category] == DismissedState::HIDDEN) | 412 if (dismissed_state_[category] == DismissedState::HIDDEN) |
| 379 return; | 413 return; |
| 380 dismissed_suggestions_[category] = std::move(dismissed_suggestions); | 414 dismissed_suggestions_[category] = std::move(dismissed_suggestions); |
| 381 dismissed_state_[category] = DismissedState::VISIBLE; | 415 dismissed_state_[category] = DismissedState::VISIBLE; |
| 382 SendContentSuggestions(); | 416 SendContentSuggestions(); |
| 383 } | 417 } |
| OLD | NEW |