Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2012 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_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
      6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/containers/hash_tables.h"
     14 #include "base/containers/mru_cache.h"
     15 #include "base/gtest_prod_util.h"
     16 #include "base/threading/non_thread_safe.h"
     17 #include "base/values.h"
     18 #include "net/base/host_port_pair.h"
     19 #include "net/base/net_export.h"
     20 #include "net/http/http_pipelined_host_capability.h"
     21 #include "net/http/http_server_properties.h"
     22 
     23 namespace base {
     24 class ListValue;
     25 }
     26 
     27 namespace net {
     28 
     29 // The implementation for setting/retrieving the HTTP server properties.
     30 class NET_EXPORT HttpServerPropertiesImpl
     31     : public HttpServerProperties,
     32       NON_EXPORTED_BASE(public base::NonThreadSafe) {
     33  public:
     34   HttpServerPropertiesImpl();
     35   virtual ~HttpServerPropertiesImpl();
     36 
     37   // Initializes |spdy_servers_table_| with the servers (host/port) from
     38   // |spdy_servers| that either support SPDY or not.
     39   void InitializeSpdyServers(std::vector<std::string>* spdy_servers,
     40                              bool support_spdy);
     41 
     42   void InitializeAlternateProtocolServers(
     43       AlternateProtocolMap* alternate_protocol_servers);
     44 
     45   void InitializeSpdySettingsServers(SpdySettingsMap* spdy_settings_map);
     46 
     47   // Initializes |pipeline_capability_map_| with the servers (host/port) from
     48   // |pipeline_capability_map| that either support HTTP pipelining or not.
     49   void InitializePipelineCapabilities(
     50       const PipelineCapabilityMap* pipeline_capability_map);
     51 
     52   // Get the list of servers (host/port) that support SPDY.
     53   void GetSpdyServerList(base::ListValue* spdy_server_list) const;
     54 
     55   // Returns flattened string representation of the |host_port_pair|. Used by
     56   // unittests.
     57   static std::string GetFlattenedSpdyServer(
     58       const net::HostPortPair& host_port_pair);
     59 
     60   // Debugging to simulate presence of an AlternateProtocol.
     61   // If we don't have an alternate protocol in the map for any given host/port
     62   // pair, force this ProtocolPortPair.
     63   static void ForceAlternateProtocol(const PortAlternateProtocolPair& pair);
     64   static void DisableForcedAlternateProtocol();
     65 
     66   // Changes the number of host/port pairs we remember pipelining capability
     67   // for. A larger number means we're more likely to be able to pipeline
     68   // immediately if a host is known good, but uses more memory. This function
     69   // can only be called if |pipeline_capability_map_| is empty.
     70   void SetNumPipelinedHostsToRemember(int max_size);
     71 
     72   // -----------------------------
     73   // HttpServerProperties methods:
     74   // -----------------------------
     75 
     76   // Gets a weak pointer for this object.
     77   virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() OVERRIDE;
     78 
     79   // Deletes all data.
     80   virtual void Clear() OVERRIDE;
     81 
     82   // Returns true if |server| supports SPDY.
     83   virtual bool SupportsSpdy(const HostPortPair& server) const OVERRIDE;
     84 
     85   // Add |server| into the persistent store.
     86   virtual void SetSupportsSpdy(const HostPortPair& server,
     87                                bool support_spdy) OVERRIDE;
     88 
     89   // Returns true if |server| has an Alternate-Protocol header.
     90   virtual bool HasAlternateProtocol(const HostPortPair& server) const OVERRIDE;
     91 
     92   // Returns the Alternate-Protocol and port for |server|.
     93   // HasAlternateProtocol(server) must be true.
     94   virtual PortAlternateProtocolPair GetAlternateProtocol(
     95       const HostPortPair& server) const OVERRIDE;
     96 
     97   // Sets the Alternate-Protocol for |server|.
     98   virtual void SetAlternateProtocol(
     99       const HostPortPair& server,
    100       uint16 alternate_port,
    101       AlternateProtocol alternate_protocol) OVERRIDE;
    102 
    103   // Sets the Alternate-Protocol for |server| to be BROKEN.
    104   virtual void SetBrokenAlternateProtocol(const HostPortPair& server) OVERRIDE;
    105 
    106   // Returns all Alternate-Protocol mappings.
    107   virtual const AlternateProtocolMap& alternate_protocol_map() const OVERRIDE;
    108 
    109   // Gets a reference to the SettingsMap stored for a host.
    110   // If no settings are stored, returns an empty SettingsMap.
    111   virtual const SettingsMap& GetSpdySettings(
    112       const HostPortPair& host_port_pair) const OVERRIDE;
    113 
    114   // Saves an individual SPDY setting for a host. Returns true if SPDY setting
    115   // is to be persisted.
    116   virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
    117                               SpdySettingsIds id,
    118                               SpdySettingsFlags flags,
    119                               uint32 value) OVERRIDE;
    120 
    121   // Clears all entries in |spdy_settings_map_| for a host.
    122   virtual void ClearSpdySettings(const HostPortPair& host_port_pair) OVERRIDE;
    123 
    124   // Clears all entries in |spdy_settings_map_|.
    125   virtual void ClearAllSpdySettings() OVERRIDE;
    126 
    127   // Returns all persistent SPDY settings.
    128   virtual const SpdySettingsMap& spdy_settings_map() const OVERRIDE;
    129 
    130   virtual HttpPipelinedHostCapability GetPipelineCapability(
    131       const HostPortPair& origin) OVERRIDE;
    132 
    133   virtual void SetPipelineCapability(
    134       const HostPortPair& origin,
    135       HttpPipelinedHostCapability capability) OVERRIDE;
    136 
    137   virtual void ClearPipelineCapabilities() OVERRIDE;
    138 
    139   virtual PipelineCapabilityMap GetPipelineCapabilityMap() const OVERRIDE;
    140 
    141  private:
    142   typedef base::MRUCache<
    143       HostPortPair, HttpPipelinedHostCapability> CachedPipelineCapabilityMap;
    144   // |spdy_servers_table_| has flattened representation of servers (host/port
    145   // pair) that either support or not support SPDY protocol.
    146   typedef base::hash_map<std::string, bool> SpdyServerHostPortTable;
    147 
    148   SpdyServerHostPortTable spdy_servers_table_;
    149 
    150   AlternateProtocolMap alternate_protocol_map_;
    151   SpdySettingsMap spdy_settings_map_;
    152   scoped_ptr<CachedPipelineCapabilityMap> pipeline_capability_map_;
    153 
    154   base::WeakPtrFactory<HttpServerPropertiesImpl> weak_ptr_factory_;
    155 
    156   DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesImpl);
    157 };
    158 
    159 }  // namespace net
    160 
    161 #endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
    162