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

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

Issue 2346263002: Extending the UserClassifier to actually support classification. (Closed)
Patch Set: Comments of Tim and Marc Created 4 years, 3 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 <utility>
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
13 #include "base/command_line.h" 14 #include "base/command_line.h"
14 #include "base/feature_list.h" 15 #include "base/feature_list.h"
15 #include "base/i18n/time_formatting.h" 16 #include "base/i18n/time_formatting.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
18 #include "base/optional.h" 19 #include "base/optional.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 case CategoryStatus::CATEGORY_EXPLICITLY_DISABLED: 67 case CategoryStatus::CATEGORY_EXPLICITLY_DISABLED:
67 return "CATEGORY_EXPLICITLY_DISABLED"; 68 return "CATEGORY_EXPLICITLY_DISABLED";
68 case CategoryStatus::SIGNED_OUT: 69 case CategoryStatus::SIGNED_OUT:
69 return "SIGNED_OUT"; 70 return "SIGNED_OUT";
70 case CategoryStatus::LOADING_ERROR: 71 case CategoryStatus::LOADING_ERROR:
71 return "LOADING_ERROR"; 72 return "LOADING_ERROR";
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),
83 weak_ptr_factory_(this) {} 84 weak_ptr_factory_(this) {}
84 85
85 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 86 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
86 87
(...skipping 25 matching lines...) Expand all
112 "clearDismissedSuggestions", 113 "clearDismissedSuggestions",
113 base::Bind( 114 base::Bind(
114 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, 115 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
115 base::Unretained(this))); 116 base::Unretained(this)));
116 117
117 web_ui()->RegisterMessageCallback( 118 web_ui()->RegisterMessageCallback(
118 "toggleDismissedSuggestions", 119 "toggleDismissedSuggestions",
119 base::Bind( 120 base::Bind(
120 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions, 121 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions,
121 base::Unretained(this))); 122 base::Unretained(this)));
123
124 web_ui()->RegisterMessageCallback(
125 "clearClassification",
126 base::Bind(
127 &SnippetsInternalsMessageHandler::ClearClassification,
128 base::Unretained(this)));
122 } 129 }
123 130
124 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { 131 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) {
125 if (!dom_loaded_) 132 if (!dom_loaded_)
126 return; 133 return;
127 SendContentSuggestions(); 134 SendContentSuggestions();
128 } 135 }
129 136
130 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( 137 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
131 Category category, 138 Category category,
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 category, 245 category,
239 base::Bind( 246 base::Bind(
240 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded, 247 &SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded,
241 weak_ptr_factory_.GetWeakPtr(), category)); 248 weak_ptr_factory_.GetWeakPtr(), category));
242 } else { 249 } else {
243 dismissed_state_[category] = DismissedState::HIDDEN; 250 dismissed_state_[category] = DismissedState::HIDDEN;
244 dismissed_suggestions_[category].clear(); 251 dismissed_suggestions_[category].clear();
245 } 252 }
246 } 253 }
247 254
255 void SnippetsInternalsMessageHandler::ClearClassification(
256 const base::ListValue* args) {
257 DCHECK_EQ(0u, args->GetSize());
258 content_suggestions_service_->user_classifier()
259 ->ClearClassificationForDebugging();
260 SendClassification();
261 }
262
248 void SnippetsInternalsMessageHandler::SendAllContent() { 263 void SnippetsInternalsMessageHandler::SendAllContent() {
249 SendHosts(); 264 SendHosts();
250 265
251 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 266 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
252 ntp_snippets::kContentSuggestionsFeature)); 267 ntp_snippets::kContentSuggestionsFeature));
253 SendBoolean("flag-recent-offline-tab-suggestions", 268 SendBoolean("flag-recent-offline-tab-suggestions",
254 base::FeatureList::IsEnabled( 269 base::FeatureList::IsEnabled(
255 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); 270 ntp_snippets::kRecentOfflineTabSuggestionsFeature));
256 SendBoolean( 271 SendBoolean(
257 "flag-download-suggestions", 272 "flag-download-suggestions",
258 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature)); 273 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature));
259 SendBoolean( 274 SendBoolean(
260 "flag-bookmark-suggestions", 275 "flag-bookmark-suggestions",
261 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); 276 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature));
262 277
263 SendBoolean("flag-physical-web-page-suggestions", 278 SendBoolean("flag-physical-web-page-suggestions",
264 base::FeatureList::IsEnabled( 279 base::FeatureList::IsEnabled(
265 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); 280 ntp_snippets::kPhysicalWebPageSuggestionsFeature));
266 281
282 SendClassification();
283
267 web_ui()->CallJavascriptFunctionUnsafe( 284 web_ui()->CallJavascriptFunctionUnsafe(
268 "chrome.SnippetsInternals.setHostRestricted", 285 "chrome.SnippetsInternals.setHostRestricted",
269 base::FundamentalValue( 286 base::FundamentalValue(
270 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); 287 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions()));
271 288
272 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { 289 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) {
273 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: 290 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal:
274 SendString("switch-personalized", "Only personalized"); 291 SendString("switch-personalized", "Only personalized");
275 break; 292 break;
276 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: 293 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth:
277 SendString("switch-personalized", 294 SendString("switch-personalized",
278 "Both personalized and non-personalized"); 295 "Both personalized and non-personalized");
279 break; 296 break;
280 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: 297 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal:
281 SendString("switch-personalized", "Only non-personalized"); 298 SendString("switch-personalized", "Only non-personalized");
282 break; 299 break;
283 } 300 }
284 301
285 SendString("switch-fetch-url", 302 SendString("switch-fetch-url",
286 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); 303 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec());
287 web_ui()->CallJavascriptFunctionUnsafe( 304 web_ui()->CallJavascriptFunctionUnsafe(
288 "chrome.SnippetsInternals.receiveJson", 305 "chrome.SnippetsInternals.receiveJson",
289 base::StringValue( 306 base::StringValue(
290 ntp_snippets_service_->snippets_fetcher()->last_json())); 307 ntp_snippets_service_->snippets_fetcher()->last_json()));
291 308
292 SendContentSuggestions(); 309 SendContentSuggestions();
293 } 310 }
294 311
312 void SnippetsInternalsMessageHandler::SendClassification() {
313 web_ui()->CallJavascriptFunctionUnsafe(
314 "chrome.SnippetsInternals.receiveClassification",
315 base::StringValue(content_suggestions_service_->user_classifier()
316 ->GetUserClassDescriptionForDebugging()),
317 base::FundamentalValue(
318 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
319 ntp_snippets::UserClassifier::Metric::NTP_OPENED)),
Marc Treib 2016/09/20 13:26:52 optional: You could add a "using ntp_snippets::Use
jkrcal 2016/09/20 13:46:39 Done.
320 base::FundamentalValue(
321 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
322 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_SHOWN)),
323 base::FundamentalValue(
324 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
325 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED)));
326 }
327
295 void SnippetsInternalsMessageHandler::SendHosts() { 328 void SnippetsInternalsMessageHandler::SendHosts() {
296 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); 329 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue);
297 330
298 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); 331 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts();
299 332
300 for (const std::string& host : hosts) { 333 for (const std::string& host : hosts) {
301 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 334 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
302 entry->SetString("url", host); 335 entry->SetString("url", host);
303 336
304 hosts_list->Append(std::move(entry)); 337 hosts_list->Append(std::move(entry));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 407
375 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( 408 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded(
376 Category category, 409 Category category,
377 std::vector<ContentSuggestion> dismissed_suggestions) { 410 std::vector<ContentSuggestion> dismissed_suggestions) {
378 if (dismissed_state_[category] == DismissedState::HIDDEN) 411 if (dismissed_state_[category] == DismissedState::HIDDEN)
379 return; 412 return;
380 dismissed_suggestions_[category] = std::move(dismissed_suggestions); 413 dismissed_suggestions_[category] = std::move(dismissed_suggestions);
381 dismissed_state_[category] = DismissedState::VISIBLE; 414 dismissed_state_[category] = DismissedState::VISIBLE;
382 SendContentSuggestions(); 415 SendContentSuggestions();
383 } 416 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698