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.
5 #ifndef CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_
6 #define CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "components/pref_registry/pref_registry_syncable.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "mojo/public/cpp/bindings/receiver.h"
19 #include "services/network/public/cpp/network_connection_tracker.h"
20 #include "services/network/public/mojom/host_resolver.mojom.h"
24 class SimpleURLLoader;
27 class PrefRegistrySimple;
29 // This object is responsible for determining whether the user is on a network
30 // that redirects requests for intranet hostnames to another site, and if so,
31 // tracking what that site is (including across restarts via a pref). For
32 // example, the user's ISP might convert a request for "http://query/" into a
33 // 302 redirect to "http://isp.domain.com/search?q=query" in order to display
34 // custom pages on typos, nonexistent sites, etc.
36 // We use this information in the OmniboxNavigationObserver to avoid displaying
37 // infobars for these cases. Our infobars are designed to allow users to get at
38 // intranet sites when they were erroneously taken to a search result page. In
39 // these cases, however, users would be shown a confusing and useless infobar
40 // when they really did mean to do a search.
42 // Consumers should call RedirectOrigin(), which is guaranteed to synchronously
43 // return a value at all times (even during startup or in unittest mode). If no
44 // redirection is in place, the returned GURL will be empty.
45 class IntranetRedirectDetector
46 : public network::NetworkConnectionTracker::NetworkConnectionObserver,
47 public network::mojom::DnsConfigChangeManagerClient {
49 // Only the main browser process loop should call this, when setting up
50 // g_browser_process->intranet_redirect_detector_. No code other than the
51 // IntranetRedirectDetector itself should actually use
52 // g_browser_process->intranet_redirect_detector() (which shouldn't be hard,
53 // since there aren't useful public functions on this object for consumers to
55 IntranetRedirectDetector();
56 ~IntranetRedirectDetector() override;
58 // Returns the current redirect origin. This will be empty if no redirection
60 static GURL RedirectOrigin();
62 static void RegisterPrefs(PrefRegistrySimple* registry);
65 // Called on connection or config change to ensure detector runs again (after
69 // Called when the startup or restart sleep has finished. Runs any pending
73 // Invoked from SimpleURLLoader after download is complete.
74 void OnSimpleLoaderComplete(network::SimpleURLLoader* source,
75 std::unique_ptr<std::string> response_body);
77 // NetworkConnectionTracker::NetworkConnectionObserver
78 void OnConnectionChanged(network::mojom::ConnectionType type) override;
80 // network::mojom::DnsConfigChangeManagerClient
81 void OnDnsConfigChanged() override;
83 void SetupDnsConfigClient();
84 void OnDnsConfigClientConnectionError();
86 // Whether the IntranetRedirectDetector is enabled, or, through policy,
88 bool IsEnabledByPolicy();
90 GURL redirect_origin_;
91 std::map<network::SimpleURLLoader*, std::unique_ptr<network::SimpleURLLoader>>
93 std::vector<GURL> resulting_origins_;
94 bool in_sleep_ = true; // True if we're in the seven-second "no fetching"
95 // period that begins at browser start, or the
96 // one-second "no fetching" period that begins after
98 mojo::Receiver<network::mojom::DnsConfigChangeManagerClient>
99 dns_config_client_receiver_{this};
100 base::WeakPtrFactory<IntranetRedirectDetector> weak_ptr_factory_{this};
102 DISALLOW_COPY_AND_ASSIGN(IntranetRedirectDetector);
105 #endif // CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_