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/browser/browser_process.h" | |
stevenjb
2016/10/27 22:54:38
This dependency should be removed now?
kirtika1
2016/10/27 23:04:11
Done.
| |
15 #include "chrome/common/pref_names.h" | |
16 #include "chromeos/network/network_state_handler.h" | |
17 #include "components/prefs/pref_member.h" | |
18 #include "components/prefs/pref_registry_simple.h" | |
19 #include "components/prefs/pref_service.h" | |
20 | |
21 namespace chromeos { | |
22 | |
23 NetworkThrottlingObserver::NetworkThrottlingObserver(PrefService* local_state) | |
24 : local_state_(local_state), weak_ptr_factory_(this) { | |
25 pref_change_registrar_.Init(local_state_); | |
26 | |
27 base::Callback<void(const std::string&)> throttle_callback = | |
28 base::Bind(&NetworkThrottlingObserver::OnPreferenceChanged, | |
29 weak_ptr_factory_.GetWeakPtr()); | |
30 | |
31 pref_change_registrar_.Add(prefs::kNetworkThrottlingEnabled, | |
32 throttle_callback); | |
33 } | |
34 | |
35 NetworkThrottlingObserver::~NetworkThrottlingObserver() { | |
36 pref_change_registrar_.RemoveAll(); | |
37 } | |
38 | |
39 void NetworkThrottlingObserver::RegisterPrefs(PrefRegistrySimple* registry) { | |
40 registry->RegisterDictionaryPref(prefs::kNetworkThrottlingEnabled); | |
41 } | |
42 | |
43 void NetworkThrottlingObserver::OnPreferenceChanged( | |
44 const std::string& pref_name) { | |
45 DCHECK(pref_name == prefs::kNetworkThrottlingEnabled); | |
46 | |
47 const base::DictionaryValue* throttling_policy = | |
48 local_state_->GetDictionary(prefs::kNetworkThrottlingEnabled); | |
49 | |
50 if (!throttling_policy) { | |
51 return; | |
52 } | |
stevenjb
2016/10/27 22:54:38
nit: {} not needed
kirtika1
2016/10/27 23:04:11
Done.
| |
53 bool enabled; | |
54 uint32_t upload_rate, download_rate; | |
55 throttling_policy->GetBoolean("enabled", &enabled); | |
56 throttling_policy->GetInteger("upload_rate_kbits", | |
57 reinterpret_cast<int*>(&upload_rate)); | |
58 throttling_policy->GetInteger("download_rate_kbits", | |
59 reinterpret_cast<int*>(&download_rate)); | |
60 NetworkHandler::Get()->network_state_handler()->SetNetworkThrottlingStatus( | |
61 enabled, upload_rate, download_rate); | |
62 } | |
63 | |
64 } // namespace chromeos | |
OLD | NEW |