Home | History | Annotate | Download | only in net
      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 // A UrlInfo object is used to store prediction related information about a host
      6 // port and scheme triplet.  When performing DNS pre-resolution of the host/port
      7 // pair, its state is monitored as it is resolved.
      8 // It includes progress, from placement in the Predictor's queue, to resolution
      9 // by the DNS service as either FOUND or NO_SUCH_NAME.  Each instance may also
     10 // hold records of previous resolution times, which might later be shown to be
     11 // savings relative to resolution time during a navigation.
     12 // UrlInfo objects are also used to describe frames, and additional instances
     13 // may describe associated subresources, for future speculative connections to
     14 // those expected subresources.
     15 
     16 #ifndef CHROME_BROWSER_NET_URL_INFO_H_
     17 #define CHROME_BROWSER_NET_URL_INFO_H_
     18 
     19 #include <string>
     20 #include <vector>
     21 
     22 #include "base/time/time.h"
     23 #include "net/base/host_port_pair.h"
     24 #include "url/gurl.h"
     25 
     26 namespace chrome_browser_net {
     27 
     28 // Use command line switch to enable detailed logging.
     29 void EnablePredictorDetailedLog(bool enable);
     30 
     31 class UrlInfo {
     32  public:
     33   // Reasons for a domain to be resolved.
     34   enum ResolutionMotivation {
     35     MOUSE_OVER_MOTIVATED,  // Mouse-over link induced resolution.
     36     PAGE_SCAN_MOTIVATED,   // Scan of rendered page induced resolution.
     37     UNIT_TEST_MOTIVATED,
     38     LINKED_MAX_MOTIVATED,    // enum demarkation above motivation from links.
     39     OMNIBOX_MOTIVATED,       // Omni-box suggested resolving this.
     40     STARTUP_LIST_MOTIVATED,  // Startup list caused this resolution.
     41     EARLY_LOAD_MOTIVATED,    // In some cases we use the prefetcher to warm up
     42                              // the connection in advance of issuing the real
     43                              // request.
     44 
     45     NO_PREFETCH_MOTIVATION,  // Browser navigation info (not prefetch related).
     46 
     47     // The following involve predictive prefetching, triggered by a navigation.
     48     // The referrinrg_url_ is also set when these are used.
     49     // TODO(jar): Support STATIC_REFERAL_MOTIVATED API and integration.
     50     STATIC_REFERAL_MOTIVATED,  // External database suggested this resolution.
     51     LEARNED_REFERAL_MOTIVATED,  // Prior navigation taught us this resolution.
     52     SELF_REFERAL_MOTIVATED,  // Guess about need for a second connection.
     53 
     54     MAX_MOTIVATED  // Beyond all enums, for use in histogram bounding.
     55   };
     56 
     57   enum DnsProcessingState {
     58       // When processed by our prefetching system, the states are:
     59       PENDING,       // Constructor has completed.
     60       QUEUED,        // In name queue but not yet being resolved.
     61       ASSIGNED,      // Being resolved (or being reset to earlier state)
     62       ASSIGNED_BUT_MARKED,  // Needs to be deleted as soon as it's resolved.
     63       FOUND,         // DNS resolution completed.
     64       NO_SUCH_NAME,  // DNS resolution completed.
     65   };
     66 
     67   typedef std::vector<UrlInfo> UrlInfoTable;
     68 
     69   static base::TimeDelta NullDuration() {
     70     return base::TimeDelta::FromMilliseconds(-1);
     71   }
     72 
     73   // UrlInfo are usually made by the default constructor during
     74   // initializing of the Predictor's map (of info for Hostnames).
     75   UrlInfo();
     76 
     77   ~UrlInfo();
     78 
     79   // NeedDnsUpdate decides, based on our internal info,
     80   // if it would be valuable to attempt to update (prefectch)
     81   // DNS data for hostname.  This decision is based
     82   // on how recently we've done DNS prefetching for hostname.
     83   bool NeedsDnsUpdate();
     84 
     85   // FOR TEST ONLY: The following access the otherwise constant values.
     86   static void set_cache_expiration(base::TimeDelta time);
     87   static base::TimeDelta get_cache_expiration();
     88 
     89   // The prefetching lifecycle.
     90   void SetQueuedState(ResolutionMotivation motivation);
     91   void SetAssignedState();
     92   void RemoveFromQueue();
     93   void SetPendingDeleteState();
     94   void SetFoundState();
     95   void SetNoSuchNameState();
     96 
     97   // Finish initialization. Must only be called once.
     98   void SetUrl(const GURL& url);
     99 
    100   bool was_linked() const { return was_linked_; }
    101 
    102   GURL referring_url() const { return referring_url_; }
    103   void SetReferringHostname(const GURL& url) {
    104     referring_url_ = url;
    105   }
    106 
    107   bool was_found() const { return FOUND == state_; }
    108   bool was_nonexistent() const { return NO_SUCH_NAME == state_; }
    109   bool is_assigned() const {
    110     return ASSIGNED == state_ || ASSIGNED_BUT_MARKED == state_;
    111   }
    112   bool is_marked_to_delete() const { return ASSIGNED_BUT_MARKED == state_; }
    113   const GURL url() const { return url_; }
    114 
    115   bool HasUrl(const GURL& url) const {
    116     return url_ == url;
    117   }
    118 
    119   base::TimeDelta resolve_duration() const { return resolve_duration_;}
    120   base::TimeDelta queue_duration() const { return queue_duration_;}
    121 
    122   void DLogResultsStats(const char* message) const;
    123 
    124   static void GetHtmlTable(const UrlInfoTable& host_infos,
    125                            const char* description,
    126                            bool brief,
    127                            std::string* output);
    128 
    129   // For testing, and use in printing tables of info, we sometimes need to
    130   // adjust the time manually.  Usually, this value is maintained by state
    131   // transition, and this call is not made.
    132   void set_time(const base::TimeTicks& time) { time_ = time; }
    133 
    134  private:
    135   base::TimeDelta GetDuration() {
    136     base::TimeTicks old_time = time_;
    137     time_ = base::TimeTicks::Now();
    138     return time_ - old_time;
    139   }
    140 
    141   // IsStillCached() guesses if the DNS cache still has IP data.
    142   bool IsStillCached() const;
    143 
    144   // Record why we created, or have updated (reqested pre-resolution) of this
    145   // instance.
    146   void SetMotivation(ResolutionMotivation motivation);
    147 
    148   // Helper function for about:dns printing.
    149   std::string GetAsciiMotivation() const;
    150 
    151   // The current state of this instance.
    152   DnsProcessingState state_;
    153 
    154   // Record the state prior to going to a queued state, in case we have to back
    155   // out of the queue.
    156   DnsProcessingState old_prequeue_state_;
    157 
    158   GURL url_;  // Host, port and scheme for this info.
    159 
    160   // When was last state changed (usually lookup completed).
    161   base::TimeTicks time_;
    162   // Time needed for DNS to resolve.
    163   base::TimeDelta resolve_duration_;
    164   // Time spent in queue.
    165   base::TimeDelta queue_duration_;
    166 
    167   int sequence_number_;  // Used to calculate potential of cache eviction.
    168   static int sequence_counter;  // Used to allocate sequence_number_'s.
    169 
    170   // Motivation for creation of this instance.
    171   ResolutionMotivation motivation_;
    172 
    173   // Record if the motivation for prefetching was ever a page-link-scan.
    174   bool was_linked_;
    175 
    176   // If this instance holds data about a navigation, we store the referrer.
    177   // If this instance hold data about a prefetch, and the prefetch was
    178   // instigated by a referrer, we store it here (for use in about:dns).
    179   GURL referring_url_;
    180 
    181   // We put these objects into a std::map, and hence we
    182   // need some "evil" constructors.
    183   // DISALLOW_COPY_AND_ASSIGN(UrlInfo);
    184 };
    185 
    186 }  // namespace chrome_browser_net
    187 
    188 #endif  // CHROME_BROWSER_NET_URL_INFO_H_
    189