Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: chrome/browser/ui/webui/snippets_internals_message_handler.cc

Issue 2187233002: Add ContentSuggestionsCategoryFactory; Store categories as ints (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/resources/snippets_internals.html ('k') | components/ntp_snippets.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 const std::vector<ContentSuggestion>& suggestions = 343 const std::vector<ContentSuggestion>& suggestions =
344 content_suggestions_service_->GetSuggestionsForCategory(category); 344 content_suggestions_service_->GetSuggestionsForCategory(category);
345 345
346 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); 346 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
347 for (const ContentSuggestion& suggestion : suggestions) { 347 for (const ContentSuggestion& suggestion : suggestions) {
348 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); 348 suggestions_list->Append(PrepareSuggestion(suggestion, index++));
349 } 349 }
350 350
351 std::unique_ptr<base::DictionaryValue> category_entry( 351 std::unique_ptr<base::DictionaryValue> category_entry(
352 new base::DictionaryValue); 352 new base::DictionaryValue);
353 category_entry->SetString("name", MapCategoryName(category)); 353 category_entry->SetString("title", GetCategoryTitle(category));
354 category_entry->SetString("status", MapCategoryStatus(status)); 354 category_entry->SetString("status", GetCategoryStatusName(status));
355 category_entry->Set("suggestions", std::move(suggestions_list)); 355 category_entry->Set("suggestions", std::move(suggestions_list));
356 categories_list->Append(std::move(category_entry)); 356 categories_list->Append(std::move(category_entry));
357 } 357 }
358 358
359 base::DictionaryValue result; 359 base::DictionaryValue result;
360 result.Set("list", std::move(categories_list)); 360 result.Set("list", std::move(categories_list));
361 web_ui()->CallJavascriptFunctionUnsafe( 361 web_ui()->CallJavascriptFunctionUnsafe(
362 "chrome.SnippetsInternals.receiveContentSuggestions", result); 362 "chrome.SnippetsInternals.receiveContentSuggestions", result);
363 } 363 }
364 364
365 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, 365 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
366 bool value) { 366 bool value) {
367 SendString(name, value ? "True" : "False"); 367 SendString(name, value ? "True" : "False");
368 } 368 }
369 369
370 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 370 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
371 const std::string& value) { 371 const std::string& value) {
372 base::StringValue string_name(name); 372 base::StringValue string_name(name);
373 base::StringValue string_value(value); 373 base::StringValue string_value(value);
374 374
375 web_ui()->CallJavascriptFunctionUnsafe( 375 web_ui()->CallJavascriptFunctionUnsafe(
376 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); 376 "chrome.SnippetsInternals.receiveProperty", string_name, string_value);
377 } 377 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/snippets_internals.html ('k') | components/ntp_snippets.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698