Home | History | Annotate | Download | only in url_request
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef NET_BASE_HTTPS_PROBER_H_
      6 #define NET_BASE_HTTPS_PROBER_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <set>
     11 #include <string>
     12 
     13 #include "base/task.h"
     14 #include "net/url_request/url_request.h"
     15 
     16 template <typename T> struct DefaultSingletonTraits;
     17 
     18 namespace net {
     19 
     20 class URLRequestContext;
     21 
     22 // This should be scoped inside HTTPSProber, but VC cannot compile
     23 // HTTPProber::Delegate when HTTPSProber also inherits from
     24 // URLRequest::Delegate.
     25 class HTTPSProberDelegate {
     26  public:
     27   virtual void ProbeComplete(bool result) = 0;
     28  protected:
     29   virtual ~HTTPSProberDelegate() {}
     30 };
     31 
     32 // HTTPSProber is a singleton object that manages HTTPS probes. A HTTPS probe
     33 // determines if we can connect to a given host over HTTPS. It's used when
     34 // transparently upgrading from HTTP to HTTPS (for example, for SPDY).
     35 class HTTPSProber : public URLRequest::Delegate {
     36  public:
     37   // Returns the singleton instance.
     38   static HTTPSProber* GetInstance();
     39 
     40   // HaveProbed returns true if the given host is known to have been probed
     41   // since the browser was last started.
     42   bool HaveProbed(const std::string& host) const;
     43 
     44   // InFlight returns true iff a probe for the given host is currently active.
     45   bool InFlight(const std::string& host) const;
     46 
     47   // ProbeHost starts a new probe for the given host. If the host is known to
     48   // have been probed since the browser was started, false is returned and no
     49   // other action is taken. If a probe to the given host in currently inflight,
     50   // false will be returned, and no other action is taken. Otherwise, a new
     51   // probe is started, true is returned and the Delegate will be called with the
     52   // results (true means a successful handshake).
     53   bool ProbeHost(const std::string& host, URLRequestContext* ctx,
     54                  HTTPSProberDelegate* delegate);
     55 
     56   // Implementation of URLRequest::Delegate
     57   virtual void OnAuthRequired(URLRequest* request,
     58                               AuthChallengeInfo* auth_info);
     59   virtual void OnSSLCertificateError(URLRequest* request,
     60                                      int cert_error,
     61                                      X509Certificate* cert);
     62   virtual void OnResponseStarted(URLRequest* request);
     63   virtual void OnReadCompleted(URLRequest* request, int bytes_read);
     64 
     65  private:
     66   friend struct DefaultSingletonTraits<HTTPSProber>;
     67 
     68   HTTPSProber();
     69   ~HTTPSProber();
     70 
     71   void Success(URLRequest* request);
     72   void Failure(URLRequest* request);
     73   void DoCallback(URLRequest* request, bool result);
     74 
     75   std::map<std::string, HTTPSProberDelegate*> inflight_probes_;
     76   std::set<std::string> probed_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(HTTPSProber);
     79 };
     80 
     81 }  // namespace net
     82 
     83 #endif  // NET_BASE_HTTPS_PROBER_H_
     84