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

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

Issue 2222853004: Add per-section clearing and dismissed suggestions to snippets-internals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Marc's comments Created 4 years, 4 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
(...skipping 15 matching lines...) Expand all
26 #include "components/ntp_snippets/switches.h" 26 #include "components/ntp_snippets/switches.h"
27 #include "content/public/browser/web_ui.h" 27 #include "content/public/browser/web_ui.h"
28 28
29 using ntp_snippets::ContentSuggestion; 29 using ntp_snippets::ContentSuggestion;
30 using ntp_snippets::Category; 30 using ntp_snippets::Category;
31 using ntp_snippets::CategoryStatus; 31 using ntp_snippets::CategoryStatus;
32 using ntp_snippets::KnownCategories; 32 using ntp_snippets::KnownCategories;
33 33
34 namespace { 34 namespace {
35 35
36 std::unique_ptr<base::DictionaryValue> PrepareSnippet(
37 const ntp_snippets::NTPSnippet& snippet,
38 int index,
39 bool dismissed) {
40 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
41 entry->SetString("snippetId", snippet.id());
42 entry->SetString("title", snippet.title());
43 entry->SetString("siteTitle", snippet.best_source().publisher_name);
44 entry->SetString("snippet", snippet.snippet());
45 entry->SetString("published",
46 TimeFormatShortDateAndTime(snippet.publish_date()));
47 entry->SetString("expires",
48 TimeFormatShortDateAndTime(snippet.expiry_date()));
49 entry->SetString("url", snippet.best_source().url.spec());
50 entry->SetString("ampUrl", snippet.best_source().amp_url.spec());
51 entry->SetString("salientImageUrl", snippet.salient_image_url().spec());
52 entry->SetDouble("score", snippet.score());
53
54 if (dismissed)
55 entry->SetString("id", "dismissed-snippet-" + base::IntToString(index));
56 else
57 entry->SetString("id", "snippet-" + base::IntToString(index));
58
59 return entry;
60 }
61
62 std::unique_ptr<base::DictionaryValue> PrepareSuggestion( 36 std::unique_ptr<base::DictionaryValue> PrepareSuggestion(
63 const ContentSuggestion& suggestion, 37 const ContentSuggestion& suggestion,
64 int index) { 38 int index) {
65 auto entry = base::MakeUnique<base::DictionaryValue>(); 39 auto entry = base::MakeUnique<base::DictionaryValue>();
66 entry->SetString("suggestionId", suggestion.id()); 40 entry->SetString("suggestionId", suggestion.id());
67 entry->SetString("url", suggestion.url().spec()); 41 entry->SetString("url", suggestion.url().spec());
68 entry->SetString("ampUrl", suggestion.amp_url().spec()); 42 entry->SetString("ampUrl", suggestion.amp_url().spec());
69 entry->SetString("title", suggestion.title()); 43 entry->SetString("title", suggestion.title());
70 entry->SetString("snippetText", suggestion.snippet_text()); 44 entry->SetString("snippetText", suggestion.snippet_text());
71 entry->SetString("publishDate", 45 entry->SetString("publishDate",
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } // namespace 88 } // namespace
115 89
116 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() 90 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler()
117 : content_suggestions_service_observer_(this), 91 : content_suggestions_service_observer_(this),
118 dom_loaded_(false), 92 dom_loaded_(false),
119 ntp_snippets_service_(nullptr), 93 ntp_snippets_service_(nullptr),
120 content_suggestions_service_(nullptr) {} 94 content_suggestions_service_(nullptr) {}
121 95
122 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 96 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
123 97
124 void SnippetsInternalsMessageHandler::OnNewSuggestions() {
125 if (!dom_loaded_)
126 return;
127 SendContentSuggestions();
128 }
129
130 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
131 Category category,
132 CategoryStatus new_status) {
133 if (!dom_loaded_)
134 return;
135 SendContentSuggestions();
136 }
137
138 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
139
140 void SnippetsInternalsMessageHandler::RegisterMessages() { 98 void SnippetsInternalsMessageHandler::RegisterMessages() {
141 // additional initialization (web_ui() does not work from the constructor) 99 // additional initialization (web_ui() does not work from the constructor)
142 Profile* profile = Profile::FromWebUI(web_ui()); 100 Profile* profile = Profile::FromWebUI(web_ui());
143 101
144 content_suggestions_service_ = 102 content_suggestions_service_ =
145 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); 103 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
146 content_suggestions_service_observer_.Add(content_suggestions_service_); 104 content_suggestions_service_observer_.Add(content_suggestions_service_);
147 105
148 ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service(); 106 ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service();
149 107
150 web_ui()->RegisterMessageCallback( 108 web_ui()->RegisterMessageCallback(
151 "refreshContent", 109 "refreshContent",
152 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent, 110 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent,
153 base::Unretained(this))); 111 base::Unretained(this)));
154 112
155 web_ui()->RegisterMessageCallback( 113 web_ui()->RegisterMessageCallback(
156 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear,
157 base::Unretained(this)));
158
159 web_ui()->RegisterMessageCallback(
160 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, 114 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
161 base::Unretained(this))); 115 base::Unretained(this)));
162 116
163 web_ui()->RegisterMessageCallback( 117 web_ui()->RegisterMessageCallback(
164 "clearDismissed",
165 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDismissed,
166 base::Unretained(this)));
167
168 web_ui()->RegisterMessageCallback(
169 "clearCachedSuggestions", 118 "clearCachedSuggestions",
170 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, 119 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
171 base::Unretained(this))); 120 base::Unretained(this)));
172 121
173 web_ui()->RegisterMessageCallback( 122 web_ui()->RegisterMessageCallback(
174 "clearDismissedSuggestions", 123 "clearDismissedSuggestions",
175 base::Bind( 124 base::Bind(
176 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, 125 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
177 base::Unretained(this))); 126 base::Unretained(this)));
178 } 127 }
179 128
129 void SnippetsInternalsMessageHandler::OnNewSuggestions() {
130 if (!dom_loaded_)
131 return;
132 SendContentSuggestions();
133 }
134
135 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
136 Category category,
137 CategoryStatus new_status) {
138 if (!dom_loaded_)
139 return;
140 SendContentSuggestions();
141 }
142
143 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
144
180 void SnippetsInternalsMessageHandler::HandleRefreshContent( 145 void SnippetsInternalsMessageHandler::HandleRefreshContent(
181 const base::ListValue* args) { 146 const base::ListValue* args) {
182 DCHECK_EQ(0u, args->GetSize()); 147 DCHECK_EQ(0u, args->GetSize());
183 148
184 dom_loaded_ = true; 149 dom_loaded_ = true;
185 150
186 SendAllContent(); 151 SendAllContent();
187 } 152 }
188 153
189 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) {
190 DCHECK_EQ(0u, args->GetSize());
191
192 ntp_snippets_service_->ClearCachedSuggestionsForDebugging();
193 }
194
195 void SnippetsInternalsMessageHandler::HandleClearDismissed(
196 const base::ListValue* args) {
197 DCHECK_EQ(0u, args->GetSize());
198
199 ntp_snippets_service_->ClearDismissedSuggestionsForDebugging();
200 SendDismissedSnippets();
201 }
202
203 void SnippetsInternalsMessageHandler::HandleDownload( 154 void SnippetsInternalsMessageHandler::HandleDownload(
204 const base::ListValue* args) { 155 const base::ListValue* args) {
205 DCHECK_EQ(1u, args->GetSize()); 156 DCHECK_EQ(1u, args->GetSize());
206 157
207 SendString("hosts-status", std::string()); 158 SendString("hosts-status", std::string());
208 159
209 std::string hosts_string; 160 std::string hosts_string;
210 args->GetString(0, &hosts_string); 161 DCHECK(args->GetString(0, &hosts_string));
211 162
212 std::vector<std::string> hosts_vector = base::SplitString( 163 std::vector<std::string> hosts_vector = base::SplitString(
213 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 164 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
214 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); 165 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end());
215 166
216 ntp_snippets_service_->FetchSnippetsFromHosts(hosts, /*force_requests=*/true); 167 ntp_snippets_service_->FetchSnippetsFromHosts(hosts, /*force_requests=*/true);
217 } 168 }
218 169
219 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( 170 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
220 const base::ListValue* args) { 171 const base::ListValue* args) {
221 DCHECK_EQ(0u, args->GetSize()); 172 DCHECK_EQ(1u, args->GetSize());
222 173
223 content_suggestions_service_->ClearCachedSuggestionsForDebugging(); 174 int category_id;
175 DCHECK(args->GetInteger(0, &category_id));
176 Category category =
177 content_suggestions_service_->category_factory()->FromIDValue(
178 category_id);
179 content_suggestions_service_->ClearCachedSuggestionsForDebugging(category);
180 SendContentSuggestions();
224 } 181 }
225 182
226 void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions( 183 void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions(
227 const base::ListValue* args) { 184 const base::ListValue* args) {
228 DCHECK_EQ(0u, args->GetSize()); 185 DCHECK_EQ(1u, args->GetSize());
229 186
230 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(); 187 int category_id;
188 DCHECK(args->GetInteger(0, &category_id));
189 Category category =
190 content_suggestions_service_->category_factory()->FromIDValue(
191 category_id);
192 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category);
193 SendContentSuggestions();
231 } 194 }
232 195
233 void SnippetsInternalsMessageHandler::SendAllContent() { 196 void SnippetsInternalsMessageHandler::SendAllContent() {
234 SendHosts(); 197 SendHosts();
235 198
236 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 199 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
237 ntp_snippets::kContentSuggestionsFeature)); 200 ntp_snippets::kContentSuggestionsFeature));
238 201
239 SendBoolean("flag-offline-page-suggestions", 202 SendBoolean("flag-offline-page-suggestions",
240 base::FeatureList::IsEnabled( 203 base::FeatureList::IsEnabled(
(...skipping 17 matching lines...) Expand all
258 break; 221 break;
259 } 222 }
260 223
261 SendString("switch-fetch-url", 224 SendString("switch-fetch-url",
262 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); 225 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec());
263 web_ui()->CallJavascriptFunctionUnsafe( 226 web_ui()->CallJavascriptFunctionUnsafe(
264 "chrome.SnippetsInternals.receiveJson", 227 "chrome.SnippetsInternals.receiveJson",
265 base::StringValue( 228 base::StringValue(
266 ntp_snippets_service_->snippets_fetcher()->last_json())); 229 ntp_snippets_service_->snippets_fetcher()->last_json()));
267 230
268 SendSnippets();
269 SendDismissedSnippets();
270 SendContentSuggestions(); 231 SendContentSuggestions();
271 } 232 }
272 233
273 void SnippetsInternalsMessageHandler::SendSnippets() {
274 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
275
276 int index = 0;
277 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet :
278 ntp_snippets_service_->snippets())
279 snippets_list->Append(PrepareSnippet(*snippet, index++, false));
280
281 base::DictionaryValue result;
282 result.Set("list", std::move(snippets_list));
283 web_ui()->CallJavascriptFunctionUnsafe(
284 "chrome.SnippetsInternals.receiveSnippets", result);
285
286 const std::string& status =
287 ntp_snippets_service_->snippets_fetcher()->last_status();
288 if (!status.empty())
289 SendString("hosts-status", "Finished: " + status);
290 }
291
292 void SnippetsInternalsMessageHandler::SendDismissedSnippets() {
293 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
294
295 int index = 0;
296 for (const auto& snippet : ntp_snippets_service_->dismissed_snippets())
297 snippets_list->Append(PrepareSnippet(*snippet, index++, true));
298
299 base::DictionaryValue result;
300 result.Set("list", std::move(snippets_list));
301 web_ui()->CallJavascriptFunctionUnsafe(
302 "chrome.SnippetsInternals.receiveDismissedSnippets", result);
303 }
304
305 void SnippetsInternalsMessageHandler::SendHosts() { 234 void SnippetsInternalsMessageHandler::SendHosts() {
306 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); 235 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue);
307 236
308 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); 237 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts();
309 238
310 for (const std::string& host : hosts) { 239 for (const std::string& host : hosts) {
311 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 240 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
312 entry->SetString("url", host); 241 entry->SetString("url", host);
313 242
314 hosts_list->Append(std::move(entry)); 243 hosts_list->Append(std::move(entry));
315 } 244 }
316 245
317 base::DictionaryValue result; 246 base::DictionaryValue result;
318 result.Set("list", std::move(hosts_list)); 247 result.Set("list", std::move(hosts_list));
319 web_ui()->CallJavascriptFunctionUnsafe( 248 web_ui()->CallJavascriptFunctionUnsafe(
320 "chrome.SnippetsInternals.receiveHosts", result); 249 "chrome.SnippetsInternals.receiveHosts", result);
321 } 250 }
322 251
323 void SnippetsInternalsMessageHandler::SendContentSuggestions() { 252 void SnippetsInternalsMessageHandler::SendContentSuggestions() {
324 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); 253 std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
325 254
326 int index = 0; 255 int index = 0;
327 for (Category category : content_suggestions_service_->GetCategories()) { 256 for (Category category : content_suggestions_service_->GetCategories()) {
328 CategoryStatus status = 257 CategoryStatus status =
329 content_suggestions_service_->GetCategoryStatus(category); 258 content_suggestions_service_->GetCategoryStatus(category);
330 const std::vector<ContentSuggestion>& suggestions = 259 const std::vector<ContentSuggestion>& suggestions =
331 content_suggestions_service_->GetSuggestionsForCategory(category); 260 content_suggestions_service_->GetSuggestionsForCategory(category);
261 std::vector<ContentSuggestion> dismissed_suggestions =
262 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
263 category);
332 264
333 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); 265 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
334 for (const ContentSuggestion& suggestion : suggestions) { 266 for (const ContentSuggestion& suggestion : suggestions) {
335 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); 267 suggestions_list->Append(PrepareSuggestion(suggestion, index++));
336 } 268 }
337 269
270 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue);
271 for (const ContentSuggestion& suggestion : dismissed_suggestions) {
272 dismissed_list->Append(PrepareSuggestion(suggestion, index++));
273 }
274
338 std::unique_ptr<base::DictionaryValue> category_entry( 275 std::unique_ptr<base::DictionaryValue> category_entry(
339 new base::DictionaryValue); 276 new base::DictionaryValue);
277 category_entry->SetInteger("categoryId", category.id());
278 category_entry->SetString(
279 "dismissedContainerId",
280 "dismissed-suggestions-" + base::IntToString(category.id()));
340 category_entry->SetString("title", GetCategoryTitle(category)); 281 category_entry->SetString("title", GetCategoryTitle(category));
341 category_entry->SetString("status", GetCategoryStatusName(status)); 282 category_entry->SetString("status", GetCategoryStatusName(status));
342 category_entry->Set("suggestions", std::move(suggestions_list)); 283 category_entry->Set("suggestions", std::move(suggestions_list));
284 category_entry->Set("dismissedSuggestions", std::move(dismissed_list));
343 categories_list->Append(std::move(category_entry)); 285 categories_list->Append(std::move(category_entry));
344 } 286 }
345 287
346 base::DictionaryValue result; 288 base::DictionaryValue result;
347 result.Set("list", std::move(categories_list)); 289 result.Set("list", std::move(categories_list));
348 web_ui()->CallJavascriptFunctionUnsafe( 290 web_ui()->CallJavascriptFunctionUnsafe(
349 "chrome.SnippetsInternals.receiveContentSuggestions", result); 291 "chrome.SnippetsInternals.receiveContentSuggestions", result);
350 } 292 }
351 293
352 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, 294 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
353 bool value) { 295 bool value) {
354 SendString(name, value ? "True" : "False"); 296 SendString(name, value ? "True" : "False");
355 } 297 }
356 298
357 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 299 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
358 const std::string& value) { 300 const std::string& value) {
359 base::StringValue string_name(name); 301 base::StringValue string_name(name);
360 base::StringValue string_value(value); 302 base::StringValue string_value(value);
361 303
362 web_ui()->CallJavascriptFunctionUnsafe( 304 web_ui()->CallJavascriptFunctionUnsafe(
363 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); 305 "chrome.SnippetsInternals.receiveProperty", string_name, string_value);
364 } 306 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698