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

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

Issue 2260783002: Make GetDismissedSuggestionsForDebugging asynchronous (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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 web_ui()->RegisterMessageCallback( 105 web_ui()->RegisterMessageCallback(
106 "clearCachedSuggestions", 106 "clearCachedSuggestions",
107 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, 107 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
108 base::Unretained(this))); 108 base::Unretained(this)));
109 109
110 web_ui()->RegisterMessageCallback( 110 web_ui()->RegisterMessageCallback(
111 "clearDismissedSuggestions", 111 "clearDismissedSuggestions",
112 base::Bind( 112 base::Bind(
113 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, 113 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
114 base::Unretained(this))); 114 base::Unretained(this)));
115
116 web_ui()->RegisterMessageCallback(
117 "toggleDismissedSuggestions",
118 base::Bind(
119 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions,
120 base::Unretained(this)));
115 } 121 }
116 122
117 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { 123 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) {
118 if (!dom_loaded_) 124 if (!dom_loaded_)
119 return; 125 return;
120 SendContentSuggestions(); 126 SendContentSuggestions();
121 } 127 }
122 128
123 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( 129 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
124 Category category, 130 Category category,
(...skipping 12 matching lines...) Expand all
137 } 143 }
138 144
139 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {} 145 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
140 146
141 void SnippetsInternalsMessageHandler::HandleRefreshContent( 147 void SnippetsInternalsMessageHandler::HandleRefreshContent(
142 const base::ListValue* args) { 148 const base::ListValue* args) {
143 DCHECK_EQ(0u, args->GetSize()); 149 DCHECK_EQ(0u, args->GetSize());
144 150
145 dom_loaded_ = true; 151 dom_loaded_ = true;
146 152
153 for (auto& it : dismissed_state_) {
Marc Treib 2016/08/19 10:07:51 Please use the actual type, and a proper name - th
Philipp Keck 2016/08/19 12:11:53 Done.
154 if (it.second == DismissedState::VISIBLE) {
155 it.second = DismissedState::LOADING;
156 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
157 it.first,
158 base::Bind(
159 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded,
160 base::Unretained(this)));
Marc Treib 2016/08/19 10:07:51 I don't think Unretained is safe here - you'll pro
Philipp Keck 2016/08/19 12:11:53 Done.
161 }
162 }
163
147 SendAllContent(); 164 SendAllContent();
148 } 165 }
149 166
150 void SnippetsInternalsMessageHandler::HandleDownload( 167 void SnippetsInternalsMessageHandler::HandleDownload(
151 const base::ListValue* args) { 168 const base::ListValue* args) {
152 DCHECK_EQ(1u, args->GetSize()); 169 DCHECK_EQ(1u, args->GetSize());
153 170
154 SendString("hosts-status", std::string()); 171 SendString("hosts-status", std::string());
155 172
156 std::string hosts_string; 173 std::string hosts_string;
(...skipping 28 matching lines...) Expand all
185 202
186 int category_id; 203 int category_id;
187 if (!args->GetInteger(0, &category_id)) 204 if (!args->GetInteger(0, &category_id))
188 return; 205 return;
189 206
190 Category category = 207 Category category =
191 content_suggestions_service_->category_factory()->FromIDValue( 208 content_suggestions_service_->category_factory()->FromIDValue(
192 category_id); 209 category_id);
193 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category); 210 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category);
194 SendContentSuggestions(); 211 SendContentSuggestions();
212 dismissed_state_[category] = DismissedState::LOADING;
213 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
214 category,
215 base::Bind(&SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded,
216 base::Unretained(this)));
Marc Treib 2016/08/19 10:07:51 Here too.
Philipp Keck 2016/08/19 12:11:53 Done.
217 }
218
219 void SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions(
220 const base::ListValue* args) {
221 DCHECK_EQ(2u, args->GetSize());
222
223 int category_id;
224 if (!args->GetInteger(0, &category_id))
225 return;
226 bool dismissed_visible;
227 if (!args->GetBoolean(1, &dismissed_visible))
228 return;
229
230 Category category =
231 content_suggestions_service_->category_factory()->FromIDValue(
232 category_id);
233 if (dismissed_visible) {
234 dismissed_state_[category] = DismissedState::LOADING;
235 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
236 category,
237 base::Bind(
238 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded,
239 base::Unretained(this)));
Marc Treib 2016/08/19 10:07:51 And here.
Philipp Keck 2016/08/19 12:11:53 Done.
240 } else {
241 dismissed_state_[category] = DismissedState::HIDDEN;
242 dismissed_suggestions_[category].clear();
243 }
195 } 244 }
196 245
197 void SnippetsInternalsMessageHandler::SendAllContent() { 246 void SnippetsInternalsMessageHandler::SendAllContent() {
198 SendHosts(); 247 SendHosts();
199 248
200 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 249 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
201 ntp_snippets::kContentSuggestionsFeature)); 250 ntp_snippets::kContentSuggestionsFeature));
202 SendBoolean("flag-recent-offline-tab-suggestions", 251 SendBoolean("flag-recent-offline-tab-suggestions",
203 base::FeatureList::IsEnabled( 252 base::FeatureList::IsEnabled(
204 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); 253 ntp_snippets::kRecentOfflineTabSuggestionsFeature));
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 313
265 int index = 0; 314 int index = 0;
266 for (Category category : content_suggestions_service_->GetCategories()) { 315 for (Category category : content_suggestions_service_->GetCategories()) {
267 CategoryStatus status = 316 CategoryStatus status =
268 content_suggestions_service_->GetCategoryStatus(category); 317 content_suggestions_service_->GetCategoryStatus(category);
269 base::Optional<CategoryInfo> info = 318 base::Optional<CategoryInfo> info =
270 content_suggestions_service_->GetCategoryInfo(category); 319 content_suggestions_service_->GetCategoryInfo(category);
271 DCHECK(info); 320 DCHECK(info);
272 const std::vector<ContentSuggestion>& suggestions = 321 const std::vector<ContentSuggestion>& suggestions =
273 content_suggestions_service_->GetSuggestionsForCategory(category); 322 content_suggestions_service_->GetSuggestionsForCategory(category);
274 std::vector<ContentSuggestion> dismissed_suggestions =
275 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
276 category);
277 323
278 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); 324 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
279 for (const ContentSuggestion& suggestion : suggestions) { 325 for (const ContentSuggestion& suggestion : suggestions) {
280 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); 326 suggestions_list->Append(PrepareSuggestion(suggestion, index++));
281 } 327 }
282 328
283 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue); 329 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue);
284 for (const ContentSuggestion& suggestion : dismissed_suggestions) { 330 for (const ContentSuggestion& suggestion :
331 dismissed_suggestions_[category]) {
285 dismissed_list->Append(PrepareSuggestion(suggestion, index++)); 332 dismissed_list->Append(PrepareSuggestion(suggestion, index++));
286 } 333 }
287 334
288 std::unique_ptr<base::DictionaryValue> category_entry( 335 std::unique_ptr<base::DictionaryValue> category_entry(
289 new base::DictionaryValue); 336 new base::DictionaryValue);
290 category_entry->SetInteger("categoryId", category.id()); 337 category_entry->SetInteger("categoryId", category.id());
291 category_entry->SetString( 338 category_entry->SetString(
292 "dismissedContainerId", 339 "dismissedContainerId",
293 "dismissed-suggestions-" + base::IntToString(category.id())); 340 "dismissed-suggestions-" + base::IntToString(category.id()));
294 category_entry->SetString("title", info->title()); 341 category_entry->SetString("title", info->title());
(...skipping 15 matching lines...) Expand all
310 } 357 }
311 358
312 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 359 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
313 const std::string& value) { 360 const std::string& value) {
314 base::StringValue string_name(name); 361 base::StringValue string_name(name);
315 base::StringValue string_value(value); 362 base::StringValue string_value(value);
316 363
317 web_ui()->CallJavascriptFunctionUnsafe( 364 web_ui()->CallJavascriptFunctionUnsafe(
318 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); 365 "chrome.SnippetsInternals.receiveProperty", string_name, string_value);
319 } 366 }
367
368 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded(
369 Category category,
370 std::vector<ContentSuggestion> dismissed_suggestions) {
371 if (dismissed_state_[category] == DismissedState::HIDDEN)
372 return;
373 dismissed_suggestions_[category] = std::move(dismissed_suggestions);
374 dismissed_state_[category] = DismissedState::VISIBLE;
375 SendContentSuggestions();
376 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698