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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_NET_NET_ERROR_TAB_HELPER_H_ 6 #define CHROME_BROWSER_NET_NET_ERROR_TAB_HELPER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/bind.h" 10 #include "base/compiler_specific.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/prefs/pref_member.h" 13 #include "chrome/browser/net/dns_probe_service.h" 14 #include "chrome/common/net/net_error_info.h" 15 #include "content/public/browser/web_contents_observer.h" 16 #include "content/public/browser/web_contents_user_data.h" 17 18 namespace chrome_browser_net { 19 20 // A TabHelper that monitors loads for certain types of network errors and 21 // does interesting things with them. Currently, starts DNS probes using the 22 // DnsProbeService whenever a page fails to load with a DNS-related error. 23 class NetErrorTabHelper 24 : public content::WebContentsObserver, 25 public content::WebContentsUserData<NetErrorTabHelper> { 26 public: 27 enum TestingState { 28 TESTING_DEFAULT, 29 TESTING_FORCE_DISABLED, 30 TESTING_FORCE_ENABLED 31 }; 32 33 typedef base::Callback<void(chrome_common_net::DnsProbeStatus)> 34 DnsProbeStatusSnoopCallback; 35 36 virtual ~NetErrorTabHelper(); 37 38 static void set_state_for_testing(TestingState testing_state); 39 40 // Sets a callback that will be called immediately after the helper sends 41 // a NetErrorHelper IPC. (Used by the DNS probe browser test to know when to 42 // check the error page for updates, instead of polling.) 43 void set_dns_probe_status_snoop_callback_for_testing( 44 const DnsProbeStatusSnoopCallback& dns_probe_status_snoop_callback) { 45 dns_probe_status_snoop_callback_ = dns_probe_status_snoop_callback; 46 } 47 48 // content::WebContentsObserver implementation. 49 virtual void DidStartProvisionalLoadForFrame( 50 int64 frame_id, 51 int64 parent_frame_id, 52 bool is_main_frame, 53 const GURL& validated_url, 54 bool is_error_page, 55 bool is_iframe_srcdoc, 56 content::RenderViewHost* render_view_host) OVERRIDE; 57 58 virtual void DidCommitProvisionalLoadForFrame( 59 int64 frame_id, 60 bool is_main_frame, 61 const GURL& url, 62 content::PageTransition transition_type, 63 content::RenderViewHost* render_view_host) OVERRIDE; 64 65 virtual void DidFailProvisionalLoad( 66 int64 frame_id, 67 bool is_main_frame, 68 const GURL& validated_url, 69 int error_code, 70 const string16& error_description, 71 content::RenderViewHost* render_view_host) OVERRIDE; 72 73 protected: 74 // |contents| is the WebContents of the tab this NetErrorTabHelper is 75 // attached to. 76 explicit NetErrorTabHelper(content::WebContents* contents); 77 virtual void StartDnsProbe(); 78 virtual void SendInfo(); 79 void OnDnsProbeFinished(chrome_common_net::DnsProbeStatus result); 80 81 chrome_common_net::DnsProbeStatus dns_probe_status() const { 82 return dns_probe_status_; 83 } 84 85 private: 86 friend class content::WebContentsUserData<NetErrorTabHelper>; 87 88 void OnMainFrameDnsError(); 89 90 void InitializePref(content::WebContents* contents); 91 bool ProbesAllowed() const; 92 93 base::WeakPtrFactory<NetErrorTabHelper> weak_factory_; 94 95 // True if the last provisional load that started was for an error page. 96 bool is_error_page_; 97 98 // True if the helper has seen a main frame page load fail with a DNS error, 99 // but has not yet seen a new page commit successfully afterwards. 100 bool dns_error_active_; 101 102 // True if the helper has seen an error page commit while |dns_error_active_| 103 // is true. (This should never be true if |dns_error_active_| is false.) 104 bool dns_error_page_committed_; 105 106 // The status of a DNS probe that may or may not have started or finished. 107 // Since the renderer can change out from under the helper (in cross-process 108 // navigations), it re-sends the status whenever an error page commits. 109 chrome_common_net::DnsProbeStatus dns_probe_status_; 110 111 // Whether probes are enabled (by the command-line option or the field trial). 112 const bool probes_enabled_; 113 114 // Optional callback for browser test to snoop on outgoing NetErrorInfo IPCs. 115 DnsProbeStatusSnoopCallback dns_probe_status_snoop_callback_; 116 117 // "Use a web service to resolve navigation errors" preference is required 118 // to allow probes. 119 BooleanPrefMember resolve_errors_with_web_service_; 120 121 DISALLOW_COPY_AND_ASSIGN(NetErrorTabHelper); 122 }; 123 124 } // namespace chrome_browser_net 125 126 #endif // CHROME_BROWSER_NET_NET_ERROR_TAB_HELPER_H_ 127