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

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: Use a lock rather than atomicops 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
« no previous file with comments | « net/android/network_change_notifier_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 SetConnectionType(
44 CheckConnectionType(new_connection_type) ?
45 new_connection_type : CONNECTION_UNKNOWN);
Ryan Sleevi 2012/09/19 17:43:27 nit: I feel this might read easier with a temporar
Philippe 2012/09/19 17:57:04 Done.
46 NotifyObserversOfConnectionTypeChange();
47 }
48
49 jint NetworkChangeNotifier::GetConnectionType(JNIEnv* env, jobject obj) {
50 return GetCurrentConnectionType();
51 }
52
53 // static
54 bool NetworkChangeNotifier::Register(JNIEnv* env) {
55 return RegisterNativesImpl(env);
56 }
57
58 NetworkChangeNotifier::NetworkChangeNotifier() {
59 SetConnectionType(CONNECTION_UNKNOWN);
60 JNIEnv* env = base::android::AttachCurrentThread();
61 CreateJavaObject(env);
62 }
63
64 void NetworkChangeNotifier::SetConnectionType(int connection_type) {
65 base::AutoLock auto_lock(lock_);
66 connection_type_ = connection_type;
67 }
68
25 void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { 69 void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) {
26 java_network_change_notifier_.Reset( 70 java_network_change_notifier_.Reset(
27 Java_NetworkChangeNotifier_create( 71 Java_NetworkChangeNotifier_create(
28 env, 72 env,
29 base::android::GetApplicationContext(), 73 base::android::GetApplicationContext(),
30 reinterpret_cast<jint>(this))); 74 reinterpret_cast<jint>(this)));
31 } 75 }
32 76
33 void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) {
34 NotifyObserversOfConnectionTypeChange();
35 }
36
37 net::NetworkChangeNotifier::ConnectionType 77 net::NetworkChangeNotifier::ConnectionType
38 NetworkChangeNotifier::GetCurrentConnectionType() const { 78 NetworkChangeNotifier::GetCurrentConnectionType() const {
39 JNIEnv* env = base::android::AttachCurrentThread(); 79 base::AutoLock auto_lock(lock_);
40 80 return static_cast<net::NetworkChangeNotifier::ConnectionType>(
Ryan Sleevi 2012/09/19 17:43:27 No need for net:: on line 77 or here (because of l
Philippe 2012/09/19 17:57:04 Indeed.
41 // Pull the connection type from the Java-side then convert it to a 81 connection_type_);
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 } 82 }
70 83
71 } // namespace android 84 } // namespace android
72 } // namespace net 85 } // namespace net
OLDNEW
« no previous file with comments | « net/android/network_change_notifier_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698