Index: chrome/browser/chromeos/net/network_throttling_observer.cc |
diff --git a/chrome/browser/chromeos/net/network_throttling_observer.cc b/chrome/browser/chromeos/net/network_throttling_observer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9e3f25cdfff918534bd0ee5de3604dde51cbcdfe |
--- /dev/null |
+++ b/chrome/browser/chromeos/net/network_throttling_observer.cc |
@@ -0,0 +1,63 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/net/network_throttling_observer.h" |
+ |
+#include <memory> |
+#include <string> |
+ |
+#include "base/macros.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/sys_info.h" |
+#include "base/values.h" |
+#include "chrome/common/pref_names.h" |
+#include "chromeos/network/network_state_handler.h" |
+#include "components/prefs/pref_member.h" |
+#include "components/prefs/pref_registry_simple.h" |
+#include "components/prefs/pref_service.h" |
+ |
+namespace chromeos { |
+ |
+NetworkThrottlingObserver::NetworkThrottlingObserver(PrefService* local_state) |
+ : local_state_(local_state), weak_ptr_factory_(this) { |
+ pref_change_registrar_.Init(local_state_); |
+ |
+ base::Callback<void(const std::string&)> throttle_callback = |
+ base::Bind(&NetworkThrottlingObserver::OnPreferenceChanged, |
+ weak_ptr_factory_.GetWeakPtr()); |
Andrew T Wilson (Slow)
2016/10/28 14:31:54
Why is this weak ptr factory required? Can the cal
stevenjb
2016/10/28 16:27:09
This was my fault actually, I haven't used PrefCha
|
+ |
+ pref_change_registrar_.Add(prefs::kNetworkThrottlingEnabled, |
+ throttle_callback); |
+} |
+ |
+NetworkThrottlingObserver::~NetworkThrottlingObserver() { |
+ pref_change_registrar_.RemoveAll(); |
+} |
+ |
+void NetworkThrottlingObserver::RegisterPrefs(PrefRegistrySimple* registry) { |
+ registry->RegisterDictionaryPref(prefs::kNetworkThrottlingEnabled); |
+} |
+ |
+void NetworkThrottlingObserver::OnPreferenceChanged( |
+ const std::string& pref_name) { |
+ DCHECK(pref_name == prefs::kNetworkThrottlingEnabled); |
+ |
+ const base::DictionaryValue* throttling_policy = |
+ local_state_->GetDictionary(prefs::kNetworkThrottlingEnabled); |
+ |
+ if (!throttling_policy) |
Andrew T Wilson (Slow)
2016/10/28 14:31:54
What does it mean if throttling_policy is null - d
|
+ return; |
+ |
+ bool enabled; |
+ uint32_t upload_rate, download_rate; |
+ throttling_policy->GetBoolean("enabled", &enabled); |
+ throttling_policy->GetInteger("upload_rate_kbits", |
+ reinterpret_cast<int*>(&upload_rate)); |
Andrew T Wilson (Slow)
2016/10/28 14:31:54
I'm OK with this, but this falls apart if int != 3
stevenjb
2016/10/28 16:27:09
I missed this, +1.
|
+ throttling_policy->GetInteger("download_rate_kbits", |
+ reinterpret_cast<int*>(&download_rate)); |
+ NetworkHandler::Get()->network_state_handler()->SetNetworkThrottlingStatus( |
+ enabled, upload_rate, download_rate); |
+} |
+ |
+} // namespace chromeos |