1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ref.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/android/chrome_jni_headers/ConnectivityChecker_jni.h"
using base::android::JavaParamRef;
namespace chrome {
namespace android {
namespace {
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.feedback
// GENERATED_JAVA_PREFIX_TO_STRIP: CONNECTIVITY_CHECK_RESULT_
enum ConnectivityCheckResult {
CONNECTIVITY_CHECK_RESULT_UNKNOWN = 0,
CONNECTIVITY_CHECK_RESULT_CONNECTED = 1,
CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED = 2,
CONNECTIVITY_CHECK_RESULT_TIMEOUT = 3,
CONNECTIVITY_CHECK_RESULT_ERROR = 4,
CONNECTIVITY_CHECK_RESULT_END = 5
};
void ExecuteCallback(const base::android::JavaRef<jobject>& callback,
ConnectivityCheckResult result) {
CHECK(result >= CONNECTIVITY_CHECK_RESULT_UNKNOWN);
CHECK(result < CONNECTIVITY_CHECK_RESULT_END);
Java_ConnectivityChecker_executeCallback(base::android::AttachCurrentThread(),
callback, result);
}
void JNI_ConnectivityChecker_PostCallback(
JNIEnv* env,
const base::android::JavaRef<jobject>& j_callback,
ConnectivityCheckResult result) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&ExecuteCallback,
base::android::ScopedJavaGlobalRef<jobject>(j_callback),
result));
}
// A utility class for checking if the device is currently connected to the
// Internet.
class ConnectivityChecker {
public:
ConnectivityChecker(
Profile* profile,
const GURL& url,
const base::TimeDelta& timeout,
const base::android::JavaRef<jobject>& java_callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation);
// Kicks off the asynchronous connectivity check. When the request has
// completed, |this| is deleted.
void StartAsyncCheck();
// Cancels the URLFetcher, and triggers the callback with a negative result
// and the timeout flag set.
void OnTimeout();
private:
void OnURLLoadComplete(scoped_refptr<net::HttpResponseHeaders> headers);
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
// The URL to connect to.
const raw_ref<const GURL> url_;
// How long to wait for a response before giving up.
const base::TimeDelta timeout_;
// Holds the Java object which will get the callback with the result.
base::android::ScopedJavaGlobalRef<jobject> java_callback_;
// Holds the traffic annotation that is created in Java for all the
// connectivity checks. The same annotation is used for the Chrome network
// stack and the Android system network stack.
net::NetworkTrafficAnnotationTag traffic_annotation_;
// The URLFetcher that executes the connectivity check.
std::unique_ptr<network::SimpleURLLoader> url_loader_;
// Whether |this| is already being destroyed, at which point the callback
// has already happened, and no further action should be taken.
bool is_being_destroyed_;
std::unique_ptr<base::OneShotTimer> expiration_timer_;
};
void ConnectivityChecker::OnURLLoadComplete(
scoped_refptr<net::HttpResponseHeaders> headers) {
if (is_being_destroyed_)
return;
is_being_destroyed_ = true;
DCHECK(url_loader_);
bool connected = headers && headers->response_code() == net::HTTP_NO_CONTENT;
if (connected)
ExecuteCallback(java_callback_, CONNECTIVITY_CHECK_RESULT_CONNECTED);
else
ExecuteCallback(java_callback_, CONNECTIVITY_CHECK_RESULT_NOT_CONNECTED);
base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE,
this);
}
ConnectivityChecker::ConnectivityChecker(
Profile* profile,
const GURL& url,
const base::TimeDelta& timeout,
const base::android::JavaRef<jobject>& java_callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation)
: shared_url_loader_factory_(profile->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess()),
url_(url),
timeout_(timeout),
java_callback_(java_callback),
traffic_annotation_(traffic_annotation),
is_being_destroyed_(false) {}
void ConnectivityChecker::StartAsyncCheck() {
auto request = std::make_unique<network::ResourceRequest>();
request->url = *url_;
request->credentials_mode = network::mojom::CredentialsMode::kOmit;
request->load_flags = net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE;
url_loader_ =
network::SimpleURLLoader::Create(std::move(request), traffic_annotation_);
url_loader_->DownloadHeadersOnly(
shared_url_loader_factory_.get(),
base::BindOnce(&ConnectivityChecker::OnURLLoadComplete,
base::Unretained(this)));
expiration_timer_ = std::make_unique<base::OneShotTimer>();
expiration_timer_->Start(FROM_HERE, timeout_, this,
&ConnectivityChecker::OnTimeout);
}
void ConnectivityChecker::OnTimeout() {
if (is_being_destroyed_)
return;
is_being_destroyed_ = true;
url_loader_.reset();
ExecuteCallback(java_callback_, CONNECTIVITY_CHECK_RESULT_TIMEOUT);
base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE,
this);
}
} // namespace
void JNI_ConnectivityChecker_CheckConnectivity(
JNIEnv* env,
Profile* profile,
std::string& j_url,
jlong j_timeout_ms,
const JavaParamRef<jobject>& j_callback,
jint j_network_annotation_hash_code) {
if (!profile) {
JNI_ConnectivityChecker_PostCallback(env, j_callback,
CONNECTIVITY_CHECK_RESULT_ERROR);
return;
}
GURL url(j_url);
if (!url.is_valid()) {
JNI_ConnectivityChecker_PostCallback(env, j_callback,
CONNECTIVITY_CHECK_RESULT_ERROR);
return;
}
// This object will be deleted when the connectivity check has completed.
ConnectivityChecker* connectivity_checker = new ConnectivityChecker(
profile, url, base::Milliseconds(j_timeout_ms), j_callback,
net::NetworkTrafficAnnotationTag::FromJavaAnnotation(
j_network_annotation_hash_code));
connectivity_checker->StartAsyncCheck();
}
jboolean JNI_ConnectivityChecker_IsUrlValid(JNIEnv* env, std::string& j_url) {
GURL url(j_url);
return url.is_valid();
}
} // namespace android
} // namespace chrome
|