1 // Copyright 2012 The Chromium Authors
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/memory/weak_ptr.h"
14 #include "build/build_config.h"
15 #include "components/pref_registry/pref_registry_syncable.h"
16 #include "mojo/public/cpp/bindings/receiver.h"
17 #include "services/network/public/cpp/network_connection_tracker.h"
18 #include "services/network/public/mojom/host_resolver.mojom.h"
22 class SimpleURLLoader;
25 class PrefRegistrySimple;
27 #if !(BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || \
28 BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA))
29 #error "IntranetRedirectDetector should only be built on Desktop platforms."
32 // This object is responsible for determining whether the user is on a network
33 // that redirects requests for intranet hostnames to another site, and if so,
34 // tracking what that site is (including across restarts via a pref). For
35 // example, the user's ISP might convert a request for "http://query/" into a
36 // 302 redirect to "http://isp.domain.com/search?q=query" in order to display
37 // custom pages on typos, nonexistent sites, etc.
39 // We use this information in the OmniboxNavigationObserver to avoid displaying
40 // infobars for these cases. Our infobars are designed to allow users to get at
41 // intranet sites when they were erroneously taken to a search result page. In
42 // these cases, however, users would be shown a confusing and useless infobar
43 // when they really did mean to do a search.
45 // Consumers should call RedirectOrigin(), which is guaranteed to synchronously
46 // return a value at all times (even during startup or in unittest mode). If no
47 // redirection is in place, the returned GURL will be empty.
48 class IntranetRedirectDetector
49 : public network::NetworkConnectionTracker::NetworkConnectionObserver,
50 public network::mojom::DnsConfigChangeManagerClient {
52 // Only the main browser process loop should call this, when setting up
53 // g_browser_process->intranet_redirect_detector_. No code other than the
54 // IntranetRedirectDetector itself should actually use
55 // g_browser_process->intranet_redirect_detector() (which shouldn't be hard,
56 // since there aren't useful public functions on this object for consumers to
58 IntranetRedirectDetector();
60 IntranetRedirectDetector(const IntranetRedirectDetector&) = delete;
61 IntranetRedirectDetector& operator=(const IntranetRedirectDetector&) = delete;
63 ~IntranetRedirectDetector() override;
65 // Returns the current redirect origin. This will be empty if no redirection
67 static GURL RedirectOrigin();
69 static void RegisterPrefs(PrefRegistrySimple* registry);
72 // Called on connection or config change to ensure detector runs again (after
76 // Called when the startup or restart sleep has finished. Runs any pending
80 // Invoked from SimpleURLLoader after download is complete.
81 void OnSimpleLoaderComplete(network::SimpleURLLoader* source,
82 std::unique_ptr<std::string> response_body);
84 // NetworkConnectionTracker::NetworkConnectionObserver
85 void OnConnectionChanged(network::mojom::ConnectionType type) override;
87 // network::mojom::DnsConfigChangeManagerClient
88 void OnDnsConfigChanged() override;
90 void SetupDnsConfigClient();
91 void OnDnsConfigClientConnectionError();
93 // Whether the IntranetRedirectDetector is enabled by policy. Disabled by
95 bool IsEnabledByPolicy();
97 GURL redirect_origin_;
98 std::map<network::SimpleURLLoader*, std::unique_ptr<network::SimpleURLLoader>>
100 std::vector<GURL> resulting_origins_;
101 bool in_sleep_ = true; // True if we're in the seven-second "no fetching"
102 // period that begins at browser start, or the
103 // one-second "no fetching" period that begins after
105 mojo::Receiver<network::mojom::DnsConfigChangeManagerClient>
106 dns_config_client_receiver_{this};
107 base::WeakPtrFactory<IntranetRedirectDetector> weak_ptr_factory_{this};
110 #endif // CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_