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/loader/chrome_navigation_data.h" |
9 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 10 #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" | 11 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
11 #include "chrome/browser/previews/previews_infobar_delegate.h" | 12 #include "chrome/browser/previews/previews_infobar_delegate.h" |
12 #include "chrome/browser/previews/previews_service.h" | 13 #include "chrome/browser/previews/previews_service.h" |
13 #include "chrome/browser/previews/previews_service_factory.h" | 14 #include "chrome/browser/previews/previews_service_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
| 17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv
ice.h" |
15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" | 18 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" |
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" | 19 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" |
17 #include "components/previews/core/previews_experiments.h" | 20 #include "components/previews/core/previews_experiments.h" |
18 #include "components/previews/core/previews_ui_service.h" | 21 #include "components/previews/core/previews_ui_service.h" |
19 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/navigation_handle.h" | 24 #include "content/public/browser/navigation_handle.h" |
22 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
23 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
24 #include "net/http/http_response_headers.h" | 27 #include "net/http/http_response_headers.h" |
(...skipping 17 matching lines...) Expand all Loading... |
42 opt_out); | 45 opt_out); |
43 } | 46 } |
44 } | 47 } |
45 | 48 |
46 } // namespace | 49 } // namespace |
47 | 50 |
48 #endif // defined(OS_ANDROID) | 51 #endif // defined(OS_ANDROID) |
49 | 52 |
50 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PreviewsInfoBarTabHelper); | 53 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PreviewsInfoBarTabHelper); |
51 | 54 |
52 PreviewsInfoBarTabHelper::~PreviewsInfoBarTabHelper() {} | 55 PreviewsInfoBarTabHelper::~PreviewsInfoBarTabHelper() { |
| 56 ClearLastNavigationAsync(); |
| 57 } |
53 | 58 |
54 PreviewsInfoBarTabHelper::PreviewsInfoBarTabHelper( | 59 PreviewsInfoBarTabHelper::PreviewsInfoBarTabHelper( |
55 content::WebContents* web_contents) | 60 content::WebContents* web_contents) |
56 : content::WebContentsObserver(web_contents), | 61 : content::WebContentsObserver(web_contents), |
57 displayed_preview_infobar_(false){ | 62 browser_context_(web_contents->GetBrowserContext()), |
| 63 displayed_preview_infobar_(false) { |
58 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
59 } | 65 } |
60 | 66 |
61 void PreviewsInfoBarTabHelper::DidFinishNavigation( | 67 void PreviewsInfoBarTabHelper::DidFinishNavigation( |
62 content::NavigationHandle* navigation_handle) { | 68 content::NavigationHandle* navigation_handle) { |
63 // Only show the infobar if this is a full main frame navigation. | 69 // Only show the infobar if this is a full main frame navigation. |
64 if (!navigation_handle->IsInMainFrame() || | 70 if (!navigation_handle->IsInMainFrame() || |
65 !navigation_handle->HasCommitted() || navigation_handle->IsSameDocument()) | 71 !navigation_handle->HasCommitted() || navigation_handle->IsSameDocument()) |
66 return; | 72 return; |
67 displayed_preview_infobar_ = false; | 73 displayed_preview_infobar_ = false; |
| 74 ClearLastNavigationAsync(); |
| 75 committed_data_saver_navigation_id_.reset(); |
| 76 |
| 77 // As documented in content/public/browser/navigation_handle.h, this |
| 78 // NavigationData is a clone of the NavigationData instance returned from |
| 79 // ResourceDispatcherHostDelegate::GetNavigationData during commit. |
| 80 // Because ChromeResourceDispatcherHostDelegate always returns a |
| 81 // ChromeNavigationData, it is safe to static_cast here. |
| 82 ChromeNavigationData* chrome_navigation_data = |
| 83 static_cast<ChromeNavigationData*>( |
| 84 navigation_handle->GetNavigationData()); |
| 85 if (chrome_navigation_data) { |
| 86 data_reduction_proxy::DataReductionProxyData* data = |
| 87 chrome_navigation_data->GetDataReductionProxyData(); |
| 88 |
| 89 if (data && data->page_id()) { |
| 90 committed_data_saver_navigation_id_ = data_reduction_proxy::NavigationID( |
| 91 data->page_id().value(), data->session_key()); |
| 92 } |
| 93 } |
68 | 94 |
69 #if defined(OS_ANDROID) | 95 #if defined(OS_ANDROID) |
70 offline_pages::OfflinePageTabHelper* tab_helper = | 96 offline_pages::OfflinePageTabHelper* tab_helper = |
71 offline_pages::OfflinePageTabHelper::FromWebContents(web_contents()); | 97 offline_pages::OfflinePageTabHelper::FromWebContents(web_contents()); |
72 | 98 |
73 if (tab_helper && tab_helper->IsShowingOfflinePreview()) { | 99 if (tab_helper && tab_helper->IsShowingOfflinePreview()) { |
74 if (navigation_handle->IsErrorPage()) { | 100 if (navigation_handle->IsErrorPage()) { |
75 // TODO(ryansturm): Add UMA for errors. | 101 // TODO(ryansturm): Add UMA for errors. |
76 return; | 102 return; |
77 } | 103 } |
78 data_reduction_proxy::DataReductionProxySettings* | 104 data_reduction_proxy::DataReductionProxySettings* |
79 data_reduction_proxy_settings = | 105 data_reduction_proxy_settings = |
80 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 106 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
81 web_contents()->GetBrowserContext()); | 107 web_contents()->GetBrowserContext()); |
82 PreviewsInfoBarDelegate::Create( | 108 PreviewsInfoBarDelegate::Create( |
83 web_contents(), PreviewsInfoBarDelegate::OFFLINE, | 109 web_contents(), PreviewsInfoBarDelegate::OFFLINE, |
84 data_reduction_proxy_settings && | 110 data_reduction_proxy_settings && |
85 data_reduction_proxy_settings->IsDataReductionProxyEnabled(), | 111 data_reduction_proxy_settings->IsDataReductionProxyEnabled(), |
86 base::Bind( | 112 base::Bind(&AddPreviewNavigationCallback, browser_context_, |
87 &AddPreviewNavigationCallback, web_contents()->GetBrowserContext(), | 113 navigation_handle->GetURL(), |
88 navigation_handle->GetURL(), previews::PreviewsType::OFFLINE)); | 114 previews::PreviewsType::OFFLINE)); |
89 // Don't try to show other infobars if this is an offline preview. | 115 // Don't try to show other infobars if this is an offline preview. |
90 return; | 116 return; |
91 } | 117 } |
92 #endif // defined(OS_ANDROID) | 118 #endif // defined(OS_ANDROID) |
93 | 119 |
94 const net::HttpResponseHeaders* headers = | 120 const net::HttpResponseHeaders* headers = |
95 navigation_handle->GetResponseHeaders(); | 121 navigation_handle->GetResponseHeaders(); |
96 if (headers && data_reduction_proxy::IsLitePagePreview(*headers)) { | 122 if (headers && data_reduction_proxy::IsLitePagePreview(*headers)) { |
97 PreviewsInfoBarDelegate::Create( | 123 PreviewsInfoBarDelegate::Create( |
98 web_contents(), PreviewsInfoBarDelegate::LITE_PAGE, | 124 web_contents(), PreviewsInfoBarDelegate::LITE_PAGE, |
99 true /* is_data_saver_user */, | 125 true /* is_data_saver_user */, |
100 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); | 126 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); |
101 } | 127 } |
102 } | 128 } |
103 | 129 |
| 130 void PreviewsInfoBarTabHelper::ClearLastNavigationAsync() const { |
| 131 if (!committed_data_saver_navigation_id_) |
| 132 return; |
| 133 auto* data_reduction_proxy_settings = |
| 134 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 135 browser_context_); |
| 136 if (!data_reduction_proxy_settings || |
| 137 !data_reduction_proxy_settings->data_reduction_proxy_service()) { |
| 138 return; |
| 139 } |
| 140 data_reduction_proxy_settings->data_reduction_proxy_service() |
| 141 ->pingback_client() |
| 142 ->ClearNavigationKeyAsync(committed_data_saver_navigation_id_.value()); |
| 143 } |
OLD | NEW |