OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SSL_COMMON_NAME_MISMATCH_HANDLER_H_ |
| 6 #define CHROME_BROWSER_SSL_COMMON_NAME_MISMATCH_HANDLER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/threading/non_thread_safe.h" |
| 13 #include "base/time/time.h" |
| 14 #include "net/url_request/url_fetcher.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace net { |
| 20 class URLFetcher; |
| 21 } |
| 22 |
| 23 // This class handles errors due to common name mismatches |
| 24 // (|ERR_CERT_COMMON_NAME_INVALID|) and helps remediate them by suggesting |
| 25 // alternative URLs that may be what the user intended to load. |
| 26 class CommonNameMismatchHandler : public net::URLFetcherDelegate, |
| 27 public base::NonThreadSafe { |
| 28 public: |
| 29 enum SuggestedUrlCheckResult { |
| 30 // The request succeeds with good response code i.e. URL exists and its |
| 31 // certificate is valid. |
| 32 SUGGESTED_URL_AVAILABLE, |
| 33 // Suggested URL is either not available or has a bad certificate. |
| 34 SUGGESTED_URL_NOT_AVAILABLE |
| 35 }; |
| 36 |
| 37 enum TestingState { |
| 38 NOT_TESTING, |
| 39 // Disables the actual request to the |suggested_url|. |
| 40 IGNORE_REQUESTS_FOR_TESTING |
| 41 }; |
| 42 |
| 43 typedef base::Callback<void(const SuggestedUrlCheckResult& result, |
| 44 const GURL& suggested_url)> CheckUrlCallback; |
| 45 |
| 46 CommonNameMismatchHandler( |
| 47 const GURL& request_url, |
| 48 const scoped_refptr<net::URLRequestContextGetter>& request_context); |
| 49 ~CommonNameMismatchHandler() override; |
| 50 |
| 51 // Performs a network request to suggested URL. After completion, runs the |
| 52 // |callback|. |
| 53 void CheckSuggestedUrl(const GURL& url, const CheckUrlCallback& callback); |
| 54 |
| 55 // Determines if, for |request_url| serving a certificate that is valid for |
| 56 // the domain names |dns_names|, there is a name that the certificate is |
| 57 // valid for that closely matches the original name in |request_url|. If |
| 58 // so, returns true, and sets |*suggested_url| to a URL that is unlikely |
| 59 // to cause an ERR_CERT_COMMON_NAME_INVALID error. |
| 60 static bool GetSuggestedUrl(const GURL& request_url, |
| 61 const std::vector<std::string>& dns_names, |
| 62 GURL* suggested_url); |
| 63 |
| 64 // Used in tests, to disable the request to |suggested_url|. |
| 65 // If |testing_state| is IGNORE_REQUESTS_FOR_TESTING, then the |
| 66 // callback won't get called. |
| 67 static void set_state_for_testing(TestingState testing_state) { |
| 68 testing_state_ = testing_state; |
| 69 } |
| 70 |
| 71 // Cancels the request to |suggested_url| |
| 72 void Cancel(); |
| 73 |
| 74 private: |
| 75 // net::URLFetcherDelegate: |
| 76 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 77 |
| 78 // Returns true if the check is currently running. |
| 79 bool IsCheckingSuggestedUrl() const; |
| 80 |
| 81 static TestingState testing_state_; |
| 82 const GURL request_url_; |
| 83 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 84 CheckUrlCallback check_url_callback_; |
| 85 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(CommonNameMismatchHandler); |
| 88 }; |
| 89 |
| 90 #endif // CHROME_BROWSER_SSL_COMMON_NAME_MISMATCH_HANDLER_H_ |
OLD | NEW |