1 // Copyright 2015 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_proxy.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "content/browser/renderer_host/render_widget_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/public/browser/navigation_details.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "jni/WebContentsObserverProxy_jni.h"
18 using base::android::AttachCurrentThread
;
19 using base::android::ScopedJavaLocalRef
;
20 using base::android::ConvertUTF8ToJavaString
;
21 using base::android::ConvertUTF16ToJavaString
;
25 // TODO(dcheng): File a bug. This class incorrectly passes just a frame ID,
26 // which is not sufficient to identify a frame (since frame IDs are scoped per
27 // render process, and so may collide).
28 WebContentsObserverProxy::WebContentsObserverProxy(JNIEnv
* env
,
30 WebContents
* web_contents
)
31 : WebContentsObserver(web_contents
) {
33 java_observer_
.Reset(env
, obj
);
36 WebContentsObserverProxy::~WebContentsObserverProxy() {
39 jlong
Init(JNIEnv
* env
, jobject obj
, jobject java_web_contents
) {
40 WebContents
* web_contents
=
41 WebContents::FromJavaWebContents(java_web_contents
);
44 WebContentsObserverProxy
* native_observer
=
45 new WebContentsObserverProxy(env
, obj
, web_contents
);
46 return reinterpret_cast<intptr_t>(native_observer
);
49 void WebContentsObserverProxy::Destroy(JNIEnv
* env
, jobject obj
) {
53 void WebContentsObserverProxy::WebContentsDestroyed() {
54 JNIEnv
* env
= AttachCurrentThread();
55 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
56 // The java side will destroy |this|
57 Java_WebContentsObserverProxy_destroy(env
, obj
.obj());
60 void WebContentsObserverProxy::RenderViewReady() {
61 JNIEnv
* env
= AttachCurrentThread();
62 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
63 Java_WebContentsObserverProxy_renderViewReady(env
, obj
.obj());
66 void WebContentsObserverProxy::RenderProcessGone(
67 base::TerminationStatus termination_status
) {
68 JNIEnv
* env
= AttachCurrentThread();
69 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
70 jboolean was_oom_protected
=
71 termination_status
== base::TERMINATION_STATUS_OOM_PROTECTED
;
72 Java_WebContentsObserverProxy_renderProcessGone(env
, obj
.obj(),
76 void WebContentsObserverProxy::DidStartLoading() {
77 JNIEnv
* env
= AttachCurrentThread();
78 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
79 ScopedJavaLocalRef
<jstring
> jstring_url(
80 ConvertUTF8ToJavaString(env
, web_contents()->GetVisibleURL().spec()));
81 Java_WebContentsObserverProxy_didStartLoading(env
, obj
.obj(),
85 void WebContentsObserverProxy::DidStopLoading() {
86 JNIEnv
* env
= AttachCurrentThread();
87 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
88 ScopedJavaLocalRef
<jstring
> jstring_url(ConvertUTF8ToJavaString(
89 env
, web_contents()->GetLastCommittedURL().spec()));
90 Java_WebContentsObserverProxy_didStopLoading(env
, obj
.obj(),
94 void WebContentsObserverProxy::DidFailProvisionalLoad(
95 RenderFrameHost
* render_frame_host
,
96 const GURL
& validated_url
,
98 const base::string16
& error_description
) {
99 DidFailLoadInternal(true, !render_frame_host
->GetParent(), error_code
,
100 error_description
, validated_url
);
103 void WebContentsObserverProxy::DidFailLoad(
104 RenderFrameHost
* render_frame_host
,
105 const GURL
& validated_url
,
107 const base::string16
& error_description
) {
108 DidFailLoadInternal(false, !render_frame_host
->GetParent(), error_code
,
109 error_description
, validated_url
);
112 void WebContentsObserverProxy::DidNavigateMainFrame(
113 const LoadCommittedDetails
& details
,
114 const FrameNavigateParams
& params
) {
115 JNIEnv
* env
= AttachCurrentThread();
116 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
117 ScopedJavaLocalRef
<jstring
> jstring_url(
118 ConvertUTF8ToJavaString(env
, params
.url
.spec()));
119 ScopedJavaLocalRef
<jstring
> jstring_base_url(
120 ConvertUTF8ToJavaString(env
, params
.base_url
.spec()));
122 // See http://crbug.com/251330 for why it's determined this way.
123 url::Replacements
<char> replacements
;
124 replacements
.ClearRef();
125 bool urls_same_ignoring_fragment
=
126 params
.url
.ReplaceComponents(replacements
) ==
127 details
.previous_url
.ReplaceComponents(replacements
);
129 // is_fragment_navigation is indicative of the intent of this variable.
130 // However, there isn't sufficient information here to determine whether this
131 // is actually a fragment navigation, or a history API navigation to a URL
132 // that would also be valid for a fragment navigation.
133 bool is_fragment_navigation
=
134 urls_same_ignoring_fragment
&&
135 (details
.type
== NAVIGATION_TYPE_IN_PAGE
|| details
.is_in_page
);
136 Java_WebContentsObserverProxy_didNavigateMainFrame(
137 env
, obj
.obj(), jstring_url
.obj(), jstring_base_url
.obj(),
138 details
.is_navigation_to_different_page(), is_fragment_navigation
,
139 details
.http_status_code
);
142 void WebContentsObserverProxy::DidNavigateAnyFrame(
143 RenderFrameHost
* render_frame_host
,
144 const LoadCommittedDetails
& details
,
145 const FrameNavigateParams
& params
) {
146 JNIEnv
* env
= AttachCurrentThread();
147 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
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
= ui::PageTransitionCoreTypeIs(
153 params
.transition
, ui::PAGE_TRANSITION_RELOAD
);
155 Java_WebContentsObserverProxy_didNavigateAnyFrame(
156 env
, obj
.obj(), jstring_url
.obj(), jstring_base_url
.obj(),
160 void WebContentsObserverProxy::DocumentAvailableInMainFrame() {
161 JNIEnv
* env
= AttachCurrentThread();
162 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
163 Java_WebContentsObserverProxy_documentAvailableInMainFrame(env
, obj
.obj());
166 void WebContentsObserverProxy::DidStartProvisionalLoadForFrame(
167 RenderFrameHost
* render_frame_host
,
168 const GURL
& validated_url
,
170 bool is_iframe_srcdoc
) {
171 JNIEnv
* env
= AttachCurrentThread();
172 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
173 ScopedJavaLocalRef
<jstring
> jstring_url(
174 ConvertUTF8ToJavaString(env
, validated_url
.spec()));
175 // TODO(dcheng): Does Java really need the parent frame ID? It doesn't appear
176 // to be used at all, and it just adds complexity here.
177 Java_WebContentsObserverProxy_didStartProvisionalLoadForFrame(
178 env
, obj
.obj(), render_frame_host
->GetRoutingID(),
179 render_frame_host
->GetParent()
180 ? render_frame_host
->GetParent()->GetRoutingID()
182 !render_frame_host
->GetParent(), jstring_url
.obj(), is_error_page
,
186 void WebContentsObserverProxy::DidCommitProvisionalLoadForFrame(
187 RenderFrameHost
* render_frame_host
,
189 ui::PageTransition transition_type
) {
190 JNIEnv
* env
= AttachCurrentThread();
191 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
192 ScopedJavaLocalRef
<jstring
> jstring_url(
193 ConvertUTF8ToJavaString(env
, url
.spec()));
194 Java_WebContentsObserverProxy_didCommitProvisionalLoadForFrame(
195 env
, obj
.obj(), render_frame_host
->GetRoutingID(),
196 !render_frame_host
->GetParent(), jstring_url
.obj(), transition_type
);
199 void WebContentsObserverProxy::DidFinishLoad(RenderFrameHost
* render_frame_host
,
200 const GURL
& validated_url
) {
201 JNIEnv
* env
= AttachCurrentThread();
202 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
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_WebContentsObserverProxy_didFinishLoad(
214 env
, obj
.obj(), render_frame_host
->GetRoutingID(), jstring_url
.obj(),
215 !render_frame_host
->GetParent());
218 void WebContentsObserverProxy::DocumentLoadedInFrame(
219 RenderFrameHost
* render_frame_host
) {
220 JNIEnv
* env
= AttachCurrentThread();
221 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
222 Java_WebContentsObserverProxy_documentLoadedInFrame(
223 env
, obj
.obj(), render_frame_host
->GetRoutingID());
226 void WebContentsObserverProxy::NavigationEntryCommitted(
227 const LoadCommittedDetails
& load_details
) {
228 JNIEnv
* env
= AttachCurrentThread();
229 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
230 Java_WebContentsObserverProxy_navigationEntryCommitted(env
, obj
.obj());
233 void WebContentsObserverProxy::DidAttachInterstitialPage() {
234 JNIEnv
* env
= AttachCurrentThread();
235 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
236 Java_WebContentsObserverProxy_didAttachInterstitialPage(env
, obj
.obj());
239 void WebContentsObserverProxy::DidDetachInterstitialPage() {
240 JNIEnv
* env
= AttachCurrentThread();
241 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
242 Java_WebContentsObserverProxy_didDetachInterstitialPage(env
, obj
.obj());
245 void WebContentsObserverProxy::DidChangeThemeColor(SkColor color
) {
246 JNIEnv
* env
= AttachCurrentThread();
247 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
248 Java_WebContentsObserverProxy_didChangeThemeColor(env
, obj
.obj(), color
);
251 void WebContentsObserverProxy::DidFailLoadInternal(
252 bool is_provisional_load
,
255 const base::string16
& description
,
257 JNIEnv
* env
= AttachCurrentThread();
258 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
259 ScopedJavaLocalRef
<jstring
> jstring_error_description(
260 ConvertUTF16ToJavaString(env
, description
));
261 ScopedJavaLocalRef
<jstring
> jstring_url(
262 ConvertUTF8ToJavaString(env
, url
.spec()));
264 Java_WebContentsObserverProxy_didFailLoad(
265 env
, obj
.obj(), is_provisional_load
, is_main_frame
, error_code
,
266 jstring_error_description
.obj(), jstring_url
.obj());
269 void WebContentsObserverProxy::DidFirstVisuallyNonEmptyPaint() {
270 JNIEnv
* env
= AttachCurrentThread();
271 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
272 Java_WebContentsObserverProxy_didFirstVisuallyNonEmptyPaint(env
, obj
.obj());
275 void WebContentsObserverProxy::DidStartNavigationToPendingEntry(
277 NavigationController::ReloadType reload_type
) {
278 JNIEnv
* env
= AttachCurrentThread();
279 ScopedJavaLocalRef
<jobject
> obj(java_observer_
);
280 ScopedJavaLocalRef
<jstring
> jstring_url(
281 ConvertUTF8ToJavaString(env
, url
.spec()));
283 Java_WebContentsObserverProxy_didStartNavigationToPendingEntry(
284 env
, obj
.obj(), jstring_url
.obj());
287 bool RegisterWebContentsObserverProxy(JNIEnv
* env
) {
288 return RegisterNativesImpl(env
);
290 } // namespace content