summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net
diff options
context:
space:
mode:
authorttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-21 20:11:38 +0000
committerttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-21 20:11:38 +0000
commitc73b501991379a6d2ef827dd2e09b0c2074966d2 (patch)
treeb6de9eb7ccad64535c01448bbaa86ee75804743c /chrome/browser/net
parent3aed6a20d2ab4add09e530a095d16a4853653ba3 (diff)
downloadchromium_src-c73b501991379a6d2ef827dd2e09b0c2074966d2.zip
chromium_src-c73b501991379a6d2ef827dd2e09b0c2074966d2.tar.gz
chromium_src-c73b501991379a6d2ef827dd2e09b0c2074966d2.tar.bz2
Check pref before running DNS probes
Right now, the NetErrorTabHelper assumes it's never allowed to run probes. This change checks the "Use web service to resolve navigation errors" pref and allows probes to run if it's checked. BUG=156415 Review URL: https://chromiumcodereview.appspot.com/11418074 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@169109 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/net')
-rw-r--r--chrome/browser/net/net_error_tab_helper.cc48
-rw-r--r--chrome/browser/net/net_error_tab_helper.h17
-rw-r--r--chrome/browser/net/net_error_tab_helper_unittest.cc37
3 files changed, 85 insertions, 17 deletions
diff --git a/chrome/browser/net/net_error_tab_helper.cc b/chrome/browser/net/net_error_tab_helper.cc
index c6d0481..0a85912 100644
--- a/chrome/browser/net/net_error_tab_helper.cc
+++ b/chrome/browser/net/net_error_tab_helper.cc
@@ -8,10 +8,17 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/io_thread.h"
#include "chrome/browser/net/dns_probe_service.h"
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/net_errors.h"
+using content::BrowserContext;
using content::BrowserThread;
+using content::RenderViewHost;
+using content::WebContents;
+using content::WebContentsObserver;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::NetErrorTabHelper)
@@ -19,6 +26,8 @@ namespace chrome_browser_net {
namespace {
+static bool enabled_for_testing_ = true;
+
// Returns whether |net_error| is a DNS-related error (and therefore whether
// the tab helper should start a DNS probe after receiving it.)
bool IsDnsError(int net_error) {
@@ -51,11 +60,30 @@ void StartDnsProbe(
} // namespace
-NetErrorTabHelper::NetErrorTabHelper(content::WebContents* contents)
- : content::WebContentsObserver(contents),
+NetErrorTabHelper::NetErrorTabHelper(WebContents* contents)
+ : WebContentsObserver(contents),
dns_probe_running_(false),
+ pref_initialized_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+
+ InitializePref(contents);
+}
+
+void NetErrorTabHelper::InitializePref(WebContents* contents) {
+ // Unit tests don't pass a WebContents, so the tab helper has no way to get
+ // to the preference. pref_initialized_ will remain false, so ProbesAllowed
+ // will return false without checking the pref.
+ if (!contents)
+ return;
+
+ BrowserContext* browser_context = contents->GetBrowserContext();
+ Profile* profile = Profile::FromBrowserContext(browser_context);
+ resolve_errors_with_web_service_.Init(
+ prefs::kAlternateErrorPagesEnabled,
+ profile->GetPrefs(),
+ NULL /* no observer */);
+ pref_initialized_ = true;
}
NetErrorTabHelper::~NetErrorTabHelper() {
@@ -67,7 +95,7 @@ void NetErrorTabHelper::DidFailProvisionalLoad(
const GURL& validated_url,
int error_code,
const string16& error_description,
- content::RenderViewHost* render_view_host) {
+ RenderViewHost* render_view_host) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Consider running a DNS probe if a main frame load fails with a DNS error
@@ -79,7 +107,7 @@ void NetErrorTabHelper::OnMainFrameDnsError() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Don't start a probe if one is running already or we're not allowed to.
- if (dns_probe_running_ || !DnsProbesAllowedByPref())
+ if (dns_probe_running_ || !ProbesAllowed())
return;
PostStartDnsProbeTask();
@@ -106,10 +134,14 @@ void NetErrorTabHelper::PostStartDnsProbeTask() {
g_browser_process->io_thread()));
}
-bool NetErrorTabHelper::DnsProbesAllowedByPref() const {
- // TODO(ttuttle): Actually check the pref.
- // TODO(ttuttle): Disable on browser_tests and maybe on mobile.
- return false;
+bool NetErrorTabHelper::ProbesAllowed() const {
+ // TODO(ttuttle): Disable on mobile?
+ return (pref_initialized_ && *resolve_errors_with_web_service_)
+ && enabled_for_testing_;
+}
+
+void NetErrorTabHelper::set_enabled_for_testing(bool enabled_for_testing) {
+ enabled_for_testing_ = enabled_for_testing;
}
} // namespace chrome_browser_net
diff --git a/chrome/browser/net/net_error_tab_helper.h b/chrome/browser/net/net_error_tab_helper.h
index 4793bab..2a4c905 100644
--- a/chrome/browser/net/net_error_tab_helper.h
+++ b/chrome/browser/net/net_error_tab_helper.h
@@ -8,6 +8,7 @@
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
+#include "chrome/browser/api/prefs/pref_member.h"
#include "chrome/browser/net/dns_probe_service.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
@@ -34,6 +35,8 @@ class NetErrorTabHelper
void OnDnsProbeFinished(DnsProbeService::Result result);
+ static void set_enabled_for_testing(bool enabled_for_testing);
+
protected:
friend class content::WebContentsUserData<NetErrorTabHelper>;
@@ -44,20 +47,26 @@ class NetErrorTabHelper
// Posts a task to the IO thread that will start a DNS probe.
virtual void PostStartDnsProbeTask();
- // Checks if the "Use web service to resolve navigation errors" preference is
- // enabled on the profile.
- virtual bool DnsProbesAllowedByPref() const;
+ // Checks if probes are allowed by enabled_for_testing and "use web service"
+ // pref.
+ virtual bool ProbesAllowed() const;
bool dns_probe_running() { return dns_probe_running_; }
void set_dns_probe_running(bool running) { dns_probe_running_ = running; }
private:
+ void InitializePref(content::WebContents* contents);
+
void OnMainFrameDnsError();
// Whether the tab helper has started a DNS probe that has not yet returned
// a result.
bool dns_probe_running_;
-
+ // "Use a web service to resolve navigation errors" preference is required
+ // to allow probes.
+ BooleanPrefMember resolve_errors_with_web_service_;
+ // Whether the above pref was initialized -- will be false in unit tests.
+ bool pref_initialized_;
base::WeakPtrFactory<NetErrorTabHelper> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(NetErrorTabHelper);
diff --git a/chrome/browser/net/net_error_tab_helper_unittest.cc b/chrome/browser/net/net_error_tab_helper_unittest.cc
index 86372f4..ab1d0ca5 100644
--- a/chrome/browser/net/net_error_tab_helper_unittest.cc
+++ b/chrome/browser/net/net_error_tab_helper_unittest.cc
@@ -23,29 +23,49 @@ namespace {
class TestNetErrorTabHelper : public NetErrorTabHelper {
public:
TestNetErrorTabHelper()
- : NetErrorTabHelper(NULL), probe_start_count_(0) { }
+ : NetErrorTabHelper(NULL),
+ probe_start_count_(0),
+ probes_allowed_(true) {
+ }
+
+ // NetErrorTabHelper implementation:
virtual void PostStartDnsProbeTask() OVERRIDE {
++probe_start_count_;
}
- virtual bool DnsProbesAllowedByPref() const OVERRIDE { return true; }
+ virtual bool ProbesAllowed() const OVERRIDE {
+ return probes_allowed_;
+ }
+
+ // Methods to control mock behavior and verify things:
+
+ void set_probes_allowed(bool probes_allowed) {
+ probes_allowed_ = probes_allowed;
+ }
void SimulateProbeResult(DnsProbeService::Result result) {
OnDnsProbeFinished(result);
}
- bool dns_probe_running() { return NetErrorTabHelper::dns_probe_running(); }
- int probe_start_count() { return probe_start_count_; }
+ bool dns_probe_running() {
+ return NetErrorTabHelper::dns_probe_running();
+ }
+
+ int probe_start_count() {
+ return probe_start_count_;
+ }
private:
int probe_start_count_;
+ bool probes_allowed_;
};
class NetErrorTabHelperTest : public testing::Test {
public:
NetErrorTabHelperTest()
- : ui_thread_(BrowserThread::UI, &message_loop_) { }
+ : ui_thread_(BrowserThread::UI, &message_loop_) {
+ }
protected:
void SimulateFailure(bool is_main_frame, int error_code) {
@@ -90,6 +110,13 @@ TEST_F(NetErrorTabHelperTest, ProbeOnDnsError) {
EXPECT_EQ(1, tab_helper_.probe_start_count());
}
+TEST_F(NetErrorTabHelperTest, NoProbeWhenNotAllowed) {
+ tab_helper_.set_probes_allowed(false);
+ SimulateFailure(true, net::ERR_NAME_NOT_RESOLVED);
+ EXPECT_FALSE(tab_helper_.dns_probe_running());
+ EXPECT_EQ(0, tab_helper_.probe_start_count());
+}
+
TEST_F(NetErrorTabHelperTest, CoalesceFailures) {
// We should only start one probe for three failures.
SimulateFailure(true, net::ERR_NAME_NOT_RESOLVED);