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 #if defined(OS_ANDROID) | |
183 return message_text_; | 196 return message_text_; |
197 #else | |
198 base::string16 timestamp = GetTimestampText(); | |
tbansal1
2017/06/19 20:52:28
Why is the implementation different for Android vs
megjablon
2017/06/20 18:42:12
https://chromiumcodereview.appspot.com/2860083003/
tbansal1
2017/06/20 21:07:14
Can we add a comment here to explain what's going
megjablon
2017/06/20 21:27:26
Done.
| |
199 if (timestamp.empty()) | |
200 return message_text_; | |
201 return message_text_ + base::ASCIIToUTF16(" ") + timestamp; | |
202 #endif | |
184 } | 203 } |
185 | 204 |
186 int PreviewsInfoBarDelegate::GetButtons() const { | 205 int PreviewsInfoBarDelegate::GetButtons() const { |
187 return BUTTON_NONE; | 206 return BUTTON_NONE; |
188 } | 207 } |
189 | 208 |
190 base::string16 PreviewsInfoBarDelegate::GetLinkText() const { | 209 base::string16 PreviewsInfoBarDelegate::GetLinkText() const { |
191 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK); | 210 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK); |
192 } | 211 } |
193 | 212 |
(...skipping 13 matching lines...) Expand all Loading... | |
207 !data_reduction_proxy::params::IsBlackListEnabledForServerPreviews()) { | 226 !data_reduction_proxy::params::IsBlackListEnabledForServerPreviews()) { |
208 IncrementDataReductionProxyPrefs(web_contents); | 227 IncrementDataReductionProxyPrefs(web_contents); |
209 } | 228 } |
210 | 229 |
211 ReloadWithoutPreviews(previews_type_, web_contents); | 230 ReloadWithoutPreviews(previews_type_, web_contents); |
212 | 231 |
213 return true; | 232 return true; |
214 } | 233 } |
215 | 234 |
216 base::string16 PreviewsInfoBarDelegate::GetTimestampText() const { | 235 base::string16 PreviewsInfoBarDelegate::GetTimestampText() const { |
217 return base::string16(); | 236 if (previews_freshness_.is_null()) |
237 return base::string16(); | |
238 if (!base::FeatureList::IsEnabled( | |
239 previews::features::kStalePreviewsTimestamp)) { | |
240 return base::string16(); | |
241 } | |
242 | |
243 int min_staleness_in_minutes = base::GetFieldTrialParamByFeatureAsInt( | |
244 previews::features::kStalePreviewsTimestamp, "min_staleness_in_minutes", | |
245 0); | |
246 int max_staleness_in_minutes = base::GetFieldTrialParamByFeatureAsInt( | |
247 previews::features::kStalePreviewsTimestamp, "max_staleness_in_minutes", | |
248 0); | |
249 | |
250 if (min_staleness_in_minutes == 0 || max_staleness_in_minutes == 0) | |
251 return base::string16(); | |
252 | |
253 base::Time network_time; | |
254 if (g_browser_process->network_time_tracker()->GetNetworkTime(&network_time, | |
255 nullptr) != | |
256 network_time::NetworkTimeTracker::NETWORK_TIME_AVAILABLE) { | |
257 // When network time has not been initialized yet, simply rely on the | |
258 // machine's current time. | |
259 network_time = base::Time::Now(); | |
260 } | |
261 | |
262 int staleness_in_minutes = (network_time - previews_freshness_).InMinutes(); | |
263 // TODO(megjablon): record metrics for out of bounds staleness. | |
264 if (staleness_in_minutes < min_staleness_in_minutes) | |
265 return base::string16(); | |
266 if (staleness_in_minutes > max_staleness_in_minutes) | |
267 return base::string16(); | |
268 | |
269 if (staleness_in_minutes < 60) { | |
270 return l10n_util::GetStringFUTF16( | |
271 IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES, | |
272 base::IntToString16(staleness_in_minutes)); | |
273 } else if (staleness_in_minutes < 120) { | |
274 return l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_ONE_HOUR); | |
275 } else { | |
276 return l10n_util::GetStringFUTF16( | |
277 IDS_PREVIEWS_INFOBAR_TIMESTAMP_HOURS, | |
278 base::IntToString16(staleness_in_minutes / 60)); | |
279 } | |
218 } | 280 } |
OLD | NEW |