| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 return "LOADING_ERROR"; | 73 return "LOADING_ERROR"; |
| 74 } | 74 } |
| 75 return std::string(); | 75 return std::string(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 } // namespace | 78 } // namespace |
| 79 | 79 |
| 80 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() | 80 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() |
| 81 : content_suggestions_service_observer_(this), | 81 : content_suggestions_service_observer_(this), |
| 82 dom_loaded_(false), | 82 dom_loaded_(false), |
| 83 ntp_snippets_service_(nullptr), | 83 remote_suggestions_provider_(nullptr), |
| 84 content_suggestions_service_(nullptr), | 84 content_suggestions_service_(nullptr), |
| 85 weak_ptr_factory_(this) {} | 85 weak_ptr_factory_(this) {} |
| 86 | 86 |
| 87 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 87 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 88 | 88 |
| 89 void SnippetsInternalsMessageHandler::RegisterMessages() { | 89 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 90 // Additional initialization (web_ui() does not work from the constructor). | 90 // Additional initialization (web_ui() does not work from the constructor). |
| 91 Profile* profile = Profile::FromWebUI(web_ui()); | 91 Profile* profile = Profile::FromWebUI(web_ui()); |
| 92 | 92 |
| 93 content_suggestions_service_ = | 93 content_suggestions_service_ = |
| 94 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); | 94 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); |
| 95 content_suggestions_service_observer_.Add(content_suggestions_service_); | 95 content_suggestions_service_observer_.Add(content_suggestions_service_); |
| 96 | 96 |
| 97 ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service(); | 97 remote_suggestions_provider_ = |
| 98 content_suggestions_service_->remote_suggestions_provider(); |
| 98 | 99 |
| 99 web_ui()->RegisterMessageCallback( | 100 web_ui()->RegisterMessageCallback( |
| 100 "refreshContent", | 101 "refreshContent", |
| 101 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent, | 102 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent, |
| 102 base::Unretained(this))); | 103 base::Unretained(this))); |
| 103 | 104 |
| 104 web_ui()->RegisterMessageCallback( | 105 web_ui()->RegisterMessageCallback( |
| 105 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, | 106 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, |
| 106 base::Unretained(this))); | 107 base::Unretained(this))); |
| 107 | 108 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 180 |
| 180 SendAllContent(); | 181 SendAllContent(); |
| 181 } | 182 } |
| 182 | 183 |
| 183 void SnippetsInternalsMessageHandler::HandleDownload( | 184 void SnippetsInternalsMessageHandler::HandleDownload( |
| 184 const base::ListValue* args) { | 185 const base::ListValue* args) { |
| 185 DCHECK_EQ(1u, args->GetSize()); | 186 DCHECK_EQ(1u, args->GetSize()); |
| 186 | 187 |
| 187 SendString("hosts-status", std::string()); | 188 SendString("hosts-status", std::string()); |
| 188 | 189 |
| 189 if (!ntp_snippets_service_) | 190 if (!remote_suggestions_provider_) |
| 190 return; | 191 return; |
| 191 | 192 |
| 192 std::string hosts_string; | 193 std::string hosts_string; |
| 193 if (!args->GetString(0, &hosts_string)) | 194 if (!args->GetString(0, &hosts_string)) |
| 194 return; | 195 return; |
| 195 | 196 |
| 196 std::vector<std::string> hosts_vector = base::SplitString( | 197 std::vector<std::string> hosts_vector = base::SplitString( |
| 197 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 198 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 198 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); | 199 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); |
| 199 | 200 |
| 200 ntp_snippets_service_->FetchSnippetsFromHosts(hosts, | 201 remote_suggestions_provider_->FetchSnippetsFromHosts(hosts, |
| 201 /*interactive_request=*/true); | 202 /*interactive_request=*/true); |
| 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; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 SendBoolean( | 290 SendBoolean( |
| 290 "flag-bookmark-suggestions", | 291 "flag-bookmark-suggestions", |
| 291 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); | 292 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); |
| 292 | 293 |
| 293 SendBoolean("flag-physical-web-page-suggestions", | 294 SendBoolean("flag-physical-web-page-suggestions", |
| 294 base::FeatureList::IsEnabled( | 295 base::FeatureList::IsEnabled( |
| 295 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); | 296 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); |
| 296 | 297 |
| 297 SendClassification(); | 298 SendClassification(); |
| 298 | 299 |
| 299 if (ntp_snippets_service_) { | 300 if (remote_suggestions_provider_) { |
| 300 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { | 301 switch ( |
| 302 remote_suggestions_provider_->snippets_fetcher()->personalization()) { |
| 301 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: | 303 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: |
| 302 SendString("switch-personalized", "Only personalized"); | 304 SendString("switch-personalized", "Only personalized"); |
| 303 break; | 305 break; |
| 304 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: | 306 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: |
| 305 SendString("switch-personalized", | 307 SendString("switch-personalized", |
| 306 "Both personalized and non-personalized"); | 308 "Both personalized and non-personalized"); |
| 307 break; | 309 break; |
| 308 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: | 310 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: |
| 309 SendString("switch-personalized", "Only non-personalized"); | 311 SendString("switch-personalized", "Only non-personalized"); |
| 310 break; | 312 break; |
| 311 } | 313 } |
| 312 | 314 |
| 313 SendString("switch-fetch-url", | 315 SendString( |
| 314 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); | 316 "switch-fetch-url", |
| 317 remote_suggestions_provider_->snippets_fetcher()->fetch_url().spec()); |
| 315 web_ui()->CallJavascriptFunctionUnsafe( | 318 web_ui()->CallJavascriptFunctionUnsafe( |
| 316 "chrome.SnippetsInternals.receiveJson", | 319 "chrome.SnippetsInternals.receiveJson", |
| 317 base::StringValue( | 320 base::StringValue( |
| 318 ntp_snippets_service_->snippets_fetcher()->last_json())); | 321 remote_suggestions_provider_->snippets_fetcher()->last_json())); |
| 319 } | 322 } |
| 320 | 323 |
| 321 SendContentSuggestions(); | 324 SendContentSuggestions(); |
| 322 } | 325 } |
| 323 | 326 |
| 324 void SnippetsInternalsMessageHandler::SendClassification() { | 327 void SnippetsInternalsMessageHandler::SendClassification() { |
| 325 web_ui()->CallJavascriptFunctionUnsafe( | 328 web_ui()->CallJavascriptFunctionUnsafe( |
| 326 "chrome.SnippetsInternals.receiveClassification", | 329 "chrome.SnippetsInternals.receiveClassification", |
| 327 base::StringValue(content_suggestions_service_->user_classifier() | 330 base::StringValue(content_suggestions_service_->user_classifier() |
| 328 ->GetUserClassDescriptionForDebugging()), | 331 ->GetUserClassDescriptionForDebugging()), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 category_entry->SetString( | 370 category_entry->SetString( |
| 368 "dismissedContainerId", | 371 "dismissedContainerId", |
| 369 "dismissed-suggestions-" + base::IntToString(category.id())); | 372 "dismissed-suggestions-" + base::IntToString(category.id())); |
| 370 category_entry->SetString("title", info->title()); | 373 category_entry->SetString("title", info->title()); |
| 371 category_entry->SetString("status", GetCategoryStatusName(status)); | 374 category_entry->SetString("status", GetCategoryStatusName(status)); |
| 372 category_entry->Set("suggestions", std::move(suggestions_list)); | 375 category_entry->Set("suggestions", std::move(suggestions_list)); |
| 373 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); | 376 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); |
| 374 categories_list->Append(std::move(category_entry)); | 377 categories_list->Append(std::move(category_entry)); |
| 375 } | 378 } |
| 376 | 379 |
| 377 if (ntp_snippets_service_) { | 380 if (remote_suggestions_provider_) { |
| 378 const std::string& status = | 381 const std::string& status = |
| 379 ntp_snippets_service_->snippets_fetcher()->last_status(); | 382 remote_suggestions_provider_->snippets_fetcher()->last_status(); |
| 380 if (!status.empty()) | 383 if (!status.empty()) |
| 381 SendString("hosts-status", "Finished: " + status); | 384 SendString("hosts-status", "Finished: " + status); |
| 382 } | 385 } |
| 383 | 386 |
| 384 base::DictionaryValue result; | 387 base::DictionaryValue result; |
| 385 result.Set("list", std::move(categories_list)); | 388 result.Set("list", std::move(categories_list)); |
| 386 web_ui()->CallJavascriptFunctionUnsafe( | 389 web_ui()->CallJavascriptFunctionUnsafe( |
| 387 "chrome.SnippetsInternals.receiveContentSuggestions", result); | 390 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
| 388 } | 391 } |
| 389 | 392 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 403 | 406 |
| 404 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( | 407 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( |
| 405 Category category, | 408 Category category, |
| 406 std::vector<ContentSuggestion> dismissed_suggestions) { | 409 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 407 if (dismissed_state_[category] == DismissedState::HIDDEN) | 410 if (dismissed_state_[category] == DismissedState::HIDDEN) |
| 408 return; | 411 return; |
| 409 dismissed_suggestions_[category] = std::move(dismissed_suggestions); | 412 dismissed_suggestions_[category] = std::move(dismissed_suggestions); |
| 410 dismissed_state_[category] = DismissedState::VISIBLE; | 413 dismissed_state_[category] = DismissedState::VISIBLE; |
| 411 SendContentSuggestions(); | 414 SendContentSuggestions(); |
| 412 } | 415 } |
| OLD | NEW |