Chromium Code Reviews| 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 static void set_state_for_testing(TestingState testing_state) { | |
|
davidben
2015/08/17 18:59:18
Since this makes you skip the callback, it might b
Bhanu Dev
2015/08/18 05:09:09
Updated the comment. Similar method names and sign
| |
| 66 testing_state_ = testing_state; | |
| 67 } | |
| 68 | |
| 69 // Cancels the request to |suggested_url| | |
| 70 void Cancel(); | |
| 71 | |
| 72 private: | |
| 73 // net::URLFetcherDelegate: | |
| 74 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 75 | |
| 76 // Returns true if the check is currently running. | |
| 77 bool IsCheckingSuggestedUrl() const; | |
| 78 | |
| 79 static TestingState testing_state_; | |
| 80 const GURL request_url_; | |
| 81 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
| 82 CheckUrlCallback check_url_callback_; | |
| 83 scoped_ptr<net::URLFetcher> url_fetcher_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(CommonNameMismatchHandler); | |
| 86 }; | |
| 87 | |
| 88 #endif // CHROME_BROWSER_SSL_COMMON_NAME_MISMATCH_HANDLER_H_ | |
| OLD | NEW |