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/optional.h" | |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
20 #include "base/values.h" | 21 #include "base/values.h" |
21 #include "chrome/browser/android/chrome_feature_list.h" | 22 #include "chrome/browser/android/chrome_feature_list.h" |
22 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 23 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
25 #include "components/ntp_snippets/category_info.h" | |
24 #include "components/ntp_snippets/features.h" | 26 #include "components/ntp_snippets/features.h" |
25 #include "components/ntp_snippets/ntp_snippet.h" | 27 #include "components/ntp_snippets/ntp_snippet.h" |
26 #include "components/ntp_snippets/switches.h" | 28 #include "components/ntp_snippets/switches.h" |
27 #include "content/public/browser/web_ui.h" | 29 #include "content/public/browser/web_ui.h" |
28 | 30 |
29 using ntp_snippets::ContentSuggestion; | 31 using ntp_snippets::ContentSuggestion; |
30 using ntp_snippets::Category; | 32 using ntp_snippets::Category; |
33 using ntp_snippets::CategoryInfo; | |
31 using ntp_snippets::CategoryStatus; | 34 using ntp_snippets::CategoryStatus; |
32 using ntp_snippets::KnownCategories; | 35 using ntp_snippets::KnownCategories; |
33 | 36 |
34 namespace { | 37 namespace { |
35 | 38 |
36 std::unique_ptr<base::DictionaryValue> PrepareSnippet( | 39 std::unique_ptr<base::DictionaryValue> PrepareSnippet( |
37 const ntp_snippets::NTPSnippet& snippet, | 40 const ntp_snippets::NTPSnippet& snippet, |
38 int index, | 41 int index, |
39 bool dismissed) { | 42 bool dismissed) { |
40 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 43 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
(...skipping 27 matching lines...) Expand all Loading... | |
68 entry->SetString("ampUrl", suggestion.amp_url().spec()); | 71 entry->SetString("ampUrl", suggestion.amp_url().spec()); |
69 entry->SetString("title", suggestion.title()); | 72 entry->SetString("title", suggestion.title()); |
70 entry->SetString("snippetText", suggestion.snippet_text()); | 73 entry->SetString("snippetText", suggestion.snippet_text()); |
71 entry->SetString("publishDate", | 74 entry->SetString("publishDate", |
72 TimeFormatShortDateAndTime(suggestion.publish_date())); | 75 TimeFormatShortDateAndTime(suggestion.publish_date())); |
73 entry->SetString("publisherName", suggestion.publisher_name()); | 76 entry->SetString("publisherName", suggestion.publisher_name()); |
74 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); | 77 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); |
75 return entry; | 78 return entry; |
76 } | 79 } |
77 | 80 |
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) { | 81 std::string GetCategoryStatusName(CategoryStatus status) { |
93 switch (status) { | 82 switch (status) { |
94 case CategoryStatus::INITIALIZING: | 83 case CategoryStatus::INITIALIZING: |
95 return "INITIALIZING"; | 84 return "INITIALIZING"; |
96 case CategoryStatus::AVAILABLE: | 85 case CategoryStatus::AVAILABLE: |
97 return "AVAILABLE"; | 86 return "AVAILABLE"; |
98 case CategoryStatus::AVAILABLE_LOADING: | 87 case CategoryStatus::AVAILABLE_LOADING: |
99 return "AVAILABLE_LOADING"; | 88 return "AVAILABLE_LOADING"; |
100 case CategoryStatus::NOT_PROVIDED: | 89 case CategoryStatus::NOT_PROVIDED: |
101 return "NOT_PROVIDED"; | 90 return "NOT_PROVIDED"; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 "chrome.SnippetsInternals.receiveHosts", result); | 309 "chrome.SnippetsInternals.receiveHosts", result); |
321 } | 310 } |
322 | 311 |
323 void SnippetsInternalsMessageHandler::SendContentSuggestions() { | 312 void SnippetsInternalsMessageHandler::SendContentSuggestions() { |
324 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); | 313 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); |
325 | 314 |
326 int index = 0; | 315 int index = 0; |
327 for (Category category : content_suggestions_service_->GetCategories()) { | 316 for (Category category : content_suggestions_service_->GetCategories()) { |
328 CategoryStatus status = | 317 CategoryStatus status = |
329 content_suggestions_service_->GetCategoryStatus(category); | 318 content_suggestions_service_->GetCategoryStatus(category); |
319 base::Optional<CategoryInfo> info = | |
320 content_suggestions_service_->GetCategoryInfo(category); | |
330 const std::vector<ContentSuggestion>& suggestions = | 321 const std::vector<ContentSuggestion>& suggestions = |
331 content_suggestions_service_->GetSuggestionsForCategory(category); | 322 content_suggestions_service_->GetSuggestionsForCategory(category); |
323 if (!info) | |
Bernhard Bauer
2016/08/09 09:18:33
This shouldn't really happen if we're iterating ov
Bernhard Bauer
2016/08/09 12:38:39
^^^
Philipp Keck
2016/08/09 12:45:14
Done.
| |
324 continue; | |
332 | 325 |
333 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); | 326 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); |
334 for (const ContentSuggestion& suggestion : suggestions) { | 327 for (const ContentSuggestion& suggestion : suggestions) { |
335 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); | 328 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); |
336 } | 329 } |
337 | 330 |
338 std::unique_ptr<base::DictionaryValue> category_entry( | 331 std::unique_ptr<base::DictionaryValue> category_entry( |
339 new base::DictionaryValue); | 332 new base::DictionaryValue); |
340 category_entry->SetString("title", GetCategoryTitle(category)); | 333 category_entry->SetString("title", info->title()); |
341 category_entry->SetString("status", GetCategoryStatusName(status)); | 334 category_entry->SetString("status", GetCategoryStatusName(status)); |
342 category_entry->Set("suggestions", std::move(suggestions_list)); | 335 category_entry->Set("suggestions", std::move(suggestions_list)); |
343 categories_list->Append(std::move(category_entry)); | 336 categories_list->Append(std::move(category_entry)); |
344 } | 337 } |
345 | 338 |
346 base::DictionaryValue result; | 339 base::DictionaryValue result; |
347 result.Set("list", std::move(categories_list)); | 340 result.Set("list", std::move(categories_list)); |
348 web_ui()->CallJavascriptFunctionUnsafe( | 341 web_ui()->CallJavascriptFunctionUnsafe( |
349 "chrome.SnippetsInternals.receiveContentSuggestions", result); | 342 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
350 } | 343 } |
351 | 344 |
352 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 345 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
353 bool value) { | 346 bool value) { |
354 SendString(name, value ? "True" : "False"); | 347 SendString(name, value ? "True" : "False"); |
355 } | 348 } |
356 | 349 |
357 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 350 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
358 const std::string& value) { | 351 const std::string& value) { |
359 base::StringValue string_name(name); | 352 base::StringValue string_name(name); |
360 base::StringValue string_value(value); | 353 base::StringValue string_value(value); |
361 | 354 |
362 web_ui()->CallJavascriptFunctionUnsafe( | 355 web_ui()->CallJavascriptFunctionUnsafe( |
363 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); | 356 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
364 } | 357 } |
OLD | NEW |