referrer.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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// This class helps to remember what domains may be needed to be resolved when a 6// navigation takes place to a given URL. This information is gathered when a 7// navigation to a subresource identifies a referring URL. 8// When future navigations take place to known referrer sites, then we 9// speculatively either pre-warm a TCP/IP conneciton, or at a minimum, resolve 10// the host name via DNS. 11 12// All access to this class is performed via the Predictor class, which only 13// operates on the IO thread. 14 15#ifndef CHROME_BROWSER_NET_REFERRER_H_ 16#define CHROME_BROWSER_NET_REFERRER_H_ 17#pragma once 18 19#include <map> 20 21#include "base/basictypes.h" 22#include "base/time.h" 23#include "googleurl/src/gurl.h" 24#include "net/base/host_port_pair.h" 25 26class Value; 27 28namespace chrome_browser_net { 29 30//------------------------------------------------------------------------------ 31// For each hostname in a Referrer, we have a ReferrerValue. It indicates 32// exactly how much value (re: latency reduction, or connection use) has 33// resulted from having this entry. 34class ReferrerValue { 35 public: 36 ReferrerValue(); 37 38 // Used during deserialization. 39 void SetSubresourceUseRate(double rate) { subresource_use_rate_ = rate; } 40 41 base::Time birth_time() const { return birth_time_; } 42 43 // Record the fact that we navigated to the associated subresource URL. This 44 // will increase the value of the expected subresource_use_rate_ 45 void SubresourceIsNeeded(); 46 47 // Record the fact that the referrer of this subresource was observed. This 48 // will diminish the expected subresource_use_rate_ (and will only be 49 // counteracted later if we really needed this subresource as a consequence 50 // of our associated referrer.) 51 void ReferrerWasObserved(); 52 53 int64 navigation_count() const { return navigation_count_; } 54 double subresource_use_rate() const { return subresource_use_rate_; } 55 56 int64 preconnection_count() const { return preconnection_count_; } 57 void IncrementPreconnectionCount() { ++preconnection_count_; } 58 59 int64 preresolution_count() const { return preresolution_count_; } 60 void preresolution_increment() { ++preresolution_count_; } 61 62 // Reduce the latency figure by a factor of 2, and return true if it still has 63 // subresources that could potentially be used. 64 bool Trim(); 65 66 private: 67 const base::Time birth_time_; 68 69 // The number of times this item was navigated to with the fixed referrer. 70 int64 navigation_count_; 71 72 // The number of times this item was preconnected as a consequence of its 73 // referrer. 74 int64 preconnection_count_; 75 76 // The number of times this item was pre-resolved (via DNS) as a consequence 77 // of its referrer. 78 int64 preresolution_count_; 79 80 // A smoothed estimate of the expected number of connections that will be made 81 // to this subresource. 82 double subresource_use_rate_; 83}; 84 85//------------------------------------------------------------------------------ 86// A list of domain names to pre-resolve. The names are the keys to this map, 87// and the values indicate the amount of benefit derived from having each name 88// around. 89typedef std::map<GURL, ReferrerValue> SubresourceMap; 90 91//------------------------------------------------------------------------------ 92// There is one Referrer instance for each hostname that has acted as an HTTP 93// referer (note mispelling is intentional) for a hostname that was otherwise 94// unexpectedly navgated towards ("unexpected" in the sense that the hostname 95// was probably needed as a subresource of a page, and was not otherwise 96// predictable until the content with the reference arrived). Most typically, 97// an outer page was a page fetched by the user, and this instance lists names 98// in SubresourceMap which are subresources and that were needed to complete the 99// rendering of the outer page. 100class Referrer : public SubresourceMap { 101 public: 102 Referrer() : use_count_(1) {} 103 void IncrementUseCount() { ++use_count_; } 104 int64 use_count() const { return use_count_; } 105 106 // Add the indicated url to the list that are resolved via DNS when the user 107 // navigates to this referrer. Note that if the list is long, an entry may be 108 // discarded to make room for this insertion. 109 void SuggestHost(const GURL& url); 110 111 // Trim the Referrer, by first diminishing (scaling down) the subresource 112 // use expectation for each ReferredValue. 113 // Returns true if there are any referring names left. 114 bool Trim(); 115 116 // Provide methods for persisting, and restoring contents into a Value class. 117 Value* Serialize() const; 118 void Deserialize(const Value& referrers); 119 120 static void SetUsePreconnectValuations(bool dns) { 121 use_preconnect_valuations_ = dns; 122 } 123 124 private: 125 // Helper function for pruning list. Metric for usefulness is "large accrued 126 // value," in the form of latency_ savings associated with a host name. We 127 // also give credit for a name being newly added, by scalling latency per 128 // lifetime (time since birth). For instance, when two names have accrued 129 // the same latency_ savings, the older one is less valuable as it didn't 130 // accrue savings as quickly. 131 void DeleteLeastUseful(); 132 133 // The number of times this referer had its subresources scaned for possible 134 // preconnection or DNS preresolution. 135 int64 use_count_; 136 137 // Select between DNS prefetch latency savings, or preconnection valuations 138 // for a metric to decide which referers to save. 139 static bool use_preconnect_valuations_; 140 141 // We put these into a std::map<>, so we need copy constructors. 142 // DISALLOW_COPY_AND_ASSIGN(Referrer); 143 // TODO(jar): Consider optimization to use pointers to these instances, and 144 // avoid deep copies during re-alloc of the containing map. 145}; 146 147} // namespace chrome_browser_net 148 149#endif // CHROME_BROWSER_NET_REFERRER_H_ 150