OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/android/content_view_core_impl.h" | 5 #include "content/browser/android/content_view_core_impl.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" |
14 #include "cc/layer.h" | 15 #include "cc/layer.h" |
15 #include "content/browser/android/interstitial_page_delegate_android.h" | 16 #include "content/browser/android/interstitial_page_delegate_android.h" |
16 #include "content/browser/android/load_url_params.h" | 17 #include "content/browser/android/load_url_params.h" |
17 #include "content/browser/android/touch_point.h" | 18 #include "content/browser/android/touch_point.h" |
18 #include "content/browser/renderer_host/java/java_bound_object.h" | 19 #include "content/browser/renderer_host/java/java_bound_object.h" |
19 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager
.h" | 20 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager
.h" |
20 #include "content/browser/renderer_host/compositor_impl_android.h" | 21 #include "content/browser/renderer_host/compositor_impl_android.h" |
21 #include "content/browser/renderer_host/render_view_host_impl.h" | 22 #include "content/browser/renderer_host/render_view_host_impl.h" |
22 #include "content/browser/renderer_host/render_widget_host_impl.h" | 23 #include "content/browser/renderer_host/render_widget_host_impl.h" |
23 #include "content/browser/renderer_host/render_widget_host_view_android.h" | 24 #include "content/browser/renderer_host/render_widget_host_view_android.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 168 |
168 InitJNI(env, obj); | 169 InitJNI(env, obj); |
169 | 170 |
170 if (!gfx::Screen::GetNativeScreen()->IsDIPEnabled()) { | 171 if (!gfx::Screen::GetNativeScreen()->IsDIPEnabled()) { |
171 dpi_scale_ = 1; | 172 dpi_scale_ = 1; |
172 } else { | 173 } else { |
173 scoped_ptr<content::DeviceInfo> device_info(new content::DeviceInfo()); | 174 scoped_ptr<content::DeviceInfo> device_info(new content::DeviceInfo()); |
174 dpi_scale_ = device_info->GetDPIScale(); | 175 dpi_scale_ = device_info->GetDPIScale(); |
175 } | 176 } |
176 | 177 |
177 notification_registrar_.Add(this, | |
178 NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, | |
179 NotificationService::AllSources()); | |
180 | |
181 // Currently, the only use case we have for overriding a user agent involves | 178 // Currently, the only use case we have for overriding a user agent involves |
182 // spoofing a desktop Linux user agent for "Request desktop site". | 179 // spoofing a desktop Linux user agent for "Request desktop site". |
183 // Automatically set it for all WebContents so that it is available when a | 180 // Automatically set it for all WebContents so that it is available when a |
184 // NavigationEntry requires the user agent to be overridden. | 181 // NavigationEntry requires the user agent to be overridden. |
185 const char kLinuxInfoStr[] = "X11; Linux x86_64"; | 182 const char kLinuxInfoStr[] = "X11; Linux x86_64"; |
186 std::string product = content::GetContentClient()->GetProduct(); | 183 std::string product = content::GetContentClient()->GetProduct(); |
187 std::string spoofed_ua = | 184 std::string spoofed_ua = |
188 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product); | 185 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product); |
189 web_contents->SetUserAgentOverride(spoofed_ua); | 186 web_contents->SetUserAgentOverride(spoofed_ua); |
190 | 187 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 int pid = GetRenderProcessIdFromRenderViewHost( | 262 int pid = GetRenderProcessIdFromRenderViewHost( |
266 web_contents_->GetRenderViewHost()); | 263 web_contents_->GetRenderViewHost()); |
267 JNIEnv* env = AttachCurrentThread(); | 264 JNIEnv* env = AttachCurrentThread(); |
268 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 265 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
269 if (!obj.is_null()) { | 266 if (!obj.is_null()) { |
270 Java_ContentViewCore_onRenderProcessSwap(env, obj.obj(), 0, pid); | 267 Java_ContentViewCore_onRenderProcessSwap(env, obj.obj(), 0, pid); |
271 } | 268 } |
272 } | 269 } |
273 break; | 270 break; |
274 } | 271 } |
275 case NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT: { | |
276 if (!web_contents_ || Source<RenderViewHost>(source).ptr() != | |
277 web_contents_->GetRenderViewHost()) { | |
278 return; | |
279 } | |
280 | |
281 JNIEnv* env = base::android::AttachCurrentThread(); | |
282 std::pair<int, Value*>* result_pair = | |
283 Details<std::pair<int, Value*> >(details).ptr(); | |
284 std::string json; | |
285 base::JSONWriter::Write(result_pair->second, &json); | |
286 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); | |
287 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | |
288 if (!j_obj.is_null()) { | |
289 Java_ContentViewCore_onEvaluateJavaScriptResult(env, j_obj.obj(), | |
290 static_cast<jint>(result_pair->first), j_json.obj()); | |
291 } | |
292 break; | |
293 } | |
294 } | 272 } |
295 } | 273 } |
296 | 274 |
297 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { | 275 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { |
298 java_object_ = new JavaObject; | 276 java_object_ = new JavaObject; |
299 java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect")); | 277 java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect")); |
300 java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( | 278 java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( |
301 env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V"); | 279 env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V"); |
302 } | 280 } |
303 | 281 |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1241 } | 1219 } |
1242 | 1220 |
1243 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( | 1221 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( |
1244 JNIEnv* env, | 1222 JNIEnv* env, |
1245 jobject obj) { | 1223 jobject obj) { |
1246 RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1224 RenderViewHost* host = web_contents_->GetRenderViewHost(); |
1247 host->Send( | 1225 host->Send( |
1248 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); | 1226 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); |
1249 } | 1227 } |
1250 | 1228 |
1251 jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, | 1229 namespace { |
| 1230 void JavaScriptResultCallback(ScopedJavaGlobalRef<jobject>* callback, |
| 1231 const base::Value* result) { |
| 1232 // |callback| is passed as base::Owned, so it will automatically be deleted |
| 1233 // when this base::Callback goes out of scope. |
| 1234 JNIEnv* env = base::android::AttachCurrentThread(); |
| 1235 std::string json; |
| 1236 base::JSONWriter::Write(result, &json); |
| 1237 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); |
| 1238 Java_ContentViewCore_onEvaluateJavaScriptResult(env, |
| 1239 j_json.obj(), |
| 1240 callback->obj()); |
| 1241 } |
| 1242 } // namespace |
| 1243 |
| 1244 void ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, |
1252 jobject obj, | 1245 jobject obj, |
1253 jstring script) { | 1246 jstring script, |
| 1247 jobject callback) { |
1254 RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1248 RenderViewHost* host = web_contents_->GetRenderViewHost(); |
1255 DCHECK(host); | 1249 DCHECK(host); |
1256 | 1250 |
1257 string16 script_utf16 = ConvertJavaStringToUTF16(env, script); | 1251 if (!callback) { |
1258 return host->ExecuteJavascriptInWebFrameNotifyResult(string16(), | 1252 // No callback requested. |
1259 script_utf16); | 1253 host->ExecuteJavascriptInWebFrame(string16(), // frame_xpath |
| 1254 ConvertJavaStringToUTF16(env, script)); |
| 1255 return; |
| 1256 } |
| 1257 |
| 1258 // Secure the Java callback in a scoped object and give ownership of it to the |
| 1259 // base::Callback. |
| 1260 ScopedJavaGlobalRef<jobject>* j_callback = new ScopedJavaGlobalRef<jobject>(); |
| 1261 j_callback->Reset(env, callback); |
| 1262 content::RenderViewHost::JavascriptResultCallback c_callback = |
| 1263 base::Bind(&JavaScriptResultCallback, base::Owned(j_callback)); |
| 1264 |
| 1265 host->ExecuteJavascriptInWebFrameCallbackResult( |
| 1266 string16(), // frame_xpath |
| 1267 ConvertJavaStringToUTF16(env, script), |
| 1268 c_callback); |
1260 } | 1269 } |
1261 | 1270 |
1262 bool ContentViewCoreImpl::GetUseDesktopUserAgent( | 1271 bool ContentViewCoreImpl::GetUseDesktopUserAgent( |
1263 JNIEnv* env, jobject obj) { | 1272 JNIEnv* env, jobject obj) { |
1264 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); | 1273 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); |
1265 return entry && entry->GetIsOverridingUserAgent(); | 1274 return entry && entry->GetIsOverridingUserAgent(); |
1266 } | 1275 } |
1267 | 1276 |
1268 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, | 1277 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, |
1269 int text_input_type, | 1278 int text_input_type, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1325 jboolean hardware_accelerated, | 1334 jboolean hardware_accelerated, |
1326 jint native_web_contents, | 1335 jint native_web_contents, |
1327 jint native_window) { | 1336 jint native_window) { |
1328 ContentViewCoreImpl* view = new ContentViewCoreImpl( | 1337 ContentViewCoreImpl* view = new ContentViewCoreImpl( |
1329 env, obj, input_events_delivered_at_vsync, hardware_accelerated, | 1338 env, obj, input_events_delivered_at_vsync, hardware_accelerated, |
1330 reinterpret_cast<WebContents*>(native_web_contents), | 1339 reinterpret_cast<WebContents*>(native_web_contents), |
1331 reinterpret_cast<ui::WindowAndroid*>(native_window)); | 1340 reinterpret_cast<ui::WindowAndroid*>(native_window)); |
1332 return reinterpret_cast<jint>(view); | 1341 return reinterpret_cast<jint>(view); |
1333 } | 1342 } |
1334 | 1343 |
1335 jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) { | |
1336 ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>( | |
1337 ContentViewCore::GetNativeContentViewCore(env, obj)); | |
1338 DCHECK(view); | |
1339 | |
1340 return view->EvaluateJavaScript(env, obj, script); | |
1341 } | |
1342 | |
1343 bool RegisterContentViewCore(JNIEnv* env) { | 1344 bool RegisterContentViewCore(JNIEnv* env) { |
1344 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { | 1345 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { |
1345 DLOG(ERROR) << "Unable to find class ContentViewCore!"; | 1346 DLOG(ERROR) << "Unable to find class ContentViewCore!"; |
1346 return false; | 1347 return false; |
1347 } | 1348 } |
1348 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); | 1349 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); |
1349 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { | 1350 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { |
1350 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; | 1351 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; |
1351 return false; | 1352 return false; |
1352 } | 1353 } |
1353 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); | 1354 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); |
1354 | 1355 |
1355 return RegisterNativesImpl(env) >= 0; | 1356 return RegisterNativesImpl(env) >= 0; |
1356 } | 1357 } |
1357 | 1358 |
1358 } // namespace content | 1359 } // namespace content |
OLD | NEW |