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

Side by Side Diff: chrome/browser/android/feedback/connectivity_checker.cc

Issue 1127983002: Add connectivity check for Android feedback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move test to new test package, and merge with upstream (conflict in chrome_jni_registrar.cc) Created 5 years, 7 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
« no previous file with comments | « chrome/browser/android/feedback/connectivity_checker.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/feedback/connectivity_checker.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_android.h"
15 #include "jni/ConnectivityChecker_jni.h"
16 #include "net/base/load_flags.h"
17 #include "net/http/http_status_code.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "net/url_request/url_request_status.h"
22 #include "url/gurl.h"
23
24 namespace chrome {
25 namespace android {
26
27 namespace {
28
29 void ExecuteCallback(jobject callback, bool connected) {
30 Java_ConnectivityChecker_executeCallback(base::android::AttachCurrentThread(),
31 callback, connected);
32 }
33
34 void ExecuteCallbackFromRef(
35 base::android::ScopedJavaGlobalRef<jobject>* callback,
36 bool connected) {
37 ExecuteCallback(callback->obj(), connected);
38 }
39
40 void PostCallback(JNIEnv* env, jobject j_callback, bool connected) {
41 base::MessageLoop::current()->PostTask(
42 FROM_HERE,
43 base::Bind(&ExecuteCallbackFromRef,
44 base::Owned(new base::android::ScopedJavaGlobalRef<jobject>(
45 env, j_callback)),
46 connected));
47 }
48
49 // A utility class for checking if the device is currently connected to the
50 // Internet.
51 class ConnectivityChecker : public net::URLFetcherDelegate {
52 public:
53 ConnectivityChecker(Profile* profile,
54 const GURL& url,
55 const base::TimeDelta& timeout,
56 const base::android::JavaRef<jobject>& java_callback);
57
58 // Kicks off the asynchronous connectivity check. When the request has
59 // completed, |this| is deleted.
60 void StartAsyncCheck();
61
62 // net::URLFetcherDelegate implementation:
63 void OnURLFetchComplete(const net::URLFetcher* source) override;
64
65 // Cancels the URLFetcher, and triggers the callback with a negative result.
66 void Cancel();
67
68 private:
69 // The context in which the connectivity check is performed.
70 net::URLRequestContextGetter* request_context_;
71
72 // The URL to connect to.
73 const GURL& url_;
74
75 // How long to wait for a response before giving up.
76 const base::TimeDelta timeout_;
77
78 // Holds the Java object which will get the callback with the result.
79 base::android::ScopedJavaGlobalRef<jobject> java_callback_;
80
81 // The URLFetcher that executes the connectivity check.
82 scoped_ptr<net::URLFetcher> url_fetcher_;
83
84 // Whether |this| is already being destroyed, at which point the callback
85 // has already happened, and no further action should be taken.
86 bool is_being_destroyed_;
87
88 scoped_ptr<base::OneShotTimer<ConnectivityChecker>> expiration_timer_;
89 };
90
91 void ConnectivityChecker::OnURLFetchComplete(const net::URLFetcher* source) {
92 if (is_being_destroyed_)
93 return;
94 is_being_destroyed_ = true;
95
96 DCHECK_EQ(url_fetcher_.get(), source);
97 net::URLRequestStatus status = source->GetStatus();
98 int response_code = source->GetResponseCode();
99
100 bool connected = status.is_success() && response_code == net::HTTP_NO_CONTENT;
101 ExecuteCallback(java_callback_.obj(), connected);
102
103 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
104 }
105
106 ConnectivityChecker::ConnectivityChecker(
107 Profile* profile,
108 const GURL& url,
109 const base::TimeDelta& timeout,
110 const base::android::JavaRef<jobject>& java_callback)
111 : request_context_(profile->GetRequestContext()),
112 url_(url),
113 timeout_(timeout),
114 java_callback_(java_callback),
115 is_being_destroyed_(false) {
116 }
117
118 void ConnectivityChecker::StartAsyncCheck() {
119 url_fetcher_ =
120 net::URLFetcher::Create(url_, net::URLFetcher::GET, this).Pass();
121 url_fetcher_->SetRequestContext(request_context_);
122 url_fetcher_->SetStopOnRedirect(true);
123 url_fetcher_->SetAutomaticallyRetryOn5xx(false);
124 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0);
125 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
126 net::LOAD_DO_NOT_SAVE_COOKIES |
127 net::LOAD_DO_NOT_SEND_COOKIES |
128 net::LOAD_DO_NOT_SEND_AUTH_DATA);
129 url_fetcher_->Start();
130 expiration_timer_.reset(new base::OneShotTimer<ConnectivityChecker>());
131 expiration_timer_->Start(FROM_HERE, timeout_, this,
132 &ConnectivityChecker::Cancel);
133 }
134
135 void ConnectivityChecker::Cancel() {
136 if (is_being_destroyed_)
137 return;
138 is_being_destroyed_ = true;
139 url_fetcher_.reset();
140 ExecuteCallback(java_callback_.obj(), false);
141 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
142 }
143
144 } // namespace
145
146 void CheckConnectivity(JNIEnv* env,
147 jclass clazz,
148 jobject j_profile,
149 jstring j_url,
150 jlong j_timeout_ms,
151 jobject j_callback) {
152 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
153 if (!profile) {
154 PostCallback(env, j_callback, false);
155 return;
156 }
157 GURL url(base::android::ConvertJavaStringToUTF8(env, j_url));
158 if (!url.is_valid()) {
159 PostCallback(env, j_callback, false);
160 return;
161 }
162
163 // This object will be deleted when the connectivity check has completed.
164 ConnectivityChecker* connectivity_checker = new ConnectivityChecker(
165 profile, url, base::TimeDelta::FromMilliseconds(j_timeout_ms),
166 base::android::ScopedJavaLocalRef<jobject>(env, j_callback));
167 connectivity_checker->StartAsyncCheck();
168 }
169
170 bool RegisterConnectivityChecker(JNIEnv* env) {
171 return RegisterNativesImpl(env);
172 }
173
174 } // namespace android
175 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/android/feedback/connectivity_checker.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698