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

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

Powered by Google App Engine
This is Rietveld 408576698