[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / intranet_redirect_detector.h
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.
4
5 #ifndef CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_
6 #define CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
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"
19 #include "url/gurl.h"
20
21 namespace network {
22 class SimpleURLLoader;
23 }
24
25 class PrefRegistrySimple;
26
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."
30 #endif
31
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.
38 //
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.
44 //
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 {
51  public:
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
57   // access anyway).
58   IntranetRedirectDetector();
59
60   IntranetRedirectDetector(const IntranetRedirectDetector&) = delete;
61   IntranetRedirectDetector& operator=(const IntranetRedirectDetector&) = delete;
62
63   ~IntranetRedirectDetector() override;
64
65   // Returns the current redirect origin.  This will be empty if no redirection
66   // is in place.
67   static GURL RedirectOrigin();
68
69   static void RegisterPrefs(PrefRegistrySimple* registry);
70
71  private:
72   // Called on connection or config change to ensure detector runs again (after
73   // a delay).
74   void Restart();
75
76   // Called when the startup or restart sleep has finished.  Runs any pending
77   // fetch.
78   void FinishSleep();
79
80   // Invoked from SimpleURLLoader after download is complete.
81   void OnSimpleLoaderComplete(network::SimpleURLLoader* source,
82                               std::unique_ptr<std::string> response_body);
83
84   // NetworkConnectionTracker::NetworkConnectionObserver
85   void OnConnectionChanged(network::mojom::ConnectionType type) override;
86
87   // network::mojom::DnsConfigChangeManagerClient
88   void OnDnsConfigChanged() override;
89
90   void SetupDnsConfigClient();
91   void OnDnsConfigClientConnectionError();
92
93   // Whether the IntranetRedirectDetector is enabled by policy. Disabled by
94   // default.
95   bool IsEnabledByPolicy();
96
97   GURL redirect_origin_;
98   std::map<network::SimpleURLLoader*, std::unique_ptr<network::SimpleURLLoader>>
99       simple_loaders_;
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
104                           // network switches.
105   mojo::Receiver<network::mojom::DnsConfigChangeManagerClient>
106       dns_config_client_receiver_{this};
107   base::WeakPtrFactory<IntranetRedirectDetector> weak_ptr_factory_{this};
108 };
109
110 #endif  // CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_