IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / android / web_contents_observer_android.cc
blob235041be67422e2924b9bf106d613693048a5df0
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 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));
143 if (obj.is_null())
144 return;
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(),
154 jboolean_is_reload);
157 void WebContentsObserverAndroid::DidStartProvisionalLoadForFrame(
158 int64 frame_id,
159 int64 parent_frame_id,
160 bool is_main_frame,
161 const GURL& validated_url,
162 bool is_error_page,
163 bool is_iframe_srcdoc,
164 RenderViewHost* render_view_host) {
165 JNIEnv* env = AttachCurrentThread();
166 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
167 if (obj.is_null())
168 return;
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(
177 int64 frame_id,
178 const base::string16& frame_unique_name,
179 bool is_main_frame,
180 const GURL& url,
181 PageTransition transition_type,
182 RenderViewHost* render_view_host) {
183 JNIEnv* env = AttachCurrentThread();
184 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
185 if (obj.is_null())
186 return;
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(),
191 transition_type);
194 void WebContentsObserverAndroid::DidFinishLoad(
195 int64 frame_id,
196 const GURL& validated_url,
197 bool is_main_frame,
198 RenderViewHost* render_view_host) {
199 JNIEnv* env = AttachCurrentThread();
200 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
201 if (obj.is_null())
202 return;
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));
221 if (obj.is_null())
222 return;
223 Java_WebContentsObserverAndroid_navigationEntryCommitted(env, obj.obj());
226 void WebContentsObserverAndroid::DidChangeVisibleSSLState() {
227 JNIEnv* env = AttachCurrentThread();
228 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
229 if (obj.is_null())
230 return;
231 Java_WebContentsObserverAndroid_didChangeVisibleSSLState(env, obj.obj());
234 void WebContentsObserverAndroid::DidAttachInterstitialPage() {
235 JNIEnv* env = AttachCurrentThread();
236 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
237 if (obj.is_null())
238 return;
239 Java_WebContentsObserverAndroid_didAttachInterstitialPage(env, obj.obj());
242 void WebContentsObserverAndroid::DidDetachInterstitialPage() {
243 JNIEnv* env = AttachCurrentThread();
244 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
245 if (obj.is_null())
246 return;
247 Java_WebContentsObserverAndroid_didDetachInterstitialPage(env, obj.obj());
250 void WebContentsObserverAndroid::DidFailLoadInternal(
251 bool is_provisional_load,
252 bool is_main_frame,
253 int error_code,
254 const base::string16& description,
255 const GURL& url) {
256 JNIEnv* env = AttachCurrentThread();
257 ScopedJavaLocalRef<jobject> obj(weak_java_observer_.get(env));
258 if (obj.is_null())
259 return;
260 ScopedJavaLocalRef<jstring> jstring_error_description(
261 ConvertUTF16ToJavaString(env, description));
262 ScopedJavaLocalRef<jstring> jstring_url(
263 ConvertUTF8ToJavaString(env, url.spec()));
265 Java_WebContentsObserverAndroid_didFailLoad(
266 env, obj.obj(),
267 is_provisional_load,
268 is_main_frame,
269 error_code,
270 jstring_error_description.obj(), jstring_url.obj());
273 bool RegisterWebContentsObserverAndroid(JNIEnv* env) {
274 return RegisterNativesImpl(env);
276 } // namespace content