Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ssl / common_name_mismatch_handler.h
blob5b8067fa7733c26228aeea98ba5e0441c97f34e3
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.
5 #ifndef CHROME_BROWSER_SSL_COMMON_NAME_MISMATCH_HANDLER_H_
6 #define CHROME_BROWSER_SSL_COMMON_NAME_MISMATCH_HANDLER_H_
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"
19 namespace net {
20 class URLFetcher;
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
37 enum TestingState {
38 NOT_TESTING,
39 // Disables the actual request to the |suggested_url|.
40 IGNORE_REQUESTS_FOR_TESTING
43 typedef base::Callback<void(const SuggestedUrlCheckResult& result,
44 const GURL& suggested_url)> CheckUrlCallback;
46 CommonNameMismatchHandler(
47 const GURL& request_url,
48 const scoped_refptr<net::URLRequestContextGetter>& request_context);
49 ~CommonNameMismatchHandler() override;
51 // Performs a network request to suggested URL. After completion, runs the
52 // |callback|.
53 void CheckSuggestedUrl(const GURL& url, const CheckUrlCallback& callback);
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);
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;
71 // Cancels the request to |suggested_url|
72 void Cancel();
74 private:
75 // net::URLFetcherDelegate:
76 void OnURLFetchComplete(const net::URLFetcher* source) override;
78 // Returns true if the check is currently running.
79 bool IsCheckingSuggestedUrl() const;
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_;
87 DISALLOW_COPY_AND_ASSIGN(CommonNameMismatchHandler);
90 #endif // CHROME_BROWSER_SSL_COMMON_NAME_MISMATCH_HANDLER_H_