Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: net/android/network_change_notifier_android.cc

Issue 10905264: Fix race condition in NetworkChangeNotifier on Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Marcus' comment Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/android/network_change_notifier_android.h" 5 #include "net/android/network_change_notifier_android.h"
6 6
7 #include "base/android/jni_android.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/android/jni_android.h"
9 #include "jni/NetworkChangeNotifier_jni.h" 9 #include "jni/NetworkChangeNotifier_jni.h"
10 10
11 namespace net { 11 namespace net {
12 namespace android { 12 namespace android {
13 13
14 NetworkChangeNotifier::NetworkChangeNotifier() { 14 namespace {
15 JNIEnv* env = base::android::AttachCurrentThread(); 15
16 CreateJavaObject(env); 16 // Returns whether the provided connection type is known.
17 bool CheckConnectionType(int connection_type) {
18 switch (connection_type) {
19 case NetworkChangeNotifier::CONNECTION_UNKNOWN:
20 case NetworkChangeNotifier::CONNECTION_ETHERNET:
21 case NetworkChangeNotifier::CONNECTION_WIFI:
22 case NetworkChangeNotifier::CONNECTION_2G:
23 case NetworkChangeNotifier::CONNECTION_3G:
24 case NetworkChangeNotifier::CONNECTION_4G:
25 case NetworkChangeNotifier::CONNECTION_NONE:
26 return true;
27 default:
28 NOTREACHED() << "Unknown connection type received: " << connection_type;
29 return false;
30 }
17 } 31 }
18 32
33 } // namespace
34
19 NetworkChangeNotifier::~NetworkChangeNotifier() { 35 NetworkChangeNotifier::~NetworkChangeNotifier() {
20 JNIEnv* env = base::android::AttachCurrentThread(); 36 JNIEnv* env = base::android::AttachCurrentThread();
21 Java_NetworkChangeNotifier_destroy( 37 Java_NetworkChangeNotifier_destroy(
22 env, java_network_change_notifier_.obj()); 38 env, java_network_change_notifier_.obj());
23 } 39 }
24 40
41 void NetworkChangeNotifier::NotifyObservers(
42 JNIEnv* env, jobject obj, jint new_connection_type) {
43 base::subtle::Release_Store(
44 &connection_type_,
45 CheckConnectionType(new_connection_type) ?
szym 2012/09/14 18:25:23 Do this jint -> int and jint -> AtomicWord implici
46 new_connection_type : CONNECTION_UNKNOWN);
47 NotifyObserversOfConnectionTypeChange();
48 }
49
50 jint NetworkChangeNotifier::GetConnectionType(JNIEnv* env, jobject obj) {
51 return GetCurrentConnectionType();
52 }
53
54 // static
55 bool NetworkChangeNotifier::Register(JNIEnv* env) {
56 return RegisterNativesImpl(env);
57 }
58
59 NetworkChangeNotifier::NetworkChangeNotifier() {
60 JNIEnv* env = base::android::AttachCurrentThread();
61 base::subtle::Release_Store(&connection_type_, CONNECTION_UNKNOWN);
62 CreateJavaObject(env);
63 }
64
25 void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { 65 void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) {
26 java_network_change_notifier_.Reset( 66 java_network_change_notifier_.Reset(
27 Java_NetworkChangeNotifier_create( 67 Java_NetworkChangeNotifier_create(
28 env, 68 env,
29 base::android::GetApplicationContext(), 69 base::android::GetApplicationContext(),
30 reinterpret_cast<jint>(this))); 70 reinterpret_cast<jint>(this)));
31 } 71 }
32 72
33 void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) {
34 NotifyObserversOfConnectionTypeChange();
35 }
36
37 net::NetworkChangeNotifier::ConnectionType 73 net::NetworkChangeNotifier::ConnectionType
38 NetworkChangeNotifier::GetCurrentConnectionType() const { 74 NetworkChangeNotifier::GetCurrentConnectionType() const {
39 JNIEnv* env = base::android::AttachCurrentThread(); 75 return static_cast<net::NetworkChangeNotifier::ConnectionType>(
40 76 base::subtle::Acquire_Load(&connection_type_));
41 // Pull the connection type from the Java-side then convert it to a
42 // native-side NetworkChangeNotifier::ConnectionType.
43 jint connection_type = Java_NetworkChangeNotifier_connectionType(
44 env, java_network_change_notifier_.obj());
45 switch (connection_type) {
46 case CONNECTION_UNKNOWN:
47 return CONNECTION_UNKNOWN;
48 case CONNECTION_ETHERNET:
49 return CONNECTION_ETHERNET;
50 case CONNECTION_WIFI:
51 return CONNECTION_WIFI;
52 case CONNECTION_2G:
53 return CONNECTION_2G;
54 case CONNECTION_3G:
55 return CONNECTION_3G;
56 case CONNECTION_4G:
57 return CONNECTION_4G;
58 case CONNECTION_NONE:
59 return CONNECTION_NONE;
60 default:
61 NOTREACHED() << "Unknown connection type received: " << connection_type;
62 return CONNECTION_NONE;
63 }
64 }
65
66 // static
67 bool NetworkChangeNotifier::Register(JNIEnv* env) {
68 return RegisterNativesImpl(env);
69 } 77 }
70 78
71 } // namespace android 79 } // namespace android
72 } // namespace net 80 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698