OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ | 5 #ifndef CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ |
6 #define CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ | 6 #define CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "chrome/common/net/url_fetcher.h" | 13 #include "chrome/common/net/url_fetcher.h" |
14 #include "chrome/common/notification_observer.h" | |
15 #include "chrome/common/notification_registrar.h" | |
16 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
17 #include "net/base/host_resolver_proc.h" | 15 #include "net/base/host_resolver_proc.h" |
18 #include "net/base/network_change_notifier.h" | 16 #include "net/base/network_change_notifier.h" |
19 | 17 |
20 class PrefService; | 18 class PrefService; |
21 | 19 |
22 // This object is responsible for determining whether the user is on a network | 20 // This object is responsible for determining whether the user is on a network |
23 // that redirects requests for intranet hostnames to another site, and if so, | 21 // that redirects requests for intranet hostnames to another site, and if so, |
24 // tracking what that site is (including across restarts via a pref). For | 22 // tracking what that site is (including across restarts via a pref). For |
25 // example, the user's ISP might convert a request for "http://query/" into a | 23 // example, the user's ISP might convert a request for "http://query/" into a |
26 // 302 redirect to "http://isp.domain.com/search?q=query" in order to display | 24 // 302 redirect to "http://isp.domain.com/search?q=query" in order to display |
27 // custom pages on typos, nonexistent sites, etc. | 25 // custom pages on typos, nonexistent sites, etc. |
28 // | 26 // |
29 // We use this information in the AlternateNavURLFetcher to avoid displaying | 27 // We use this information in the AlternateNavURLFetcher to avoid displaying |
30 // infobars for these cases. Our infobars are designed to allow users to get at | 28 // infobars for these cases. Our infobars are designed to allow users to get at |
31 // intranet sites when they were erroneously taken to a search result page. In | 29 // intranet sites when they were erroneously taken to a search result page. In |
32 // these cases, however, users would be shown a confusing and useless infobar | 30 // these cases, however, users would be shown a confusing and useless infobar |
33 // when they really did mean to do a search. | 31 // when they really did mean to do a search. |
34 // | 32 // |
35 // Consumers should call RedirectOrigin(), which is guaranteed to synchronously | 33 // Consumers should call RedirectOrigin(), which is guaranteed to synchronously |
36 // return a value at all times (even during startup or in unittest mode). If no | 34 // return a value at all times (even during startup or in unittest mode). If no |
37 // redirection is in place, the returned GURL will be empty. | 35 // redirection is in place, the returned GURL will be empty. |
38 class IntranetRedirectDetector | 36 class IntranetRedirectDetector |
39 : public URLFetcher::Delegate, | 37 : public URLFetcher::Delegate, |
40 public NotificationObserver, | |
41 public net::NetworkChangeNotifier::IPAddressObserver { | 38 public net::NetworkChangeNotifier::IPAddressObserver { |
42 public: | 39 public: |
43 // Only the main browser process loop should call this, when setting up | 40 // Only the main browser process loop should call this, when setting up |
44 // g_browser_process->intranet_redirect_detector_. No code other than the | 41 // g_browser_process->intranet_redirect_detector_. No code other than the |
45 // IntranetRedirectDetector itself should actually use | 42 // IntranetRedirectDetector itself should actually use |
46 // g_browser_process->intranet_redirect_detector() (which shouldn't be hard, | 43 // g_browser_process->intranet_redirect_detector() (which shouldn't be hard, |
47 // since there aren't useful public functions on this object for consumers to | 44 // since there aren't useful public functions on this object for consumers to |
48 // access anyway). | 45 // access anyway). |
49 IntranetRedirectDetector(); | 46 IntranetRedirectDetector(); |
50 ~IntranetRedirectDetector(); | 47 ~IntranetRedirectDetector(); |
51 | 48 |
52 // Returns the current redirect origin. This will be empty if no redirection | 49 // Returns the current redirect origin. This will be empty if no redirection |
53 // is in place. | 50 // is in place. |
54 static GURL RedirectOrigin(); | 51 static GURL RedirectOrigin(); |
55 | 52 |
56 static void RegisterPrefs(PrefService* prefs); | 53 static void RegisterPrefs(PrefService* prefs); |
57 | 54 |
58 // The number of characters the fetcher will use for its randomly-generated | 55 // The number of characters the fetcher will use for its randomly-generated |
59 // hostnames. | 56 // hostnames. |
60 static const size_t kNumCharsInHostnames; | 57 static const size_t kNumCharsInHostnames; |
61 | 58 |
62 private: | 59 private: |
63 typedef std::set<URLFetcher*> Fetchers; | 60 typedef std::set<URLFetcher*> Fetchers; |
64 | 61 |
65 // Called when the seven second startup sleep or the one second network | 62 // Called when the seven second startup sleep or the one second network |
66 // switch sleep has finished. Runs any pending fetch. | 63 // switch sleep has finished. Runs any pending fetch. |
67 void FinishSleep(); | 64 void FinishSleep(); |
68 | 65 |
69 // Starts the fetches to determine the redirect URL if we can currently do so. | |
70 void StartFetchesIfPossible(); | |
71 | |
72 // URLFetcher::Delegate | 66 // URLFetcher::Delegate |
73 virtual void OnURLFetchComplete(const URLFetcher* source, | 67 virtual void OnURLFetchComplete(const URLFetcher* source, |
74 const GURL& url, | 68 const GURL& url, |
75 const net::URLRequestStatus& status, | 69 const net::URLRequestStatus& status, |
76 int response_code, | 70 int response_code, |
77 const ResponseCookies& cookies, | 71 const ResponseCookies& cookies, |
78 const std::string& data); | 72 const std::string& data); |
79 | 73 |
80 // NotificationObserver | |
81 virtual void Observe(NotificationType type, | |
82 const NotificationSource& source, | |
83 const NotificationDetails& details); | |
84 | |
85 // NetworkChangeNotifier::IPAddressObserver | 74 // NetworkChangeNotifier::IPAddressObserver |
86 virtual void OnIPAddressChanged(); | 75 virtual void OnIPAddressChanged(); |
87 | 76 |
88 NotificationRegistrar registrar_; | |
89 GURL redirect_origin_; | 77 GURL redirect_origin_; |
90 ScopedRunnableMethodFactory<IntranetRedirectDetector> fetcher_factory_; | 78 ScopedRunnableMethodFactory<IntranetRedirectDetector> fetcher_factory_; |
91 Fetchers fetchers_; | 79 Fetchers fetchers_; |
92 std::vector<GURL> resulting_origins_; | 80 std::vector<GURL> resulting_origins_; |
93 bool in_sleep_; // True if we're in the seven-second "no fetching" period | 81 bool in_sleep_; // True if we're in the seven-second "no fetching" period |
94 // that begins at browser start, or the one-second "no | 82 // that begins at browser start, or the one-second "no |
95 // fetching" period that begins after network switches. | 83 // fetching" period that begins after network switches. |
96 bool request_context_available_; | |
97 // True when the profile has been loaded and the | |
98 // default request context created, so we can | |
99 // actually do the fetch with the right data. | |
100 | 84 |
101 DISALLOW_COPY_AND_ASSIGN(IntranetRedirectDetector); | 85 DISALLOW_COPY_AND_ASSIGN(IntranetRedirectDetector); |
102 }; | 86 }; |
103 | 87 |
104 // This is for use in testing, where we don't want our fetches to actually go | 88 // This is for use in testing, where we don't want our fetches to actually go |
105 // over the network. It captures the requests and causes them to fail. | 89 // over the network. It captures the requests and causes them to fail. |
106 class IntranetRedirectHostResolverProc : public net::HostResolverProc { | 90 class IntranetRedirectHostResolverProc : public net::HostResolverProc { |
107 public: | 91 public: |
108 explicit IntranetRedirectHostResolverProc(net::HostResolverProc* previous); | 92 explicit IntranetRedirectHostResolverProc(net::HostResolverProc* previous); |
109 | 93 |
110 virtual int Resolve(const std::string& host, | 94 virtual int Resolve(const std::string& host, |
111 net::AddressFamily address_family, | 95 net::AddressFamily address_family, |
112 net::HostResolverFlags host_resolver_flags, | 96 net::HostResolverFlags host_resolver_flags, |
113 net::AddressList* addrlist, | 97 net::AddressList* addrlist, |
114 int* os_error); | 98 int* os_error); |
115 }; | 99 }; |
116 | 100 |
117 #endif // CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ | 101 #endif // CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ |
OLD | NEW |