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

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: Replace function pointer, use multiple if-continue 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 } 72 }
73 return std::string(); 73 return std::string();
74 } 74 }
75 75
76 } // namespace 76 } // namespace
77 77
78 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() 78 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler()
79 : content_suggestions_service_observer_(this), 79 : content_suggestions_service_observer_(this),
80 dom_loaded_(false), 80 dom_loaded_(false),
81 ntp_snippets_service_(nullptr), 81 ntp_snippets_service_(nullptr),
82 content_suggestions_service_(nullptr) {} 82 content_suggestions_service_(nullptr),
83 weak_ptr_factory_(this) {}
83 84
84 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 85 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
85 86
86 void SnippetsInternalsMessageHandler::RegisterMessages() { 87 void SnippetsInternalsMessageHandler::RegisterMessages() {
87 // additional initialization (web_ui() does not work from the constructor) 88 // additional initialization (web_ui() does not work from the constructor)
88 Profile* profile = Profile::FromWebUI(web_ui()); 89 Profile* profile = Profile::FromWebUI(web_ui());
89 90
90 content_suggestions_service_ = 91 content_suggestions_service_ =
91 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); 92 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
92 content_suggestions_service_observer_.Add(content_suggestions_service_); 93 content_suggestions_service_observer_.Add(content_suggestions_service_);
(...skipping 12 matching lines...) Expand all
105 web_ui()->RegisterMessageCallback( 106 web_ui()->RegisterMessageCallback(
106 "clearCachedSuggestions", 107 "clearCachedSuggestions",
107 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, 108 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
108 base::Unretained(this))); 109 base::Unretained(this)));
109 110
110 web_ui()->RegisterMessageCallback( 111 web_ui()->RegisterMessageCallback(
111 "clearDismissedSuggestions", 112 "clearDismissedSuggestions",
112 base::Bind( 113 base::Bind(
113 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, 114 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
114 base::Unretained(this))); 115 base::Unretained(this)));
116
117 web_ui()->RegisterMessageCallback(
118 "toggleDismissedSuggestions",
119 base::Bind(
120 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions,
121 base::Unretained(this)));
115 } 122 }
116 123
117 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { 124 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) {
118 if (!dom_loaded_) 125 if (!dom_loaded_)
119 return; 126 return;
120 SendContentSuggestions(); 127 SendContentSuggestions();
121 } 128 }
122 129
123 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( 130 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
124 Category category, 131 Category category,
(...skipping 12 matching lines...) Expand all
137 } 144 }
138 145
139 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {} 146 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
140 147
141 void SnippetsInternalsMessageHandler::HandleRefreshContent( 148 void SnippetsInternalsMessageHandler::HandleRefreshContent(
142 const base::ListValue* args) { 149 const base::ListValue* args) {
143 DCHECK_EQ(0u, args->GetSize()); 150 DCHECK_EQ(0u, args->GetSize());
144 151
145 dom_loaded_ = true; 152 dom_loaded_ = true;
146 153
154 for (std::pair<const Category, DismissedState>& category_state_pair :
155 dismissed_state_) {
156 if (category_state_pair.second == DismissedState::VISIBLE) {
157 category_state_pair.second = DismissedState::LOADING;
158 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
159 category_state_pair.first,
160 base::Bind(
161 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded,
162 weak_ptr_factory_.GetWeakPtr(), category_state_pair.first));
163 }
164 }
165
147 SendAllContent(); 166 SendAllContent();
148 } 167 }
149 168
150 void SnippetsInternalsMessageHandler::HandleDownload( 169 void SnippetsInternalsMessageHandler::HandleDownload(
151 const base::ListValue* args) { 170 const base::ListValue* args) {
152 DCHECK_EQ(1u, args->GetSize()); 171 DCHECK_EQ(1u, args->GetSize());
153 172
154 SendString("hosts-status", std::string()); 173 SendString("hosts-status", std::string());
155 174
156 std::string hosts_string; 175 std::string hosts_string;
(...skipping 28 matching lines...) Expand all
185 204
186 int category_id; 205 int category_id;
187 if (!args->GetInteger(0, &category_id)) 206 if (!args->GetInteger(0, &category_id))
188 return; 207 return;
189 208
190 Category category = 209 Category category =
191 content_suggestions_service_->category_factory()->FromIDValue( 210 content_suggestions_service_->category_factory()->FromIDValue(
192 category_id); 211 category_id);
193 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category); 212 content_suggestions_service_->ClearDismissedSuggestionsForDebugging(category);
194 SendContentSuggestions(); 213 SendContentSuggestions();
214 dismissed_state_[category] = DismissedState::LOADING;
215 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
216 category,
217 base::Bind(&SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded,
218 weak_ptr_factory_.GetWeakPtr(), category));
219 }
220
221 void SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions(
222 const base::ListValue* args) {
223 DCHECK_EQ(2u, args->GetSize());
224
225 int category_id;
226 if (!args->GetInteger(0, &category_id))
227 return;
228 bool dismissed_visible;
229 if (!args->GetBoolean(1, &dismissed_visible))
230 return;
231
232 Category category =
233 content_suggestions_service_->category_factory()->FromIDValue(
234 category_id);
235 if (dismissed_visible) {
236 dismissed_state_[category] = DismissedState::LOADING;
237 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
238 category,
239 base::Bind(
240 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded,
241 weak_ptr_factory_.GetWeakPtr(), category));
242 } else {
243 dismissed_state_[category] = DismissedState::HIDDEN;
244 dismissed_suggestions_[category].clear();
245 }
195 } 246 }
196 247
197 void SnippetsInternalsMessageHandler::SendAllContent() { 248 void SnippetsInternalsMessageHandler::SendAllContent() {
198 SendHosts(); 249 SendHosts();
199 250
200 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 251 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
201 ntp_snippets::kContentSuggestionsFeature)); 252 ntp_snippets::kContentSuggestionsFeature));
202 SendBoolean("flag-recent-offline-tab-suggestions", 253 SendBoolean("flag-recent-offline-tab-suggestions",
203 base::FeatureList::IsEnabled( 254 base::FeatureList::IsEnabled(
204 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); 255 ntp_snippets::kRecentOfflineTabSuggestionsFeature));
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 315
265 int index = 0; 316 int index = 0;
266 for (Category category : content_suggestions_service_->GetCategories()) { 317 for (Category category : content_suggestions_service_->GetCategories()) {
267 CategoryStatus status = 318 CategoryStatus status =
268 content_suggestions_service_->GetCategoryStatus(category); 319 content_suggestions_service_->GetCategoryStatus(category);
269 base::Optional<CategoryInfo> info = 320 base::Optional<CategoryInfo> info =
270 content_suggestions_service_->GetCategoryInfo(category); 321 content_suggestions_service_->GetCategoryInfo(category);
271 DCHECK(info); 322 DCHECK(info);
272 const std::vector<ContentSuggestion>& suggestions = 323 const std::vector<ContentSuggestion>& suggestions =
273 content_suggestions_service_->GetSuggestionsForCategory(category); 324 content_suggestions_service_->GetSuggestionsForCategory(category);
274 std::vector<ContentSuggestion> dismissed_suggestions =
275 content_suggestions_service_->GetDismissedSuggestionsForDebugging(
276 category);
277 325
278 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); 326 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
279 for (const ContentSuggestion& suggestion : suggestions) { 327 for (const ContentSuggestion& suggestion : suggestions) {
280 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); 328 suggestions_list->Append(PrepareSuggestion(suggestion, index++));
281 } 329 }
282 330
283 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue); 331 std::unique_ptr<base::ListValue> dismissed_list(new base::ListValue);
284 for (const ContentSuggestion& suggestion : dismissed_suggestions) { 332 for (const ContentSuggestion& suggestion :
333 dismissed_suggestions_[category]) {
285 dismissed_list->Append(PrepareSuggestion(suggestion, index++)); 334 dismissed_list->Append(PrepareSuggestion(suggestion, index++));
286 } 335 }
287 336
288 std::unique_ptr<base::DictionaryValue> category_entry( 337 std::unique_ptr<base::DictionaryValue> category_entry(
289 new base::DictionaryValue); 338 new base::DictionaryValue);
290 category_entry->SetInteger("categoryId", category.id()); 339 category_entry->SetInteger("categoryId", category.id());
291 category_entry->SetString( 340 category_entry->SetString(
292 "dismissedContainerId", 341 "dismissedContainerId",
293 "dismissed-suggestions-" + base::IntToString(category.id())); 342 "dismissed-suggestions-" + base::IntToString(category.id()));
294 category_entry->SetString("title", info->title()); 343 category_entry->SetString("title", info->title());
(...skipping 15 matching lines...) Expand all
310 } 359 }
311 360
312 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 361 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
313 const std::string& value) { 362 const std::string& value) {
314 base::StringValue string_name(name); 363 base::StringValue string_name(name);
315 base::StringValue string_value(value); 364 base::StringValue string_value(value);
316 365
317 web_ui()->CallJavascriptFunctionUnsafe( 366 web_ui()->CallJavascriptFunctionUnsafe(
318 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); 367 "chrome.SnippetsInternals.receiveProperty", string_name, string_value);
319 } 368 }
369
370 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded(
371 Category category,
372 std::vector<ContentSuggestion> dismissed_suggestions) {
373 if (dismissed_state_[category] == DismissedState::HIDDEN)
374 return;
375 dismissed_suggestions_[category] = std::move(dismissed_suggestions);
376 dismissed_state_[category] = DismissedState::VISIBLE;
377 SendContentSuggestions();
378 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698