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

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

Issue 2569663004: [NTPSnippets] Switch from TickClock to Clock for storing the last background fetched time. (Closed)
Patch Set: Add a DCHECK to check that the num or args passed to the new method is 0 Created 4 years 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 <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/feature_list.h" 15 #include "base/feature_list.h"
16 #include "base/i18n/time_formatting.h" 16 #include "base/i18n/time_formatting.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/optional.h" 19 #include "base/optional.h"
20 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_split.h" 21 #include "base/strings/string_split.h"
22 #include "base/strings/utf_string_conversions.h"
22 #include "base/values.h" 23 #include "base/values.h"
23 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" 24 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/common/chrome_features.h" 26 #include "chrome/common/chrome_features.h"
26 #include "components/ntp_snippets/category_info.h" 27 #include "components/ntp_snippets/category_info.h"
27 #include "components/ntp_snippets/features.h" 28 #include "components/ntp_snippets/features.h"
29 #include "components/ntp_snippets/pref_names.h"
30 #include "components/ntp_snippets/remote/remote_suggestions_provider.h"
28 #include "components/ntp_snippets/switches.h" 31 #include "components/ntp_snippets/switches.h"
32 #include "components/prefs/pref_service.h"
29 #include "content/public/browser/web_ui.h" 33 #include "content/public/browser/web_ui.h"
30 34
31 using ntp_snippets::ContentSuggestion; 35 using ntp_snippets::ContentSuggestion;
32 using ntp_snippets::Category; 36 using ntp_snippets::Category;
33 using ntp_snippets::CategoryInfo; 37 using ntp_snippets::CategoryInfo;
34 using ntp_snippets::CategoryStatus; 38 using ntp_snippets::CategoryStatus;
35 using ntp_snippets::KnownCategories; 39 using ntp_snippets::KnownCategories;
36 using ntp_snippets::UserClassifier; 40 using ntp_snippets::UserClassifier;
37 41
38 namespace { 42 namespace {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 case CategoryStatus::SIGNED_OUT: 74 case CategoryStatus::SIGNED_OUT:
71 return "SIGNED_OUT"; 75 return "SIGNED_OUT";
72 case CategoryStatus::LOADING_ERROR: 76 case CategoryStatus::LOADING_ERROR:
73 return "LOADING_ERROR"; 77 return "LOADING_ERROR";
74 } 78 }
75 return std::string(); 79 return std::string();
76 } 80 }
77 81
78 } // namespace 82 } // namespace
79 83
80 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() 84 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler(
85 ntp_snippets::ContentSuggestionsService* content_suggestions_service,
86 PrefService* pref_service)
81 : content_suggestions_service_observer_(this), 87 : content_suggestions_service_observer_(this),
82 dom_loaded_(false), 88 dom_loaded_(false),
83 ntp_snippets_service_(nullptr), 89 content_suggestions_service_(content_suggestions_service),
84 content_suggestions_service_(nullptr), 90 remote_suggestions_provider_(
91 content_suggestions_service_->ntp_snippets_service()),
92 pref_service_(pref_service),
85 weak_ptr_factory_(this) {} 93 weak_ptr_factory_(this) {}
86 94
87 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 95 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
88 96
89 void SnippetsInternalsMessageHandler::RegisterMessages() { 97 void SnippetsInternalsMessageHandler::RegisterMessages() {
90 // Additional initialization (web_ui() does not work from the constructor).
91 Profile* profile = Profile::FromWebUI(web_ui());
92
93 content_suggestions_service_ =
94 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
95 content_suggestions_service_observer_.Add(content_suggestions_service_);
96
97 ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service();
98
99 web_ui()->RegisterMessageCallback( 98 web_ui()->RegisterMessageCallback(
100 "refreshContent", 99 "clearCachedSuggestions",
101 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent, 100 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
102 base::Unretained(this))); 101 base::Unretained(this)));
103 102
104 web_ui()->RegisterMessageCallback( 103 web_ui()->RegisterMessageCallback(
105 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, 104 "clearClassification",
106 base::Unretained(this))); 105 base::Bind(&SnippetsInternalsMessageHandler::ClearClassification,
107
108 web_ui()->RegisterMessageCallback(
109 "clearCachedSuggestions",
110 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
111 base::Unretained(this))); 106 base::Unretained(this)));
112 107
113 web_ui()->RegisterMessageCallback( 108 web_ui()->RegisterMessageCallback(
114 "clearDismissedSuggestions", 109 "clearDismissedSuggestions",
115 base::Bind( 110 base::Bind(
116 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, 111 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
117 base::Unretained(this))); 112 base::Unretained(this)));
118 113
119 web_ui()->RegisterMessageCallback( 114 web_ui()->RegisterMessageCallback(
115 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
116 base::Unretained(this)));
117
118 web_ui()->RegisterMessageCallback(
119 "fetchRemoteSuggestionsInTheBackground",
120 base::Bind(&SnippetsInternalsMessageHandler::
121 FetchRemoteSuggestionsInTheBackground,
122 base::Unretained(this)));
123
124 web_ui()->RegisterMessageCallback(
125 "refreshContent",
126 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent,
127 base::Unretained(this)));
128
129 web_ui()->RegisterMessageCallback(
120 "toggleDismissedSuggestions", 130 "toggleDismissedSuggestions",
121 base::Bind( 131 base::Bind(
122 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions, 132 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions,
123 base::Unretained(this))); 133 base::Unretained(this)));
124 134
125 web_ui()->RegisterMessageCallback( 135 content_suggestions_service_observer_.Add(content_suggestions_service_);
126 "clearClassification",
127 base::Bind(
128 &SnippetsInternalsMessageHandler::ClearClassification,
129 base::Unretained(this)));
130 } 136 }
131 137
132 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { 138 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) {
133 if (!dom_loaded_) 139 if (!dom_loaded_)
134 return; 140 return;
135 SendContentSuggestions(); 141 SendContentSuggestions();
136 } 142 }
137 143
138 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( 144 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
139 Category category, 145 Category category,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 185
180 SendAllContent(); 186 SendAllContent();
181 } 187 }
182 188
183 void SnippetsInternalsMessageHandler::HandleDownload( 189 void SnippetsInternalsMessageHandler::HandleDownload(
184 const base::ListValue* args) { 190 const base::ListValue* args) {
185 DCHECK_EQ(1u, args->GetSize()); 191 DCHECK_EQ(1u, args->GetSize());
186 192
187 SendString("hosts-status", std::string()); 193 SendString("hosts-status", std::string());
188 194
189 if (!ntp_snippets_service_) 195 if (!remote_suggestions_provider_)
190 return; 196 return;
191 197
192 std::string hosts_string; 198 std::string hosts_string;
193 if (!args->GetString(0, &hosts_string)) 199 if (!args->GetString(0, &hosts_string))
194 return; 200 return;
195 201
196 std::vector<std::string> hosts_vector = base::SplitString( 202 std::vector<std::string> hosts_vector = base::SplitString(
197 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 203 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
198 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); 204 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end());
199 205
200 ntp_snippets_service_->FetchSnippetsFromHosts(hosts, 206 remote_suggestions_provider_->FetchSnippetsFromHosts(
201 /*interactive_request=*/true); 207 hosts,
208 /*interactive_request=*/true);
202 } 209 }
203 210
204 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( 211 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
205 const base::ListValue* args) { 212 const base::ListValue* args) {
206 DCHECK_EQ(1u, args->GetSize()); 213 DCHECK_EQ(1u, args->GetSize());
207 214
208 int category_id; 215 int category_id;
209 if (!args->GetInteger(0, &category_id)) 216 if (!args->GetInteger(0, &category_id))
210 return; 217 return;
211 218
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 } 271 }
265 272
266 void SnippetsInternalsMessageHandler::ClearClassification( 273 void SnippetsInternalsMessageHandler::ClearClassification(
267 const base::ListValue* args) { 274 const base::ListValue* args) {
268 DCHECK_EQ(0u, args->GetSize()); 275 DCHECK_EQ(0u, args->GetSize());
269 content_suggestions_service_->user_classifier() 276 content_suggestions_service_->user_classifier()
270 ->ClearClassificationForDebugging(); 277 ->ClearClassificationForDebugging();
271 SendClassification(); 278 SendClassification();
272 } 279 }
273 280
281 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground(
282 const base::ListValue* args) {
283 DCHECK_EQ(0u, args->GetSize());
284 remote_suggestions_provider_->FetchSnippetsInTheBackground();
285 }
286
274 void SnippetsInternalsMessageHandler::SendAllContent() { 287 void SnippetsInternalsMessageHandler::SendAllContent() {
275 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 288 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
276 ntp_snippets::kContentSuggestionsFeature)); 289 ntp_snippets::kContentSuggestionsFeature));
277 SendBoolean( 290 SendBoolean(
278 "flag-article-suggestions", 291 "flag-article-suggestions",
279 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)); 292 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature));
280 SendBoolean("flag-recent-offline-tab-suggestions", 293 SendBoolean("flag-recent-offline-tab-suggestions",
281 base::FeatureList::IsEnabled( 294 base::FeatureList::IsEnabled(
282 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); 295 ntp_snippets::kRecentOfflineTabSuggestionsFeature));
283 SendBoolean( 296 SendBoolean(
284 "flag-asset-download-suggestions", 297 "flag-asset-download-suggestions",
285 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature)); 298 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature));
286 SendBoolean("flag-offline-page-download-suggestions", 299 SendBoolean("flag-offline-page-download-suggestions",
287 base::FeatureList::IsEnabled( 300 base::FeatureList::IsEnabled(
288 features::kOfflinePageDownloadSuggestionsFeature)); 301 features::kOfflinePageDownloadSuggestionsFeature));
289 SendBoolean( 302 SendBoolean(
290 "flag-bookmark-suggestions", 303 "flag-bookmark-suggestions",
291 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); 304 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature));
292 305
293 SendBoolean("flag-physical-web-page-suggestions", 306 SendBoolean("flag-physical-web-page-suggestions",
294 base::FeatureList::IsEnabled( 307 base::FeatureList::IsEnabled(
295 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); 308 ntp_snippets::kPhysicalWebPageSuggestionsFeature));
296 309
297 SendClassification(); 310 SendClassification();
311 SendLastRemoteSuggestionsBackgroundFetchTime();
298 312
299 if (ntp_snippets_service_) { 313 if (remote_suggestions_provider_) {
300 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { 314 switch (
315 remote_suggestions_provider_->snippets_fetcher()->personalization()) {
301 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: 316 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal:
302 SendString("switch-personalized", "Only personalized"); 317 SendString("switch-personalized", "Only personalized");
303 break; 318 break;
304 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: 319 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth:
305 SendString("switch-personalized", 320 SendString("switch-personalized",
306 "Both personalized and non-personalized"); 321 "Both personalized and non-personalized");
307 break; 322 break;
308 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: 323 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal:
309 SendString("switch-personalized", "Only non-personalized"); 324 SendString("switch-personalized", "Only non-personalized");
310 break; 325 break;
311 } 326 }
312 327
313 SendString("switch-fetch-url", 328 SendString(
314 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); 329 "switch-fetch-url",
330 remote_suggestions_provider_->snippets_fetcher()->fetch_url().spec());
315 web_ui()->CallJavascriptFunctionUnsafe( 331 web_ui()->CallJavascriptFunctionUnsafe(
316 "chrome.SnippetsInternals.receiveJson", 332 "chrome.SnippetsInternals.receiveJson",
317 base::StringValue( 333 base::StringValue(
318 ntp_snippets_service_->snippets_fetcher()->last_json())); 334 remote_suggestions_provider_->snippets_fetcher()->last_json()));
319 } 335 }
320 336
321 SendContentSuggestions(); 337 SendContentSuggestions();
322 } 338 }
323 339
324 void SnippetsInternalsMessageHandler::SendClassification() { 340 void SnippetsInternalsMessageHandler::SendClassification() {
325 web_ui()->CallJavascriptFunctionUnsafe( 341 web_ui()->CallJavascriptFunctionUnsafe(
326 "chrome.SnippetsInternals.receiveClassification", 342 "chrome.SnippetsInternals.receiveClassification",
327 base::StringValue(content_suggestions_service_->user_classifier() 343 base::StringValue(content_suggestions_service_->user_classifier()
328 ->GetUserClassDescriptionForDebugging()), 344 ->GetUserClassDescriptionForDebugging()),
329 base::FundamentalValue( 345 base::FundamentalValue(
330 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( 346 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
331 UserClassifier::Metric::NTP_OPENED)), 347 UserClassifier::Metric::NTP_OPENED)),
332 base::FundamentalValue( 348 base::FundamentalValue(
333 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( 349 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
334 UserClassifier::Metric::SUGGESTIONS_SHOWN)), 350 UserClassifier::Metric::SUGGESTIONS_SHOWN)),
335 base::FundamentalValue( 351 base::FundamentalValue(
336 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( 352 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
337 UserClassifier::Metric::SUGGESTIONS_USED))); 353 UserClassifier::Metric::SUGGESTIONS_USED)));
338 } 354 }
339 355
356 void SnippetsInternalsMessageHandler::
357 SendLastRemoteSuggestionsBackgroundFetchTime() {
358 base::Time time = base::Time::FromInternalValue(pref_service_->GetInt64(
359 ntp_snippets::prefs::kLastSuccessfulBackgroundFetchTime));
360 web_ui()->CallJavascriptFunctionUnsafe(
361 "chrome.SnippetsInternals."
362 "receiveLastRemoteSuggestionsBackgroundFetchTime",
363 base::StringValue(base::TimeFormatShortDateAndTime(time)));
364 }
365
340 void SnippetsInternalsMessageHandler::SendContentSuggestions() { 366 void SnippetsInternalsMessageHandler::SendContentSuggestions() {
341 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); 367 std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
342 368
343 int index = 0; 369 int index = 0;
344 for (Category category : content_suggestions_service_->GetCategories()) { 370 for (Category category : content_suggestions_service_->GetCategories()) {
345 CategoryStatus status = 371 CategoryStatus status =
346 content_suggestions_service_->GetCategoryStatus(category); 372 content_suggestions_service_->GetCategoryStatus(category);
347 base::Optional<CategoryInfo> info = 373 base::Optional<CategoryInfo> info =
348 content_suggestions_service_->GetCategoryInfo(category); 374 content_suggestions_service_->GetCategoryInfo(category);
349 DCHECK(info); 375 DCHECK(info);
(...skipping 17 matching lines...) Expand all
367 category_entry->SetString( 393 category_entry->SetString(
368 "dismissedContainerId", 394 "dismissedContainerId",
369 "dismissed-suggestions-" + base::IntToString(category.id())); 395 "dismissed-suggestions-" + base::IntToString(category.id()));
370 category_entry->SetString("title", info->title()); 396 category_entry->SetString("title", info->title());
371 category_entry->SetString("status", GetCategoryStatusName(status)); 397 category_entry->SetString("status", GetCategoryStatusName(status));
372 category_entry->Set("suggestions", std::move(suggestions_list)); 398 category_entry->Set("suggestions", std::move(suggestions_list));
373 category_entry->Set("dismissedSuggestions", std::move(dismissed_list)); 399 category_entry->Set("dismissedSuggestions", std::move(dismissed_list));
374 categories_list->Append(std::move(category_entry)); 400 categories_list->Append(std::move(category_entry));
375 } 401 }
376 402
377 if (ntp_snippets_service_) { 403 if (remote_suggestions_provider_) {
378 const std::string& status = 404 const std::string& status =
379 ntp_snippets_service_->snippets_fetcher()->last_status(); 405 remote_suggestions_provider_->snippets_fetcher()->last_status();
380 if (!status.empty()) 406 if (!status.empty())
381 SendString("hosts-status", "Finished: " + status); 407 SendString("hosts-status", "Finished: " + status);
382 } 408 }
383 409
384 base::DictionaryValue result; 410 base::DictionaryValue result;
385 result.Set("list", std::move(categories_list)); 411 result.Set("list", std::move(categories_list));
386 web_ui()->CallJavascriptFunctionUnsafe( 412 web_ui()->CallJavascriptFunctionUnsafe(
387 "chrome.SnippetsInternals.receiveContentSuggestions", result); 413 "chrome.SnippetsInternals.receiveContentSuggestions", result);
388 } 414 }
389 415
(...skipping 13 matching lines...) Expand all
403 429
404 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( 430 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded(
405 Category category, 431 Category category,
406 std::vector<ContentSuggestion> dismissed_suggestions) { 432 std::vector<ContentSuggestion> dismissed_suggestions) {
407 if (dismissed_state_[category] == DismissedState::HIDDEN) 433 if (dismissed_state_[category] == DismissedState::HIDDEN)
408 return; 434 return;
409 dismissed_suggestions_[category] = std::move(dismissed_suggestions); 435 dismissed_suggestions_[category] = std::move(dismissed_suggestions);
410 dismissed_state_[category] = DismissedState::VISIBLE; 436 dismissed_state_[category] = DismissedState::VISIBLE;
411 SendContentSuggestions(); 437 SendContentSuggestions();
412 } 438 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/snippets_internals_message_handler.h ('k') | chrome/browser/ui/webui/snippets_internals_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698