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

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

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

Powered by Google App Engine
This is Rietveld 408576698