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