| OLD | NEW |
| 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/previews/previews_infobar_delegate.h" | 5 #include "chrome/browser/previews/previews_infobar_delegate.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" |
| 8 #include "base/metrics/field_trial_params.h" |
| 7 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 8 #include "base/optional.h" | 10 #include "base/optional.h" |
| 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/android/android_theme_resources.h" | 14 #include "chrome/browser/android/android_theme_resources.h" |
| 15 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/infobars/infobar_service.h" | 16 #include "chrome/browser/infobars/infobar_service.h" |
| 11 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| 12 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" | 18 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
| 13 #include "chrome/browser/previews/previews_infobar_tab_helper.h" | 19 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 14 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" | 21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" |
| 16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv
ice.h" | 22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv
ice.h" |
| 17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" | 23 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" |
| 18 #include "components/infobars/core/infobar.h" | 24 #include "components/infobars/core/infobar.h" |
| 25 #include "components/network_time/network_time_tracker.h" |
| 26 #include "components/previews/core/previews_features.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 27 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 22 | 30 |
| 23 #if defined(OS_ANDROID) | 31 #if defined(OS_ANDROID) |
| 24 #include "chrome/browser/ui/android/infobars/previews_infobar.h" | 32 #include "chrome/browser/ui/android/infobars/previews_infobar.h" |
| 25 #endif | 33 #endif |
| 26 | 34 |
| 27 namespace { | 35 namespace { |
| 28 | 36 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 if (!on_dismiss_callback_.is_null()) | 102 if (!on_dismiss_callback_.is_null()) |
| 95 on_dismiss_callback_.Run(false); | 103 on_dismiss_callback_.Run(false); |
| 96 | 104 |
| 97 RecordPreviewsInfoBarAction(previews_type_, infobar_dismissed_action_); | 105 RecordPreviewsInfoBarAction(previews_type_, infobar_dismissed_action_); |
| 98 } | 106 } |
| 99 | 107 |
| 100 // static | 108 // static |
| 101 void PreviewsInfoBarDelegate::Create( | 109 void PreviewsInfoBarDelegate::Create( |
| 102 content::WebContents* web_contents, | 110 content::WebContents* web_contents, |
| 103 previews::PreviewsType previews_type, | 111 previews::PreviewsType previews_type, |
| 112 base::Time previews_freshness, |
| 104 bool is_data_saver_user, | 113 bool is_data_saver_user, |
| 105 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) { | 114 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) { |
| 106 PreviewsInfoBarTabHelper* infobar_tab_helper = | 115 PreviewsInfoBarTabHelper* infobar_tab_helper = |
| 107 PreviewsInfoBarTabHelper::FromWebContents(web_contents); | 116 PreviewsInfoBarTabHelper::FromWebContents(web_contents); |
| 108 InfoBarService* infobar_service = | 117 InfoBarService* infobar_service = |
| 109 InfoBarService::FromWebContents(web_contents); | 118 InfoBarService::FromWebContents(web_contents); |
| 110 | 119 |
| 111 // The WebContents may not have TabHelpers set. If TabHelpers are not set, | 120 // The WebContents may not have TabHelpers set. If TabHelpers are not set, |
| 112 // don't show Previews infobars. | 121 // don't show Previews infobars. |
| 113 if (!infobar_tab_helper || !infobar_service) | 122 if (!infobar_tab_helper || !infobar_service) |
| 114 return; | 123 return; |
| 115 if (infobar_tab_helper->displayed_preview_infobar()) | 124 if (infobar_tab_helper->displayed_preview_infobar()) |
| 116 return; | 125 return; |
| 117 | 126 |
| 118 std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate( | 127 std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate( |
| 119 web_contents, previews_type, is_data_saver_user, on_dismiss_callback)); | 128 web_contents, previews_type, previews_freshness, is_data_saver_user, |
| 129 on_dismiss_callback)); |
| 120 | 130 |
| 121 #if defined(OS_ANDROID) | 131 #if defined(OS_ANDROID) |
| 122 std::unique_ptr<infobars::InfoBar> infobar_ptr( | 132 std::unique_ptr<infobars::InfoBar> infobar_ptr( |
| 123 PreviewsInfoBar::CreateInfoBar(infobar_service, std::move(delegate))); | 133 PreviewsInfoBar::CreateInfoBar(infobar_service, std::move(delegate))); |
| 124 #else | 134 #else |
| 125 std::unique_ptr<infobars::InfoBar> infobar_ptr( | 135 std::unique_ptr<infobars::InfoBar> infobar_ptr( |
| 126 infobar_service->CreateConfirmInfoBar(std::move(delegate))); | 136 infobar_service->CreateConfirmInfoBar(std::move(delegate))); |
| 127 #endif | 137 #endif |
| 128 | 138 |
| 129 infobars::InfoBar* infobar = | 139 infobars::InfoBar* infobar = |
| 130 infobar_service->AddInfoBar(std::move(infobar_ptr)); | 140 infobar_service->AddInfoBar(std::move(infobar_ptr)); |
| 131 | 141 |
| 132 if (infobar && (previews_type == previews::PreviewsType::LITE_PAGE || | 142 if (infobar && (previews_type == previews::PreviewsType::LITE_PAGE || |
| 133 previews_type == previews::PreviewsType::LOFI)) { | 143 previews_type == previews::PreviewsType::LOFI)) { |
| 134 auto* data_reduction_proxy_settings = | 144 auto* data_reduction_proxy_settings = |
| 135 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 145 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 136 web_contents->GetBrowserContext()); | 146 web_contents->GetBrowserContext()); |
| 137 data_reduction_proxy_settings->IncrementLoFiUIShown(); | 147 data_reduction_proxy_settings->IncrementLoFiUIShown(); |
| 138 } | 148 } |
| 139 | 149 |
| 140 RecordPreviewsInfoBarAction(previews_type, INFOBAR_SHOWN); | 150 RecordPreviewsInfoBarAction(previews_type, INFOBAR_SHOWN); |
| 141 infobar_tab_helper->set_displayed_preview_infobar(true); | 151 infobar_tab_helper->set_displayed_preview_infobar(true); |
| 142 } | 152 } |
| 143 | 153 |
| 144 PreviewsInfoBarDelegate::PreviewsInfoBarDelegate( | 154 PreviewsInfoBarDelegate::PreviewsInfoBarDelegate( |
| 145 content::WebContents* web_contents, | 155 content::WebContents* web_contents, |
| 146 previews::PreviewsType previews_type, | 156 previews::PreviewsType previews_type, |
| 157 base::Time previews_freshness, |
| 147 bool is_data_saver_user, | 158 bool is_data_saver_user, |
| 148 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) | 159 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) |
| 149 : ConfirmInfoBarDelegate(), | 160 : ConfirmInfoBarDelegate(), |
| 150 previews_type_(previews_type), | 161 previews_type_(previews_type), |
| 162 previews_freshness_(previews_freshness), |
| 151 infobar_dismissed_action_(INFOBAR_DISMISSED_BY_TAB_CLOSURE), | 163 infobar_dismissed_action_(INFOBAR_DISMISSED_BY_TAB_CLOSURE), |
| 152 message_text_(l10n_util::GetStringUTF16( | 164 message_text_(l10n_util::GetStringUTF16( |
| 153 is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE | 165 is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE |
| 154 : IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE)), | 166 : IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE)), |
| 155 on_dismiss_callback_(on_dismiss_callback) {} | 167 on_dismiss_callback_(on_dismiss_callback) {} |
| 156 | 168 |
| 157 infobars::InfoBarDelegate::InfoBarIdentifier | 169 infobars::InfoBarDelegate::InfoBarIdentifier |
| 158 PreviewsInfoBarDelegate::GetIdentifier() const { | 170 PreviewsInfoBarDelegate::GetIdentifier() const { |
| 159 return DATA_REDUCTION_PROXY_PREVIEW_INFOBAR_DELEGATE; | 171 return DATA_REDUCTION_PROXY_PREVIEW_INFOBAR_DELEGATE; |
| 160 } | 172 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 173 ? INFOBAR_DISMISSED_BY_RELOAD | 185 ? INFOBAR_DISMISSED_BY_RELOAD |
| 174 : INFOBAR_DISMISSED_BY_NAVIGATION; | 186 : INFOBAR_DISMISSED_BY_NAVIGATION; |
| 175 return InfoBarDelegate::ShouldExpire(details); | 187 return InfoBarDelegate::ShouldExpire(details); |
| 176 } | 188 } |
| 177 | 189 |
| 178 void PreviewsInfoBarDelegate::InfoBarDismissed() { | 190 void PreviewsInfoBarDelegate::InfoBarDismissed() { |
| 179 infobar_dismissed_action_ = INFOBAR_DISMISSED_BY_USER; | 191 infobar_dismissed_action_ = INFOBAR_DISMISSED_BY_USER; |
| 180 } | 192 } |
| 181 | 193 |
| 182 base::string16 PreviewsInfoBarDelegate::GetMessageText() const { | 194 base::string16 PreviewsInfoBarDelegate::GetMessageText() const { |
| 195 // Android has a custom infobar that calls GetTimestampText() and adds the |
| 196 // timestamp in a separate description view. Other OS's can enable previews |
| 197 // for debugging purposes and don't have a custom infobar with a description |
| 198 // view, so the timestamp should be appended to the message. |
| 199 #if defined(OS_ANDROID) |
| 183 return message_text_; | 200 return message_text_; |
| 201 #else |
| 202 base::string16 timestamp = GetTimestampText(); |
| 203 if (timestamp.empty()) |
| 204 return message_text_; |
| 205 return message_text_ + base::ASCIIToUTF16(" ") + timestamp; |
| 206 #endif |
| 184 } | 207 } |
| 185 | 208 |
| 186 int PreviewsInfoBarDelegate::GetButtons() const { | 209 int PreviewsInfoBarDelegate::GetButtons() const { |
| 187 return BUTTON_NONE; | 210 return BUTTON_NONE; |
| 188 } | 211 } |
| 189 | 212 |
| 190 base::string16 PreviewsInfoBarDelegate::GetLinkText() const { | 213 base::string16 PreviewsInfoBarDelegate::GetLinkText() const { |
| 191 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK); | 214 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK); |
| 192 } | 215 } |
| 193 | 216 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 207 !data_reduction_proxy::params::IsBlackListEnabledForServerPreviews()) { | 230 !data_reduction_proxy::params::IsBlackListEnabledForServerPreviews()) { |
| 208 IncrementDataReductionProxyPrefs(web_contents); | 231 IncrementDataReductionProxyPrefs(web_contents); |
| 209 } | 232 } |
| 210 | 233 |
| 211 ReloadWithoutPreviews(previews_type_, web_contents); | 234 ReloadWithoutPreviews(previews_type_, web_contents); |
| 212 | 235 |
| 213 return true; | 236 return true; |
| 214 } | 237 } |
| 215 | 238 |
| 216 base::string16 PreviewsInfoBarDelegate::GetTimestampText() const { | 239 base::string16 PreviewsInfoBarDelegate::GetTimestampText() const { |
| 217 return base::string16(); | 240 if (previews_freshness_.is_null()) |
| 241 return base::string16(); |
| 242 if (!base::FeatureList::IsEnabled( |
| 243 previews::features::kStalePreviewsTimestamp)) { |
| 244 return base::string16(); |
| 245 } |
| 246 |
| 247 int min_staleness_in_minutes = base::GetFieldTrialParamByFeatureAsInt( |
| 248 previews::features::kStalePreviewsTimestamp, "min_staleness_in_minutes", |
| 249 0); |
| 250 int max_staleness_in_minutes = base::GetFieldTrialParamByFeatureAsInt( |
| 251 previews::features::kStalePreviewsTimestamp, "max_staleness_in_minutes", |
| 252 0); |
| 253 |
| 254 if (min_staleness_in_minutes == 0 || max_staleness_in_minutes == 0) |
| 255 return base::string16(); |
| 256 |
| 257 base::Time network_time; |
| 258 if (g_browser_process->network_time_tracker()->GetNetworkTime(&network_time, |
| 259 nullptr) != |
| 260 network_time::NetworkTimeTracker::NETWORK_TIME_AVAILABLE) { |
| 261 // When network time has not been initialized yet, simply rely on the |
| 262 // machine's current time. |
| 263 network_time = base::Time::Now(); |
| 264 } |
| 265 |
| 266 int staleness_in_minutes = (network_time - previews_freshness_).InMinutes(); |
| 267 // TODO(megjablon): record metrics for out of bounds staleness. |
| 268 if (staleness_in_minutes < min_staleness_in_minutes) |
| 269 return base::string16(); |
| 270 if (staleness_in_minutes > max_staleness_in_minutes) |
| 271 return base::string16(); |
| 272 |
| 273 if (staleness_in_minutes < 60) { |
| 274 return l10n_util::GetStringFUTF16( |
| 275 IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES, |
| 276 base::IntToString16(staleness_in_minutes)); |
| 277 } else if (staleness_in_minutes < 120) { |
| 278 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_ONE_HOUR); |
| 279 } else { |
| 280 return l10n_util::GetStringFUTF16( |
| 281 IDS_PREVIEWS_INFOBAR_TIMESTAMP_HOURS, |
| 282 base::IntToString16(staleness_in_minutes / 60)); |
| 283 } |
| 218 } | 284 } |
| OLD | NEW |