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

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

Issue 2223073002: Add per-section clearing and dismissed suggestions to snippets-internals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix non-Debug builds 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 if (!args->GetString(0, &hosts_string))
162 return;
211 163
212 std::vector<std::string> hosts_vector = base::SplitString( 164 std::vector<std::string> hosts_vector = base::SplitString(
213 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 165 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
214 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); 166 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end());
215 167
216 ntp_snippets_service_->FetchSnippetsFromHosts(hosts, /*force_requests=*/true); 168 ntp_snippets_service_->FetchSnippetsFromHosts(hosts, /*force_requests=*/true);
217 } 169 }
218 170
219 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( 171 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
220 const base::ListValue* args) { 172 const base::ListValue* args) {
221 DCHECK_EQ(0u, args->GetSize()); 173 DCHECK_EQ(1u, args->GetSize());
222 174
223 content_suggestions_service_->ClearCachedSuggestionsForDebugging(); 175 int category_id;
176 if (!args->GetInteger(0, &category_id))
177 return;
178
179 Category category =
180 content_suggestions_service_->category_factory()->FromIDValue(
181 category_id);
182 content_suggestions_service_->ClearCachedSuggestionsForDebugging(category);
183 SendContentSuggestions();
224 } 184 }
225 185
226 void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions( 186 void SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions(
227 const base::ListValue* args) { 187 const base::ListValue* args) {
228 DCHECK_EQ(0u, args->GetSize()); 188 DCHECK_EQ(1u, args->GetSize());
229 189
230 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(); 190 int category_id;
191 if (!args->GetInteger(0, &category_id))
192 return;
193
194 Category category =
195 content_suggestions_service_->category_factory()->FromIDValue(
196 category_id);
197 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category);
198 SendContentSuggestions();
231 } 199 }
232 200
233 void SnippetsInternalsMessageHandler::SendAllContent() { 201 void SnippetsInternalsMessageHandler::SendAllContent() {
234 SendHosts(); 202 SendHosts();
235 203
236 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 204 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
237 ntp_snippets::kContentSuggestionsFeature)); 205 ntp_snippets::kContentSuggestionsFeature));
238 206
239 SendBoolean("flag-offline-page-suggestions", 207 SendBoolean("flag-offline-page-suggestions",
240 base::FeatureList::IsEnabled( 208 base::FeatureList::IsEnabled(
(...skipping 17 matching lines...) Expand all
258 break; 226 break;
259 } 227 }
260 228
261 SendString("switch-fetch-url", 229 SendString("switch-fetch-url",
262 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); 230 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec());
263 web_ui()->CallJavascriptFunctionUnsafe( 231 web_ui()->CallJavascriptFunctionUnsafe(
264 "chrome.SnippetsInternals.receiveJson", 232 "chrome.SnippetsInternals.receiveJson",
265 base::StringValue( 233 base::StringValue(
266 ntp_snippets_service_->snippets_fetcher()->last_json())); 234 ntp_snippets_service_->snippets_fetcher()->last_json()));
267 235
268 SendSnippets();
269 SendDismissedSnippets();
270 SendContentSuggestions(); 236 SendContentSuggestions();
271 } 237 }
272 238
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() { 239 void SnippetsInternalsMessageHandler::SendHosts() {
306 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); 240 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue);
307 241
308 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); 242 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts();
309 243
310 for (const std::string& host : hosts) { 244 for (const std::string& host : hosts) {
311 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 245 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
312 entry->SetString("url", host); 246 entry->SetString("url", host);
313 247
314 hosts_list->Append(std::move(entry)); 248 hosts_list->Append(std::move(entry));
315 } 249 }
316 250
317 base::DictionaryValue result; 251 base::DictionaryValue result;
318 result.Set("list", std::move(hosts_list)); 252 result.Set("list", std::move(hosts_list));
319 web_ui()->CallJavascriptFunctionUnsafe( 253 web_ui()->CallJavascriptFunctionUnsafe(
320 "chrome.SnippetsInternals.receiveHosts", result); 254 "chrome.SnippetsInternals.receiveHosts", result);
321 } 255 }
322 256
323 void SnippetsInternalsMessageHandler::SendContentSuggestions() { 257 void SnippetsInternalsMessageHandler::SendContentSuggestions() {
324 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); 258 std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
325 259
326 int index = 0; 260 int index = 0;
327 for (Category category : content_suggestions_service_->GetCategories()) { 261 for (Category category : content_suggestions_service_->GetCategories()) {
328 CategoryStatus status = 262 CategoryStatus status =
329 content_suggestions_service_->GetCategoryStatus(category); 263 content_suggestions_service_->GetCategoryStatus(category);
330 const std::vector<ContentSuggestion>& suggestions = 264 const std::vector<ContentSuggestion>& suggestions =
331 content_suggestions_service_->GetSuggestionsForCategory(category); 265 content_suggestions_service_->GetSuggestionsForCategory(category);
266 std::vector<ContentSuggestion> dismissed_suggestions =
267 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
268 category);
332 269
333 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); 270 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
334 for (const ContentSuggestion& suggestion : suggestions) { 271 for (const ContentSuggestion& suggestion : suggestions) {
335 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); 272 suggestions_list->Append(PrepareSuggestion(suggestion, index++));
336 } 273 }
337 274
275 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue);
276 for (const ContentSuggestion& suggestion : dismissed_suggestions) {
277 dismissed_list->Append(PrepareSuggestion(suggestion, index++));
278 }
279
338 std::unique_ptr<base::DictionaryValue> category_entry( 280 std::unique_ptr<base::DictionaryValue> category_entry(
339 new base::DictionaryValue); 281 new base::DictionaryValue);
282 category_entry->SetInteger("categoryId", category.id());
283 category_entry->SetString(
284 "dismissedContainerId",
285 "dismissed-suggestions-" + base::IntToString(category.id()));
340 category_entry->SetString("title", GetCategoryTitle(category)); 286 category_entry->SetString("title", GetCategoryTitle(category));
341 category_entry->SetString("status", GetCategoryStatusName(status)); 287 category_entry->SetString("status", GetCategoryStatusName(status));
342 category_entry->Set("suggestions", std::move(suggestions_list)); 288 category_entry->Set("suggestions", std::move(suggestions_list));
289 category_entry->Set("dismissedSuggestions", std::move(dismissed_list));
343 categories_list->Append(std::move(category_entry)); 290 categories_list->Append(std::move(category_entry));
344 } 291 }
345 292
346 base::DictionaryValue result; 293 base::DictionaryValue result;
347 result.Set("list", std::move(categories_list)); 294 result.Set("list", std::move(categories_list));
348 web_ui()->CallJavascriptFunctionUnsafe( 295 web_ui()->CallJavascriptFunctionUnsafe(
349 "chrome.SnippetsInternals.receiveContentSuggestions", result); 296 "chrome.SnippetsInternals.receiveContentSuggestions", result);
350 } 297 }
351 298
352 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, 299 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
353 bool value) { 300 bool value) {
354 SendString(name, value ? "True" : "False"); 301 SendString(name, value ? "True" : "False");
355 } 302 }
356 303
357 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 304 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
358 const std::string& value) { 305 const std::string& value) {
359 base::StringValue string_name(name); 306 base::StringValue string_name(name);
360 base::StringValue string_value(value); 307 base::StringValue string_value(value);
361 308
362 web_ui()->CallJavascriptFunctionUnsafe( 309 web_ui()->CallJavascriptFunctionUnsafe(
363 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); 310 "chrome.SnippetsInternals.receiveProperty", string_name, string_value);
364 } 311 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698