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 Java_WebContentsObserverAndroid_didNavigateMainFrame(
134 env
, obj
.obj(), jstring_url
.obj(), jstring_base_url
.obj(),
135 details
.is_navigation_to_different_page());
138 void WebContentsObserverAndroid::DidNavigateAnyFrame(
139 const LoadCommittedDetails
& details
,
140 const FrameNavigateParams
& params
) {
141 JNIEnv
* env
= AttachCurrentThread();
142 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
145 ScopedJavaLocalRef
<jstring
> jstring_url(
146 ConvertUTF8ToJavaString(env
, params
.url
.spec()));
147 ScopedJavaLocalRef
<jstring
> jstring_base_url(
148 ConvertUTF8ToJavaString(env
, params
.base_url
.spec()));
149 jboolean jboolean_is_reload
=
150 PageTransitionCoreTypeIs(params
.transition
, PAGE_TRANSITION_RELOAD
);
152 Java_WebContentsObserverAndroid_didNavigateAnyFrame(
153 env
, obj
.obj(), jstring_url
.obj(), jstring_base_url
.obj(),
157 void WebContentsObserverAndroid::DidStartProvisionalLoadForFrame(
159 int64 parent_frame_id
,
161 const GURL
& validated_url
,
163 bool is_iframe_srcdoc
,
164 RenderViewHost
* render_view_host
) {
165 JNIEnv
* env
= AttachCurrentThread();
166 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
169 ScopedJavaLocalRef
<jstring
> jstring_url(
170 ConvertUTF8ToJavaString(env
, validated_url
.spec()));
171 Java_WebContentsObserverAndroid_didStartProvisionalLoadForFrame(
172 env
, obj
.obj(), frame_id
, parent_frame_id
, is_main_frame
,
173 jstring_url
.obj(), is_error_page
, is_iframe_srcdoc
);
176 void WebContentsObserverAndroid::DidCommitProvisionalLoadForFrame(
178 const base::string16
& frame_unique_name
,
181 PageTransition transition_type
,
182 RenderViewHost
* render_view_host
) {
183 JNIEnv
* env
= AttachCurrentThread();
184 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
187 ScopedJavaLocalRef
<jstring
> jstring_url(
188 ConvertUTF8ToJavaString(env
, url
.spec()));
189 Java_WebContentsObserverAndroid_didCommitProvisionalLoadForFrame(
190 env
, obj
.obj(), frame_id
, is_main_frame
, jstring_url
.obj(),
194 void WebContentsObserverAndroid::DidFinishLoad(
196 const GURL
& validated_url
,
198 RenderViewHost
* render_view_host
) {
199 JNIEnv
* env
= AttachCurrentThread();
200 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
204 std::string url_string
= validated_url
.spec();
205 NavigationEntry
* entry
=
206 web_contents()->GetController().GetLastCommittedEntry();
207 // Note that GetBaseURLForDataURL is only used by the Android WebView.
208 if (entry
&& !entry
->GetBaseURLForDataURL().is_empty())
209 url_string
= entry
->GetBaseURLForDataURL().possibly_invalid_spec();
211 ScopedJavaLocalRef
<jstring
> jstring_url(
212 ConvertUTF8ToJavaString(env
, url_string
));
213 Java_WebContentsObserverAndroid_didFinishLoad(
214 env
, obj
.obj(), frame_id
, jstring_url
.obj(), is_main_frame
);
217 void WebContentsObserverAndroid::NavigationEntryCommitted(
218 const LoadCommittedDetails
& load_details
) {
219 JNIEnv
* env
= AttachCurrentThread();
220 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
223 Java_WebContentsObserverAndroid_navigationEntryCommitted(env
, obj
.obj());
226 void WebContentsObserverAndroid::DidChangeVisibleSSLState() {
227 JNIEnv
* env
= AttachCurrentThread();
228 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
231 Java_WebContentsObserverAndroid_didChangeVisibleSSLState(env
, obj
.obj());
234 void WebContentsObserverAndroid::DidAttachInterstitialPage() {
235 JNIEnv
* env
= AttachCurrentThread();
236 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
239 Java_WebContentsObserverAndroid_didAttachInterstitialPage(env
, obj
.obj());
242 void WebContentsObserverAndroid::DidDetachInterstitialPage() {
243 JNIEnv
* env
= AttachCurrentThread();
244 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
247 Java_WebContentsObserverAndroid_didDetachInterstitialPage(env
, obj
.obj());
250 void WebContentsObserverAndroid::DidFailLoadInternal(
251 bool is_provisional_load
,
254 const base::string16
& description
,
256 JNIEnv
* env
= AttachCurrentThread();
257 ScopedJavaLocalRef
<jobject
> obj(weak_java_observer_
.get(env
));
260 ScopedJavaLocalRef
<jstring
> jstring_error_description(
261 ConvertUTF16ToJavaString(env
, description
));
262 ScopedJavaLocalRef
<jstring
> jstring_url(
263 ConvertUTF8ToJavaString(env
, url
.spec()));
265 Java_WebContentsObserverAndroid_didFailLoad(
270 jstring_error_description
.obj(), jstring_url
.obj());
273 bool RegisterWebContentsObserverAndroid(JNIEnv
* env
) {
274 return RegisterNativesImpl(env
);
276 } // namespace content