OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "network_change_notifier_util.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/macros.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/run_loop.h" | |
11 #include "jni/NetworkChangeNotifierUtil_jni.h" | |
12 #include "net/base/network_change_notifier.h" | |
13 | |
14 using base::android::JavaParamRef; | |
15 | |
16 namespace cronet { | |
17 | |
18 namespace { | |
19 | |
20 // An IPAddressObserver to test whether Cronet can receive notification | |
21 // when network changes. | |
22 class TestIPAddressObserver | |
23 : public net::NetworkChangeNotifier::IPAddressObserver { | |
24 public: | |
25 TestIPAddressObserver() : ip_address_changed_(false) { | |
26 } | |
27 | |
28 ~TestIPAddressObserver() override {} | |
29 | |
30 // net::NetworkChangeNotifier::IPAddressObserver implementation. | |
31 void OnIPAddressChanged() override { | |
32 ip_address_changed_ = true; | |
33 } | |
34 | |
35 bool ip_address_changed() const { | |
36 return ip_address_changed_; | |
37 } | |
38 | |
39 private: | |
40 bool ip_address_changed_; | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver); | |
43 }; | |
44 | |
45 } // namespace | |
46 | |
47 // Adds a TestIPAddressObserver to the list of IPAddressObservers, and returns | |
48 // a boolean indicating whether the TestIPAddressObserver has received | |
49 // notification when network changes. | |
50 static jboolean IsTestIPAddressObserverCalled( | |
51 JNIEnv* jenv, | |
52 const JavaParamRef<jclass>& jcaller) { | |
53 // This method is called on a Java thread with no MessageLoop, but we need | |
54 // one for the NetworkChangeNotifier to call the observer on. | |
55 base::MessageLoop loop; | |
56 TestIPAddressObserver test_observer; | |
57 net::NetworkChangeNotifier::AddIPAddressObserver(&test_observer); | |
58 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
59 | |
60 base::RunLoop().RunUntilIdle(); | |
61 net::NetworkChangeNotifier::RemoveIPAddressObserver(&test_observer); | |
62 | |
63 return test_observer.ip_address_changed(); | |
64 } | |
65 | |
66 bool RegisterNetworkChangeNotifierUtil(JNIEnv* jenv) { | |
67 return RegisterNativesImpl(jenv); | |
68 } | |
69 | |
70 } // namespace cronet | |
OLD | NEW |