| 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" | 7 #include "base/feature_list.h" |
| 8 #include "base/metrics/field_trial_params.h" | 8 #include "base/metrics/field_trial_params.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 RecordPreviewsInfoBarAction(previews_type_, infobar_dismissed_action_); | 116 RecordPreviewsInfoBarAction(previews_type_, infobar_dismissed_action_); |
| 117 } | 117 } |
| 118 | 118 |
| 119 // static | 119 // static |
| 120 void PreviewsInfoBarDelegate::Create( | 120 void PreviewsInfoBarDelegate::Create( |
| 121 content::WebContents* web_contents, | 121 content::WebContents* web_contents, |
| 122 previews::PreviewsType previews_type, | 122 previews::PreviewsType previews_type, |
| 123 base::Time previews_freshness, | 123 base::Time previews_freshness, |
| 124 bool is_data_saver_user, | 124 bool is_data_saver_user, |
| 125 bool is_reload, |
| 125 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) { | 126 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) { |
| 126 PreviewsInfoBarTabHelper* infobar_tab_helper = | 127 PreviewsInfoBarTabHelper* infobar_tab_helper = |
| 127 PreviewsInfoBarTabHelper::FromWebContents(web_contents); | 128 PreviewsInfoBarTabHelper::FromWebContents(web_contents); |
| 128 InfoBarService* infobar_service = | 129 InfoBarService* infobar_service = |
| 129 InfoBarService::FromWebContents(web_contents); | 130 InfoBarService::FromWebContents(web_contents); |
| 130 | 131 |
| 131 // The WebContents may not have TabHelpers set. If TabHelpers are not set, | 132 // The WebContents may not have TabHelpers set. If TabHelpers are not set, |
| 132 // don't show Previews infobars. | 133 // don't show Previews infobars. |
| 133 if (!infobar_tab_helper || !infobar_service) | 134 if (!infobar_tab_helper || !infobar_service) |
| 134 return; | 135 return; |
| 135 if (infobar_tab_helper->displayed_preview_infobar()) | 136 if (infobar_tab_helper->displayed_preview_infobar()) |
| 136 return; | 137 return; |
| 137 | 138 |
| 138 std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate( | 139 std::unique_ptr<PreviewsInfoBarDelegate> delegate(new PreviewsInfoBarDelegate( |
| 139 web_contents, previews_type, previews_freshness, is_data_saver_user, | 140 infobar_tab_helper, previews_type, previews_freshness, is_data_saver_user, |
| 140 on_dismiss_callback)); | 141 is_reload, on_dismiss_callback)); |
| 141 | 142 |
| 142 #if defined(OS_ANDROID) | 143 #if defined(OS_ANDROID) |
| 143 std::unique_ptr<infobars::InfoBar> infobar_ptr( | 144 std::unique_ptr<infobars::InfoBar> infobar_ptr( |
| 144 PreviewsInfoBar::CreateInfoBar(infobar_service, std::move(delegate))); | 145 PreviewsInfoBar::CreateInfoBar(infobar_service, std::move(delegate))); |
| 145 #else | 146 #else |
| 146 std::unique_ptr<infobars::InfoBar> infobar_ptr( | 147 std::unique_ptr<infobars::InfoBar> infobar_ptr( |
| 147 infobar_service->CreateConfirmInfoBar(std::move(delegate))); | 148 infobar_service->CreateConfirmInfoBar(std::move(delegate))); |
| 148 #endif | 149 #endif |
| 149 | 150 |
| 150 infobars::InfoBar* infobar = | 151 infobars::InfoBar* infobar = |
| 151 infobar_service->AddInfoBar(std::move(infobar_ptr)); | 152 infobar_service->AddInfoBar(std::move(infobar_ptr)); |
| 152 | 153 |
| 153 if (infobar && (previews_type == previews::PreviewsType::LITE_PAGE || | 154 if (infobar && (previews_type == previews::PreviewsType::LITE_PAGE || |
| 154 previews_type == previews::PreviewsType::LOFI)) { | 155 previews_type == previews::PreviewsType::LOFI)) { |
| 155 auto* data_reduction_proxy_settings = | 156 auto* data_reduction_proxy_settings = |
| 156 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 157 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 157 web_contents->GetBrowserContext()); | 158 web_contents->GetBrowserContext()); |
| 158 data_reduction_proxy_settings->IncrementLoFiUIShown(); | 159 data_reduction_proxy_settings->IncrementLoFiUIShown(); |
| 159 } | 160 } |
| 160 | 161 |
| 161 RecordPreviewsInfoBarAction(previews_type, INFOBAR_SHOWN); | 162 RecordPreviewsInfoBarAction(previews_type, INFOBAR_SHOWN); |
| 162 infobar_tab_helper->set_displayed_preview_infobar(true); | 163 infobar_tab_helper->set_displayed_preview_infobar(true); |
| 163 } | 164 } |
| 164 | 165 |
| 165 PreviewsInfoBarDelegate::PreviewsInfoBarDelegate( | 166 PreviewsInfoBarDelegate::PreviewsInfoBarDelegate( |
| 166 content::WebContents* web_contents, | 167 PreviewsInfoBarTabHelper* infobar_tab_helper, |
| 167 previews::PreviewsType previews_type, | 168 previews::PreviewsType previews_type, |
| 168 base::Time previews_freshness, | 169 base::Time previews_freshness, |
| 169 bool is_data_saver_user, | 170 bool is_data_saver_user, |
| 171 bool is_reload, |
| 170 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) | 172 const OnDismissPreviewsInfobarCallback& on_dismiss_callback) |
| 171 : ConfirmInfoBarDelegate(), | 173 : ConfirmInfoBarDelegate(), |
| 174 infobar_tab_helper_(infobar_tab_helper), |
| 172 previews_type_(previews_type), | 175 previews_type_(previews_type), |
| 173 previews_freshness_(previews_freshness), | 176 previews_freshness_(previews_freshness), |
| 177 is_reload_(is_reload), |
| 174 infobar_dismissed_action_(INFOBAR_DISMISSED_BY_TAB_CLOSURE), | 178 infobar_dismissed_action_(INFOBAR_DISMISSED_BY_TAB_CLOSURE), |
| 175 message_text_(l10n_util::GetStringUTF16( | 179 message_text_(l10n_util::GetStringUTF16( |
| 176 is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE | 180 is_data_saver_user ? IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE |
| 177 : IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE)), | 181 : IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE)), |
| 178 on_dismiss_callback_(on_dismiss_callback) {} | 182 on_dismiss_callback_(on_dismiss_callback) {} |
| 179 | 183 |
| 180 infobars::InfoBarDelegate::InfoBarIdentifier | 184 infobars::InfoBarDelegate::InfoBarIdentifier |
| 181 PreviewsInfoBarDelegate::GetIdentifier() const { | 185 PreviewsInfoBarDelegate::GetIdentifier() const { |
| 182 return DATA_REDUCTION_PROXY_PREVIEW_INFOBAR_DELEGATE; | 186 return DATA_REDUCTION_PROXY_PREVIEW_INFOBAR_DELEGATE; |
| 183 } | 187 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 network_time = base::Time::Now(); | 280 network_time = base::Time::Now(); |
| 277 } | 281 } |
| 278 | 282 |
| 279 if (network_time < previews_freshness_) { | 283 if (network_time < previews_freshness_) { |
| 280 RecordStaleness(TIMESTAMP_NOT_SHOWN_STALENESS_NEGATIVE); | 284 RecordStaleness(TIMESTAMP_NOT_SHOWN_STALENESS_NEGATIVE); |
| 281 return base::string16(); | 285 return base::string16(); |
| 282 } | 286 } |
| 283 | 287 |
| 284 int staleness_in_minutes = (network_time - previews_freshness_).InMinutes(); | 288 int staleness_in_minutes = (network_time - previews_freshness_).InMinutes(); |
| 285 if (staleness_in_minutes < min_staleness_in_minutes) { | 289 if (staleness_in_minutes < min_staleness_in_minutes) { |
| 290 if (is_reload_) { |
| 291 RecordStaleness(TIMESTAMP_UPDATED_NOW_SHOWN); |
| 292 if (infobar_tab_helper_) |
| 293 infobar_tab_helper_->set_displayed_preview_timestamp(true); |
| 294 return l10n_util::GetStringUTF16( |
| 295 IDS_PREVIEWS_INFOBAR_TIMESTAMP_UPDATED_NOW); |
| 296 } |
| 286 RecordStaleness(TIMESTAMP_NOT_SHOWN_PREVIEW_NOT_STALE); | 297 RecordStaleness(TIMESTAMP_NOT_SHOWN_PREVIEW_NOT_STALE); |
| 287 return base::string16(); | 298 return base::string16(); |
| 288 } | 299 } |
| 289 if (staleness_in_minutes > max_staleness_in_minutes) { | 300 if (staleness_in_minutes > max_staleness_in_minutes) { |
| 290 RecordStaleness(TIMESTAMP_NOT_SHOWN_STALENESS_GREATER_THAN_MAX); | 301 RecordStaleness(TIMESTAMP_NOT_SHOWN_STALENESS_GREATER_THAN_MAX); |
| 291 return base::string16(); | 302 return base::string16(); |
| 292 } | 303 } |
| 293 | 304 |
| 294 RecordStaleness(TIMESTAMP_SHOWN); | 305 RecordStaleness(TIMESTAMP_SHOWN); |
| 306 if (infobar_tab_helper_) |
| 307 infobar_tab_helper_->set_displayed_preview_timestamp(true); |
| 295 | 308 |
| 296 if (staleness_in_minutes < 60) { | 309 if (staleness_in_minutes < 60) { |
| 297 return l10n_util::GetStringFUTF16( | 310 return l10n_util::GetStringFUTF16( |
| 298 IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES, | 311 IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES, |
| 299 base::IntToString16(staleness_in_minutes)); | 312 base::IntToString16(staleness_in_minutes)); |
| 300 } else if (staleness_in_minutes < 120) { | 313 } else if (staleness_in_minutes < 120) { |
| 301 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_ONE_HOUR); | 314 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_ONE_HOUR); |
| 302 } else { | 315 } else { |
| 303 return l10n_util::GetStringFUTF16( | 316 return l10n_util::GetStringFUTF16( |
| 304 IDS_PREVIEWS_INFOBAR_TIMESTAMP_HOURS, | 317 IDS_PREVIEWS_INFOBAR_TIMESTAMP_HOURS, |
| 305 base::IntToString16(staleness_in_minutes / 60)); | 318 base::IntToString16(staleness_in_minutes / 60)); |
| 306 } | 319 } |
| 307 } | 320 } |
| OLD | NEW |