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 "net/http/http_server_properties_manager.h" | 5 #include "net/http/http_server_properties_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "net/base/ip_address.h" | 17 #include "net/base/ip_address.h" |
18 #include "net/base/port_util.h" | 18 #include "net/base/port_util.h" |
19 #include "net/quic/platform/api/quic_url_utils.h" | 19 #include "net/quic/platform/api/quic_url_utils.h" |
20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // Time to wait before starting an update the http_server_properties_impl_ cache | 26 // Time to wait before starting an update the http_server_properties_impl_ cache |
27 // from preferences. Scheduling another update during this period will be a | 27 // from preferences. Scheduling another update during this period will be a |
28 // no-op. | 28 // no-op. |
29 const int64_t kUpdateCacheDelayMs = 1000; | 29 constexpr base::TimeDelta kUpdateCacheDelay = base::TimeDelta::FromSeconds(1); |
30 | 30 |
31 // Time to wait before starting an update the preferences from the | 31 // Time to wait before starting an update the preferences from the |
32 // http_server_properties_impl_ cache. Scheduling another update during this | 32 // http_server_properties_impl_ cache. Scheduling another update during this |
33 // period will be a no-op. | 33 // period will be a no-op. |
34 const int64_t kUpdatePrefsDelayMs = 60000; | 34 constexpr base::TimeDelta kUpdatePrefsDelay = base::TimeDelta::FromSeconds(60); |
35 | 35 |
36 // "version" 0 indicates, http_server_properties doesn't have "version" | 36 // "version" 0 indicates, http_server_properties doesn't have "version" |
37 // property. | 37 // property. |
38 const int kMissingVersion = 0; | 38 const int kMissingVersion = 0; |
39 | 39 |
40 // The version number of persisted http_server_properties. | 40 // The version number of persisted http_server_properties. |
41 const int kVersionNumber = 5; | 41 const int kVersionNumber = 5; |
42 | 42 |
43 // Persist 200 MRU AlternateProtocolHostPortPairs. | 43 // Persist 200 MRU AlternateProtocolHostPortPairs. |
44 const int kMaxAlternateProtocolHostsToPersist = 200; | 44 const int kMaxAlternateProtocolHostsToPersist = 200; |
(...skipping 24 matching lines...) Expand all Loading... |
69 | 69 |
70 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
71 // HttpServerPropertiesManager | 71 // HttpServerPropertiesManager |
72 | 72 |
73 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {} | 73 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {} |
74 | 74 |
75 HttpServerPropertiesManager::HttpServerPropertiesManager( | 75 HttpServerPropertiesManager::HttpServerPropertiesManager( |
76 PrefDelegate* pref_delegate, | 76 PrefDelegate* pref_delegate, |
77 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, | 77 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, |
78 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) | 78 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) |
79 : pref_task_runner_(pref_task_runner), | 79 : pref_task_runner_(std::move(pref_task_runner)), |
80 pref_delegate_(pref_delegate), | 80 pref_delegate_(pref_delegate), |
81 setting_prefs_(false), | 81 setting_prefs_(false), |
82 is_initialized_(false), | 82 is_initialized_(false), |
83 network_task_runner_(network_task_runner) { | 83 network_task_runner_(std::move(network_task_runner)) { |
| 84 DCHECK(pref_task_runner->RunsTasksOnCurrentThread()); |
84 DCHECK(pref_delegate_); | 85 DCHECK(pref_delegate_); |
85 pref_weak_ptr_factory_.reset( | 86 pref_weak_ptr_factory_.reset( |
86 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 87 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
87 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); | 88 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); |
88 pref_cache_update_timer_.reset(new base::OneShotTimer); | 89 pref_cache_update_timer_.reset(new base::OneShotTimer); |
89 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); | 90 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); |
90 pref_delegate_->StartListeningForUpdates( | 91 pref_delegate_->StartListeningForUpdates( |
91 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, | 92 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, |
92 base::Unretained(this))); | 93 base::Unretained(this))); |
93 } | 94 } |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 return http_server_properties_impl_->SetMaxServerConfigsStoredInProperties( | 360 return http_server_properties_impl_->SetMaxServerConfigsStoredInProperties( |
360 max_server_configs_stored_in_properties); | 361 max_server_configs_stored_in_properties); |
361 } | 362 } |
362 | 363 |
363 bool HttpServerPropertiesManager::IsInitialized() const { | 364 bool HttpServerPropertiesManager::IsInitialized() const { |
364 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 365 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
365 | 366 |
366 return is_initialized_; | 367 return is_initialized_; |
367 } | 368 } |
368 | 369 |
| 370 // static |
| 371 base::TimeDelta HttpServerPropertiesManager::GetUpdateCacheDelayForTesting() { |
| 372 return kUpdateCacheDelay; |
| 373 } |
| 374 |
| 375 // static |
| 376 base::TimeDelta HttpServerPropertiesManager::GetUpdatePrefsDelayForTesting() { |
| 377 return kUpdatePrefsDelay; |
| 378 } |
| 379 |
369 // | 380 // |
370 // Update the HttpServerPropertiesImpl's cache with data from preferences. | 381 // Update the HttpServerPropertiesImpl's cache with data from preferences. |
371 // | 382 // |
372 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread() { | 383 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread() { |
373 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 384 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
374 // Do not schedule a new update if there is already one scheduled. | 385 // Do not schedule a new update if there is already one scheduled. |
375 if (pref_cache_update_timer_->IsRunning()) | 386 if (pref_cache_update_timer_->IsRunning()) |
376 return; | 387 return; |
377 | 388 |
378 pref_cache_update_timer_->Start( | 389 pref_cache_update_timer_->Start( |
379 FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs), this, | 390 FROM_HERE, kUpdateCacheDelay, this, |
380 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread); | 391 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread); |
381 } | 392 } |
382 | 393 |
383 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { | 394 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { |
384 // The preferences can only be read on the pref thread. | 395 // The preferences can only be read on the pref thread. |
385 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 396 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
386 | 397 |
387 if (!pref_delegate_->HasServerProperties()) | 398 if (!pref_delegate_->HasServerProperties()) |
388 return; | 399 return; |
389 | 400 |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 // Update Preferences with data from the cached data. | 796 // Update Preferences with data from the cached data. |
786 // | 797 // |
787 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread( | 798 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread( |
788 Location location) { | 799 Location location) { |
789 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 800 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
790 // Do not schedule a new update if there is already one scheduled. | 801 // Do not schedule a new update if there is already one scheduled. |
791 if (network_prefs_update_timer_->IsRunning()) | 802 if (network_prefs_update_timer_->IsRunning()) |
792 return; | 803 return; |
793 | 804 |
794 network_prefs_update_timer_->Start( | 805 network_prefs_update_timer_->Start( |
795 FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs), this, | 806 FROM_HERE, kUpdatePrefsDelay, this, |
796 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread); | 807 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread); |
797 | 808 |
798 // TODO(rtenneti): Delete the following histogram after collecting some data. | 809 // TODO(rtenneti): Delete the following histogram after collecting some data. |
799 UMA_HISTOGRAM_ENUMERATION("Net.HttpServerProperties.UpdatePrefs", location, | 810 UMA_HISTOGRAM_ENUMERATION("Net.HttpServerProperties.UpdatePrefs", location, |
800 HttpServerPropertiesManager::NUM_LOCATIONS); | 811 HttpServerPropertiesManager::NUM_LOCATIONS); |
801 } | 812 } |
802 | 813 |
803 // This is required so we can set this as the callback for a timer. | 814 // This is required so we can set this as the callback for a timer. |
804 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() { | 815 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() { |
805 UpdatePrefsFromCacheOnNetworkThread(base::Closure()); | 816 UpdatePrefsFromCacheOnNetworkThread(base::Closure()); |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 if (!setting_prefs_) | 1132 if (!setting_prefs_) |
1122 ScheduleUpdateCacheOnPrefThread(); | 1133 ScheduleUpdateCacheOnPrefThread(); |
1123 } | 1134 } |
1124 | 1135 |
1125 void HttpServerPropertiesManager::SetInitialized() { | 1136 void HttpServerPropertiesManager::SetInitialized() { |
1126 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 1137 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
1127 is_initialized_ = true; | 1138 is_initialized_ = true; |
1128 } | 1139 } |
1129 | 1140 |
1130 } // namespace net | 1141 } // namespace net |
OLD | NEW |