| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/predictors/resource_prefetch_common.h" | 5 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <tuple> | 8 #include <tuple> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "chrome/browser/net/prediction_options.h" | 11 #include "chrome/browser/net/prediction_options.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 | 19 |
| 20 namespace predictors { | 20 namespace predictors { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 constexpr int kLearningMode = ResourcePrefetchPredictorConfig::URL_LEARNING | | 24 bool IsPrefetchingEnabledInternal(Profile* profile, int mode, int mask) { |
| 25 ResourcePrefetchPredictorConfig::HOST_LEARNING; | 25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 26 constexpr int kPrefetchingMode = | 26 if ((mode & mask) == 0) |
| 27 ResourcePrefetchPredictorConfig::URL_PREFETCHING | | 27 return false; |
| 28 ResourcePrefetchPredictorConfig::HOST_PREFETCHING; | 28 |
| 29 if (!profile || !profile->GetPrefs() || |
| 30 chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs()) != |
| 31 chrome_browser_net::NetworkPredictionStatus::ENABLED) { |
| 32 return false; |
| 33 } |
| 34 |
| 35 return true; |
| 36 } |
| 29 | 37 |
| 30 } // namespace | 38 } // namespace |
| 31 | 39 |
| 32 bool IsSpeculativeResourcePrefetchingEnabled( | 40 bool IsSpeculativeResourcePrefetchingEnabled( |
| 33 Profile* profile, | 41 Profile* profile, |
| 34 ResourcePrefetchPredictorConfig* config) { | 42 ResourcePrefetchPredictorConfig* config) { |
| 35 DCHECK(config); | 43 DCHECK(config); |
| 36 | 44 |
| 37 // Off the record - disabled. | 45 // Off the record - disabled. |
| 38 if (!profile || profile->IsOffTheRecord()) | 46 if (!profile || profile->IsOffTheRecord()) |
| 39 return false; | 47 return false; |
| 40 | 48 |
| 41 // Enabled by command line switch. The config has the default params already | 49 // Enabled by command line switch. The config has the default params already |
| 42 // set. The command line with just enable them with the default params. | 50 // set. The command line with just enable them with the default params. |
| 43 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 51 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 44 switches::kSpeculativeResourcePrefetching)) { | 52 switches::kSpeculativeResourcePrefetching)) { |
| 45 const std::string value = | 53 const std::string value = |
| 46 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 54 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 47 switches::kSpeculativeResourcePrefetching); | 55 switches::kSpeculativeResourcePrefetching); |
| 48 | 56 |
| 49 if (value == switches::kSpeculativeResourcePrefetchingDisabled) { | 57 if (value == switches::kSpeculativeResourcePrefetchingDisabled) { |
| 50 return false; | 58 return false; |
| 51 } else if (value == switches::kSpeculativeResourcePrefetchingLearning) { | 59 } else if (value == switches::kSpeculativeResourcePrefetchingLearning) { |
| 52 config->mode |= kLearningMode; | 60 config->mode |= ResourcePrefetchPredictorConfig::LEARNING; |
| 61 return true; |
| 62 } else if (value == |
| 63 switches::kSpeculativeResourcePrefetchingEnabledExternal) { |
| 64 config->mode |= ResourcePrefetchPredictorConfig::LEARNING | |
| 65 ResourcePrefetchPredictorConfig::PREFETCHING_FOR_EXTERNAL; |
| 53 return true; | 66 return true; |
| 54 } else if (value == switches::kSpeculativeResourcePrefetchingEnabled) { | 67 } else if (value == switches::kSpeculativeResourcePrefetchingEnabled) { |
| 55 config->mode |= kLearningMode | kPrefetchingMode; | 68 config->mode |= |
| 69 ResourcePrefetchPredictorConfig::LEARNING | |
| 70 ResourcePrefetchPredictorConfig::PREFETCHING_FOR_NAVIGATION | |
| 71 ResourcePrefetchPredictorConfig::PREFETCHING_FOR_EXTERNAL; |
| 56 return true; | 72 return true; |
| 57 } | 73 } |
| 58 } | 74 } |
| 59 | 75 |
| 60 return false; | 76 return false; |
| 61 } | 77 } |
| 62 | 78 |
| 63 NavigationID::NavigationID() | 79 NavigationID::NavigationID() |
| 64 : render_process_id(-1), | 80 : render_process_id(-1), |
| 65 render_frame_id(-1) { | 81 render_frame_id(-1) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 max_prefetches_inflight_per_host_per_navigation(3) { | 137 max_prefetches_inflight_per_host_per_navigation(3) { |
| 122 } | 138 } |
| 123 | 139 |
| 124 ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig( | 140 ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig( |
| 125 const ResourcePrefetchPredictorConfig& other) = default; | 141 const ResourcePrefetchPredictorConfig& other) = default; |
| 126 | 142 |
| 127 ResourcePrefetchPredictorConfig::~ResourcePrefetchPredictorConfig() { | 143 ResourcePrefetchPredictorConfig::~ResourcePrefetchPredictorConfig() { |
| 128 } | 144 } |
| 129 | 145 |
| 130 bool ResourcePrefetchPredictorConfig::IsLearningEnabled() const { | 146 bool ResourcePrefetchPredictorConfig::IsLearningEnabled() const { |
| 131 return IsURLLearningEnabled() || IsHostLearningEnabled(); | 147 return (mode & LEARNING) > 0; |
| 132 } | 148 } |
| 133 | 149 |
| 134 bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabled( | 150 bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabledForSomeOrigin( |
| 135 Profile* profile) const { | 151 Profile* profile) const { |
| 136 return IsURLPrefetchingEnabled(profile) || IsHostPrefetchingEnabled(profile); | 152 int mask = PREFETCHING_FOR_NAVIGATION | PREFETCHING_FOR_EXTERNAL; |
| 153 return IsPrefetchingEnabledInternal(profile, mode, mask); |
| 137 } | 154 } |
| 138 | 155 |
| 139 bool ResourcePrefetchPredictorConfig::IsURLLearningEnabled() const { | 156 bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabledForOrigin( |
| 140 return (mode & URL_LEARNING) > 0; | 157 Profile* profile, |
| 141 } | 158 PrefetchOrigin origin) const { |
| 142 | 159 int mask = 0; |
| 143 bool ResourcePrefetchPredictorConfig::IsHostLearningEnabled() const { | 160 switch (origin) { |
| 144 return (mode & HOST_LEARNING) > 0; | 161 case PrefetchOrigin::NAVIGATION: |
| 145 } | 162 mask = PREFETCHING_FOR_NAVIGATION; |
| 146 | 163 break; |
| 147 bool ResourcePrefetchPredictorConfig::IsURLPrefetchingEnabled( | 164 case PrefetchOrigin::EXTERNAL: |
| 148 Profile* profile) const { | 165 mask = PREFETCHING_FOR_EXTERNAL; |
| 149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 166 break; |
| 150 if (!profile || !profile->GetPrefs() || | |
| 151 chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs()) != | |
| 152 chrome_browser_net::NetworkPredictionStatus::ENABLED) { | |
| 153 return false; | |
| 154 } | 167 } |
| 155 return (mode & URL_PREFETCHING) > 0; | 168 return IsPrefetchingEnabledInternal(profile, mode, mask); |
| 156 } | |
| 157 | |
| 158 bool ResourcePrefetchPredictorConfig::IsHostPrefetchingEnabled( | |
| 159 Profile* profile) const { | |
| 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 161 if (!profile || !profile->GetPrefs() || | |
| 162 chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs()) != | |
| 163 chrome_browser_net::NetworkPredictionStatus::ENABLED) { | |
| 164 return false; | |
| 165 } | |
| 166 return (mode & HOST_PREFETCHING) > 0; | |
| 167 } | 169 } |
| 168 | 170 |
| 169 bool ResourcePrefetchPredictorConfig::IsLowConfidenceForTest() const { | 171 bool ResourcePrefetchPredictorConfig::IsLowConfidenceForTest() const { |
| 170 return min_url_visit_count == 1 && | 172 return min_url_visit_count == 1 && |
| 171 std::abs(min_resource_confidence_to_trigger_prefetch - 0.5f) < 1e-6 && | 173 std::abs(min_resource_confidence_to_trigger_prefetch - 0.5f) < 1e-6 && |
| 172 min_resource_hits_to_trigger_prefetch == 1; | 174 min_resource_hits_to_trigger_prefetch == 1; |
| 173 } | 175 } |
| 174 | 176 |
| 175 bool ResourcePrefetchPredictorConfig::IsHighConfidenceForTest() const { | 177 bool ResourcePrefetchPredictorConfig::IsHighConfidenceForTest() const { |
| 176 return min_url_visit_count == 3 && | 178 return min_url_visit_count == 3 && |
| 177 std::abs(min_resource_confidence_to_trigger_prefetch - 0.9f) < 1e-6 && | 179 std::abs(min_resource_confidence_to_trigger_prefetch - 0.9f) < 1e-6 && |
| 178 min_resource_hits_to_trigger_prefetch == 3; | 180 min_resource_hits_to_trigger_prefetch == 3; |
| 179 } | 181 } |
| 180 | 182 |
| 181 bool ResourcePrefetchPredictorConfig::IsMoreResourcesEnabledForTest() const { | 183 bool ResourcePrefetchPredictorConfig::IsMoreResourcesEnabledForTest() const { |
| 182 return max_resources_per_entry == 100; | 184 return max_resources_per_entry == 100; |
| 183 } | 185 } |
| 184 | 186 |
| 185 bool ResourcePrefetchPredictorConfig::IsSmallDBEnabledForTest() const { | 187 bool ResourcePrefetchPredictorConfig::IsSmallDBEnabledForTest() const { |
| 186 return max_urls_to_track == 200 && max_hosts_to_track == 100; | 188 return max_urls_to_track == 200 && max_hosts_to_track == 100; |
| 187 } | 189 } |
| 188 | 190 |
| 189 } // namespace predictors | 191 } // namespace predictors |
| OLD | NEW |