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 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
15 #include "base/i18n/time_formatting.h" | 15 #include "base/i18n/time_formatting.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/android/chrome_feature_list.h" | 21 #include "chrome/browser/android/chrome_feature_list.h" |
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/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "components/ntp_snippets/category_info.h" |
24 #include "components/ntp_snippets/ntp_snippet.h" | 25 #include "components/ntp_snippets/ntp_snippet.h" |
25 #include "components/ntp_snippets/switches.h" | 26 #include "components/ntp_snippets/switches.h" |
26 #include "content/public/browser/web_ui.h" | 27 #include "content/public/browser/web_ui.h" |
27 | 28 |
28 using ntp_snippets::ContentSuggestion; | 29 using ntp_snippets::ContentSuggestion; |
29 using ntp_snippets::Category; | 30 using ntp_snippets::Category; |
| 31 using ntp_snippets::CategoryInfo; |
30 using ntp_snippets::CategoryStatus; | 32 using ntp_snippets::CategoryStatus; |
31 using ntp_snippets::KnownCategories; | 33 using ntp_snippets::KnownCategories; |
32 | 34 |
33 namespace { | 35 namespace { |
34 | 36 |
35 std::unique_ptr<base::DictionaryValue> PrepareSnippet( | 37 std::unique_ptr<base::DictionaryValue> PrepareSnippet( |
36 const ntp_snippets::NTPSnippet& snippet, | 38 const ntp_snippets::NTPSnippet& snippet, |
37 int index, | 39 int index, |
38 bool dismissed) { | 40 bool dismissed) { |
39 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 41 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
(...skipping 27 matching lines...) Expand all Loading... |
67 entry->SetString("ampUrl", suggestion.amp_url().spec()); | 69 entry->SetString("ampUrl", suggestion.amp_url().spec()); |
68 entry->SetString("title", suggestion.title()); | 70 entry->SetString("title", suggestion.title()); |
69 entry->SetString("snippetText", suggestion.snippet_text()); | 71 entry->SetString("snippetText", suggestion.snippet_text()); |
70 entry->SetString("publishDate", | 72 entry->SetString("publishDate", |
71 TimeFormatShortDateAndTime(suggestion.publish_date())); | 73 TimeFormatShortDateAndTime(suggestion.publish_date())); |
72 entry->SetString("publisherName", suggestion.publisher_name()); | 74 entry->SetString("publisherName", suggestion.publisher_name()); |
73 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); | 75 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); |
74 return entry; | 76 return entry; |
75 } | 77 } |
76 | 78 |
77 // TODO(pke): Replace this as soon as the service delivers the title directly. | |
78 std::string GetCategoryTitle(Category category) { | |
79 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { | |
80 return "Articles"; | |
81 } | |
82 if (category.IsKnownCategory(KnownCategories::OFFLINE_PAGES)) { | |
83 return "Offline pages (continue browsing)"; | |
84 } | |
85 return std::string(); | |
86 } | |
87 | |
88 std::string GetCategoryStatusName(CategoryStatus status) { | 79 std::string GetCategoryStatusName(CategoryStatus status) { |
89 switch (status) { | 80 switch (status) { |
90 case CategoryStatus::INITIALIZING: | 81 case CategoryStatus::INITIALIZING: |
91 return "INITIALIZING"; | 82 return "INITIALIZING"; |
92 case CategoryStatus::AVAILABLE: | 83 case CategoryStatus::AVAILABLE: |
93 return "AVAILABLE"; | 84 return "AVAILABLE"; |
94 case CategoryStatus::AVAILABLE_LOADING: | 85 case CategoryStatus::AVAILABLE_LOADING: |
95 return "AVAILABLE_LOADING"; | 86 return "AVAILABLE_LOADING"; |
96 case CategoryStatus::NOT_PROVIDED: | 87 case CategoryStatus::NOT_PROVIDED: |
97 return "NOT_PROVIDED"; | 88 return "NOT_PROVIDED"; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 "chrome.SnippetsInternals.receiveHosts", result); | 307 "chrome.SnippetsInternals.receiveHosts", result); |
317 } | 308 } |
318 | 309 |
319 void SnippetsInternalsMessageHandler::SendContentSuggestions() { | 310 void SnippetsInternalsMessageHandler::SendContentSuggestions() { |
320 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); | 311 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); |
321 | 312 |
322 int index = 0; | 313 int index = 0; |
323 for (Category category : content_suggestions_service_->GetCategories()) { | 314 for (Category category : content_suggestions_service_->GetCategories()) { |
324 CategoryStatus status = | 315 CategoryStatus status = |
325 content_suggestions_service_->GetCategoryStatus(category); | 316 content_suggestions_service_->GetCategoryStatus(category); |
| 317 const CategoryInfo& info = |
| 318 content_suggestions_service_->GetCategoryInfo(category); |
326 const std::vector<ContentSuggestion>& suggestions = | 319 const std::vector<ContentSuggestion>& suggestions = |
327 content_suggestions_service_->GetSuggestionsForCategory(category); | 320 content_suggestions_service_->GetSuggestionsForCategory(category); |
328 | 321 |
329 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); | 322 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); |
330 for (const ContentSuggestion& suggestion : suggestions) { | 323 for (const ContentSuggestion& suggestion : suggestions) { |
331 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); | 324 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); |
332 } | 325 } |
333 | 326 |
334 std::unique_ptr<base::DictionaryValue> category_entry( | 327 std::unique_ptr<base::DictionaryValue> category_entry( |
335 new base::DictionaryValue); | 328 new base::DictionaryValue); |
336 category_entry->SetString("title", GetCategoryTitle(category)); | 329 category_entry->SetString("title", info.title()); |
337 category_entry->SetString("status", GetCategoryStatusName(status)); | 330 category_entry->SetString("status", GetCategoryStatusName(status)); |
338 category_entry->Set("suggestions", std::move(suggestions_list)); | 331 category_entry->Set("suggestions", std::move(suggestions_list)); |
339 categories_list->Append(std::move(category_entry)); | 332 categories_list->Append(std::move(category_entry)); |
340 } | 333 } |
341 | 334 |
342 base::DictionaryValue result; | 335 base::DictionaryValue result; |
343 result.Set("list", std::move(categories_list)); | 336 result.Set("list", std::move(categories_list)); |
344 web_ui()->CallJavascriptFunctionUnsafe( | 337 web_ui()->CallJavascriptFunctionUnsafe( |
345 "chrome.SnippetsInternals.receiveContentSuggestions", result); | 338 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
346 } | 339 } |
347 | 340 |
348 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 341 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
349 bool value) { | 342 bool value) { |
350 SendString(name, value ? "True" : "False"); | 343 SendString(name, value ? "True" : "False"); |
351 } | 344 } |
352 | 345 |
353 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 346 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
354 const std::string& value) { | 347 const std::string& value) { |
355 base::StringValue string_name(name); | 348 base::StringValue string_name(name); |
356 base::StringValue string_value(value); | 349 base::StringValue string_value(value); |
357 | 350 |
358 web_ui()->CallJavascriptFunctionUnsafe( | 351 web_ui()->CallJavascriptFunctionUnsafe( |
359 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); | 352 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
360 } | 353 } |
OLD | NEW |