| 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 11 matching lines...) Expand all Loading... |
| 22 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 22 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 23 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | 23 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "components/ntp_snippets/ntp_snippet.h" | 25 #include "components/ntp_snippets/ntp_snippet.h" |
| 26 #include "components/ntp_snippets/switches.h" | 26 #include "components/ntp_snippets/switches.h" |
| 27 #include "content/public/browser/web_ui.h" | 27 #include "content/public/browser/web_ui.h" |
| 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 using ntp_snippets::KnownSuggestionsCategories; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 std::unique_ptr<base::DictionaryValue> PrepareSnippet( | 36 std::unique_ptr<base::DictionaryValue> PrepareSnippet( |
| 36 const ntp_snippets::NTPSnippet& snippet, | 37 const ntp_snippets::NTPSnippet& snippet, |
| 37 int index, | 38 int index, |
| 38 bool dismissed) { | 39 bool dismissed) { |
| 39 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 40 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 40 entry->SetString("snippetId", snippet.id()); | 41 entry->SetString("snippetId", snippet.id()); |
| 41 entry->SetString("title", snippet.title()); | 42 entry->SetString("title", snippet.title()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 67 entry->SetString("ampUrl", suggestion.amp_url().spec()); | 68 entry->SetString("ampUrl", suggestion.amp_url().spec()); |
| 68 entry->SetString("title", suggestion.title()); | 69 entry->SetString("title", suggestion.title()); |
| 69 entry->SetString("snippetText", suggestion.snippet_text()); | 70 entry->SetString("snippetText", suggestion.snippet_text()); |
| 70 entry->SetString("publishDate", | 71 entry->SetString("publishDate", |
| 71 TimeFormatShortDateAndTime(suggestion.publish_date())); | 72 TimeFormatShortDateAndTime(suggestion.publish_date())); |
| 72 entry->SetString("publisherName", suggestion.publisher_name()); | 73 entry->SetString("publisherName", suggestion.publisher_name()); |
| 73 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); | 74 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); |
| 74 return entry; | 75 return entry; |
| 75 } | 76 } |
| 76 | 77 |
| 77 std::string MapCategoryName(ContentSuggestionsCategory category) { | 78 // TODO(pke): Replace this as soon as the service delivers the title directly. |
| 78 switch (category) { | 79 std::string GetCategoryTitle(ContentSuggestionsCategory category) { |
| 79 case ContentSuggestionsCategory::ARTICLES: | 80 if (category.IsKnownCategory(KnownSuggestionsCategories::ARTICLES)) { |
| 80 return "Articles"; | 81 return "Articles"; |
| 81 case ContentSuggestionsCategory::OFFLINE_PAGES: | 82 } |
| 82 return "Offline pages (continue browsing)"; | 83 if (category.IsKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES)) { |
| 83 case ContentSuggestionsCategory::COUNT: | 84 return "Offline pages (continue browsing)"; |
| 84 NOTREACHED() << "Category::COUNT must not be used as a value"; | |
| 85 } | 85 } |
| 86 return std::string(); | 86 return std::string(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 std::string MapCategoryStatus(ContentSuggestionsCategoryStatus status) { | 89 std::string GetCategoryStatusName(ContentSuggestionsCategoryStatus status) { |
| 90 switch (status) { | 90 switch (status) { |
| 91 case ContentSuggestionsCategoryStatus::INITIALIZING: | 91 case ContentSuggestionsCategoryStatus::INITIALIZING: |
| 92 return "INITIALIZING"; | 92 return "INITIALIZING"; |
| 93 case ContentSuggestionsCategoryStatus::AVAILABLE: | 93 case ContentSuggestionsCategoryStatus::AVAILABLE: |
| 94 return "AVAILABLE"; | 94 return "AVAILABLE"; |
| 95 case ContentSuggestionsCategoryStatus::AVAILABLE_LOADING: | 95 case ContentSuggestionsCategoryStatus::AVAILABLE_LOADING: |
| 96 return "AVAILABLE_LOADING"; | 96 return "AVAILABLE_LOADING"; |
| 97 case ContentSuggestionsCategoryStatus::NOT_PROVIDED: | 97 case ContentSuggestionsCategoryStatus::NOT_PROVIDED: |
| 98 return "NOT_PROVIDED"; | 98 return "NOT_PROVIDED"; |
| 99 case ContentSuggestionsCategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED: | 99 case ContentSuggestionsCategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED: |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 const std::vector<ContentSuggestion>& suggestions = | 349 const std::vector<ContentSuggestion>& suggestions = |
| 350 content_suggestions_service_->GetSuggestionsForCategory(category); | 350 content_suggestions_service_->GetSuggestionsForCategory(category); |
| 351 | 351 |
| 352 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); | 352 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); |
| 353 for (const ContentSuggestion& suggestion : suggestions) { | 353 for (const ContentSuggestion& suggestion : suggestions) { |
| 354 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); | 354 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); |
| 355 } | 355 } |
| 356 | 356 |
| 357 std::unique_ptr<base::DictionaryValue> category_entry( | 357 std::unique_ptr<base::DictionaryValue> category_entry( |
| 358 new base::DictionaryValue); | 358 new base::DictionaryValue); |
| 359 category_entry->SetString("name", MapCategoryName(category)); | 359 category_entry->SetString("title", GetCategoryTitle(category)); |
| 360 category_entry->SetString("status", MapCategoryStatus(status)); | 360 category_entry->SetString("status", GetCategoryStatusName(status)); |
| 361 category_entry->Set("suggestions", std::move(suggestions_list)); | 361 category_entry->Set("suggestions", std::move(suggestions_list)); |
| 362 categories_list->Append(std::move(category_entry)); | 362 categories_list->Append(std::move(category_entry)); |
| 363 } | 363 } |
| 364 | 364 |
| 365 base::DictionaryValue result; | 365 base::DictionaryValue result; |
| 366 result.Set("list", std::move(categories_list)); | 366 result.Set("list", std::move(categories_list)); |
| 367 web_ui()->CallJavascriptFunctionUnsafe( | 367 web_ui()->CallJavascriptFunctionUnsafe( |
| 368 "chrome.SnippetsInternals.receiveContentSuggestions", result); | 368 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
| 369 } | 369 } |
| 370 | 370 |
| 371 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 371 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
| 372 bool value) { | 372 bool value) { |
| 373 SendString(name, value ? "True" : "False"); | 373 SendString(name, value ? "True" : "False"); |
| 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 |