Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/net/net_error_tab_helper.h" | 5 #include "chrome/browser/net/net_error_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/io_thread.h" | 11 #include "chrome/browser/io_thread.h" |
| 12 #include "chrome/browser/net/dns_probe_service.h" | 12 #include "chrome/browser/net/dns_probe_service.h" |
| 13 #include "chrome/browser/net/net_error_diagnostics_dialog.h" | 13 #include "chrome/browser/net/net_error_diagnostics_dialog.h" |
| 14 #include "chrome/browser/platform_util.h" | 14 #include "chrome/browser/platform_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/localized_error.h" | |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/common/render_messages.h" | 18 #include "chrome/common/render_messages.h" |
| 18 #include "components/error_page/common/net_error_info.h" | 19 #include "components/error_page/common/net_error_info.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/navigation_entry.h" | |
| 22 #include "content/public/browser/navigation_handle.h" | |
| 20 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
| 21 #include "ipc/ipc_message_macros.h" | 24 #include "ipc/ipc_message_macros.h" |
| 22 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 23 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 24 | 27 |
| 25 #if defined(OS_ANDROID) | 28 #if defined(OS_ANDROID) |
| 29 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
| 26 #include "chrome/browser/android/tab_android.h" | 30 #include "chrome/browser/android/tab_android.h" |
| 27 #endif | 31 #include "components/offline_pages/offline_page_feature.h" |
| 32 #include "components/offline_pages/offline_page_item.h" | |
| 33 #include "components/offline_pages/offline_page_model.h" | |
| 34 #endif // defined(OS_ANDROID) | |
| 28 | 35 |
| 29 using content::BrowserContext; | 36 using content::BrowserContext; |
| 30 using content::BrowserThread; | 37 using content::BrowserThread; |
| 31 using content::WebContents; | 38 using content::WebContents; |
| 32 using content::WebContentsObserver; | 39 using content::WebContentsObserver; |
| 33 using error_page::DnsProbeStatus; | 40 using error_page::DnsProbeStatus; |
| 34 using error_page::DnsProbeStatusToString; | 41 using error_page::DnsProbeStatusToString; |
| 42 using error_page::OfflinePageStatus; | |
| 35 using ui::PageTransition; | 43 using ui::PageTransition; |
| 36 | 44 |
| 37 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::NetErrorTabHelper); | 45 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::NetErrorTabHelper); |
| 38 | 46 |
| 39 namespace chrome_browser_net { | 47 namespace chrome_browser_net { |
| 40 | 48 |
| 41 namespace { | 49 namespace { |
| 42 | 50 |
| 43 static NetErrorTabHelper::TestingState testing_state_ = | 51 static NetErrorTabHelper::TestingState testing_state_ = |
| 44 NetErrorTabHelper::TESTING_DEFAULT; | 52 NetErrorTabHelper::TESTING_DEFAULT; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 bool is_error_page, | 125 bool is_error_page, |
| 118 bool is_iframe_srcdoc) { | 126 bool is_iframe_srcdoc) { |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 120 | 128 |
| 121 if (render_frame_host->GetParent()) | 129 if (render_frame_host->GetParent()) |
| 122 return; | 130 return; |
| 123 | 131 |
| 124 is_error_page_ = is_error_page; | 132 is_error_page_ = is_error_page; |
| 125 | 133 |
| 126 #if defined(OS_ANDROID) | 134 #if defined(OS_ANDROID) |
| 127 SetHasOfflinePages(render_frame_host); | 135 SetOfflinePageInfo(render_frame_host, validated_url); |
| 128 #endif | 136 #endif // defined(OS_ANDROID) |
| 129 } | 137 } |
| 130 | 138 |
| 131 void NetErrorTabHelper::DidCommitProvisionalLoadForFrame( | 139 void NetErrorTabHelper::DidCommitProvisionalLoadForFrame( |
| 132 content::RenderFrameHost* render_frame_host, | 140 content::RenderFrameHost* render_frame_host, |
| 133 const GURL& url, | 141 const GURL& url, |
| 134 PageTransition transition_type) { | 142 PageTransition transition_type) { |
| 135 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 143 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 136 | 144 |
| 137 if (render_frame_host->GetParent()) | 145 if (render_frame_host->GetParent()) |
| 138 return; | 146 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 | 178 |
| 171 bool NetErrorTabHelper::OnMessageReceived( | 179 bool NetErrorTabHelper::OnMessageReceived( |
| 172 const IPC::Message& message, | 180 const IPC::Message& message, |
| 173 content::RenderFrameHost* render_frame_host) { | 181 content::RenderFrameHost* render_frame_host) { |
| 174 bool handled = true; | 182 bool handled = true; |
| 175 IPC_BEGIN_MESSAGE_MAP(NetErrorTabHelper, message) | 183 IPC_BEGIN_MESSAGE_MAP(NetErrorTabHelper, message) |
| 176 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RunNetworkDiagnostics, | 184 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RunNetworkDiagnostics, |
| 177 RunNetworkDiagnostics) | 185 RunNetworkDiagnostics) |
| 178 #if defined(OS_ANDROID) | 186 #if defined(OS_ANDROID) |
| 179 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowOfflinePages, ShowOfflinePages) | 187 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowOfflinePages, ShowOfflinePages) |
| 180 #endif | 188 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LoadOfflineCopy, LoadOfflineCopy) |
| 189 #endif // defined(OS_ANDROID) | |
| 181 IPC_MESSAGE_UNHANDLED(handled = false) | 190 IPC_MESSAGE_UNHANDLED(handled = false) |
| 182 IPC_END_MESSAGE_MAP() | 191 IPC_END_MESSAGE_MAP() |
| 183 | 192 |
| 184 return handled; | 193 return handled; |
| 185 } | 194 } |
| 186 | 195 |
| 187 NetErrorTabHelper::NetErrorTabHelper(WebContents* contents) | 196 NetErrorTabHelper::NetErrorTabHelper(WebContents* contents) |
| 188 : WebContentsObserver(contents), | 197 : WebContentsObserver(contents), |
| 189 is_error_page_(false), | 198 is_error_page_(false), |
| 190 dns_error_active_(false), | 199 dns_error_active_(false), |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 // Sanitize URL prior to running diagnostics on it. | 288 // Sanitize URL prior to running diagnostics on it. |
| 280 RunNetworkDiagnosticsHelper(url.GetOrigin().spec()); | 289 RunNetworkDiagnosticsHelper(url.GetOrigin().spec()); |
| 281 } | 290 } |
| 282 | 291 |
| 283 void NetErrorTabHelper::RunNetworkDiagnosticsHelper( | 292 void NetErrorTabHelper::RunNetworkDiagnosticsHelper( |
| 284 const std::string& sanitized_url) { | 293 const std::string& sanitized_url) { |
| 285 ShowNetworkDiagnosticsDialog(web_contents(), sanitized_url); | 294 ShowNetworkDiagnosticsDialog(web_contents(), sanitized_url); |
| 286 } | 295 } |
| 287 | 296 |
| 288 #if defined(OS_ANDROID) | 297 #if defined(OS_ANDROID) |
| 289 void NetErrorTabHelper::SetHasOfflinePages( | 298 void NetErrorTabHelper::SetOfflinePageInfo( |
| 290 content::RenderFrameHost* render_frame_host) { | 299 content::RenderFrameHost* render_frame_host, const GURL& url) { |
| 291 DCHECK(web_contents()); | 300 // Bails out if offline pages not supported. |
| 292 TabAndroid* tab = TabAndroid::FromWebContents(web_contents()); | 301 if (!offline_pages::IsOfflinePagesEnabled()) |
| 293 bool has_offline_pages = tab && tab->HasOfflinePages(); | 302 return; |
|
mmenke
2015/11/23 19:55:22
nit: Suggest blank lines after this return and th
jianli
2015/11/23 22:08:34
Done.
| |
| 303 offline_pages::OfflinePageModel* offline_page_model = | |
| 304 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
| 305 Profile::FromBrowserContext(web_contents()->GetBrowserContext())); | |
| 306 if (!offline_page_model) | |
| 307 return; | |
| 308 OfflinePageStatus status = OfflinePageStatus::NONE; | |
| 309 if (offline_page_model->HasOfflinePages()) | |
|
mmenke
2015/11/23 19:54:26
nit: Use braces.
jianli
2015/11/23 22:08:34
Done.
| |
| 310 status = offline_page_model->GetPageByOnlineURL(url) ? | |
| 311 OfflinePageStatus::HAS_OFFLINE_PAGE : | |
| 312 OfflinePageStatus::HAS_OTHER_OFFLINE_PAGES; | |
| 294 render_frame_host->Send( | 313 render_frame_host->Send( |
| 295 new ChromeViewMsg_SetHasOfflinePages( | 314 new ChromeViewMsg_SetOfflinePageInfo( |
| 296 render_frame_host->GetRoutingID(), | 315 render_frame_host->GetRoutingID(), status)); |
| 297 has_offline_pages)); | |
| 298 } | 316 } |
| 299 | 317 |
| 300 void NetErrorTabHelper::ShowOfflinePages() { | 318 void NetErrorTabHelper::ShowOfflinePages() { |
| 319 // Makes sure that this is coming from from an error page. | |
| 320 if (!IsFromErrorPage()) | |
| 321 return; | |
| 322 | |
| 301 DCHECK(web_contents()); | 323 DCHECK(web_contents()); |
| 302 TabAndroid* tab = TabAndroid::FromWebContents(web_contents()); | 324 TabAndroid* tab = TabAndroid::FromWebContents(web_contents()); |
| 303 if (tab) | 325 if (tab) |
| 304 tab->ShowOfflinePages(); | 326 tab->ShowOfflinePages(); |
| 305 } | 327 } |
| 306 #endif | 328 |
| 329 void NetErrorTabHelper::LoadOfflineCopy(const GURL& url) { | |
| 330 // Makes sure that this is coming from from an error page. | |
| 331 if (!IsFromErrorPage()) | |
| 332 return; | |
| 333 | |
| 334 GURL validated_url(url); | |
|
mmenke
2015/11/23 19:54:26
I don't think you need to make a copy here? can j
jianli
2015/11/23 22:08:34
This is introduced based on nasko's comment:
Since
mmenke
2015/11/23 22:15:14
Sorry, I missed Nasko's comment. I completely to
| |
| 335 if (!validated_url.is_valid()) | |
| 336 return; | |
| 337 | |
| 338 DCHECK(web_contents()); | |
| 339 if (validated_url != web_contents()->GetURL()) | |
| 340 return; | |
| 341 | |
| 342 TabAndroid* tab = TabAndroid::FromWebContents(web_contents()); | |
| 343 if (tab) | |
| 344 tab->LoadOfflineCopy(url); | |
| 345 } | |
| 346 | |
| 347 bool NetErrorTabHelper::IsFromErrorPage() const { | |
| 348 content::NavigationEntry* entry = | |
| 349 web_contents()->GetController().GetLastCommittedEntry(); | |
| 350 if (!entry) | |
| 351 return false; | |
| 352 return entry->GetPageType() == content::PAGE_TYPE_ERROR; | |
| 353 } | |
| 354 | |
| 355 #endif // defined(OS_ANDROID) | |
| 307 | 356 |
| 308 } // namespace chrome_browser_net | 357 } // namespace chrome_browser_net |
| OLD | NEW |