| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/android/feedback/connectivity_checker.h" | 5 #include "chrome/browser/android/feedback/connectivity_checker.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 // GENERATED_JAVA_PREFIX_TO_STRIP: CONNECTIVITY_CHECK_RESULT_ | 34 // GENERATED_JAVA_PREFIX_TO_STRIP: CONNECTIVITY_CHECK_RESULT_ |
| 35 enum ConnectivityCheckResult { | 35 enum ConnectivityCheckResult { |
| 36 CONNECTIVITY_CHECK_RESULT_UNKNOWN = 0, | 36 CONNECTIVITY_CHECK_RESULT_UNKNOWN = 0, |
| 37 CONNECTIVITY_CHECK_RESULT_CONNECTED = 1, | 37 CONNECTIVITY_CHECK_RESULT_CONNECTED = 1, |
| 38 CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED = 2, | 38 CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED = 2, |
| 39 CONNECTIVITY_CHECK_RESULT_TIMEOUT = 3, | 39 CONNECTIVITY_CHECK_RESULT_TIMEOUT = 3, |
| 40 CONNECTIVITY_CHECK_RESULT_ERROR = 4, | 40 CONNECTIVITY_CHECK_RESULT_ERROR = 4, |
| 41 CONNECTIVITY_CHECK_RESULT_END = 5 | 41 CONNECTIVITY_CHECK_RESULT_END = 5 |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 void ExecuteCallback(jobject callback, ConnectivityCheckResult result) { | 44 void ExecuteCallback(const base::android::JavaRef<jobject>& callback, |
| 45 ConnectivityCheckResult result) { |
| 45 CHECK(result >= CONNECTIVITY_CHECK_RESULT_UNKNOWN); | 46 CHECK(result >= CONNECTIVITY_CHECK_RESULT_UNKNOWN); |
| 46 CHECK(result < CONNECTIVITY_CHECK_RESULT_END); | 47 CHECK(result < CONNECTIVITY_CHECK_RESULT_END); |
| 47 Java_ConnectivityChecker_executeCallback(base::android::AttachCurrentThread(), | 48 Java_ConnectivityChecker_executeCallback(base::android::AttachCurrentThread(), |
| 48 callback, result); | 49 callback, result); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void ExecuteCallbackFromRef( | |
| 52 base::android::ScopedJavaGlobalRef<jobject>* callback, | |
| 53 ConnectivityCheckResult result) { | |
| 54 ExecuteCallback(callback->obj(), result); | |
| 55 } | |
| 56 | |
| 57 void PostCallback(JNIEnv* env, | 52 void PostCallback(JNIEnv* env, |
| 58 jobject j_callback, | 53 const base::android::JavaRef<jobject>& j_callback, |
| 59 ConnectivityCheckResult result) { | 54 ConnectivityCheckResult result) { |
| 60 base::ThreadTaskRunnerHandle::Get()->PostTask( | 55 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 61 FROM_HERE, | 56 FROM_HERE, |
| 62 base::Bind(&ExecuteCallbackFromRef, | 57 base::Bind(&ExecuteCallback, |
| 63 base::Owned(new base::android::ScopedJavaGlobalRef<jobject>( | 58 base::android::ScopedJavaGlobalRef<jobject>(j_callback), |
| 64 env, j_callback)), | |
| 65 result)); | 59 result)); |
| 66 } | 60 } |
| 67 | 61 |
| 68 // A utility class for checking if the device is currently connected to the | 62 // A utility class for checking if the device is currently connected to the |
| 69 // Internet. | 63 // Internet. |
| 70 class ConnectivityChecker : public net::URLFetcherDelegate { | 64 class ConnectivityChecker : public net::URLFetcherDelegate { |
| 71 public: | 65 public: |
| 72 ConnectivityChecker(Profile* profile, | 66 ConnectivityChecker(Profile* profile, |
| 73 const GURL& url, | 67 const GURL& url, |
| 74 const base::TimeDelta& timeout, | 68 const base::TimeDelta& timeout, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 if (is_being_destroyed_) | 106 if (is_being_destroyed_) |
| 113 return; | 107 return; |
| 114 is_being_destroyed_ = true; | 108 is_being_destroyed_ = true; |
| 115 | 109 |
| 116 DCHECK_EQ(url_fetcher_.get(), source); | 110 DCHECK_EQ(url_fetcher_.get(), source); |
| 117 net::URLRequestStatus status = source->GetStatus(); | 111 net::URLRequestStatus status = source->GetStatus(); |
| 118 int response_code = source->GetResponseCode(); | 112 int response_code = source->GetResponseCode(); |
| 119 | 113 |
| 120 bool connected = status.is_success() && response_code == net::HTTP_NO_CONTENT; | 114 bool connected = status.is_success() && response_code == net::HTTP_NO_CONTENT; |
| 121 if (connected) { | 115 if (connected) { |
| 122 ExecuteCallback(java_callback_.obj(), CONNECTIVITY_CHECK_RESULT_CONNECTED); | 116 ExecuteCallback(java_callback_, CONNECTIVITY_CHECK_RESULT_CONNECTED); |
| 123 } else { | 117 } else { |
| 124 ExecuteCallback(java_callback_.obj(), | 118 ExecuteCallback(java_callback_, CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED); |
| 125 CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED); | |
| 126 } | 119 } |
| 127 | 120 |
| 128 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 121 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 129 } | 122 } |
| 130 | 123 |
| 131 ConnectivityChecker::ConnectivityChecker( | 124 ConnectivityChecker::ConnectivityChecker( |
| 132 Profile* profile, | 125 Profile* profile, |
| 133 const GURL& url, | 126 const GURL& url, |
| 134 const base::TimeDelta& timeout, | 127 const base::TimeDelta& timeout, |
| 135 const base::android::JavaRef<jobject>& java_callback) | 128 const base::android::JavaRef<jobject>& java_callback) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 157 expiration_timer_.reset(new base::OneShotTimer()); | 150 expiration_timer_.reset(new base::OneShotTimer()); |
| 158 expiration_timer_->Start(FROM_HERE, timeout_, this, | 151 expiration_timer_->Start(FROM_HERE, timeout_, this, |
| 159 &ConnectivityChecker::OnTimeout); | 152 &ConnectivityChecker::OnTimeout); |
| 160 } | 153 } |
| 161 | 154 |
| 162 void ConnectivityChecker::OnTimeout() { | 155 void ConnectivityChecker::OnTimeout() { |
| 163 if (is_being_destroyed_) | 156 if (is_being_destroyed_) |
| 164 return; | 157 return; |
| 165 is_being_destroyed_ = true; | 158 is_being_destroyed_ = true; |
| 166 url_fetcher_.reset(); | 159 url_fetcher_.reset(); |
| 167 ExecuteCallback(java_callback_.obj(), CONNECTIVITY_CHECK_RESULT_TIMEOUT); | 160 ExecuteCallback(java_callback_, CONNECTIVITY_CHECK_RESULT_TIMEOUT); |
| 168 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 161 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 169 } | 162 } |
| 170 | 163 |
| 171 } // namespace | 164 } // namespace |
| 172 | 165 |
| 173 void CheckConnectivity(JNIEnv* env, | 166 void CheckConnectivity(JNIEnv* env, |
| 174 const JavaParamRef<jclass>& clazz, | 167 const JavaParamRef<jclass>& clazz, |
| 175 const JavaParamRef<jobject>& j_profile, | 168 const JavaParamRef<jobject>& j_profile, |
| 176 const JavaParamRef<jstring>& j_url, | 169 const JavaParamRef<jstring>& j_url, |
| 177 jlong j_timeout_ms, | 170 jlong j_timeout_ms, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 200 GURL url(base::android::ConvertJavaStringToUTF8(env, j_url)); | 193 GURL url(base::android::ConvertJavaStringToUTF8(env, j_url)); |
| 201 return url.is_valid(); | 194 return url.is_valid(); |
| 202 } | 195 } |
| 203 | 196 |
| 204 bool RegisterConnectivityChecker(JNIEnv* env) { | 197 bool RegisterConnectivityChecker(JNIEnv* env) { |
| 205 return RegisterNativesImpl(env); | 198 return RegisterNativesImpl(env); |
| 206 } | 199 } |
| 207 | 200 |
| 208 } // namespace android | 201 } // namespace android |
| 209 } // namespace chrome | 202 } // namespace chrome |
| OLD | NEW |