Chromium Code Reviews| 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..352c9d0b514e50f3d217ca6c471a64460363d696 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/net/network_throttling_observer.cc |
| @@ -0,0 +1,69 @@ |
| +// 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) { |
| + pref_change_registrar_.Init(local_state_); |
| + |
| + base::Callback<void(const std::string&)> throttle_callback = base::Bind( |
| + &NetworkThrottlingObserver::OnPreferenceChanged, base::Unretained(this)); |
| + |
| + pref_change_registrar_.Add(prefs::kNetworkThrottlingEnabled, |
| + throttle_callback); |
| +} |
| + |
| +NetworkThrottlingObserver::~NetworkThrottlingObserver() {} |
| + |
| +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); |
| + |
| + // Default is to disable throttling if the policy is not found. |
| + bool enabled = false; |
| + uint32_t upload_rate = 0, download_rate = 0; |
|
Lei Zhang
2016/10/29 16:04:04
nit: 1 declaration per line please
kirtika1
2016/10/29 21:26:38
Done.
|
| + if (throttling_policy) { |
| + int upload_rate_read, download_rate_read; |
| + |
| + throttling_policy->GetBoolean("enabled", &enabled); |
| + |
| + if (throttling_policy->GetInteger("upload_rate_kbits", &upload_rate_read) && |
| + upload_rate_read > 0) { |
| + upload_rate = upload_rate_read; |
| + } |
| + |
| + if (throttling_policy->GetInteger("download_rate_kbits", |
| + &download_rate_read) && |
| + download_rate_read > 0) { |
| + download_rate = download_rate_read; |
| + } |
| + } |
| + NetworkHandler::Get()->network_state_handler()->SetNetworkThrottlingStatus( |
| + enabled, upload_rate, download_rate); |
| +} |
| + |
| +} // namespace chromeos |