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), weak_ptr_factory_(this) { | |
24 pref_change_registrar_.Init(local_state_); | |
25 | |
26 base::Callback<void(const std::string&)> throttle_callback = | |
27 base::Bind(&NetworkThrottlingObserver::OnPreferenceChanged, | |
28 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
| |
29 | |
30 pref_change_registrar_.Add(prefs::kNetworkThrottlingEnabled, | |
31 throttle_callback); | |
32 } | |
33 | |
34 NetworkThrottlingObserver::~NetworkThrottlingObserver() { | |
35 pref_change_registrar_.RemoveAll(); | |
36 } | |
37 | |
38 void NetworkThrottlingObserver::RegisterPrefs(PrefRegistrySimple* registry) { | |
39 registry->RegisterDictionaryPref(prefs::kNetworkThrottlingEnabled); | |
40 } | |
41 | |
42 void NetworkThrottlingObserver::OnPreferenceChanged( | |
43 const std::string& pref_name) { | |
44 DCHECK(pref_name == prefs::kNetworkThrottlingEnabled); | |
45 | |
46 const base::DictionaryValue* throttling_policy = | |
47 local_state_->GetDictionary(prefs::kNetworkThrottlingEnabled); | |
48 | |
49 if (!throttling_policy) | |
Andrew T Wilson (Slow)
2016/10/28 14:31:54
What does it mean if throttling_policy is null - d
| |
50 return; | |
51 | |
52 bool enabled; | |
53 uint32_t upload_rate, download_rate; | |
54 throttling_policy->GetBoolean("enabled", &enabled); | |
55 throttling_policy->GetInteger("upload_rate_kbits", | |
56 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.
| |
57 throttling_policy->GetInteger("download_rate_kbits", | |
58 reinterpret_cast<int*>(&download_rate)); | |
59 NetworkHandler::Get()->network_state_handler()->SetNetworkThrottlingStatus( | |
60 enabled, upload_rate, download_rate); | |
61 } | |
62 | |
63 } // namespace chromeos | |
OLD | NEW |