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 930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1212 } | 1234 } |
1213 | 1235 |
1214 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( | 1236 void ContentViewCoreImpl::UndoScrollFocusedEditableNodeIntoView( |
1215 JNIEnv* env, | 1237 JNIEnv* env, |
1216 jobject obj) { | 1238 jobject obj) { |
1217 RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1239 RenderViewHost* host = web_contents_->GetRenderViewHost(); |
1218 host->Send( | 1240 host->Send( |
1219 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); | 1241 new ViewMsg_UndoScrollFocusedEditableNodeIntoView(host->GetRoutingID())); |
1220 } | 1242 } |
1221 | 1243 |
1222 namespace { | 1244 jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, |
1223 void JavaScriptResultCallback(ScopedJavaGlobalRef<jobject>* message, | |
1224 const base::Value* result) { | |
1225 // |message| is passed as base::Owned, so it will automatically be deleted | |
1226 // when the callback goes out of scope. | |
1227 JNIEnv* env = base::android::AttachCurrentThread(); | |
1228 std::string json; | |
1229 base::JSONWriter::Write(result, &json); | |
1230 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json); | |
1231 Java_ContentViewCore_onEvaluateJavaScriptResult(env, | |
1232 j_json.obj(), | |
1233 message->obj()); | |
1234 } | |
1235 } // namespace | |
1236 | |
1237 void ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, | |
1238 jobject obj, | 1245 jobject obj, |
1239 jstring script, | 1246 jstring script) { |
1240 jobject message) { | |
1241 RenderViewHost* host = web_contents_->GetRenderViewHost(); | 1247 RenderViewHost* host = web_contents_->GetRenderViewHost(); |
1242 DCHECK(host); | 1248 DCHECK(host); |
1243 | 1249 |
1244 if (!message) { | 1250 string16 script_utf16 = ConvertJavaStringToUTF16(env, script); |
1245 // No callback requested. | 1251 return host->ExecuteJavascriptInWebFrameNotifyResult(string16(), |
1246 host->ExecuteJavascriptInWebFrame(string16(), // frame_xpath | 1252 script_utf16); |
1247 ConvertJavaStringToUTF16(env, script)); | |
1248 return; | |
1249 } | |
1250 | |
1251 // Secure the message in a scoped object and give ownership of it to the | |
1252 // callback. | |
1253 ScopedJavaGlobalRef<jobject>* j_message = new ScopedJavaGlobalRef<jobject>(); | |
1254 j_message->Reset(env, message); | |
1255 content::RenderViewHost::JavascriptResultCallback callback = | |
1256 base::Bind(&JavaScriptResultCallback, base::Owned(j_message)); | |
1257 | |
1258 host->ExecuteJavascriptInWebFrameCallbackResult( | |
1259 string16(), // frame_xpath | |
1260 ConvertJavaStringToUTF16(env, script), | |
1261 callback); | |
1262 } | 1253 } |
1263 | 1254 |
1264 bool ContentViewCoreImpl::GetUseDesktopUserAgent( | 1255 bool ContentViewCoreImpl::GetUseDesktopUserAgent( |
1265 JNIEnv* env, jobject obj) { | 1256 JNIEnv* env, jobject obj) { |
1266 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); | 1257 NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); |
1267 return entry && entry->GetIsOverridingUserAgent(); | 1258 return entry && entry->GetIsOverridingUserAgent(); |
1268 } | 1259 } |
1269 | 1260 |
1270 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, | 1261 void ContentViewCoreImpl::ImeUpdateAdapter(int native_ime_adapter, |
1271 int text_input_type, | 1262 int text_input_type, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1327 jboolean hardware_accelerated, | 1318 jboolean hardware_accelerated, |
1328 jint native_web_contents, | 1319 jint native_web_contents, |
1329 jint native_window) { | 1320 jint native_window) { |
1330 ContentViewCoreImpl* view = new ContentViewCoreImpl( | 1321 ContentViewCoreImpl* view = new ContentViewCoreImpl( |
1331 env, obj, input_events_delivered_at_vsync, hardware_accelerated, | 1322 env, obj, input_events_delivered_at_vsync, hardware_accelerated, |
1332 reinterpret_cast<WebContents*>(native_web_contents), | 1323 reinterpret_cast<WebContents*>(native_web_contents), |
1333 reinterpret_cast<ui::WindowAndroid*>(native_window)); | 1324 reinterpret_cast<ui::WindowAndroid*>(native_window)); |
1334 return reinterpret_cast<jint>(view); | 1325 return reinterpret_cast<jint>(view); |
1335 } | 1326 } |
1336 | 1327 |
| 1328 jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) { |
| 1329 ContentViewCoreImpl* view = static_cast<ContentViewCoreImpl*>( |
| 1330 ContentViewCore::GetNativeContentViewCore(env, obj)); |
| 1331 DCHECK(view); |
| 1332 |
| 1333 return view->EvaluateJavaScript(env, obj, script); |
| 1334 } |
| 1335 |
1337 bool RegisterContentViewCore(JNIEnv* env) { | 1336 bool RegisterContentViewCore(JNIEnv* env) { |
1338 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { | 1337 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { |
1339 DLOG(ERROR) << "Unable to find class ContentViewCore!"; | 1338 DLOG(ERROR) << "Unable to find class ContentViewCore!"; |
1340 return false; | 1339 return false; |
1341 } | 1340 } |
1342 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); | 1341 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); |
1343 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { | 1342 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { |
1344 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; | 1343 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; |
1345 return false; | 1344 return false; |
1346 } | 1345 } |
1347 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); | 1346 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); |
1348 | 1347 |
1349 return RegisterNativesImpl(env) >= 0; | 1348 return RegisterNativesImpl(env) >= 0; |
1350 } | 1349 } |
1351 | 1350 |
1352 } // namespace content | 1351 } // namespace content |
OLD | NEW |