Commit fa7b3da2 authored by megjablon's avatar megjablon Committed by Commit Bot

Add timestamp to infobar for stale Previews

Add a feature to enable a timestamp on the Previews infobar. This
timestamp should be shown when the previews_freshness is greater
than the finch controlled "min_staleness_in_minutes" and less than
"max_staleness_in_minutes".

BUG=718643

Review-Url: https://codereview.chromium.org/2940083003
Cr-Commit-Position: refs/heads/master@{#481018}
parent 783f0ba8
...@@ -4975,6 +4975,15 @@ Keep your key file in a safe place. You will need it to create new versions of y ...@@ -4975,6 +4975,15 @@ Keep your key file in a safe place. You will need it to create new versions of y
<message name="IDS_PREVIEWS_INFOBAR_LINK" desc="The text of the link to load the original page from the infobar notifying the user that a preview page has been shown."> <message name="IDS_PREVIEWS_INFOBAR_LINK" desc="The text of the link to load the original page from the infobar notifying the user that a preview page has been shown.">
Show original Show original
</message> </message>
<message name="IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES" desc="The desciption stating how old a preview version of a page is in minutes. This is shown on the infobar notifying the user that a preview page has been shown.">
Updated <ph name="MINUTES">$1<ex>30</ex></ph>min ago
</message>
<message name="IDS_PREVIEWS_INFOBAR_TIMESTAMP_ONE_HOUR" desc="The desciption stating a preview version of a page is one hour old. This is shown on the infobar notifying the user that a preview page has been shown.">
Updated 1hr ago
</message>
<message name="IDS_PREVIEWS_INFOBAR_TIMESTAMP_HOURS" desc="The desciption stating how old a preview version of a page is in hours. This is shown on the infobar notifying the user that a preview page has been shown.">
Updated <ph name="HOURS">$1<ex>4</ex></ph>hrs ago
</message>
<!-- WebRTC logs --> <!-- WebRTC logs -->
<message name="IDS_WEBRTC_LOGS_TITLE" desc="Title for the chrome://webrtc-logs page."> <message name="IDS_WEBRTC_LOGS_TITLE" desc="Title for the chrome://webrtc-logs page.">
......
...@@ -60,7 +60,8 @@ void AddPreviewNavigationToBlackListCallback( ...@@ -60,7 +60,8 @@ void AddPreviewNavigationToBlackListCallback(
void OnLoFiResponseReceivedOnUI(content::WebContents* web_contents) { void OnLoFiResponseReceivedOnUI(content::WebContents* web_contents) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
PreviewsInfoBarDelegate::Create( PreviewsInfoBarDelegate::Create(
web_contents, previews::PreviewsType::LOFI, true /* is_data_saver_user */, web_contents, previews::PreviewsType::LOFI,
base::Time() /* previews_freshness */, true /* is_data_saver_user */,
base::Bind(&AddPreviewNavigationToBlackListCallback, base::Bind(&AddPreviewNavigationToBlackListCallback,
web_contents->GetBrowserContext(), web_contents->GetBrowserContext(),
web_contents->GetController() web_contents->GetController()
......
...@@ -4,9 +4,15 @@ ...@@ -4,9 +4,15 @@
#include "chrome/browser/previews/previews_infobar_delegate.h" #include "chrome/browser/previews/previews_infobar_delegate.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/android/android_theme_resources.h" #include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h" #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
...@@ -16,6 +22,8 @@ ...@@ -16,6 +22,8 @@
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
#include "components/infobars/core/infobar.h" #include "components/infobars/core/infobar.h"
#include "components/network_time/network_time_tracker.h"
#include "components/previews/core/previews_features.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
...@@ -101,6 +109,7 @@ PreviewsInfoBarDelegate::~PreviewsInfoBarDelegate() { ...@@ -101,6 +109,7 @@ PreviewsInfoBarDelegate::~PreviewsInfoBarDelegate() {
void PreviewsInfoBarDelegate::Create( void PreviewsInfoBarDelegate::Create(
content::WebContents* web_contents, content::WebContents* web_contents,
previews::PreviewsType previews_type, previews::PreviewsType previews_type,
base::Time previews_freshness,
bool is_data_saver_user, bool is_data_saver_user,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback) { const OnDismissPreviewsInfobarCallback& on_dismiss_callback) {
PreviewsInfoBarTabHelper* infobar_tab_helper = PreviewsInfoBarTabHelper* infobar_tab_helper =
...@@ -116,7 +125,8 @@ void PreviewsInfoBarDelegate::Create( ...@@ -116,7 +125,8 @@ void PreviewsInfoBarDelegate::Create(
return; return;
std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate( std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate(
web_contents, previews_type, is_data_saver_user, on_dismiss_callback)); web_contents, previews_type, previews_freshness, is_data_saver_user,
on_dismiss_callback));
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
std::unique_ptr<infobars::InfoBar> infobar_ptr( std::unique_ptr<infobars::InfoBar> infobar_ptr(
...@@ -144,10 +154,12 @@ void PreviewsInfoBarDelegate::Create( ...@@ -144,10 +154,12 @@ void PreviewsInfoBarDelegate::Create(
PreviewsInfoBarDelegate::PreviewsInfoBarDelegate( PreviewsInfoBarDelegate::PreviewsInfoBarDelegate(
content::WebContents* web_contents, content::WebContents* web_contents,
previews::PreviewsType previews_type, previews::PreviewsType previews_type,
base::Time previews_freshness,
bool is_data_saver_user, bool is_data_saver_user,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback) const OnDismissPreviewsInfobarCallback& on_dismiss_callback)
: ConfirmInfoBarDelegate(), : ConfirmInfoBarDelegate(),
previews_type_(previews_type), previews_type_(previews_type),
previews_freshness_(previews_freshness),
infobar_dismissed_action_(INFOBAR_DISMISSED_BY_TAB_CLOSURE), infobar_dismissed_action_(INFOBAR_DISMISSED_BY_TAB_CLOSURE),
message_text_(l10n_util::GetStringUTF16( message_text_(l10n_util::GetStringUTF16(
is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE
...@@ -180,7 +192,18 @@ void PreviewsInfoBarDelegate::InfoBarDismissed() { ...@@ -180,7 +192,18 @@ void PreviewsInfoBarDelegate::InfoBarDismissed() {
} }
base::string16 PreviewsInfoBarDelegate::GetMessageText() const { base::string16 PreviewsInfoBarDelegate::GetMessageText() const {
// Android has a custom infobar that calls GetTimestampText() and adds the
// timestamp in a separate description view. Other OS's can enable previews
// for debugging purposes and don't have a custom infobar with a description
// view, so the timestamp should be appended to the message.
#if defined(OS_ANDROID)
return message_text_; return message_text_;
#else
base::string16 timestamp = GetTimestampText();
if (timestamp.empty())
return message_text_;
return message_text_ + base::ASCIIToUTF16(" ") + timestamp;
#endif
} }
int PreviewsInfoBarDelegate::GetButtons() const { int PreviewsInfoBarDelegate::GetButtons() const {
...@@ -214,5 +237,48 @@ bool PreviewsInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { ...@@ -214,5 +237,48 @@ bool PreviewsInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
} }
base::string16 PreviewsInfoBarDelegate::GetTimestampText() const { base::string16 PreviewsInfoBarDelegate::GetTimestampText() const {
return base::string16(); if (previews_freshness_.is_null())
return base::string16();
if (!base::FeatureList::IsEnabled(
previews::features::kStalePreviewsTimestamp)) {
return base::string16();
}
int min_staleness_in_minutes = base::GetFieldTrialParamByFeatureAsInt(
previews::features::kStalePreviewsTimestamp, "min_staleness_in_minutes",
0);
int max_staleness_in_minutes = base::GetFieldTrialParamByFeatureAsInt(
previews::features::kStalePreviewsTimestamp, "max_staleness_in_minutes",
0);
if (min_staleness_in_minutes == 0 || max_staleness_in_minutes == 0)
return base::string16();
base::Time network_time;
if (g_browser_process->network_time_tracker()->GetNetworkTime(&network_time,
nullptr) !=
network_time::NetworkTimeTracker::NETWORK_TIME_AVAILABLE) {
// When network time has not been initialized yet, simply rely on the
// machine's current time.
network_time = base::Time::Now();
}
int staleness_in_minutes = (network_time - previews_freshness_).InMinutes();
// TODO(megjablon): record metrics for out of bounds staleness.
if (staleness_in_minutes < min_staleness_in_minutes)
return base::string16();
if (staleness_in_minutes > max_staleness_in_minutes)
return base::string16();
if (staleness_in_minutes < 60) {
return l10n_util::GetStringFUTF16(
IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES,
base::IntToString16(staleness_in_minutes));
} else if (staleness_in_minutes < 120) {
return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_ONE_HOUR);
} else {
return l10n_util::GetStringFUTF16(
IDS_PREVIEWS_INFOBAR_TIMESTAMP_HOURS,
base::IntToString16(staleness_in_minutes / 60));
}
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h"
#include "components/infobars/core/confirm_infobar_delegate.h" #include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/previews/core/previews_experiments.h" #include "components/previews/core/previews_experiments.h"
...@@ -41,10 +42,12 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate { ...@@ -41,10 +42,12 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
static void Create( static void Create(
content::WebContents* web_contents, content::WebContents* web_contents,
previews::PreviewsType previews_type, previews::PreviewsType previews_type,
base::Time previews_freshness,
bool is_data_saver_user, bool is_data_saver_user,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback); const OnDismissPreviewsInfobarCallback& on_dismiss_callback);
// ConfirmInfoBarDelegate overrides: // ConfirmInfoBarDelegate overrides:
int GetIconId() const override;
base::string16 GetMessageText() const override; base::string16 GetMessageText() const override;
base::string16 GetLinkText() const override; base::string16 GetLinkText() const override;
...@@ -54,18 +57,21 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate { ...@@ -54,18 +57,21 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
PreviewsInfoBarDelegate( PreviewsInfoBarDelegate(
content::WebContents* web_contents, content::WebContents* web_contents,
previews::PreviewsType previews_type, previews::PreviewsType previews_type,
base::Time previews_freshness,
bool is_data_saver_user, bool is_data_saver_user,
const OnDismissPreviewsInfobarCallback& on_dismiss_callback); const OnDismissPreviewsInfobarCallback& on_dismiss_callback);
// ConfirmInfoBarDelegate overrides: // ConfirmInfoBarDelegate overrides:
infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override; infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
int GetIconId() const override;
bool ShouldExpire(const NavigationDetails& details) const override; bool ShouldExpire(const NavigationDetails& details) const override;
void InfoBarDismissed() override; void InfoBarDismissed() override;
int GetButtons() const override; int GetButtons() const override;
bool LinkClicked(WindowOpenDisposition disposition) override; bool LinkClicked(WindowOpenDisposition disposition) override;
previews::PreviewsType previews_type_; previews::PreviewsType previews_type_;
// The time at which the preview associated with this infobar was created. A
// value of zero means that the creation time is unknown.
const base::Time previews_freshness_;
mutable PreviewsInfoBarAction infobar_dismissed_action_; mutable PreviewsInfoBarAction infobar_dismissed_action_;
const base::string16 message_text_; const base::string16 message_text_;
......
...@@ -105,6 +105,7 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation( ...@@ -105,6 +105,7 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
web_contents()->GetBrowserContext()); web_contents()->GetBrowserContext());
PreviewsInfoBarDelegate::Create( PreviewsInfoBarDelegate::Create(
web_contents(), previews::PreviewsType::OFFLINE, web_contents(), previews::PreviewsType::OFFLINE,
base::Time() /* previews_freshness */,
data_reduction_proxy_settings && data_reduction_proxy_settings &&
data_reduction_proxy_settings->IsDataReductionProxyEnabled(), data_reduction_proxy_settings->IsDataReductionProxyEnabled(),
base::Bind(&AddPreviewNavigationCallback, browser_context_, base::Bind(&AddPreviewNavigationCallback, browser_context_,
...@@ -118,8 +119,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation( ...@@ -118,8 +119,10 @@ void PreviewsInfoBarTabHelper::DidFinishNavigation(
const net::HttpResponseHeaders* headers = const net::HttpResponseHeaders* headers =
navigation_handle->GetResponseHeaders(); navigation_handle->GetResponseHeaders();
if (headers && data_reduction_proxy::IsLitePagePreview(*headers)) { if (headers && data_reduction_proxy::IsLitePagePreview(*headers)) {
base::Time previews_freshness;
headers->GetDateValue(&previews_freshness);
PreviewsInfoBarDelegate::Create( PreviewsInfoBarDelegate::Create(
web_contents(), previews::PreviewsType::LITE_PAGE, web_contents(), previews::PreviewsType::LITE_PAGE, previews_freshness,
true /* is_data_saver_user */, true /* is_data_saver_user */,
base::Bind(&AddPreviewNavigationCallback, browser_context_, base::Bind(&AddPreviewNavigationCallback, browser_context_,
navigation_handle->GetRedirectChain()[0], navigation_handle->GetRedirectChain()[0],
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"
......
...@@ -15,5 +15,9 @@ const base::Feature kOfflinePreviews{"OfflinePreviews", ...@@ -15,5 +15,9 @@ const base::Feature kOfflinePreviews{"OfflinePreviews",
const base::Feature kClientLoFi{"ClientLoFi", const base::Feature kClientLoFi{"ClientLoFi",
base::FEATURE_DISABLED_BY_DEFAULT}; base::FEATURE_DISABLED_BY_DEFAULT};
// Enables the Stale Previews timestamp on Previews infobars.
const base::Feature kStalePreviewsTimestamp{"StalePreviewsTimestamp",
base::FEATURE_DISABLED_BY_DEFAULT};
} // namespace features } // namespace features
} // namespace previews } // namespace previews
...@@ -12,6 +12,7 @@ namespace features { ...@@ -12,6 +12,7 @@ namespace features {
extern const base::Feature kOfflinePreviews; extern const base::Feature kOfflinePreviews;
extern const base::Feature kClientLoFi; extern const base::Feature kClientLoFi;
extern const base::Feature kStalePreviewsTimestamp;
} // namespace features } // namespace features
} // namespace previews } // namespace previews
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment