| 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 "content/common/net/url_fetcher.h" | 13 #include "content/public/common/url_fetcher_delegate.h" |
| 14 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 16 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 #include "net/base/host_resolver_proc.h" | 17 #include "net/base/host_resolver_proc.h" |
| 18 #include "net/base/network_change_notifier.h" | 18 #include "net/base/network_change_notifier.h" |
| 19 | 19 |
| 20 class PrefService; | 20 class PrefService; |
| 21 | 21 |
| 22 // This object is responsible for determining whether the user is on a network | 22 // 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, | 23 // 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 | 24 // 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 | 25 // 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 | 26 // 302 redirect to "http://isp.domain.com/search?q=query" in order to display |
| 27 // custom pages on typos, nonexistent sites, etc. | 27 // custom pages on typos, nonexistent sites, etc. |
| 28 // | 28 // |
| 29 // We use this information in the AlternateNavURLFetcher to avoid displaying | 29 // 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 | 30 // 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 | 31 // 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 | 32 // these cases, however, users would be shown a confusing and useless infobar |
| 33 // when they really did mean to do a search. | 33 // when they really did mean to do a search. |
| 34 // | 34 // |
| 35 // Consumers should call RedirectOrigin(), which is guaranteed to synchronously | 35 // 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 | 36 // 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. | 37 // redirection is in place, the returned GURL will be empty. |
| 38 class IntranetRedirectDetector | 38 class IntranetRedirectDetector |
| 39 : public URLFetcher::Delegate, | 39 : public content::URLFetcherDelegate, |
| 40 public net::NetworkChangeNotifier::IPAddressObserver { | 40 public net::NetworkChangeNotifier::IPAddressObserver { |
| 41 public: | 41 public: |
| 42 // Only the main browser process loop should call this, when setting up | 42 // Only the main browser process loop should call this, when setting up |
| 43 // g_browser_process->intranet_redirect_detector_. No code other than the | 43 // g_browser_process->intranet_redirect_detector_. No code other than the |
| 44 // IntranetRedirectDetector itself should actually use | 44 // IntranetRedirectDetector itself should actually use |
| 45 // g_browser_process->intranet_redirect_detector() (which shouldn't be hard, | 45 // g_browser_process->intranet_redirect_detector() (which shouldn't be hard, |
| 46 // since there aren't useful public functions on this object for consumers to | 46 // since there aren't useful public functions on this object for consumers to |
| 47 // access anyway). | 47 // access anyway). |
| 48 IntranetRedirectDetector(); | 48 IntranetRedirectDetector(); |
| 49 virtual ~IntranetRedirectDetector(); | 49 virtual ~IntranetRedirectDetector(); |
| 50 | 50 |
| 51 // Returns the current redirect origin. This will be empty if no redirection | 51 // Returns the current redirect origin. This will be empty if no redirection |
| 52 // is in place. | 52 // is in place. |
| 53 static GURL RedirectOrigin(); | 53 static GURL RedirectOrigin(); |
| 54 | 54 |
| 55 static void RegisterPrefs(PrefService* prefs); | 55 static void RegisterPrefs(PrefService* prefs); |
| 56 | 56 |
| 57 // The number of characters the fetcher will use for its randomly-generated | 57 // The number of characters the fetcher will use for its randomly-generated |
| 58 // hostnames. | 58 // hostnames. |
| 59 static const size_t kNumCharsInHostnames; | 59 static const size_t kNumCharsInHostnames; |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 typedef std::set<URLFetcher*> Fetchers; | 62 typedef std::set<URLFetcher*> Fetchers; |
| 63 | 63 |
| 64 // Called when the seven second startup sleep or the one second network | 64 // Called when the seven second startup sleep or the one second network |
| 65 // switch sleep has finished. Runs any pending fetch. | 65 // switch sleep has finished. Runs any pending fetch. |
| 66 void FinishSleep(); | 66 void FinishSleep(); |
| 67 | 67 |
| 68 // URLFetcher::Delegate | 68 // content::URLFetcherDelegate |
| 69 virtual void OnURLFetchComplete(const URLFetcher* source, | 69 virtual void OnURLFetchComplete(const URLFetcher* source); |
| 70 const GURL& url, | |
| 71 const net::URLRequestStatus& status, | |
| 72 int response_code, | |
| 73 const net::ResponseCookies& cookies, | |
| 74 const std::string& data); | |
| 75 | 70 |
| 76 // NetworkChangeNotifier::IPAddressObserver | 71 // NetworkChangeNotifier::IPAddressObserver |
| 77 virtual void OnIPAddressChanged(); | 72 virtual void OnIPAddressChanged(); |
| 78 | 73 |
| 79 GURL redirect_origin_; | 74 GURL redirect_origin_; |
| 80 ScopedRunnableMethodFactory<IntranetRedirectDetector> fetcher_factory_; | 75 ScopedRunnableMethodFactory<IntranetRedirectDetector> fetcher_factory_; |
| 81 Fetchers fetchers_; | 76 Fetchers fetchers_; |
| 82 std::vector<GURL> resulting_origins_; | 77 std::vector<GURL> resulting_origins_; |
| 83 bool in_sleep_; // True if we're in the seven-second "no fetching" period | 78 bool in_sleep_; // True if we're in the seven-second "no fetching" period |
| 84 // that begins at browser start, or the one-second "no | 79 // that begins at browser start, or the one-second "no |
| 85 // fetching" period that begins after network switches. | 80 // fetching" period that begins after network switches. |
| 86 | 81 |
| 87 DISALLOW_COPY_AND_ASSIGN(IntranetRedirectDetector); | 82 DISALLOW_COPY_AND_ASSIGN(IntranetRedirectDetector); |
| 88 }; | 83 }; |
| 89 | 84 |
| 90 // This is for use in testing, where we don't want our fetches to actually go | 85 // This is for use in testing, where we don't want our fetches to actually go |
| 91 // over the network. It captures the requests and causes them to fail. | 86 // over the network. It captures the requests and causes them to fail. |
| 92 class IntranetRedirectHostResolverProc : public net::HostResolverProc { | 87 class IntranetRedirectHostResolverProc : public net::HostResolverProc { |
| 93 public: | 88 public: |
| 94 explicit IntranetRedirectHostResolverProc(net::HostResolverProc* previous); | 89 explicit IntranetRedirectHostResolverProc(net::HostResolverProc* previous); |
| 95 | 90 |
| 96 virtual int Resolve(const std::string& host, | 91 virtual int Resolve(const std::string& host, |
| 97 net::AddressFamily address_family, | 92 net::AddressFamily address_family, |
| 98 net::HostResolverFlags host_resolver_flags, | 93 net::HostResolverFlags host_resolver_flags, |
| 99 net::AddressList* addrlist, | 94 net::AddressList* addrlist, |
| 100 int* os_error); | 95 int* os_error); |
| 101 }; | 96 }; |
| 102 | 97 |
| 103 #endif // CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ | 98 #endif // CHROME_BROWSER_INTRANET_REDIRECT_DETECTOR_H_ |
| OLD | NEW |