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.
5 #include "content/browser/android/web_contents_observer_android.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_string.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "content/browser/android/content_view_core_impl.h"
15 #include "content/browser/renderer_host/render_widget_host_impl.h"
16 #include "content/browser/web_contents/web_contents_impl.h"
17 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "jni/WebContentsObserverAndroid_jni.h"
21 using base::android::AttachCurrentThread
;
22 using base::android::ScopedJavaLocalRef
;
23 using base::android::ConvertUTF8ToJavaString
;
24 using base::android::ConvertUTF16ToJavaString
;
28 WebContentsObserverAndroid::WebContentsObserverAndroid(
31 WebContents
* web_contents
)
32 : WebContentsObserver(web_contents
),
33 weak_java_observer_(env
, obj
){
36 WebContentsObserverAndroid::~WebContentsObserverAndroid() {
39 jlong
Init(JNIEnv
* env
, jobject obj
, jlong native_content_view_core
) {
40 ContentViewCore
* content_view_core
=
41 reinterpret_cast<ContentViewCore
*>(native_content_view_core
);
42 WebContentsObserverAndroid
* native_observer
= new WebContentsObserverAndroid(
43 env
, obj
, content_view_core
->GetWebContents());
44 return reinterpret_cast<intptr_t>(native_observer
);
47 void WebContentsObserverAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
51 void WebContentsObserverAndroid::WebContentsDestroyed(
52 WebContents
* web_contents
) {
53 JNIEnv
* env
= AttachCurrentThread();
54 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
58 // The java side will destroy |this|
59 Java_WebContentsObserverAndroid_detachFromWebContents(env
, obj
.obj());
63 void WebContentsObserverAndroid::RenderProcessGone(
64 base::TerminationStatus termination_status
) {
65 JNIEnv
* env
= AttachCurrentThread();
66 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
69 jboolean was_oom_protected
=
70 termination_status
== base::TERMINATION_STATUS_OOM_PROTECTED
;
71 Java_WebContentsObserverAndroid_renderProcessGone(
72 env
, obj
.obj(), was_oom_protected
);
75 void WebContentsObserverAndroid::DidStartLoading(
76 RenderViewHost
* render_view_host
) {
77 JNIEnv
* env
= AttachCurrentThread();
78 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
81 ScopedJavaLocalRef
<jstring
> jstring_url(ConvertUTF8ToJavaString(
82 env
, web_contents()->GetVisibleURL().spec()));
83 Java_WebContentsObserverAndroid_didStartLoading(
84 env
, obj
.obj(), jstring_url
.obj());
87 void WebContentsObserverAndroid::DidStopLoading(
88 RenderViewHost
* render_view_host
) {
89 JNIEnv
* env
= AttachCurrentThread();
90 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
93 ScopedJavaLocalRef
<jstring
> jstring_url(ConvertUTF8ToJavaString(
94 env
, web_contents()->GetLastCommittedURL().spec()));
95 Java_WebContentsObserverAndroid_didStopLoading(
96 env
, obj
.obj(), jstring_url
.obj());
99 void WebContentsObserverAndroid::DidFailProvisionalLoad(
101 const base::string16
& frame_unique_name
,
103 const GURL
& validated_url
,
105 const base::string16
& error_description
,
106 RenderViewHost
* render_view_host
) {
108 true, is_main_frame
, error_code
, error_description
, validated_url
);
111 void WebContentsObserverAndroid::DidFailLoad(
113 const GURL
& validated_url
,
116 const base::string16
& error_description
,
117 RenderViewHost
* render_view_host
) {
119 false, is_main_frame
, error_code
, error_description
, validated_url
);
122 void WebContentsObserverAndroid::DidNavigateMainFrame(
123 const LoadCommittedDetails
& details
,
124 const FrameNavigateParams
& params
) {
125 JNIEnv
* env
= AttachCurrentThread();
126 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
129 ScopedJavaLocalRef
<jstring
> jstring_url(
130 ConvertUTF8ToJavaString(env
, params
.url
.spec()));
131 ScopedJavaLocalRef
<jstring
> jstring_base_url(
132 ConvertUTF8ToJavaString(env
, params
.base_url
.spec()));
133 // See http://crbug.com/251330 for why it's determined this way.
134 bool in_page_navigation
=
135 details
.type
== NAVIGATION_TYPE_IN_PAGE
|| details
.is_in_page
;
136 Java_WebContentsObserverAndroid_didNavigateMainFrame(
137 env
, obj
.obj(), jstring_url
.obj(), jstring_base_url
.obj(),
138 details
.is_navigation_to_different_page(), in_page_navigation
);
141 void WebContentsObserverAndroid::DidNavigateAnyFrame(
142 const LoadCommittedDetails
& details
,
143 const FrameNavigateParams
& params
) {
144 JNIEnv
* env
= AttachCurrentThread();
145 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
148 ScopedJavaLocalRef
<jstring
> jstring_url(
149 ConvertUTF8ToJavaString(env
, params
.url
.spec()));
150 ScopedJavaLocalRef
<jstring
> jstring_base_url(
151 ConvertUTF8ToJavaString(env
, params
.base_url
.spec()));
152 jboolean jboolean_is_reload
=
153 PageTransitionCoreTypeIs(params
.transition
, PAGE_TRANSITION_RELOAD
);
155 Java_WebContentsObserverAndroid_didNavigateAnyFrame(
156 env
, obj
.obj(), jstring_url
.obj(), jstring_base_url
.obj(),
160 void WebContentsObserverAndroid::DidStartProvisionalLoadForFrame(
162 int64 parent_frame_id
,
164 const GURL
& validated_url
,
166 bool is_iframe_srcdoc
,
167 RenderViewHost
* render_view_host
) {
168 JNIEnv
* env
= AttachCurrentThread();
169 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
172 ScopedJavaLocalRef
<jstring
> jstring_url(
173 ConvertUTF8ToJavaString(env
, validated_url
.spec()));
174 Java_WebContentsObserverAndroid_didStartProvisionalLoadForFrame(
175 env
, obj
.obj(), frame_id
, parent_frame_id
, is_main_frame
,
176 jstring_url
.obj(), is_error_page
, is_iframe_srcdoc
);
179 void WebContentsObserverAndroid::DidCommitProvisionalLoadForFrame(
181 const base::string16
& frame_unique_name
,
184 PageTransition transition_type
,
185 RenderViewHost
* render_view_host
) {
186 JNIEnv
* env
= AttachCurrentThread();
187 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
190 ScopedJavaLocalRef
<jstring
> jstring_url(
191 ConvertUTF8ToJavaString(env
, url
.spec()));
192 Java_WebContentsObserverAndroid_didCommitProvisionalLoadForFrame(
193 env
, obj
.obj(), frame_id
, is_main_frame
, jstring_url
.obj(),
197 void WebContentsObserverAndroid::DidFinishLoad(
199 const GURL
& validated_url
,
201 RenderViewHost
* render_view_host
) {
202 JNIEnv
* env
= AttachCurrentThread();
203 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
207 std::string url_string
= validated_url
.spec();
208 NavigationEntry
* entry
=
209 web_contents()->GetController().GetLastCommittedEntry();
210 // Note that GetBaseURLForDataURL is only used by the Android WebView.
211 if (entry
&& !entry
->GetBaseURLForDataURL().is_empty())
212 url_string
= entry
->GetBaseURLForDataURL().possibly_invalid_spec();
214 ScopedJavaLocalRef
<jstring
> jstring_url(
215 ConvertUTF8ToJavaString(env
, url_string
));
216 Java_WebContentsObserverAndroid_didFinishLoad(
217 env
, obj
.obj(), frame_id
, jstring_url
.obj(), is_main_frame
);
220 void WebContentsObserverAndroid::NavigationEntryCommitted(
221 const LoadCommittedDetails
& load_details
) {
222 JNIEnv
* env
= AttachCurrentThread();
223 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
226 Java_WebContentsObserverAndroid_navigationEntryCommitted(env
, obj
.obj());
229 void WebContentsObserverAndroid::DidChangeVisibleSSLState() {
230 JNIEnv
* env
= AttachCurrentThread();
231 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
234 Java_WebContentsObserverAndroid_didChangeVisibleSSLState(env
, obj
.obj());
237 void WebContentsObserverAndroid::DidAttachInterstitialPage() {
238 JNIEnv
* env
= AttachCurrentThread();
239 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
242 Java_WebContentsObserverAndroid_didAttachInterstitialPage(env
, obj
.obj());
245 void WebContentsObserverAndroid::DidDetachInterstitialPage() {
246 JNIEnv
* env
= AttachCurrentThread();
247 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
250 Java_WebContentsObserverAndroid_didDetachInterstitialPage(env
, obj
.obj());
253 void WebContentsObserverAndroid::DidFailLoadInternal(
254 bool is_provisional_load
,
257 const base::string16
& description
,
259 JNIEnv
* env
= AttachCurrentThread();
260 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
263 ScopedJavaLocalRef
<jstring
> jstring_error_description(
264 ConvertUTF16ToJavaString(env
, description
));
265 ScopedJavaLocalRef
<jstring
> jstring_url(
266 ConvertUTF8ToJavaString(env
, url
.spec()));
268 Java_WebContentsObserverAndroid_didFailLoad(
273 jstring_error_description
.obj(), jstring_url
.obj());
276 void WebContentsObserverAndroid::DidFirstVisuallyNonEmptyPaint() {
277 JNIEnv
* env
= AttachCurrentThread();
278 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
281 Java_WebContentsObserverAndroid_didFirstVisuallyNonEmptyPaint(
285 bool RegisterWebContentsObserverAndroid(JNIEnv
* env
) {
286 return RegisterNativesImpl(env
);
288 } // namespace content