| 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/web_contents_observer_android.h" | 5 #include "content/browser/android/web_contents_observer_android.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include <jni.h> | 9 #include <jni.h> |
| 10 | 10 |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
| 14 #include "content/browser/renderer_host/render_widget_host_impl.h" | 14 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 15 #include "content/browser/web_contents/web_contents_impl.h" | 15 #include "content/browser/web_contents/web_contents_impl.h" |
| 16 #include "content/public/browser/navigation_details.h" | 16 #include "content/public/browser/navigation_details.h" |
| 17 #include "content/public/browser/navigation_entry.h" | 17 #include "content/public/browser/navigation_entry.h" |
| 18 #include "jni/WebContentsObserverAndroid_jni.h" | 18 #include "jni/WebContentsObserverAndroid_jni.h" |
| 19 | 19 |
| 20 using base::android::AttachCurrentThread; | 20 using base::android::AttachCurrentThread; |
| 21 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
| 22 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
| 23 using base::android::ConvertUTF16ToJavaString; | 23 using base::android::ConvertUTF16ToJavaString; |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 | 26 |
| 27 // TODO(dcheng): File a bug. This class incorrectly passes just a frame ID, |
| 28 // which is not sufficient to identify a frame (since frame IDs are scoped per |
| 29 // render process, and so may collide). |
| 27 WebContentsObserverAndroid::WebContentsObserverAndroid( | 30 WebContentsObserverAndroid::WebContentsObserverAndroid( |
| 28 JNIEnv* env, | 31 JNIEnv* env, |
| 29 jobject obj, | 32 jobject obj, |
| 30 WebContents* web_contents) | 33 WebContents* web_contents) |
| 31 : WebContentsObserver(web_contents), | 34 : WebContentsObserver(web_contents), |
| 32 weak_java_observer_(env, obj){ | 35 weak_java_observer_(env, obj){ |
| 33 } | 36 } |
| 34 | 37 |
| 35 WebContentsObserverAndroid::~WebContentsObserverAndroid() { | 38 WebContentsObserverAndroid::~WebContentsObserverAndroid() { |
| 36 } | 39 } |
| 37 | 40 |
| 38 jlong Init(JNIEnv* env, jobject obj, jobject java_web_contents) { | 41 jlong Init(JNIEnv* env, jobject obj, jobject java_web_contents) { |
| 39 content::WebContents* web_contents = | 42 WebContents* web_contents = |
| 40 content::WebContents::FromJavaWebContents(java_web_contents); | 43 WebContents::FromJavaWebContents(java_web_contents); |
| 41 CHECK(web_contents); | 44 CHECK(web_contents); |
| 42 | 45 |
| 43 WebContentsObserverAndroid* native_observer = new WebContentsObserverAndroid( | 46 WebContentsObserverAndroid* native_observer = new WebContentsObserverAndroid( |
| 44 env, obj, web_contents); | 47 env, obj, web_contents); |
| 45 return reinterpret_cast<intptr_t>(native_observer); | 48 return reinterpret_cast<intptr_t>(native_observer); |
| 46 } | 49 } |
| 47 | 50 |
| 48 void WebContentsObserverAndroid::Destroy(JNIEnv* env, jobject obj) { | 51 void WebContentsObserverAndroid::Destroy(JNIEnv* env, jobject obj) { |
| 49 delete this; | 52 delete this; |
| 50 } | 53 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); | 93 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); |
| 91 if (obj.is_null()) | 94 if (obj.is_null()) |
| 92 return; | 95 return; |
| 93 ScopedJavaLocalRef<jstring> jstring_url(ConvertUTF8ToJavaString( | 96 ScopedJavaLocalRef<jstring> jstring_url(ConvertUTF8ToJavaString( |
| 94 env, web_contents()->GetLastCommittedURL().spec())); | 97 env, web_contents()->GetLastCommittedURL().spec())); |
| 95 Java_WebContentsObserverAndroid_didStopLoading( | 98 Java_WebContentsObserverAndroid_didStopLoading( |
| 96 env, obj.obj(), jstring_url.obj()); | 99 env, obj.obj(), jstring_url.obj()); |
| 97 } | 100 } |
| 98 | 101 |
| 99 void WebContentsObserverAndroid::DidFailProvisionalLoad( | 102 void WebContentsObserverAndroid::DidFailProvisionalLoad( |
| 100 content::RenderFrameHost* render_frame_host, | 103 RenderFrameHost* render_frame_host, |
| 101 const GURL& validated_url, | 104 const GURL& validated_url, |
| 102 int error_code, | 105 int error_code, |
| 103 const base::string16& error_description) { | 106 const base::string16& error_description) { |
| 104 DidFailLoadInternal(true, | 107 DidFailLoadInternal(true, |
| 105 !render_frame_host->GetParent(), | 108 !render_frame_host->GetParent(), |
| 106 error_code, | 109 error_code, |
| 107 error_description, | 110 error_description, |
| 108 validated_url); | 111 validated_url); |
| 109 } | 112 } |
| 110 | 113 |
| 111 void WebContentsObserverAndroid::DidFailLoad( | 114 void WebContentsObserverAndroid::DidFailLoad( |
| 112 int64 frame_id, | 115 RenderFrameHost* render_frame_host, |
| 113 const GURL& validated_url, | 116 const GURL& validated_url, |
| 114 bool is_main_frame, | |
| 115 int error_code, | 117 int error_code, |
| 116 const base::string16& error_description, | 118 const base::string16& error_description) { |
| 117 RenderViewHost* render_view_host) { | 119 DidFailLoadInternal(false, |
| 118 DidFailLoadInternal( | 120 !render_frame_host->GetParent(), |
| 119 false, is_main_frame, error_code, error_description, validated_url); | 121 error_code, |
| 122 error_description, |
| 123 validated_url); |
| 120 } | 124 } |
| 121 | 125 |
| 122 void WebContentsObserverAndroid::DidNavigateMainFrame( | 126 void WebContentsObserverAndroid::DidNavigateMainFrame( |
| 123 const LoadCommittedDetails& details, | 127 const LoadCommittedDetails& details, |
| 124 const FrameNavigateParams& params) { | 128 const FrameNavigateParams& params) { |
| 125 JNIEnv* env = AttachCurrentThread(); | 129 JNIEnv* env = AttachCurrentThread(); |
| 126 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); | 130 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); |
| 127 if (obj.is_null()) | 131 if (obj.is_null()) |
| 128 return; | 132 return; |
| 129 ScopedJavaLocalRef<jstring> jstring_url( | 133 ScopedJavaLocalRef<jstring> jstring_url( |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 Java_WebContentsObserverAndroid_didCommitProvisionalLoadForFrame( | 211 Java_WebContentsObserverAndroid_didCommitProvisionalLoadForFrame( |
| 208 env, | 212 env, |
| 209 obj.obj(), | 213 obj.obj(), |
| 210 render_frame_host->GetRoutingID(), | 214 render_frame_host->GetRoutingID(), |
| 211 !render_frame_host->GetParent(), | 215 !render_frame_host->GetParent(), |
| 212 jstring_url.obj(), | 216 jstring_url.obj(), |
| 213 transition_type); | 217 transition_type); |
| 214 } | 218 } |
| 215 | 219 |
| 216 void WebContentsObserverAndroid::DidFinishLoad( | 220 void WebContentsObserverAndroid::DidFinishLoad( |
| 217 int64 frame_id, | 221 RenderFrameHost* render_frame_host, |
| 218 const GURL& validated_url, | 222 const GURL& validated_url) { |
| 219 bool is_main_frame, | |
| 220 RenderViewHost* render_view_host) { | |
| 221 JNIEnv* env = AttachCurrentThread(); | 223 JNIEnv* env = AttachCurrentThread(); |
| 222 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); | 224 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); |
| 223 if (obj.is_null()) | 225 if (obj.is_null()) |
| 224 return; | 226 return; |
| 225 | 227 |
| 226 std::string url_string = validated_url.spec(); | 228 std::string url_string = validated_url.spec(); |
| 227 NavigationEntry* entry = | 229 NavigationEntry* entry = |
| 228 web_contents()->GetController().GetLastCommittedEntry(); | 230 web_contents()->GetController().GetLastCommittedEntry(); |
| 229 // Note that GetBaseURLForDataURL is only used by the Android WebView. | 231 // Note that GetBaseURLForDataURL is only used by the Android WebView. |
| 230 if (entry && !entry->GetBaseURLForDataURL().is_empty()) | 232 if (entry && !entry->GetBaseURLForDataURL().is_empty()) |
| 231 url_string = entry->GetBaseURLForDataURL().possibly_invalid_spec(); | 233 url_string = entry->GetBaseURLForDataURL().possibly_invalid_spec(); |
| 232 | 234 |
| 233 ScopedJavaLocalRef<jstring> jstring_url( | 235 ScopedJavaLocalRef<jstring> jstring_url( |
| 234 ConvertUTF8ToJavaString(env, url_string)); | 236 ConvertUTF8ToJavaString(env, url_string)); |
| 235 Java_WebContentsObserverAndroid_didFinishLoad( | 237 Java_WebContentsObserverAndroid_didFinishLoad( |
| 236 env, obj.obj(), frame_id, jstring_url.obj(), is_main_frame); | 238 env, |
| 239 obj.obj(), |
| 240 render_frame_host->GetRoutingID(), |
| 241 jstring_url.obj(), |
| 242 !render_frame_host->GetParent()); |
| 237 } | 243 } |
| 238 | 244 |
| 239 void WebContentsObserverAndroid::DocumentLoadedInFrame( | 245 void WebContentsObserverAndroid::DocumentLoadedInFrame( |
| 240 int64 frame_id, | 246 RenderFrameHost* render_frame_host) { |
| 241 RenderViewHost* render_view_host) { | |
| 242 JNIEnv* env = AttachCurrentThread(); | 247 JNIEnv* env = AttachCurrentThread(); |
| 243 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); | 248 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); |
| 244 if (obj.is_null()) | 249 if (obj.is_null()) |
| 245 return; | 250 return; |
| 246 Java_WebContentsObserverAndroid_documentLoadedInFrame( | 251 Java_WebContentsObserverAndroid_documentLoadedInFrame( |
| 247 env, obj.obj(), frame_id); | 252 env, obj.obj(), render_frame_host->GetRoutingID()); |
| 248 } | 253 } |
| 249 | 254 |
| 250 void WebContentsObserverAndroid::NavigationEntryCommitted( | 255 void WebContentsObserverAndroid::NavigationEntryCommitted( |
| 251 const LoadCommittedDetails& load_details) { | 256 const LoadCommittedDetails& load_details) { |
| 252 JNIEnv* env = AttachCurrentThread(); | 257 JNIEnv* env = AttachCurrentThread(); |
| 253 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); | 258 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env)); |
| 254 if (obj.is_null()) | 259 if (obj.is_null()) |
| 255 return; | 260 return; |
| 256 Java_WebContentsObserverAndroid_navigationEntryCommitted(env, obj.obj()); | 261 Java_WebContentsObserverAndroid_navigationEntryCommitted(env, obj.obj()); |
| 257 } | 262 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 if (obj.is_null()) | 314 if (obj.is_null()) |
| 310 return; | 315 return; |
| 311 Java_WebContentsObserverAndroid_didFirstVisuallyNonEmptyPaint( | 316 Java_WebContentsObserverAndroid_didFirstVisuallyNonEmptyPaint( |
| 312 env, obj.obj()); | 317 env, obj.obj()); |
| 313 } | 318 } |
| 314 | 319 |
| 315 bool RegisterWebContentsObserverAndroid(JNIEnv* env) { | 320 bool RegisterWebContentsObserverAndroid(JNIEnv* env) { |
| 316 return RegisterNativesImpl(env); | 321 return RegisterNativesImpl(env); |
| 317 } | 322 } |
| 318 } // namespace content | 323 } // namespace content |
| OLD | NEW |