| 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 <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 using ntp_snippets::ContentSuggestion; | 29 using ntp_snippets::ContentSuggestion; |
| 30 using ntp_snippets::ContentSuggestionsCategory; | 30 using ntp_snippets::ContentSuggestionsCategory; |
| 31 using ntp_snippets::ContentSuggestionsCategoryStatus; | 31 using ntp_snippets::ContentSuggestionsCategoryStatus; |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 std::unique_ptr<base::DictionaryValue> PrepareSnippet( | 35 std::unique_ptr<base::DictionaryValue> PrepareSnippet( |
| 36 const ntp_snippets::NTPSnippet& snippet, | 36 const ntp_snippets::NTPSnippet& snippet, |
| 37 int index, | 37 int index, |
| 38 bool discarded) { | 38 bool dismissed) { |
| 39 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 39 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 40 entry->SetString("snippetId", snippet.id()); | 40 entry->SetString("snippetId", snippet.id()); |
| 41 entry->SetString("title", snippet.title()); | 41 entry->SetString("title", snippet.title()); |
| 42 entry->SetString("siteTitle", snippet.best_source().publisher_name); | 42 entry->SetString("siteTitle", snippet.best_source().publisher_name); |
| 43 entry->SetString("snippet", snippet.snippet()); | 43 entry->SetString("snippet", snippet.snippet()); |
| 44 entry->SetString("published", | 44 entry->SetString("published", |
| 45 TimeFormatShortDateAndTime(snippet.publish_date())); | 45 TimeFormatShortDateAndTime(snippet.publish_date())); |
| 46 entry->SetString("expires", | 46 entry->SetString("expires", |
| 47 TimeFormatShortDateAndTime(snippet.expiry_date())); | 47 TimeFormatShortDateAndTime(snippet.expiry_date())); |
| 48 entry->SetString("url", snippet.best_source().url.spec()); | 48 entry->SetString("url", snippet.best_source().url.spec()); |
| 49 entry->SetString("ampUrl", snippet.best_source().amp_url.spec()); | 49 entry->SetString("ampUrl", snippet.best_source().amp_url.spec()); |
| 50 entry->SetString("salientImageUrl", snippet.salient_image_url().spec()); | 50 entry->SetString("salientImageUrl", snippet.salient_image_url().spec()); |
| 51 entry->SetDouble("score", snippet.score()); | 51 entry->SetDouble("score", snippet.score()); |
| 52 | 52 |
| 53 if (discarded) | 53 if (dismissed) |
| 54 entry->SetString("id", "discarded-snippet-" + base::IntToString(index)); | 54 entry->SetString("id", "dismissed-snippet-" + base::IntToString(index)); |
| 55 else | 55 else |
| 56 entry->SetString("id", "snippet-" + base::IntToString(index)); | 56 entry->SetString("id", "snippet-" + base::IntToString(index)); |
| 57 | 57 |
| 58 return entry; | 58 return entry; |
| 59 } | 59 } |
| 60 | 60 |
| 61 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( | 61 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( |
| 62 const ContentSuggestion& suggestion, | 62 const ContentSuggestion& suggestion, |
| 63 int index) { | 63 int index) { |
| 64 auto entry = base::MakeUnique<base::DictionaryValue>(); | 64 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 176 |
| 177 web_ui()->RegisterMessageCallback( | 177 web_ui()->RegisterMessageCallback( |
| 178 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, | 178 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, |
| 179 base::Unretained(this))); | 179 base::Unretained(this))); |
| 180 | 180 |
| 181 web_ui()->RegisterMessageCallback( | 181 web_ui()->RegisterMessageCallback( |
| 182 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, | 182 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, |
| 183 base::Unretained(this))); | 183 base::Unretained(this))); |
| 184 | 184 |
| 185 web_ui()->RegisterMessageCallback( | 185 web_ui()->RegisterMessageCallback( |
| 186 "clearDiscarded", | 186 "clearDismissed", |
| 187 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, | 187 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDismissed, |
| 188 base::Unretained(this))); | 188 base::Unretained(this))); |
| 189 | 189 |
| 190 web_ui()->RegisterMessageCallback( | 190 web_ui()->RegisterMessageCallback( |
| 191 "clearCachedSuggestions", | 191 "clearCachedSuggestions", |
| 192 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, | 192 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, |
| 193 base::Unretained(this))); | 193 base::Unretained(this))); |
| 194 | 194 |
| 195 web_ui()->RegisterMessageCallback( | 195 web_ui()->RegisterMessageCallback( |
| 196 "clearDiscardedSuggestions", | 196 "clearDismissedSuggestions", |
| 197 base::Bind( | 197 base::Bind( |
| 198 &SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions, | 198 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, |
| 199 base::Unretained(this))); | 199 base::Unretained(this))); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void SnippetsInternalsMessageHandler::HandleRefreshContent( | 202 void SnippetsInternalsMessageHandler::HandleRefreshContent( |
| 203 const base::ListValue* args) { | 203 const base::ListValue* args) { |
| 204 DCHECK_EQ(0u, args->GetSize()); | 204 DCHECK_EQ(0u, args->GetSize()); |
| 205 | 205 |
| 206 dom_loaded_ = true; | 206 dom_loaded_ = true; |
| 207 | 207 |
| 208 SendAllContent(); | 208 SendAllContent(); |
| 209 } | 209 } |
| 210 | 210 |
| 211 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) { | 211 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) { |
| 212 DCHECK_EQ(0u, args->GetSize()); | 212 DCHECK_EQ(0u, args->GetSize()); |
| 213 | 213 |
| 214 ntp_snippets_service_->ClearCachedSuggestionsForDebugging(); | 214 ntp_snippets_service_->ClearCachedSuggestionsForDebugging(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 void SnippetsInternalsMessageHandler::HandleClearDiscarded( | 217 void SnippetsInternalsMessageHandler::HandleClearDismissed( |
| 218 const base::ListValue* args) { | 218 const base::ListValue* args) { |
| 219 DCHECK_EQ(0u, args->GetSize()); | 219 DCHECK_EQ(0u, args->GetSize()); |
| 220 | 220 |
| 221 ntp_snippets_service_->ClearDiscardedSuggestionsForDebugging(); | 221 ntp_snippets_service_->ClearDismissedSuggestionsForDebugging(); |
| 222 SendDiscardedSnippets(); | 222 SendDismissedSnippets(); |
| 223 } | 223 } |
| 224 | 224 |
| 225 void SnippetsInternalsMessageHandler::HandleDownload( | 225 void SnippetsInternalsMessageHandler::HandleDownload( |
| 226 const base::ListValue* args) { | 226 const base::ListValue* args) { |
| 227 DCHECK_EQ(1u, args->GetSize()); | 227 DCHECK_EQ(1u, args->GetSize()); |
| 228 | 228 |
| 229 SendString("hosts-status", std::string()); | 229 SendString("hosts-status", std::string()); |
| 230 | 230 |
| 231 std::string hosts_string; | 231 std::string hosts_string; |
| 232 args->GetString(0, &hosts_string); | 232 args->GetString(0, &hosts_string); |
| 233 | 233 |
| 234 std::vector<std::string> hosts_vector = base::SplitString( | 234 std::vector<std::string> hosts_vector = base::SplitString( |
| 235 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 235 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 236 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); | 236 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); |
| 237 | 237 |
| 238 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); | 238 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); |
| 239 } | 239 } |
| 240 | 240 |
| 241 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( | 241 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( |
| 242 const base::ListValue* args) { | 242 const base::ListValue* args) { |
| 243 DCHECK_EQ(0u, args->GetSize()); | 243 DCHECK_EQ(0u, args->GetSize()); |
| 244 | 244 |
| 245 content_suggestions_service_->ClearCachedSuggestionsForDebugging(); | 245 content_suggestions_service_->ClearCachedSuggestionsForDebugging(); |
| 246 } | 246 } |
| 247 | 247 |
| 248 void SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions( | 248 void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions( |
| 249 const base::ListValue* args) { | 249 const base::ListValue* args) { |
| 250 DCHECK_EQ(0u, args->GetSize()); | 250 DCHECK_EQ(0u, args->GetSize()); |
| 251 | 251 |
| 252 content_suggestions_service_->ClearDiscardedSuggestionsForDebugging(); | 252 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(); |
| 253 } | 253 } |
| 254 | 254 |
| 255 void SnippetsInternalsMessageHandler::SendAllContent() { | 255 void SnippetsInternalsMessageHandler::SendAllContent() { |
| 256 SendHosts(); | 256 SendHosts(); |
| 257 | 257 |
| 258 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 258 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 259 chrome::android::kNTPSnippetsFeature)); | 259 chrome::android::kNTPSnippetsFeature)); |
| 260 | 260 |
| 261 SendBoolean("flag-offline-page-suggestions", | 261 SendBoolean("flag-offline-page-suggestions", |
| 262 base::FeatureList::IsEnabled( | 262 base::FeatureList::IsEnabled( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 277 break; | 277 break; |
| 278 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: | 278 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: |
| 279 SendString("switch-personalized", "Only non-personalized"); | 279 SendString("switch-personalized", "Only non-personalized"); |
| 280 break; | 280 break; |
| 281 } | 281 } |
| 282 | 282 |
| 283 SendString("switch-fetch-url", | 283 SendString("switch-fetch-url", |
| 284 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); | 284 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); |
| 285 | 285 |
| 286 SendSnippets(); | 286 SendSnippets(); |
| 287 SendDiscardedSnippets(); | 287 SendDismissedSnippets(); |
| 288 SendContentSuggestions(); | 288 SendContentSuggestions(); |
| 289 } | 289 } |
| 290 | 290 |
| 291 void SnippetsInternalsMessageHandler::SendSnippets() { | 291 void SnippetsInternalsMessageHandler::SendSnippets() { |
| 292 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 292 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 293 | 293 |
| 294 int index = 0; | 294 int index = 0; |
| 295 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 295 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
| 296 ntp_snippets_service_->snippets()) | 296 ntp_snippets_service_->snippets()) |
| 297 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); | 297 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); |
| 298 | 298 |
| 299 base::DictionaryValue result; | 299 base::DictionaryValue result; |
| 300 result.Set("list", std::move(snippets_list)); | 300 result.Set("list", std::move(snippets_list)); |
| 301 web_ui()->CallJavascriptFunctionUnsafe( | 301 web_ui()->CallJavascriptFunctionUnsafe( |
| 302 "chrome.SnippetsInternals.receiveSnippets", result); | 302 "chrome.SnippetsInternals.receiveSnippets", result); |
| 303 | 303 |
| 304 const std::string& status = | 304 const std::string& status = |
| 305 ntp_snippets_service_->snippets_fetcher()->last_status(); | 305 ntp_snippets_service_->snippets_fetcher()->last_status(); |
| 306 if (!status.empty()) | 306 if (!status.empty()) |
| 307 SendString("hosts-status", "Finished: " + status); | 307 SendString("hosts-status", "Finished: " + status); |
| 308 } | 308 } |
| 309 | 309 |
| 310 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { | 310 void SnippetsInternalsMessageHandler::SendDismissedSnippets() { |
| 311 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 311 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 312 | 312 |
| 313 int index = 0; | 313 int index = 0; |
| 314 for (const auto& snippet : ntp_snippets_service_->discarded_snippets()) | 314 for (const auto& snippet : ntp_snippets_service_->dismissed_snippets()) |
| 315 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); | 315 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); |
| 316 | 316 |
| 317 base::DictionaryValue result; | 317 base::DictionaryValue result; |
| 318 result.Set("list", std::move(snippets_list)); | 318 result.Set("list", std::move(snippets_list)); |
| 319 web_ui()->CallJavascriptFunctionUnsafe( | 319 web_ui()->CallJavascriptFunctionUnsafe( |
| 320 "chrome.SnippetsInternals.receiveDiscardedSnippets", result); | 320 "chrome.SnippetsInternals.receiveDismissedSnippets", result); |
| 321 } | 321 } |
| 322 | 322 |
| 323 void SnippetsInternalsMessageHandler::SendHosts() { | 323 void SnippetsInternalsMessageHandler::SendHosts() { |
| 324 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); | 324 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); |
| 325 | 325 |
| 326 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); | 326 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); |
| 327 | 327 |
| 328 for (const std::string& host : hosts) { | 328 for (const std::string& host : hosts) { |
| 329 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 329 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 330 entry->SetString("url", host); | 330 entry->SetString("url", host); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 } | 374 } |
| 375 | 375 |
| 376 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 376 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 377 const std::string& value) { | 377 const std::string& value) { |
| 378 base::StringValue string_name(name); | 378 base::StringValue string_name(name); |
| 379 base::StringValue string_value(value); | 379 base::StringValue string_value(value); |
| 380 | 380 |
| 381 web_ui()->CallJavascriptFunctionUnsafe( | 381 web_ui()->CallJavascriptFunctionUnsafe( |
| 382 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); | 382 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
| 383 } | 383 } |
| OLD | NEW |