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_tab_helper.h" | 5 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 9 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
10 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" | 10 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
11 #include "chrome/browser/previews/previews_infobar_delegate.h" | 11 #include "chrome/browser/previews/previews_infobar_delegate.h" |
12 #include "chrome/browser/previews/previews_service.h" | 12 #include "chrome/browser/previews/previews_service.h" |
13 #include "chrome/browser/previews/previews_service_factory.h" | 13 #include "chrome/browser/previews/previews_service_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/features.h" | |
16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" | 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" |
17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" | 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" |
18 #include "components/previews/core/previews_experiments.h" | 17 #include "components/previews/core/previews_experiments.h" |
19 #include "components/previews/core/previews_ui_service.h" | 18 #include "components/previews/core/previews_ui_service.h" |
20 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
21 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/navigation_handle.h" | 21 #include "content/public/browser/navigation_handle.h" |
23 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
24 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
25 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
26 #include "url/gurl.h" | 25 #include "url/gurl.h" |
27 | 26 |
28 #if BUILDFLAG(ANDROID_JAVA_UI) | 27 #if defined(OS_ANDROID) |
29 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" | 28 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
30 | 29 |
31 namespace { | 30 namespace { |
32 | 31 |
33 // Adds the preview navigation to the black list. This method is only used on | 32 // Adds the preview navigation to the black list. This method is only used on |
34 // android currently. | 33 // android currently. |
35 void AddPreviewNavigationCallback(content::BrowserContext* browser_context, | 34 void AddPreviewNavigationCallback(content::BrowserContext* browser_context, |
36 const GURL& url, | 35 const GURL& url, |
37 previews::PreviewsType type, | 36 previews::PreviewsType type, |
38 bool opt_out) { | 37 bool opt_out) { |
39 PreviewsService* previews_service = PreviewsServiceFactory::GetForProfile( | 38 PreviewsService* previews_service = PreviewsServiceFactory::GetForProfile( |
40 Profile::FromBrowserContext(browser_context)); | 39 Profile::FromBrowserContext(browser_context)); |
41 if (previews_service && previews_service->previews_ui_service()) { | 40 if (previews_service && previews_service->previews_ui_service()) { |
42 previews_service->previews_ui_service()->AddPreviewNavigation(url, type, | 41 previews_service->previews_ui_service()->AddPreviewNavigation(url, type, |
43 opt_out); | 42 opt_out); |
44 } | 43 } |
45 } | 44 } |
46 | 45 |
47 } // namespace | 46 } // namespace |
48 | 47 |
49 #endif // BUILDFLAG(ANDROID_JAVA_UI) | 48 #endif // defined(OS_ANDROID) |
50 | 49 |
51 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PreviewsInfoBarTabHelper); | 50 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PreviewsInfoBarTabHelper); |
52 | 51 |
53 PreviewsInfoBarTabHelper::~PreviewsInfoBarTabHelper() {} | 52 PreviewsInfoBarTabHelper::~PreviewsInfoBarTabHelper() {} |
54 | 53 |
55 PreviewsInfoBarTabHelper::PreviewsInfoBarTabHelper( | 54 PreviewsInfoBarTabHelper::PreviewsInfoBarTabHelper( |
56 content::WebContents* web_contents) | 55 content::WebContents* web_contents) |
57 : content::WebContentsObserver(web_contents), | 56 : content::WebContentsObserver(web_contents), |
58 displayed_preview_infobar_(false){ | 57 displayed_preview_infobar_(false){ |
59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 58 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
60 } | 59 } |
61 | 60 |
62 void PreviewsInfoBarTabHelper::DidFinishNavigation( | 61 void PreviewsInfoBarTabHelper::DidFinishNavigation( |
63 content::NavigationHandle* navigation_handle) { | 62 content::NavigationHandle* navigation_handle) { |
64 // Only show the infobar if this is a full main frame navigation. | 63 // Only show the infobar if this is a full main frame navigation. |
65 if (!navigation_handle->IsInMainFrame() || | 64 if (!navigation_handle->IsInMainFrame() || |
66 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) | 65 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) |
67 return; | 66 return; |
68 displayed_preview_infobar_ = false; | 67 displayed_preview_infobar_ = false; |
69 | 68 |
70 #if BUILDFLAG(ANDROID_JAVA_UI) | 69 #if defined(OS_ANDROID) |
71 offline_pages::OfflinePageTabHelper* tab_helper = | 70 offline_pages::OfflinePageTabHelper* tab_helper = |
72 offline_pages::OfflinePageTabHelper::FromWebContents(web_contents()); | 71 offline_pages::OfflinePageTabHelper::FromWebContents(web_contents()); |
73 | 72 |
74 if (tab_helper && tab_helper->IsShowingOfflinePreview()) { | 73 if (tab_helper && tab_helper->IsShowingOfflinePreview()) { |
75 if (navigation_handle->IsErrorPage()) { | 74 if (navigation_handle->IsErrorPage()) { |
76 // TODO(ryansturm): Add UMA for errors. | 75 // TODO(ryansturm): Add UMA for errors. |
77 return; | 76 return; |
78 } | 77 } |
79 data_reduction_proxy::DataReductionProxySettings* | 78 data_reduction_proxy::DataReductionProxySettings* |
80 data_reduction_proxy_settings = | 79 data_reduction_proxy_settings = |
81 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 80 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
82 web_contents()->GetBrowserContext()); | 81 web_contents()->GetBrowserContext()); |
83 PreviewsInfoBarDelegate::Create( | 82 PreviewsInfoBarDelegate::Create( |
84 web_contents(), PreviewsInfoBarDelegate::OFFLINE, | 83 web_contents(), PreviewsInfoBarDelegate::OFFLINE, |
85 data_reduction_proxy_settings && | 84 data_reduction_proxy_settings && |
86 data_reduction_proxy_settings->IsDataReductionProxyEnabled(), | 85 data_reduction_proxy_settings->IsDataReductionProxyEnabled(), |
87 base::Bind( | 86 base::Bind( |
88 &AddPreviewNavigationCallback, web_contents()->GetBrowserContext(), | 87 &AddPreviewNavigationCallback, web_contents()->GetBrowserContext(), |
89 navigation_handle->GetURL(), previews::PreviewsType::OFFLINE)); | 88 navigation_handle->GetURL(), previews::PreviewsType::OFFLINE)); |
90 // Don't try to show other infobars if this is an offline preview. | 89 // Don't try to show other infobars if this is an offline preview. |
91 return; | 90 return; |
92 } | 91 } |
93 #endif // BUILDFLAG(ANDROID_JAVA_UI) | 92 #endif // defined(OS_ANDROID) |
94 | 93 |
95 const net::HttpResponseHeaders* headers = | 94 const net::HttpResponseHeaders* headers = |
96 navigation_handle->GetResponseHeaders(); | 95 navigation_handle->GetResponseHeaders(); |
97 if (headers && data_reduction_proxy::IsLitePagePreview(*headers)) { | 96 if (headers && data_reduction_proxy::IsLitePagePreview(*headers)) { |
98 PreviewsInfoBarDelegate::Create( | 97 PreviewsInfoBarDelegate::Create( |
99 web_contents(), PreviewsInfoBarDelegate::LITE_PAGE, | 98 web_contents(), PreviewsInfoBarDelegate::LITE_PAGE, |
100 true /* is_data_saver_user */, | 99 true /* is_data_saver_user */, |
101 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); | 100 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); |
102 } | 101 } |
103 } | 102 } |
104 | 103 |
OLD | NEW |