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> PrepareSuggestion( | 39 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( |
37 const ContentSuggestion& suggestion, | 40 const ContentSuggestion& suggestion, |
38 int index) { | 41 int index) { |
39 auto entry = base::MakeUnique<base::DictionaryValue>(); | 42 auto entry = base::MakeUnique<base::DictionaryValue>(); |
40 entry->SetString("suggestionId", suggestion.id()); | 43 entry->SetString("suggestionId", suggestion.id()); |
41 entry->SetString("url", suggestion.url().spec()); | 44 entry->SetString("url", suggestion.url().spec()); |
42 entry->SetString("ampUrl", suggestion.amp_url().spec()); | 45 entry->SetString("ampUrl", suggestion.amp_url().spec()); |
43 entry->SetString("title", suggestion.title()); | 46 entry->SetString("title", suggestion.title()); |
44 entry->SetString("snippetText", suggestion.snippet_text()); | 47 entry->SetString("snippetText", suggestion.snippet_text()); |
45 entry->SetString("publishDate", | 48 entry->SetString("publishDate", |
46 TimeFormatShortDateAndTime(suggestion.publish_date())); | 49 TimeFormatShortDateAndTime(suggestion.publish_date())); |
47 entry->SetString("publisherName", suggestion.publisher_name()); | 50 entry->SetString("publisherName", suggestion.publisher_name()); |
48 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); | 51 entry->SetString("id", "content-suggestion-" + base::IntToString(index)); |
49 return entry; | 52 return entry; |
50 } | 53 } |
51 | 54 |
52 // TODO(pke): Replace this as soon as the service delivers the title directly. | |
53 std::string GetCategoryTitle(Category category) { | |
54 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { | |
55 return "Articles"; | |
56 } | |
57 if (category.IsKnownCategory(KnownCategories::OFFLINE_PAGES)) { | |
58 return "Offline pages (continue browsing)"; | |
59 } | |
60 if (category.IsKnownCategory(KnownCategories::BOOKMARKS)) { | |
61 return "Recently visited bookmarks"; | |
62 } | |
63 return std::string(); | |
64 } | |
65 | |
66 std::string GetCategoryStatusName(CategoryStatus status) { | 55 std::string GetCategoryStatusName(CategoryStatus status) { |
67 switch (status) { | 56 switch (status) { |
68 case CategoryStatus::INITIALIZING: | 57 case CategoryStatus::INITIALIZING: |
69 return "INITIALIZING"; | 58 return "INITIALIZING"; |
70 case CategoryStatus::AVAILABLE: | 59 case CategoryStatus::AVAILABLE: |
71 return "AVAILABLE"; | 60 return "AVAILABLE"; |
72 case CategoryStatus::AVAILABLE_LOADING: | 61 case CategoryStatus::AVAILABLE_LOADING: |
73 return "AVAILABLE_LOADING"; | 62 return "AVAILABLE_LOADING"; |
74 case CategoryStatus::NOT_PROVIDED: | 63 case CategoryStatus::NOT_PROVIDED: |
75 return "NOT_PROVIDED"; | 64 return "NOT_PROVIDED"; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 "chrome.SnippetsInternals.receiveHosts", result); | 243 "chrome.SnippetsInternals.receiveHosts", result); |
255 } | 244 } |
256 | 245 |
257 void SnippetsInternalsMessageHandler::SendContentSuggestions() { | 246 void SnippetsInternalsMessageHandler::SendContentSuggestions() { |
258 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); | 247 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); |
259 | 248 |
260 int index = 0; | 249 int index = 0; |
261 for (Category category : content_suggestions_service_->GetCategories()) { | 250 for (Category category : content_suggestions_service_->GetCategories()) { |
262 CategoryStatus status = | 251 CategoryStatus status = |
263 content_suggestions_service_->GetCategoryStatus(category); | 252 content_suggestions_service_->GetCategoryStatus(category); |
| 253 base::Optional<CategoryInfo> info = |
| 254 content_suggestions_service_->GetCategoryInfo(category); |
| 255 DCHECK(info); |
264 const std::vector<ContentSuggestion>& suggestions = | 256 const std::vector<ContentSuggestion>& suggestions = |
265 content_suggestions_service_->GetSuggestionsForCategory(category); | 257 content_suggestions_service_->GetSuggestionsForCategory(category); |
266 std::vector<ContentSuggestion> dismissed_suggestions = | 258 std::vector<ContentSuggestion> dismissed_suggestions = |
267 content_suggestions_service_->GetDismissedSuggestionsForDebugging( | 259 content_suggestions_service_->GetDismissedSuggestionsForDebugging( |
268 category); | 260 category); |
269 | 261 |
270 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); | 262 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); |
271 for (const ContentSuggestion& suggestion : suggestions) { | 263 for (const ContentSuggestion& suggestion : suggestions) { |
272 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); | 264 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); |
273 } | 265 } |
274 | 266 |
275 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue); | 267 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue); |
276 for (const ContentSuggestion& suggestion : dismissed_suggestions) { | 268 for (const ContentSuggestion& suggestion : dismissed_suggestions) { |
277 dismissed_list->Append(PrepareSuggestion(suggestion, index++)); | 269 dismissed_list->Append(PrepareSuggestion(suggestion, index++)); |
278 } | 270 } |
279 | 271 |
280 std::unique_ptr<base::DictionaryValue> category_entry( | 272 std::unique_ptr<base::DictionaryValue> category_entry( |
281 new base::DictionaryValue); | 273 new base::DictionaryValue); |
282 category_entry->SetInteger("categoryId", category.id()); | 274 category_entry->SetInteger("categoryId", category.id()); |
283 category_entry->SetString( | 275 category_entry->SetString( |
284 "dismissedContainerId", | 276 "dismissedContainerId", |
285 "dismissed-suggestions-" + base::IntToString(category.id())); | 277 "dismissed-suggestions-" + base::IntToString(category.id())); |
286 category_entry->SetString("title", GetCategoryTitle(category)); | 278 category_entry->SetString("title", info->title()); |
287 category_entry->SetString("status", GetCategoryStatusName(status)); | 279 category_entry->SetString("status", GetCategoryStatusName(status)); |
288 category_entry->Set("suggestions", std::move(suggestions_list)); | 280 category_entry->Set("suggestions", std::move(suggestions_list)); |
289 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); | 281 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); |
290 categories_list->Append(std::move(category_entry)); | 282 categories_list->Append(std::move(category_entry)); |
291 } | 283 } |
292 | 284 |
293 base::DictionaryValue result; | 285 base::DictionaryValue result; |
294 result.Set("list", std::move(categories_list)); | 286 result.Set("list", std::move(categories_list)); |
295 web_ui()->CallJavascriptFunctionUnsafe( | 287 web_ui()->CallJavascriptFunctionUnsafe( |
296 "chrome.SnippetsInternals.receiveContentSuggestions", result); | 288 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
297 } | 289 } |
298 | 290 |
299 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 291 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
300 bool value) { | 292 bool value) { |
301 SendString(name, value ? "True" : "False"); | 293 SendString(name, value ? "True" : "False"); |
302 } | 294 } |
303 | 295 |
304 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 296 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
305 const std::string& value) { | 297 const std::string& value) { |
306 base::StringValue string_name(name); | 298 base::StringValue string_name(name); |
307 base::StringValue string_value(value); | 299 base::StringValue string_value(value); |
308 | 300 |
309 web_ui()->CallJavascriptFunctionUnsafe( | 301 web_ui()->CallJavascriptFunctionUnsafe( |
310 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); | 302 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
311 } | 303 } |
OLD | NEW |