Chromium Code Reviews| 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/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_android.h" | 14 #include "chrome/browser/profiles/profile_android.h" |
| 15 #include "jni/ConnectivityChecker_jni.h" | 15 #include "jni/ConnectivityChecker_jni.h" |
| 16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 17 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| 19 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace chrome { | 24 namespace chrome { |
| 25 namespace android { | 25 namespace android { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.feedback | |
| 29 // GENERATED_JAVA_PREFIX_TO_STRIP: CONNECTIVITY_CHECK_RESULT_ | |
| 30 enum ConnectivityCheckResult { | |
| 31 CONNECTIVITY_CHECK_RESULT_UNKNOWN = 0, | |
| 32 CONNECTIVITY_CHECK_RESULT_CONNECTED = 1, | |
| 33 CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED = 2, | |
| 34 CONNECTIVITY_CHECK_RESULT_TIMEOUT = 3, | |
| 35 CONNECTIVITY_CHECK_RESULT_ERROR = 4, | |
| 36 CONNECTIVITY_CHECK_RESULT_END = 5 | |
| 37 }; | |
| 28 | 38 |
| 29 void ExecuteCallback(jobject callback, bool connected) { | 39 void ExecuteCallback(jobject callback, ConnectivityCheckResult result) { |
| 40 CHECK(result >= CONNECTIVITY_CHECK_RESULT_UNKNOWN); | |
| 41 CHECK(result < CONNECTIVITY_CHECK_RESULT_END); | |
| 30 Java_ConnectivityChecker_executeCallback(base::android::AttachCurrentThread(), | 42 Java_ConnectivityChecker_executeCallback(base::android::AttachCurrentThread(), |
| 31 callback, connected); | 43 callback, result); |
| 32 } | 44 } |
| 33 | 45 |
| 34 void ExecuteCallbackFromRef( | 46 void ExecuteCallbackFromRef( |
| 35 base::android::ScopedJavaGlobalRef<jobject>* callback, | 47 base::android::ScopedJavaGlobalRef<jobject>* callback, |
| 36 bool connected) { | 48 ConnectivityCheckResult result) { |
| 37 ExecuteCallback(callback->obj(), connected); | 49 ExecuteCallback(callback->obj(), result); |
| 38 } | 50 } |
| 39 | 51 |
| 40 void PostCallback(JNIEnv* env, jobject j_callback, bool connected) { | 52 void PostCallback(JNIEnv* env, |
| 53 jobject j_callback, | |
| 54 ConnectivityCheckResult result) { | |
| 41 base::MessageLoop::current()->PostTask( | 55 base::MessageLoop::current()->PostTask( |
| 42 FROM_HERE, | 56 FROM_HERE, |
| 43 base::Bind(&ExecuteCallbackFromRef, | 57 base::Bind(&ExecuteCallbackFromRef, |
| 44 base::Owned(new base::android::ScopedJavaGlobalRef<jobject>( | 58 base::Owned(new base::android::ScopedJavaGlobalRef<jobject>( |
| 45 env, j_callback)), | 59 env, j_callback)), |
| 46 connected)); | 60 result)); |
| 47 } | 61 } |
| 48 | 62 |
| 49 // A utility class for checking if the device is currently connected to the | 63 // A utility class for checking if the device is currently connected to the |
| 50 // Internet. | 64 // Internet. |
| 51 class ConnectivityChecker : public net::URLFetcherDelegate { | 65 class ConnectivityChecker : public net::URLFetcherDelegate { |
| 52 public: | 66 public: |
| 53 ConnectivityChecker(Profile* profile, | 67 ConnectivityChecker(Profile* profile, |
| 54 const GURL& url, | 68 const GURL& url, |
| 55 const base::TimeDelta& timeout, | 69 const base::TimeDelta& timeout, |
| 56 const base::android::JavaRef<jobject>& java_callback); | 70 const base::android::JavaRef<jobject>& java_callback); |
| 57 | 71 |
| 58 // Kicks off the asynchronous connectivity check. When the request has | 72 // Kicks off the asynchronous connectivity check. When the request has |
| 59 // completed, |this| is deleted. | 73 // completed, |this| is deleted. |
| 60 void StartAsyncCheck(); | 74 void StartAsyncCheck(); |
| 61 | 75 |
| 62 // net::URLFetcherDelegate implementation: | 76 // net::URLFetcherDelegate implementation: |
| 63 void OnURLFetchComplete(const net::URLFetcher* source) override; | 77 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 64 | 78 |
| 65 // Cancels the URLFetcher, and triggers the callback with a negative result. | 79 // Cancels the URLFetcher, and triggers the callback with a negative result |
| 66 void Cancel(); | 80 // and the timeout flag set. |
|
gone
2015/07/01 15:56:48
This sentence is weird; it took me several tries t
| |
| 81 void OnTimeout(); | |
| 67 | 82 |
| 68 private: | 83 private: |
| 69 // The context in which the connectivity check is performed. | 84 // The context in which the connectivity check is performed. |
| 70 net::URLRequestContextGetter* request_context_; | 85 net::URLRequestContextGetter* request_context_; |
| 71 | 86 |
| 72 // The URL to connect to. | 87 // The URL to connect to. |
| 73 const GURL& url_; | 88 const GURL& url_; |
| 74 | 89 |
| 75 // How long to wait for a response before giving up. | 90 // How long to wait for a response before giving up. |
| 76 const base::TimeDelta timeout_; | 91 const base::TimeDelta timeout_; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 91 void ConnectivityChecker::OnURLFetchComplete(const net::URLFetcher* source) { | 106 void ConnectivityChecker::OnURLFetchComplete(const net::URLFetcher* source) { |
| 92 if (is_being_destroyed_) | 107 if (is_being_destroyed_) |
| 93 return; | 108 return; |
| 94 is_being_destroyed_ = true; | 109 is_being_destroyed_ = true; |
| 95 | 110 |
| 96 DCHECK_EQ(url_fetcher_.get(), source); | 111 DCHECK_EQ(url_fetcher_.get(), source); |
| 97 net::URLRequestStatus status = source->GetStatus(); | 112 net::URLRequestStatus status = source->GetStatus(); |
| 98 int response_code = source->GetResponseCode(); | 113 int response_code = source->GetResponseCode(); |
| 99 | 114 |
| 100 bool connected = status.is_success() && response_code == net::HTTP_NO_CONTENT; | 115 bool connected = status.is_success() && response_code == net::HTTP_NO_CONTENT; |
| 101 ExecuteCallback(java_callback_.obj(), connected); | 116 if (connected) { |
| 117 ExecuteCallback(java_callback_.obj(), CONNECTIVITY_CHECK_RESULT_CONNECTED); | |
| 118 } else { | |
| 119 ExecuteCallback(java_callback_.obj(), | |
| 120 CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED); | |
| 121 } | |
| 102 | 122 |
| 103 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 123 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 104 } | 124 } |
| 105 | 125 |
| 106 ConnectivityChecker::ConnectivityChecker( | 126 ConnectivityChecker::ConnectivityChecker( |
| 107 Profile* profile, | 127 Profile* profile, |
| 108 const GURL& url, | 128 const GURL& url, |
| 109 const base::TimeDelta& timeout, | 129 const base::TimeDelta& timeout, |
| 110 const base::android::JavaRef<jobject>& java_callback) | 130 const base::android::JavaRef<jobject>& java_callback) |
| 111 : request_context_(profile->GetRequestContext()), | 131 : request_context_(profile->GetRequestContext()), |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 122 url_fetcher_->SetStopOnRedirect(true); | 142 url_fetcher_->SetStopOnRedirect(true); |
| 123 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | 143 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 124 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); | 144 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); |
| 125 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | 145 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | |
| 126 net::LOAD_DO_NOT_SAVE_COOKIES | | 146 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 127 net::LOAD_DO_NOT_SEND_COOKIES | | 147 net::LOAD_DO_NOT_SEND_COOKIES | |
| 128 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 148 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 129 url_fetcher_->Start(); | 149 url_fetcher_->Start(); |
| 130 expiration_timer_.reset(new base::OneShotTimer<ConnectivityChecker>()); | 150 expiration_timer_.reset(new base::OneShotTimer<ConnectivityChecker>()); |
| 131 expiration_timer_->Start(FROM_HERE, timeout_, this, | 151 expiration_timer_->Start(FROM_HERE, timeout_, this, |
| 132 &ConnectivityChecker::Cancel); | 152 &ConnectivityChecker::OnTimeout); |
| 133 } | 153 } |
| 134 | 154 |
| 135 void ConnectivityChecker::Cancel() { | 155 void ConnectivityChecker::OnTimeout() { |
| 136 if (is_being_destroyed_) | 156 if (is_being_destroyed_) |
| 137 return; | 157 return; |
| 138 is_being_destroyed_ = true; | 158 is_being_destroyed_ = true; |
| 139 url_fetcher_.reset(); | 159 url_fetcher_.reset(); |
| 140 ExecuteCallback(java_callback_.obj(), false); | 160 ExecuteCallback(java_callback_.obj(), CONNECTIVITY_CHECK_RESULT_TIMEOUT); |
| 141 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 161 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 142 } | 162 } |
| 143 | 163 |
| 144 } // namespace | 164 } // namespace |
| 145 | 165 |
| 146 void CheckConnectivity(JNIEnv* env, | 166 void CheckConnectivity(JNIEnv* env, |
| 147 jclass clazz, | 167 jclass clazz, |
| 148 jobject j_profile, | 168 jobject j_profile, |
| 149 jstring j_url, | 169 jstring j_url, |
| 150 jlong j_timeout_ms, | 170 jlong j_timeout_ms, |
| 151 jobject j_callback) { | 171 jobject j_callback) { |
| 152 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 172 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 153 if (!profile) { | 173 if (!profile) { |
| 154 PostCallback(env, j_callback, false); | 174 PostCallback(env, j_callback, CONNECTIVITY_CHECK_RESULT_ERROR); |
| 155 return; | 175 return; |
| 156 } | 176 } |
| 157 GURL url(base::android::ConvertJavaStringToUTF8(env, j_url)); | 177 GURL url(base::android::ConvertJavaStringToUTF8(env, j_url)); |
| 158 if (!url.is_valid()) { | 178 if (!url.is_valid()) { |
| 159 PostCallback(env, j_callback, false); | 179 PostCallback(env, j_callback, CONNECTIVITY_CHECK_RESULT_ERROR); |
| 160 return; | 180 return; |
| 161 } | 181 } |
| 162 | 182 |
| 163 // This object will be deleted when the connectivity check has completed. | 183 // This object will be deleted when the connectivity check has completed. |
| 164 ConnectivityChecker* connectivity_checker = new ConnectivityChecker( | 184 ConnectivityChecker* connectivity_checker = new ConnectivityChecker( |
| 165 profile, url, base::TimeDelta::FromMilliseconds(j_timeout_ms), | 185 profile, url, base::TimeDelta::FromMilliseconds(j_timeout_ms), |
| 166 base::android::ScopedJavaLocalRef<jobject>(env, j_callback)); | 186 base::android::ScopedJavaLocalRef<jobject>(env, j_callback)); |
| 167 connectivity_checker->StartAsyncCheck(); | 187 connectivity_checker->StartAsyncCheck(); |
| 168 } | 188 } |
| 169 | 189 |
| 190 jboolean IsUrlValid(JNIEnv* env, jclass clazz, jstring j_url) { | |
| 191 GURL url(base::android::ConvertJavaStringToUTF8(env, j_url)); | |
| 192 return url.is_valid(); | |
| 193 } | |
| 194 | |
| 170 bool RegisterConnectivityChecker(JNIEnv* env) { | 195 bool RegisterConnectivityChecker(JNIEnv* env) { |
| 171 return RegisterNativesImpl(env); | 196 return RegisterNativesImpl(env); |
| 172 } | 197 } |
| 173 | 198 |
| 174 } // namespace android | 199 } // namespace android |
| 175 } // namespace chrome | 200 } // namespace chrome |
| OLD | NEW |