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