OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "chrome/browser/chromeos/net/network_throttling_observer.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/sys_info.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chromeos/network/network_state_handler.h" |
| 16 #include "components/prefs/pref_member.h" |
| 17 #include "components/prefs/pref_registry_simple.h" |
| 18 #include "components/prefs/pref_service.h" |
| 19 |
| 20 namespace chromeos { |
| 21 |
| 22 NetworkThrottlingObserver::NetworkThrottlingObserver(PrefService* local_state) |
| 23 : local_state_(local_state) { |
| 24 pref_change_registrar_.Init(local_state_); |
| 25 |
| 26 base::Callback<void(const std::string&)> throttle_callback = base::Bind( |
| 27 &NetworkThrottlingObserver::OnPreferenceChanged, base::Unretained(this)); |
| 28 |
| 29 pref_change_registrar_.Add(prefs::kNetworkThrottlingEnabled, |
| 30 throttle_callback); |
| 31 } |
| 32 |
| 33 NetworkThrottlingObserver::~NetworkThrottlingObserver() {} |
| 34 |
| 35 void NetworkThrottlingObserver::RegisterPrefs(PrefRegistrySimple* registry) { |
| 36 registry->RegisterDictionaryPref(prefs::kNetworkThrottlingEnabled); |
| 37 } |
| 38 |
| 39 void NetworkThrottlingObserver::OnPreferenceChanged( |
| 40 const std::string& pref_name) { |
| 41 DCHECK(pref_name == prefs::kNetworkThrottlingEnabled); |
| 42 |
| 43 const base::DictionaryValue* throttling_policy = |
| 44 local_state_->GetDictionary(prefs::kNetworkThrottlingEnabled); |
| 45 |
| 46 // Default is to disable throttling if the policy is not found. |
| 47 bool enabled = false; |
| 48 uint32_t upload_rate = 0, download_rate = 0; |
| 49 if (throttling_policy) { |
| 50 int upload_rate_read = 0; |
| 51 int download_rate_read = 0; |
| 52 |
| 53 throttling_policy->GetBoolean("enabled", &enabled); |
| 54 |
| 55 if (throttling_policy->GetInteger("upload_rate_kbits", &upload_rate_read) && |
| 56 upload_rate_read > 0) { |
| 57 upload_rate = upload_rate_read; |
| 58 } |
| 59 |
| 60 if (throttling_policy->GetInteger("download_rate_kbits", |
| 61 &download_rate_read) && |
| 62 download_rate_read > 0) { |
| 63 download_rate = download_rate_read; |
| 64 } |
| 65 } |
| 66 NetworkHandler::Get()->network_state_handler()->SetNetworkThrottlingStatus( |
| 67 enabled, upload_rate, download_rate); |
| 68 } |
| 69 |
| 70 } // namespace chromeos |
OLD | NEW |