1// Copyright 2014 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 CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_COMMON_H_
6#define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_COMMON_H_
7
8#include "base/time/time.h"
9#include "url/gurl.h"
10
11class Profile;
12
13namespace content {
14class WebContents;
15}
16
17namespace predictors {
18
19struct ResourcePrefetchPredictorConfig;
20
21// Returns true if prefetching is enabled. And will initilize the |config|
22// fields to the appropritate values.
23bool IsSpeculativeResourcePrefetchingEnabled(
24    Profile* profile,
25    ResourcePrefetchPredictorConfig* config);
26
27// Represents the type of key based on which prefetch data is stored.
28enum PrefetchKeyType {
29  PREFETCH_KEY_TYPE_HOST,
30  PREFETCH_KEY_TYPE_URL
31};
32
33// Represents a single navigation for a render frame.
34struct NavigationID {
35  // TODO(shishir): Maybe take process_id, frame_id and url as input in
36  // constructor.
37  NavigationID();
38  NavigationID(const NavigationID& other);
39  explicit NavigationID(content::WebContents* web_contents);
40  bool operator<(const NavigationID& rhs) const;
41  bool operator==(const NavigationID& rhs) const;
42
43  bool IsSameRenderer(const NavigationID& other) const;
44
45  // Returns true iff the render_process_id_, render_frame_id_ and
46  // frame_url_ has been set correctly.
47  bool is_valid() const;
48
49  int render_process_id;
50  int render_frame_id;
51  GURL main_frame_url;
52
53  // NOTE: Even though we store the creation time here, it is not used during
54  // comparison of two NavigationIDs because it cannot always be determined
55  // correctly.
56  base::TimeTicks creation_time;
57};
58
59// Represents the config for the resource prefetch prediction algorithm. It is
60// useful for running experiments.
61struct ResourcePrefetchPredictorConfig {
62  // Initializes the config with default values.
63  ResourcePrefetchPredictorConfig();
64  ~ResourcePrefetchPredictorConfig();
65
66  // The mode the prefetcher is running in. Forms a bit map.
67  enum Mode {
68    URL_LEARNING    = 1 << 0,
69    HOST_LEARNING   = 1 << 1,
70    URL_PREFETCHING = 1 << 2,  // Should also turn on URL_LEARNING.
71    HOST_PRFETCHING = 1 << 3   // Should also turn on HOST_LEARNING.
72  };
73  int mode;
74
75  // Helpers to deal with mode.
76  bool IsLearningEnabled() const;
77  bool IsPrefetchingEnabled() const;
78  bool IsURLLearningEnabled() const;
79  bool IsHostLearningEnabled() const;
80  bool IsURLPrefetchingEnabled() const;
81  bool IsHostPrefetchingEnabled() const;
82
83  // If a navigation hasn't seen a load complete event in this much time, it
84  // is considered abandoned.
85  size_t max_navigation_lifetime_seconds;
86
87  // Size of LRU caches for the URL and host data.
88  size_t max_urls_to_track;
89  size_t max_hosts_to_track;
90
91  // The number of times we should have seen a visit to this URL in history
92  // to start tracking it. This is to ensure we don't bother with oneoff
93  // entries. For hosts we track each one.
94  size_t min_url_visit_count;
95
96  // The maximum number of resources to store per entry.
97  size_t max_resources_per_entry;
98  // The number of consecutive misses after we stop tracking a resource URL.
99  size_t max_consecutive_misses;
100
101  // The minimum confidence (accuracy of hits) required for a resource to be
102  // prefetched.
103  float min_resource_confidence_to_trigger_prefetch;
104  // The minimum number of times we must have a URL on record to prefetch it.
105  size_t min_resource_hits_to_trigger_prefetch;
106
107  // Maximum number of prefetches that can be inflight for a single navigation.
108  size_t max_prefetches_inflight_per_navigation;
109  // Maximum number of prefetches that can be inflight for a host for a single
110  // navigation.
111  size_t max_prefetches_inflight_per_host_per_navigation;
112};
113
114}  // namespace predictors
115
116#endif  // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_COMMON_H_
117