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

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

Issue 2145563002: Add ContentSuggestionsService to SnippetsInternals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@articleprovider2
Patch Set: Created 4 years, 5 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
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
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/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
19 #include "base/values.h" 19 #include "base/values.h"
20 #include "chrome/browser/android/chrome_feature_list.h" 20 #include "chrome/browser/android/chrome_feature_list.h"
21 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h"
21 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" 22 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h"
22 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
23 #include "components/ntp_snippets/ntp_snippet.h" 24 #include "components/ntp_snippets/ntp_snippet.h"
24 #include "components/ntp_snippets/switches.h" 25 #include "components/ntp_snippets/switches.h"
25 #include "content/public/browser/web_ui.h" 26 #include "content/public/browser/web_ui.h"
26 27
28 using ntp_snippets::ContentSuggestion;
29 using ntp_snippets::ContentSuggestionsCategory;
30 using ntp_snippets::ContentSuggestionsCategoryStatus;
31
27 namespace { 32 namespace {
28 33
29 std::unique_ptr<base::DictionaryValue> PrepareSnippet( 34 std::unique_ptr<base::DictionaryValue> PrepareSnippet(
30 const ntp_snippets::NTPSnippet& snippet, 35 const ntp_snippets::NTPSnippet& snippet,
31 int index, 36 int index,
32 bool discarded) { 37 bool discarded) {
33 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 38 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
34 entry->SetString("snippetId", snippet.id()); 39 entry->SetString("snippetId", snippet.id());
35 entry->SetString("title", snippet.title()); 40 entry->SetString("title", snippet.title());
36 entry->SetString("siteTitle", snippet.best_source().publisher_name); 41 entry->SetString("siteTitle", snippet.best_source().publisher_name);
37 entry->SetString("snippet", snippet.snippet()); 42 entry->SetString("snippet", snippet.snippet());
38 entry->SetString("published", 43 entry->SetString("published",
39 TimeFormatShortDateAndTime(snippet.publish_date())); 44 TimeFormatShortDateAndTime(snippet.publish_date()));
40 entry->SetString("expires", 45 entry->SetString("expires",
41 TimeFormatShortDateAndTime(snippet.expiry_date())); 46 TimeFormatShortDateAndTime(snippet.expiry_date()));
42 entry->SetString("url", snippet.best_source().url.spec()); 47 entry->SetString("url", snippet.best_source().url.spec());
43 entry->SetString("ampUrl", snippet.best_source().amp_url.spec()); 48 entry->SetString("ampUrl", snippet.best_source().amp_url.spec());
44 entry->SetString("salientImageUrl", snippet.salient_image_url().spec()); 49 entry->SetString("salientImageUrl", snippet.salient_image_url().spec());
45 entry->SetDouble("score", snippet.score()); 50 entry->SetDouble("score", snippet.score());
46 51
47 if (discarded) 52 if (discarded)
48 entry->SetString("id", "discarded-snippet-" + base::IntToString(index)); 53 entry->SetString("id", "discarded-snippet-" + base::IntToString(index));
49 else 54 else
50 entry->SetString("id", "snippet-" + base::IntToString(index)); 55 entry->SetString("id", "snippet-" + base::IntToString(index));
51 56
52 return entry; 57 return entry;
53 } 58 }
54 59
60 std::unique_ptr<base::DictionaryValue> PrepareSuggestion(
61 const ntp_snippets::ContentSuggestion& suggestion,
Marc Treib 2016/07/12 13:04:52 nit: ntp_snippets:: not needed, you have a "using"
Philipp Keck 2016/07/12 13:45:02 Done.
62 int index) {
63 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
Bernhard Bauer 2016/07/12 13:09:45 You might be able to do `auto entry = base::MakeUn
Philipp Keck 2016/07/12 13:45:02 Done.
64 entry->SetString("suggestionId", suggestion.id());
65 entry->SetString("url", suggestion.url().spec());
66 entry->SetString("ampUrl", suggestion.amp_url().spec());
67 entry->SetString("title", suggestion.title());
68 entry->SetString("snippetText", suggestion.snippet_text());
69 entry->SetString("publishDate",
70 TimeFormatShortDateAndTime(suggestion.publish_date()));
71 entry->SetString("publisherName", suggestion.publisher_name());
72 entry->SetString("id", "content-suggestion-" + base::IntToString(index));
73 return entry;
74 }
75
76 std::string MapCategoryName(ContentSuggestionsCategory category) {
77 switch (category) {
78 case ContentSuggestionsCategory::ARTICLES:
79 return "Articles";
80 default:
Bernhard Bauer 2016/07/12 13:09:45 If you remove the default case from the switch sta
Philipp Keck 2016/07/12 13:45:02 Done. Oh great. I didn't know it would warn and th
Bernhard Bauer 2016/07/12 14:44:51 Indeed!
Philipp Keck 2016/07/12 15:19:56 Done. (other CL)
Bernhard Bauer 2016/07/12 15:50:31 You could have an int (not an enum) that represent
Philipp Keck 2016/07/12 16:05:36 You'd always have to remember to update that int,
Bernhard Bauer 2016/07/12 16:16:27 Yes, precisely.
81 return "Unknown category " + base::IntToString(int(category));
82 }
83 }
84
85 std::string MapCategoryStatus(ContentSuggestionsCategoryStatus status) {
86 switch (status) {
87 case ContentSuggestionsCategoryStatus::INITIALIZING:
88 return "INITIALIZING";
89 case ContentSuggestionsCategoryStatus::AVAILABLE:
90 return "AVAILABLE";
91 case ContentSuggestionsCategoryStatus::AVAILABLE_LOADING:
92 return "AVAILABLE_LOADING";
93 case ContentSuggestionsCategoryStatus::NOT_PROVIDED:
94 return "NOT_PROVIDED";
95 case ContentSuggestionsCategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED:
96 return "ALL_SUGGESTIONS_EXPLICITLY_DISABLED";
97 case ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED:
98 return "CATEGORY_EXPLICITLY_DISABLED";
99 case ContentSuggestionsCategoryStatus::SIGNED_OUT:
100 return "SIGNED_OUT";
101 case ContentSuggestionsCategoryStatus::SYNC_DISABLED:
102 return "SYNC_DISABLED";
103 case ContentSuggestionsCategoryStatus::PASSPHRASE_ENCRYPTION_ENABLED:
104 return "PASSPHRASE_ENCRYPTION_ENABLED";
105 case ContentSuggestionsCategoryStatus::HISTORY_SYNC_DISABLED:
106 return "HISTORY_SYNC_DISABLED";
107 case ContentSuggestionsCategoryStatus::LOADING_ERROR:
108 return "LOADING_ERROR";
109 default:
Bernhard Bauer 2016/07/12 13:09:45 Same here.
Philipp Keck 2016/07/12 13:45:02 Done.
110 return "Unknown status: " + base::IntToString(int(status));
Bernhard Bauer 2016/07/12 13:09:45 Should this be a static_cast<int>? The style guide
Philipp Keck 2016/07/12 13:45:02 I delete this line anyway, but I use `int(...)` an
Bernhard Bauer 2016/07/12 14:44:51 Hm. I do prefer static_cast<>, but I don't want to
Philipp Keck 2016/07/12 15:19:56 Done. Actually the style guide is quite clear on t
111 }
112 }
113
55 } // namespace 114 } // namespace
56 115
57 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() 116 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler()
58 : observer_(this), 117 : ntp_snippets_service_observer_(this),
118 content_suggestions_service_observer_(this),
59 dom_loaded_(false), 119 dom_loaded_(false),
60 ntp_snippets_service_(nullptr) {} 120 ntp_snippets_service_(nullptr),
121 content_suggestions_service_(nullptr) {}
61 122
62 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 123 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
63 124
64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} 125 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {}
65 126
66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { 127 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() {
67 if (!dom_loaded_) return; 128 if (!dom_loaded_) return;
68 129
69 SendSnippets(); 130 SendSnippets();
70 131
71 web_ui()->CallJavascriptFunctionUnsafe( 132 web_ui()->CallJavascriptFunctionUnsafe(
72 "chrome.SnippetsInternals.receiveJson", 133 "chrome.SnippetsInternals.receiveJson",
73 base::StringValue( 134 base::StringValue(
74 ntp_snippets_service_->snippets_fetcher()->last_json())); 135 ntp_snippets_service_->snippets_fetcher()->last_json()));
75 } 136 }
76 137
77 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged( 138 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged(
78 ntp_snippets::DisabledReason disabled_reason) {} 139 ntp_snippets::DisabledReason disabled_reason) {}
79 140
141 void SnippetsInternalsMessageHandler::OnNewSuggestions() {
142 if (!dom_loaded_)
143 return;
144 SendContentSuggestions();
145 }
146 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
Marc Treib 2016/07/12 13:04:53 nit: newlines between methods please
Philipp Keck 2016/07/12 13:45:02 Done.
147 ContentSuggestionsCategory category,
148 ContentSuggestionsCategoryStatus new_status) {
149 if (!dom_loaded_)
150 return;
151 SendContentSuggestions();
152 }
153 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
154
80 void SnippetsInternalsMessageHandler::RegisterMessages() { 155 void SnippetsInternalsMessageHandler::RegisterMessages() {
81 // additional initialization (web_ui() does not work from the constructor) 156 // additional initialization (web_ui() does not work from the constructor)
82 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> 157 Profile* profile = Profile::FromWebUI(web_ui());
83 GetForProfile(Profile::FromWebUI(web_ui())); 158
84 observer_.Add(ntp_snippets_service_); 159 ntp_snippets_service_ =
160 NTPSnippetsServiceFactory::GetInstance()->GetForProfile(profile);
161 ntp_snippets_service_observer_.Add(ntp_snippets_service_);
162
163 content_suggestions_service_ =
164 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
165 content_suggestions_service_observer_.Add(content_suggestions_service_);
85 166
86 web_ui()->RegisterMessageCallback( 167 web_ui()->RegisterMessageCallback(
87 "loaded", 168 "loaded",
88 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded, 169 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded,
89 base::Unretained(this))); 170 base::Unretained(this)));
90 171
91 web_ui()->RegisterMessageCallback( 172 web_ui()->RegisterMessageCallback(
92 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, 173 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear,
93 base::Unretained(this))); 174 base::Unretained(this)));
94 175
95 web_ui()->RegisterMessageCallback( 176 web_ui()->RegisterMessageCallback(
96 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, 177 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
97 base::Unretained(this))); 178 base::Unretained(this)));
98 179
99 web_ui()->RegisterMessageCallback( 180 web_ui()->RegisterMessageCallback(
100 "clearDiscarded", 181 "clearDiscarded",
101 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, 182 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded,
102 base::Unretained(this))); 183 base::Unretained(this)));
184
185 web_ui()->RegisterMessageCallback(
186 "clearCachedSuggestions",
187 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
188 base::Unretained(this)));
189
190 web_ui()->RegisterMessageCallback(
191 "clearDiscardedSuggestions",
192 base::Bind(
193 &SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions,
194 base::Unretained(this)));
103 } 195 }
104 196
105 void SnippetsInternalsMessageHandler::HandleLoaded( 197 void SnippetsInternalsMessageHandler::HandleLoaded(
106 const base::ListValue* args) { 198 const base::ListValue* args) {
107 DCHECK_EQ(0u, args->GetSize()); 199 DCHECK_EQ(0u, args->GetSize());
108 200
109 dom_loaded_ = true; 201 dom_loaded_ = true;
110 202
111 SendInitialData(); 203 SendInitialData();
112 } 204 }
(...skipping 21 matching lines...) Expand all
134 std::string hosts_string; 226 std::string hosts_string;
135 args->GetString(0, &hosts_string); 227 args->GetString(0, &hosts_string);
136 228
137 std::vector<std::string> hosts_vector = base::SplitString( 229 std::vector<std::string> hosts_vector = base::SplitString(
138 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 230 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
139 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); 231 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end());
140 232
141 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); 233 ntp_snippets_service_->FetchSnippetsFromHosts(hosts);
142 } 234 }
143 235
236 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
237 const base::ListValue* args) {
238 DCHECK_EQ(0u, args->GetSize());
239
240 content_suggestions_service_->ClearCachedSuggestionsForDebugging();
241 }
242
243 void SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions(
244 const base::ListValue* args) {
245 DCHECK_EQ(0u, args->GetSize());
246
247 content_suggestions_service_->ClearDiscardedSuggestionsForDebugging();
248 }
249
144 void SnippetsInternalsMessageHandler::SendInitialData() { 250 void SnippetsInternalsMessageHandler::SendInitialData() {
145 SendHosts(); 251 SendHosts();
146 252
147 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 253 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
148 chrome::android::kNTPSnippetsFeature)); 254 chrome::android::kNTPSnippetsFeature));
149 255
150 web_ui()->CallJavascriptFunctionUnsafe( 256 web_ui()->CallJavascriptFunctionUnsafe(
151 "chrome.SnippetsInternals.setHostRestricted", 257 "chrome.SnippetsInternals.setHostRestricted",
152 base::FundamentalValue( 258 base::FundamentalValue(
153 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); 259 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions()));
154 260
155 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { 261 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) {
156 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: 262 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal:
157 SendString("switch-personalized", "Only personalized"); 263 SendString("switch-personalized", "Only personalized");
158 break; 264 break;
159 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: 265 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth:
160 SendString("switch-personalized", 266 SendString("switch-personalized",
161 "Both personalized and non-personalized"); 267 "Both personalized and non-personalized");
162 break; 268 break;
163 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: 269 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal:
164 SendString("switch-personalized", "Only non-personalized"); 270 SendString("switch-personalized", "Only non-personalized");
165 break; 271 break;
166 } 272 }
167 273
168 SendString("switch-fetch-url", 274 SendString("switch-fetch-url",
169 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); 275 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec());
170 276
171 SendSnippets(); 277 SendSnippets();
172 SendDiscardedSnippets(); 278 SendDiscardedSnippets();
279 SendContentSuggestions();
173 } 280 }
174 281
175 void SnippetsInternalsMessageHandler::SendSnippets() { 282 void SnippetsInternalsMessageHandler::SendSnippets() {
176 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); 283 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
177 284
178 int index = 0; 285 int index = 0;
179 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : 286 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet :
180 ntp_snippets_service_->snippets()) 287 ntp_snippets_service_->snippets())
181 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); 288 snippets_list->Append(PrepareSnippet(*snippet, index++, false));
182 289
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 322
216 hosts_list->Append(std::move(entry)); 323 hosts_list->Append(std::move(entry));
217 } 324 }
218 325
219 base::DictionaryValue result; 326 base::DictionaryValue result;
220 result.Set("list", std::move(hosts_list)); 327 result.Set("list", std::move(hosts_list));
221 web_ui()->CallJavascriptFunctionUnsafe( 328 web_ui()->CallJavascriptFunctionUnsafe(
222 "chrome.SnippetsInternals.receiveHosts", result); 329 "chrome.SnippetsInternals.receiveHosts", result);
223 } 330 }
224 331
332 void SnippetsInternalsMessageHandler::SendContentSuggestions() {
333 std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
334
335 int index = 0;
336 for (ContentSuggestionsCategory category :
337 content_suggestions_service_->GetCategories()) {
338 ContentSuggestionsCategoryStatus status =
339 content_suggestions_service_->GetCategoryStatus(category);
340 const std::vector<ContentSuggestion>& suggestions =
341 content_suggestions_service_->GetSuggestionsForCategory(category);
342
343 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
344 for (const ContentSuggestion& suggestion : suggestions) {
345 suggestions_list->Append(PrepareSuggestion(suggestion, index++));
346 }
347
348 std::unique_ptr<base::DictionaryValue> category_entry(
349 new base::DictionaryValue);
350 category_entry->SetString("name", MapCategoryName(category));
351 category_entry->SetString("status", MapCategoryStatus(status));
352 category_entry->Set("suggestions", std::move(suggestions_list));
353 categories_list->Append(std::move(category_entry));
354 }
355
356 base::DictionaryValue result;
357 result.Set("list", std::move(categories_list));
358 web_ui()->CallJavascriptFunctionUnsafe(
359 "chrome.SnippetsInternals.receiveContentSuggestions", result);
360 }
361
225 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, 362 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
226 bool value) { 363 bool value) {
227 SendString(name, value ? "True" : "False"); 364 SendString(name, value ? "True" : "False");
228 } 365 }
229 366
230 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 367 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
231 const std::string& value) { 368 const std::string& value) {
232 base::StringValue string_name(name); 369 base::StringValue string_name(name);
233 base::StringValue string_value(value); 370 base::StringValue string_value(value);
234 371
235 web_ui()->CallJavascriptFunctionUnsafe( 372 web_ui()->CallJavascriptFunctionUnsafe(
236 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); 373 "chrome.SnippetsInternals.receiveProperty", string_name, string_value);
237 } 374 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698