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" | |
15 #include "cc/layer.h" | 14 #include "cc/layer.h" |
16 #include "content/browser/android/interstitial_page_delegate_android.h" | 15 #include "content/browser/android/interstitial_page_delegate_android.h" |
17 #include "content/browser/android/load_url_params.h" | 16 #include "content/browser/android/load_url_params.h" |
18 #include "content/browser/android/touch_point.h" | 17 #include "content/browser/android/touch_point.h" |
19 #include "content/browser/renderer_host/java/java_bound_object.h" | 18 #include "content/browser/renderer_host/java/java_bound_object.h" |
20 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager
.h" | 19 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager
.h" |
21 #include "content/browser/renderer_host/compositor_impl_android.h" | 20 #include "content/browser/renderer_host/compositor_impl_android.h" |
22 #include "content/browser/renderer_host/render_view_host_impl.h" | 21 #include "content/browser/renderer_host/render_view_host_impl.h" |
23 #include "content/browser/renderer_host/render_widget_host_impl.h" | 22 #include "content/browser/renderer_host/render_widget_host_impl.h" |
24 #include "content/browser/renderer_host/render_widget_host_view_android.h" | 23 #include "content/browser/renderer_host/render_widget_host_view_android.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 167 |
169 InitJNI(env, obj); | 168 InitJNI(env, obj); |
170 | 169 |
171 if (!gfx::Screen::GetNativeScreen()->IsDIPEnabled()) { | 170 if (!gfx::Screen::GetNativeScreen()->IsDIPEnabled()) { |
172 dpi_scale_ = 1; | 171 dpi_scale_ = 1; |
173 } else { | 172 } else { |
174 scoped_ptr<content::DeviceInfo> device_info(new content::DeviceInfo()); | 173 scoped_ptr<content::DeviceInfo> device_info(new content::DeviceInfo()); |
175 dpi_scale_ = device_info->GetDPIScale(); | 174 dpi_scale_ = device_info->GetDPIScale(); |
176 } | 175 } |
177 | 176 |
| 177 notification_registrar_.Add(this, |
| 178 NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, |
| 179 NotificationService::AllSources()); |
| 180 |
178 // Currently, the only use case we have for overriding a user agent involves | 181 // Currently, the only use case we have for overriding a user agent involves |
179 // spoofing a desktop Linux user agent for "Request desktop site". | 182 // spoofing a desktop Linux user agent for "Request desktop site". |
180 // Automatically set it for all WebContents so that it is available when a | 183 // Automatically set it for all WebContents so that it is available when a |
181 // NavigationEntry requires the user agent to be overridden. | 184 // NavigationEntry requires the user agent to be overridden. |
182 const char kLinuxInfoStr[] = "X11; Linux x86_64"; | 185 const char kLinuxInfoStr[] = "X11; Linux x86_64"; |
183 std::string product = content::GetContentClient()->GetProduct(); | 186 std::string product = content::GetContentClient()->GetProduct(); |
184 std::string spoofed_ua = | 187 std::string spoofed_ua = |
185 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product); | 188 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product); |
186 web_contents->SetUserAgentOverride(spoofed_ua); | 189 web_contents->SetUserAgentOverride(spoofed_ua); |
187 | 190 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 int pid = GetRenderProcessIdFromRenderViewHost( | 265 int pid = GetRenderProcessIdFromRenderViewHost( |
263 web_contents_->GetRenderViewHost()); | 266 web_contents_->GetRenderViewHost()); |
264 JNIEnv* env = AttachCurrentThread(); | 267 JNIEnv* env = AttachCurrentThread(); |
265 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 268 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
266 if (!obj.is_null()) { | 269 if (!obj.is_null()) { |
267 Java_ContentViewCore_onRenderProcessSwap(env, obj.obj(), 0, pid); | 270 Java_ContentViewCore_onRenderProcessSwap(env, obj.obj(), 0, pid); |
268 } | 271 } |
269 } | 272 } |
270 break; | 273 break; |
271 } | 274 } |
| 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 } |
272 } | 294 } |
273 } | 295 } |
274 | 296 |
275 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { | 297 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { |
276 java_object_ = new JavaObject; | 298 java_object_ = new JavaObject; |
277 java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect")); | 299 java_object_->rect_clazz.Reset(GetClass(env, "android/graphics/Rect")); |
278 java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( | 300 java_object_->rect_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( |
279 env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V"); | 301 env, java_object_->rect_clazz.obj(), "<init>", "(IIII)V"); |
280 } | 302 } |
281 | 303 |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1219 } | 1241 } |
1220 | 1242 |
1221 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( | 1243 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( |
1222 JNIEnv* env, | 1244 JNIEnv* env, |
1223 jobject obj) { | 1245 jobject obj) { |
1224 RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1246 RenderViewHost* host = web_contents_->GetRenderViewHost(); |
1225 host->Send( | 1247 host->Send( |
1226 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); | 1248 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); |
1227 } | 1249 } |
1228 | 1250 |
1229 namespace { | 1251 jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, |
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, | |
1245 jobject obj, | 1252 jobject obj, |
1246 jstring script, | 1253 jstring script) { |
1247 jobject callback) { | |
1248 RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1254 RenderViewHost* host = web_contents_->GetRenderViewHost(); |
1249 DCHECK(host); | 1255 DCHECK(host); |
1250 | 1256 |
1251 if (!callback) { | 1257 string16 script_utf16 = ConvertJavaStringToUTF16(env, script); |
1252 // No callback requested. | 1258 return host->ExecuteJavascriptInWebFrameNotifyResult(string16(), |
1253 host->ExecuteJavascriptInWebFrame(string16(), // frame_xpath | 1259 script_utf16); |
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); | |
1269 } | 1260 } |
1270 | 1261 |
1271 bool ContentViewCoreImpl::GetUseDesktopUserAgent( | 1262 bool ContentViewCoreImpl::GetUseDesktopUserAgent( |
1272 JNIEnv* env, jobject obj) { | 1263 JNIEnv* env, jobject obj) { |
1273 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); | 1264 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); |
1274 return entry && entry->GetIsOverridingUserAgent(); | 1265 return entry && entry->GetIsOverridingUserAgent(); |
1275 } | 1266 } |
1276 | 1267 |
1277 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, | 1268 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, |
1278 int text_input_type, | 1269 int text_input_type, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1334 jboolean hardware_accelerated, | 1325 jboolean hardware_accelerated, |
1335 jint native_web_contents, | 1326 jint native_web_contents, |
1336 jint native_window) { | 1327 jint native_window) { |
1337 ContentViewCoreImpl* view = new ContentViewCoreImpl( | 1328 ContentViewCoreImpl* view = new ContentViewCoreImpl( |
1338 env, obj, input_events_delivered_at_vsync, hardware_accelerated, | 1329 env, obj, input_events_delivered_at_vsync, hardware_accelerated, |
1339 reinterpret_cast<WebContents*>(native_web_contents), | 1330 reinterpret_cast<WebContents*>(native_web_contents), |
1340 reinterpret_cast<ui::WindowAndroid*>(native_window)); | 1331 reinterpret_cast<ui::WindowAndroid*>(native_window)); |
1341 return reinterpret_cast<jint>(view); | 1332 return reinterpret_cast<jint>(view); |
1342 } | 1333 } |
1343 | 1334 |
| 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 |
1344 bool RegisterContentViewCore(JNIEnv* env) { | 1343 bool RegisterContentViewCore(JNIEnv* env) { |
1345 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { | 1344 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { |
1346 DLOG(ERROR) << "Unable to find class ContentViewCore!"; | 1345 DLOG(ERROR) << "Unable to find class ContentViewCore!"; |
1347 return false; | 1346 return false; |
1348 } | 1347 } |
1349 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); | 1348 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); |
1350 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { | 1349 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { |
1351 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; | 1350 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; |
1352 return false; | 1351 return false; |
1353 } | 1352 } |
1354 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); | 1353 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); |
1355 | 1354 |
1356 return RegisterNativesImpl(env) >= 0; | 1355 return RegisterNativesImpl(env) >= 0; |
1357 } | 1356 } |
1358 | 1357 |
1359 } // namespace content | 1358 } // namespace content |
OLD | NEW |