NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / net / url_info.h
blobe8e3408739f9026e23bd32be889a10bfdfb6d15c
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.
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.
16 #ifndef CHROME_BROWSER_NET_URL_INFO_H_
17 #define CHROME_BROWSER_NET_URL_INFO_H_
19 #include <string>
20 #include <vector>
22 #include "base/time/time.h"
23 #include "net/base/host_port_pair.h"
24 #include "url/gurl.h"
26 namespace chrome_browser_net {
28 // Use command line switch to enable detailed logging.
29 void EnablePredictorDetailedLog(bool enable);
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.
45 NO_PREFETCH_MOTIVATION, // Browser navigation info (not prefetch related).
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.
54 MAX_MOTIVATED // Beyond all enums, for use in histogram bounding.
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.
67 typedef std::vector<UrlInfo> UrlInfoTable;
69 static base::TimeDelta NullDuration() {
70 return base::TimeDelta::FromMilliseconds(-1);
73 // UrlInfo are usually made by the default constructor during
74 // initializing of the Predictor's map (of info for Hostnames).
75 UrlInfo();
77 ~UrlInfo();
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();
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();
89 // The prefetching lifecycle.
90 void SetQueuedState(ResolutionMotivation motivation);
91 void SetAssignedState();
92 void RemoveFromQueue();
93 void SetPendingDeleteState();
94 void SetFoundState();
95 void SetNoSuchNameState();
97 // Finish initialization. Must only be called once.
98 void SetUrl(const GURL& url);
100 bool was_linked() const { return was_linked_; }
102 GURL referring_url() const { return referring_url_; }
103 void SetReferringHostname(const GURL& url) {
104 referring_url_ = url;
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_;
112 bool is_marked_to_delete() const { return ASSIGNED_BUT_MARKED == state_; }
113 const GURL url() const { return url_; }
115 bool HasUrl(const GURL& url) const {
116 return url_ == url;
119 base::TimeDelta resolve_duration() const { return resolve_duration_;}
120 base::TimeDelta queue_duration() const { return queue_duration_;}
122 void DLogResultsStats(const char* message) const;
124 static void GetHtmlTable(const UrlInfoTable& host_infos,
125 const char* description,
126 bool brief,
127 std::string* output);
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; }
134 private:
135 base::TimeDelta GetDuration() {
136 base::TimeTicks old_time = time_;
137 time_ = base::TimeTicks::Now();
138 return time_ - old_time;
141 // IsStillCached() guesses if the DNS cache still has IP data.
142 bool IsStillCached() const;
144 // Record why we created, or have updated (reqested pre-resolution) of this
145 // instance.
146 void SetMotivation(ResolutionMotivation motivation);
148 // Helper function for about:dns printing.
149 std::string GetAsciiMotivation() const;
151 // The current state of this instance.
152 DnsProcessingState state_;
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_;
158 GURL url_; // Host, port and scheme for this info.
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_;
167 int sequence_number_; // Used to calculate potential of cache eviction.
168 static int sequence_counter; // Used to allocate sequence_number_'s.
170 // Motivation for creation of this instance.
171 ResolutionMotivation motivation_;
173 // Record if the motivation for prefetching was ever a page-link-scan.
174 bool was_linked_;
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_;
181 // We put these objects into a std::map, and hence we
182 // need some "evil" constructors.
183 // DISALLOW_COPY_AND_ASSIGN(UrlInfo);
186 } // namespace chrome_browser_net
188 #endif // CHROME_BROWSER_NET_URL_INFO_H_