Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / android / web_contents_observer_android.cc
blobdb50efc8eb3fc645c5ef789290f7bf06b40f4338
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"
7 #include <string>
9 #include <jni.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;
26 namespace content {
28 WebContentsObserverAndroid::WebContentsObserverAndroid(
29 JNIEnv* env,
30 jobject obj,
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) {
48 delete this;
51 void WebContentsObserverAndroid::WebContentsDestroyed(
52 WebContents* web_contents) {
53 JNIEnv* env = AttachCurrentThread();
54 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
55 if (obj.is_null()) {
56 delete this;
57 } else {
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));
67 if (obj.is_null())
68 return;
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));
79 if (obj.is_null())
80 return;
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));
91 if (obj.is_null())
92 return;
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(
100 int64 frame_id,
101 const base::string16& frame_unique_name,
102 bool is_main_frame,
103 const GURL& validated_url,
104 int error_code,
105 const base::string16& error_description,
106 RenderViewHost* render_view_host) {
107 DidFailLoadInternal(
108 true, is_main_frame, error_code, error_description, validated_url);
111 void WebContentsObserverAndroid::DidFailLoad(
112 int64 frame_id,
113 const GURL& validated_url,
114 bool is_main_frame,
115 int error_code,
116 const base::string16& error_description,
117 RenderViewHost* render_view_host) {
118 DidFailLoadInternal(
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));
127 if (obj.is_null())
128 return;
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));
146 if (obj.is_null())
147 return;
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(),
157 jboolean_is_reload);
160 void WebContentsObserverAndroid::DidStartProvisionalLoadForFrame(
161 int64 frame_id,
162 int64 parent_frame_id,
163 bool is_main_frame,
164 const GURL& validated_url,
165 bool is_error_page,
166 bool is_iframe_srcdoc,
167 RenderViewHost* render_view_host) {
168 JNIEnv* env = AttachCurrentThread();
169 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
170 if (obj.is_null())
171 return;
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(
180 int64 frame_id,
181 const base::string16& frame_unique_name,
182 bool is_main_frame,
183 const GURL& url,
184 PageTransition transition_type,
185 RenderViewHost* render_view_host) {
186 JNIEnv* env = AttachCurrentThread();
187 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
188 if (obj.is_null())
189 return;
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(),
194 transition_type);
197 void WebContentsObserverAndroid::DidFinishLoad(
198 int64 frame_id,
199 const GURL& validated_url,
200 bool is_main_frame,
201 RenderViewHost* render_view_host) {
202 JNIEnv* env = AttachCurrentThread();
203 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
204 if (obj.is_null())
205 return;
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));
224 if (obj.is_null())
225 return;
226 Java_WebContentsObserverAndroid_navigationEntryCommitted(env, obj.obj());
229 void WebContentsObserverAndroid::DidChangeVisibleSSLState() {
230 JNIEnv* env = AttachCurrentThread();
231 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
232 if (obj.is_null())
233 return;
234 Java_WebContentsObserverAndroid_didChangeVisibleSSLState(env, obj.obj());
237 void WebContentsObserverAndroid::DidAttachInterstitialPage() {
238 JNIEnv* env = AttachCurrentThread();
239 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
240 if (obj.is_null())
241 return;
242 Java_WebContentsObserverAndroid_didAttachInterstitialPage(env, obj.obj());
245 void WebContentsObserverAndroid::DidDetachInterstitialPage() {
246 JNIEnv* env = AttachCurrentThread();
247 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
248 if (obj.is_null())
249 return;
250 Java_WebContentsObserverAndroid_didDetachInterstitialPage(env, obj.obj());
253 void WebContentsObserverAndroid::DidFailLoadInternal(
254 bool is_provisional_load,
255 bool is_main_frame,
256 int error_code,
257 const base::string16& description,
258 const GURL& url) {
259 JNIEnv* env = AttachCurrentThread();
260 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
261 if (obj.is_null())
262 return;
263 ScopedJavaLocalRef<jstring> jstring_error_description(
264 ConvertUTF16ToJavaString(env, description));
265 ScopedJavaLocalRef<jstring> jstring_url(
266 ConvertUTF8ToJavaString(env, url.spec()));
268 Java_WebContentsObserverAndroid_didFailLoad(
269 env, obj.obj(),
270 is_provisional_load,
271 is_main_frame,
272 error_code,
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));
279 if (obj.is_null())
280 return;
281 Java_WebContentsObserverAndroid_didFirstVisuallyNonEmptyPaint(
282 env, obj.obj());
285 bool RegisterWebContentsObserverAndroid(JNIEnv* env) {
286 return RegisterNativesImpl(env);
288 } // namespace content