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

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: 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 PrefService* pref_service)
81 : content_suggestions_service_observer_(this), 86 : content_suggestions_service_observer_(this),
82 dom_loaded_(false), 87 dom_loaded_(false),
83 ntp_snippets_service_(nullptr), 88 ntp_snippets_service_(nullptr),
84 content_suggestions_service_(nullptr), 89 content_suggestions_service_(nullptr),
90 pref_service_(pref_service),
Bernhard Bauer 2016/12/13 13:06:54 Couldn't you get the PrefService from the profile
markusheintz_ 2016/12/13 14:01:28 Oh good catch I didn't notice this. I think this l
85 weak_ptr_factory_(this) {} 91 weak_ptr_factory_(this) {}
86 92
87 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 93 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
88 94
89 void SnippetsInternalsMessageHandler::RegisterMessages() { 95 void SnippetsInternalsMessageHandler::RegisterMessages() {
90 // Additional initialization (web_ui() does not work from the constructor). 96 // Additional initialization (web_ui() does not work from the constructor).
91 Profile* profile = Profile::FromWebUI(web_ui()); 97 Profile* profile = Profile::FromWebUI(web_ui());
92 98
93 content_suggestions_service_ = 99 content_suggestions_service_ =
94 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); 100 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
95 content_suggestions_service_observer_.Add(content_suggestions_service_); 101 content_suggestions_service_observer_.Add(content_suggestions_service_);
96 102
97 ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service(); 103 ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service();
98 104
99 web_ui()->RegisterMessageCallback( 105 web_ui()->RegisterMessageCallback(
100 "refreshContent",
101 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent,
102 base::Unretained(this)));
103
104 web_ui()->RegisterMessageCallback(
105 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
106 base::Unretained(this)));
107
108 web_ui()->RegisterMessageCallback(
109 "clearCachedSuggestions", 106 "clearCachedSuggestions",
110 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, 107 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
111 base::Unretained(this))); 108 base::Unretained(this)));
112 109
113 web_ui()->RegisterMessageCallback( 110 web_ui()->RegisterMessageCallback(
111 "clearClassification",
112 base::Bind(&SnippetsInternalsMessageHandler::ClearClassification,
113 base::Unretained(this)));
114
115 web_ui()->RegisterMessageCallback(
114 "clearDismissedSuggestions", 116 "clearDismissedSuggestions",
115 base::Bind( 117 base::Bind(
116 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions, 118 &SnippetsInternalsMessageHandler::HandleClearDismissedSuggestions,
117 base::Unretained(this))); 119 base::Unretained(this)));
118 120
119 web_ui()->RegisterMessageCallback( 121 web_ui()->RegisterMessageCallback(
122 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
123 base::Unretained(this)));
124
125 web_ui()->RegisterMessageCallback(
126 "fetchRemoteSuggestionsInTheBackground",
127 base::Bind(&SnippetsInternalsMessageHandler::
128 FetchRemoteSuggestionsInTheBackground,
129 base::Unretained(this)));
130
131 web_ui()->RegisterMessageCallback(
132 "refreshContent",
133 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent,
134 base::Unretained(this)));
135
136 web_ui()->RegisterMessageCallback(
120 "toggleDismissedSuggestions", 137 "toggleDismissedSuggestions",
121 base::Bind( 138 base::Bind(
122 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions, 139 &SnippetsInternalsMessageHandler::HandleToggleDismissedSuggestions,
123 base::Unretained(this))); 140 base::Unretained(this)));
124
125 web_ui()->RegisterMessageCallback(
126 "clearClassification",
127 base::Bind(
128 &SnippetsInternalsMessageHandler::ClearClassification,
129 base::Unretained(this)));
130 } 141 }
131 142
132 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) { 143 void SnippetsInternalsMessageHandler::OnNewSuggestions(Category category) {
133 if (!dom_loaded_) 144 if (!dom_loaded_)
134 return; 145 return;
135 SendContentSuggestions(); 146 SendContentSuggestions();
136 } 147 }
137 148
138 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( 149 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
139 Category category, 150 Category category,
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 } 275 }
265 276
266 void SnippetsInternalsMessageHandler::ClearClassification( 277 void SnippetsInternalsMessageHandler::ClearClassification(
267 const base::ListValue* args) { 278 const base::ListValue* args) {
268 DCHECK_EQ(0u, args->GetSize()); 279 DCHECK_EQ(0u, args->GetSize());
269 content_suggestions_service_->user_classifier() 280 content_suggestions_service_->user_classifier()
270 ->ClearClassificationForDebugging(); 281 ->ClearClassificationForDebugging();
271 SendClassification(); 282 SendClassification();
272 } 283 }
273 284
285 void SnippetsInternalsMessageHandler::FetchRemoteSuggestionsInTheBackground(
286 const base::ListValue* args) {
287 ntp_snippets_service_->FetchSnippetsInTheBackground();
288 }
289
274 void SnippetsInternalsMessageHandler::SendAllContent() { 290 void SnippetsInternalsMessageHandler::SendAllContent() {
275 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 291 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
276 ntp_snippets::kContentSuggestionsFeature)); 292 ntp_snippets::kContentSuggestionsFeature));
277 SendBoolean( 293 SendBoolean(
278 "flag-article-suggestions", 294 "flag-article-suggestions",
279 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)); 295 base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature));
280 SendBoolean("flag-recent-offline-tab-suggestions", 296 SendBoolean("flag-recent-offline-tab-suggestions",
281 base::FeatureList::IsEnabled( 297 base::FeatureList::IsEnabled(
282 ntp_snippets::kRecentOfflineTabSuggestionsFeature)); 298 ntp_snippets::kRecentOfflineTabSuggestionsFeature));
283 SendBoolean( 299 SendBoolean(
284 "flag-asset-download-suggestions", 300 "flag-asset-download-suggestions",
285 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature)); 301 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature));
286 SendBoolean("flag-offline-page-download-suggestions", 302 SendBoolean("flag-offline-page-download-suggestions",
287 base::FeatureList::IsEnabled( 303 base::FeatureList::IsEnabled(
288 features::kOfflinePageDownloadSuggestionsFeature)); 304 features::kOfflinePageDownloadSuggestionsFeature));
289 SendBoolean( 305 SendBoolean(
290 "flag-bookmark-suggestions", 306 "flag-bookmark-suggestions",
291 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)); 307 base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature));
292 308
293 SendBoolean("flag-physical-web-page-suggestions", 309 SendBoolean("flag-physical-web-page-suggestions",
294 base::FeatureList::IsEnabled( 310 base::FeatureList::IsEnabled(
295 ntp_snippets::kPhysicalWebPageSuggestionsFeature)); 311 ntp_snippets::kPhysicalWebPageSuggestionsFeature));
296 312
297 SendClassification(); 313 SendClassification();
314 SendLastRemoteSuggestionsBackgroundFetchTime();
298 315
299 if (ntp_snippets_service_) { 316 if (ntp_snippets_service_) {
300 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { 317 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) {
301 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: 318 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal:
302 SendString("switch-personalized", "Only personalized"); 319 SendString("switch-personalized", "Only personalized");
303 break; 320 break;
304 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: 321 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth:
305 SendString("switch-personalized", 322 SendString("switch-personalized",
306 "Both personalized and non-personalized"); 323 "Both personalized and non-personalized");
307 break; 324 break;
(...skipping 22 matching lines...) Expand all
330 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( 347 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
331 UserClassifier::Metric::NTP_OPENED)), 348 UserClassifier::Metric::NTP_OPENED)),
332 base::FundamentalValue( 349 base::FundamentalValue(
333 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( 350 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
334 UserClassifier::Metric::SUGGESTIONS_SHOWN)), 351 UserClassifier::Metric::SUGGESTIONS_SHOWN)),
335 base::FundamentalValue( 352 base::FundamentalValue(
336 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime( 353 content_suggestions_service_->user_classifier()->GetEstimatedAvgTime(
337 UserClassifier::Metric::SUGGESTIONS_USED))); 354 UserClassifier::Metric::SUGGESTIONS_USED)));
338 } 355 }
339 356
357 void SnippetsInternalsMessageHandler::
358 SendLastRemoteSuggestionsBackgroundFetchTime() {
359 base::Time time = base::Time::FromInternalValue(pref_service_->GetInt64(
360 ntp_snippets::prefs::kLastSuccessfulBackgroundFetchTime));
361 web_ui()->CallJavascriptFunctionUnsafe(
362 "chrome.SnippetsInternals."
363 "receiveLastRemoteSuggestionsBackgroundFetchTime",
364 base::StringValue(base::TimeFormatShortDateAndTime(time)));
365 }
366
340 void SnippetsInternalsMessageHandler::SendContentSuggestions() { 367 void SnippetsInternalsMessageHandler::SendContentSuggestions() {
341 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); 368 std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
342 369
343 int index = 0; 370 int index = 0;
344 for (Category category : content_suggestions_service_->GetCategories()) { 371 for (Category category : content_suggestions_service_->GetCategories()) {
345 CategoryStatus status = 372 CategoryStatus status =
346 content_suggestions_service_->GetCategoryStatus(category); 373 content_suggestions_service_->GetCategoryStatus(category);
347 base::Optional<CategoryInfo> info = 374 base::Optional<CategoryInfo> info =
348 content_suggestions_service_->GetCategoryInfo(category); 375 content_suggestions_service_->GetCategoryInfo(category);
349 DCHECK(info); 376 DCHECK(info);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 430
404 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded( 431 void SnippetsInternalsMessageHandler::OnDismissedSuggestionsLoaded(
405 Category category, 432 Category category,
406 std::vector<ContentSuggestion> dismissed_suggestions) { 433 std::vector<ContentSuggestion> dismissed_suggestions) {
407 if (dismissed_state_[category] == DismissedState::HIDDEN) 434 if (dismissed_state_[category] == DismissedState::HIDDEN)
408 return; 435 return;
409 dismissed_suggestions_[category] = std::move(dismissed_suggestions); 436 dismissed_suggestions_[category] = std::move(dismissed_suggestions);
410 dismissed_state_[category] = DismissedState::VISIBLE; 437 dismissed_state_[category] = DismissedState::VISIBLE;
411 SendContentSuggestions(); 438 SendContentSuggestions();
412 } 439 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698