OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/android/content_view_client.h" | |
6 | |
7 #include <android/keycodes.h> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/jni_string.h" | |
11 #include "content/browser/android/content_view_core_impl.h" | |
12 #include "content/browser/android/download_controller.h" | |
13 #include "content/browser/renderer_host/render_view_host_impl.h" | |
14 #include "content/public/browser/render_widget_host_view.h" | |
15 #include "content/public/browser/download_item.h" | |
16 #include "content/public/browser/invalidate_type.h" | |
17 #include "content/public/browser/page_navigator.h" | |
18 #include "content/public/browser/navigation_controller.h" | |
19 #include "content/public/browser/navigation_entry.h" | |
20 #include "content/public/browser/web_contents.h" | |
21 #include "content/public/common/page_transition_types.h" | |
22 #include "content/public/common/referrer.h" | |
23 #include "jni/ContentViewClient_jni.h" | |
24 #include "net/http/http_request_headers.h" | |
25 | |
26 using base::android::AttachCurrentThread; | |
27 using base::android::CheckException; | |
28 using base::android::ConvertUTF8ToJavaString; | |
29 using base::android::ConvertUTF16ToJavaString; | |
30 using base::android::GetClass; | |
31 using base::android::GetMethodID; | |
32 using base::android::HasClass; | |
33 using base::android::ScopedJavaLocalRef; | |
34 | |
35 namespace content { | |
36 | |
37 ContentViewClient::ContentViewClient(JNIEnv* env, jobject obj) | |
38 : weak_java_client_(env, obj) { | |
39 } | |
40 | |
41 ContentViewClient::~ContentViewClient() { | |
42 } | |
43 | |
44 // static | |
45 ContentViewClient* ContentViewClient::CreateNativeContentViewClient( | |
46 JNIEnv* env, jobject obj) { | |
47 DCHECK(obj); | |
48 return new ContentViewClient(env, obj); | |
49 } | |
50 | |
51 void ContentViewClient::OnPageStarted(const GURL& url) { | |
52 JNIEnv* env = AttachCurrentThread(); | |
53 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); | |
54 if (obj.is_null()) | |
55 return; | |
56 ScopedJavaLocalRef<jstring> jstring_url = | |
57 ConvertUTF8ToJavaString(env, url.spec()); | |
58 Java_ContentViewClient_onPageStarted(env, obj.obj(), jstring_url.obj()); | |
59 } | |
60 | |
61 void ContentViewClient::OnPageFinished(const GURL& url) { | |
62 JNIEnv* env = AttachCurrentThread(); | |
63 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); | |
64 if (obj.is_null()) | |
65 return; | |
66 ScopedJavaLocalRef<jstring> jstring_url = | |
67 ConvertUTF8ToJavaString(env, url.spec()); | |
68 | |
69 Java_ContentViewClient_onPageFinished(env, obj.obj(), jstring_url.obj()); | |
70 CheckException(env); | |
71 } | |
72 | |
73 void ContentViewClient::OnReceivedError(int error_code, | |
74 const string16& description, | |
75 const GURL& url) { | |
76 JNIEnv* env = AttachCurrentThread(); | |
77 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); | |
78 if (obj.is_null()) | |
79 return; | |
80 ScopedJavaLocalRef<jstring> jstring_error_description = | |
81 ConvertUTF8ToJavaString(env, url.spec()); | |
82 ScopedJavaLocalRef<jstring> jstring_url = | |
83 ConvertUTF8ToJavaString(env, url.spec()); | |
84 | |
85 Java_ContentViewClient_onReceivedError( | |
86 env, obj.obj(), | |
87 ToContentViewClientError(error_code), | |
88 jstring_error_description.obj(), jstring_url.obj()); | |
89 } | |
90 | |
91 void ContentViewClient::OnDidCommitMainFrame(const GURL& url, | |
92 const GURL& base_url) { | |
93 JNIEnv* env = AttachCurrentThread(); | |
94 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); | |
95 if (obj.is_null()) | |
96 return; | |
97 ScopedJavaLocalRef<jstring> jstring_url = | |
98 ConvertUTF8ToJavaString(env, url.spec()); | |
99 ScopedJavaLocalRef<jstring> jstring_base_url = | |
100 ConvertUTF8ToJavaString(env, base_url.spec()); | |
101 | |
102 Java_ContentViewClient_onMainFrameCommitted( | |
103 env, obj.obj(), | |
104 jstring_url.obj(), jstring_base_url.obj()); | |
105 } | |
106 | |
107 void ContentViewClient::OnInterstitialShown() { | |
108 JNIEnv* env = AttachCurrentThread(); | |
109 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); | |
110 if (obj.is_null()) | |
111 return; | |
112 Java_ContentViewClient_onInterstitialShown(env, obj.obj()); | |
113 } | |
114 | |
115 void ContentViewClient::OnInterstitialHidden() { | |
116 JNIEnv* env = AttachCurrentThread(); | |
117 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); | |
118 if (obj.is_null()) | |
119 return; | |
120 Java_ContentViewClient_onInterstitialHidden(env, obj.obj()); | |
121 } | |
122 | |
123 ContentViewClientError ContentViewClient::ToContentViewClientError( | |
124 int net_error) { | |
125 // Note: many net::Error constants don't have an obvious mapping. | |
126 // These will be handled by the default case, ERROR_UNKNOWN. | |
127 switch(net_error) { | |
128 case net::ERR_UNSUPPORTED_AUTH_SCHEME: | |
129 return CONTENT_VIEW_CLIENT_ERROR_UNSUPPORTED_AUTH_SCHEME; | |
130 | |
131 case net::ERR_INVALID_AUTH_CREDENTIALS: | |
132 case net::ERR_MISSING_AUTH_CREDENTIALS: | |
133 case net::ERR_MISCONFIGURED_AUTH_ENVIRONMENT: | |
134 return CONTENT_VIEW_CLIENT_ERROR_AUTHENTICATION; | |
135 | |
136 case net::ERR_TOO_MANY_REDIRECTS: | |
137 return CONTENT_VIEW_CLIENT_ERROR_REDIRECT_LOOP; | |
138 | |
139 case net::ERR_UPLOAD_FILE_CHANGED: | |
140 return CONTENT_VIEW_CLIENT_ERROR_FILE_NOT_FOUND; | |
141 | |
142 case net::ERR_INVALID_URL: | |
143 return CONTENT_VIEW_CLIENT_ERROR_BAD_URL; | |
144 | |
145 case net::ERR_DISALLOWED_URL_SCHEME: | |
146 case net::ERR_UNKNOWN_URL_SCHEME: | |
147 return CONTENT_VIEW_CLIENT_ERROR_UNSUPPORTED_SCHEME; | |
148 | |
149 case net::ERR_IO_PENDING: | |
150 case net::ERR_NETWORK_IO_SUSPENDED: | |
151 return CONTENT_VIEW_CLIENT_ERROR_IO; | |
152 | |
153 case net::ERR_CONNECTION_TIMED_OUT: | |
154 case net::ERR_TIMED_OUT: | |
155 return CONTENT_VIEW_CLIENT_ERROR_TIMEOUT; | |
156 | |
157 case net::ERR_FILE_TOO_BIG: | |
158 return CONTENT_VIEW_CLIENT_ERROR_FILE; | |
159 | |
160 case net::ERR_HOST_RESOLVER_QUEUE_TOO_LARGE: | |
161 case net::ERR_INSUFFICIENT_RESOURCES: | |
162 case net::ERR_OUT_OF_MEMORY: | |
163 return CONTENT_VIEW_CLIENT_ERROR_TOO_MANY_REQUESTS; | |
164 | |
165 case net::ERR_CONNECTION_CLOSED: | |
166 case net::ERR_CONNECTION_RESET: | |
167 case net::ERR_CONNECTION_REFUSED: | |
168 case net::ERR_CONNECTION_ABORTED: | |
169 case net::ERR_CONNECTION_FAILED: | |
170 case net::ERR_SOCKET_NOT_CONNECTED: | |
171 return CONTENT_VIEW_CLIENT_ERROR_CONNECT; | |
172 | |
173 case net::ERR_INTERNET_DISCONNECTED: | |
174 case net::ERR_ADDRESS_INVALID: | |
175 case net::ERR_ADDRESS_UNREACHABLE: | |
176 case net::ERR_NAME_NOT_RESOLVED: | |
177 case net::ERR_NAME_RESOLUTION_FAILED: | |
178 return CONTENT_VIEW_CLIENT_ERROR_HOST_LOOKUP; | |
179 | |
180 case net::ERR_SSL_PROTOCOL_ERROR: | |
181 case net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED: | |
182 case net::ERR_TUNNEL_CONNECTION_FAILED: | |
183 case net::ERR_NO_SSL_VERSIONS_ENABLED: | |
184 case net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH: | |
185 case net::ERR_SSL_RENEGOTIATION_REQUESTED: | |
186 case net::ERR_CERT_ERROR_IN_SSL_RENEGOTIATION: | |
187 case net::ERR_BAD_SSL_CLIENT_AUTH_CERT: | |
188 case net::ERR_SSL_NO_RENEGOTIATION: | |
189 case net::ERR_SSL_DECOMPRESSION_FAILURE_ALERT: | |
190 case net::ERR_SSL_BAD_RECORD_MAC_ALERT: | |
191 case net::ERR_SSL_UNSAFE_NEGOTIATION: | |
192 case net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY: | |
193 case net::ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED: | |
194 case net::ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY: | |
195 return CONTENT_VIEW_CLIENT_ERROR_FAILED_SSL_HANDSHAKE; | |
196 | |
197 case net::ERR_PROXY_AUTH_UNSUPPORTED: | |
198 case net::ERR_PROXY_AUTH_REQUESTED: | |
199 case net::ERR_PROXY_CONNECTION_FAILED: | |
200 case net::ERR_UNEXPECTED_PROXY_AUTH: | |
201 return CONTENT_VIEW_CLIENT_ERROR_PROXY_AUTHENTICATION; | |
202 | |
203 /* The certificate errors are handled by onReceivedSslError | |
204 * and don't need to be reported here. | |
205 */ | |
206 case net::ERR_CERT_COMMON_NAME_INVALID: | |
207 case net::ERR_CERT_DATE_INVALID: | |
208 case net::ERR_CERT_AUTHORITY_INVALID: | |
209 case net::ERR_CERT_CONTAINS_ERRORS: | |
210 case net::ERR_CERT_NO_REVOCATION_MECHANISM: | |
211 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION: | |
212 case net::ERR_CERT_REVOKED: | |
213 case net::ERR_CERT_INVALID: | |
214 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM: | |
215 case net::ERR_CERT_NON_UNIQUE_NAME: | |
216 return CONTENT_VIEW_CLIENT_ERROR_OK; | |
217 | |
218 default: | |
219 VLOG(1) << "ContentViewClient::ToContentViewClientError: Unknown " | |
220 << "chromium error: " | |
221 << net_error; | |
222 return CONTENT_VIEW_CLIENT_ERROR_UNKNOWN; | |
223 } | |
224 } | |
225 | |
226 // ---------------------------------------------------------------------------- | |
227 // Native JNI methods | |
228 // ---------------------------------------------------------------------------- | |
229 | |
230 // Register native methods | |
231 | |
232 bool RegisterContentViewClient(JNIEnv* env) { | |
233 if (!HasClass(env, kContentViewClientClassPath)) { | |
234 DLOG(ERROR) << "Unable to find class ContentViewClient!"; | |
235 return false; | |
236 } | |
237 return RegisterNativesImpl(env); | |
238 } | |
239 | |
240 } // namespace content | |
OLD | NEW |