OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/android/web_contents_observer_proxy.h" | 5 #include "content/browser/android/web_contents_observer_proxy.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 ScopedJavaLocalRef<jobject> obj(java_observer_); | 97 ScopedJavaLocalRef<jobject> obj(java_observer_); |
98 std::string url_string = web_contents()->GetLastCommittedURL().spec(); | 98 std::string url_string = web_contents()->GetLastCommittedURL().spec(); |
99 SetToBaseURLForDataURLIfNeeded(&url_string); | 99 SetToBaseURLForDataURLIfNeeded(&url_string); |
100 // DidStopLoading is the last event we should get. | 100 // DidStopLoading is the last event we should get. |
101 base_url_of_last_started_data_url_ = GURL::EmptyGURL(); | 101 base_url_of_last_started_data_url_ = GURL::EmptyGURL(); |
102 ScopedJavaLocalRef<jstring> jstring_url(ConvertUTF8ToJavaString( | 102 ScopedJavaLocalRef<jstring> jstring_url(ConvertUTF8ToJavaString( |
103 env, url_string)); | 103 env, url_string)); |
104 Java_WebContentsObserverProxy_didStopLoading(env, obj, jstring_url); | 104 Java_WebContentsObserverProxy_didStopLoading(env, obj, jstring_url); |
105 } | 105 } |
106 | 106 |
107 void WebContentsObserverProxy::DidFailProvisionalLoad( | |
108 RenderFrameHost* render_frame_host, | |
109 const GURL& validated_url, | |
110 int error_code, | |
111 const base::string16& error_description, | |
112 bool was_ignored_by_handler) { | |
113 DidFailLoadInternal(true, !render_frame_host->GetParent(), error_code, | |
114 error_description, validated_url, was_ignored_by_handler); | |
115 } | |
116 | |
117 void WebContentsObserverProxy::DidFailLoad( | 107 void WebContentsObserverProxy::DidFailLoad( |
118 RenderFrameHost* render_frame_host, | 108 RenderFrameHost* render_frame_host, |
119 const GURL& validated_url, | 109 const GURL& validated_url, |
120 int error_code, | 110 int error_code, |
121 const base::string16& error_description, | 111 const base::string16& error_description, |
122 bool was_ignored_by_handler) { | 112 bool was_ignored_by_handler) { |
123 DidFailLoadInternal(false, !render_frame_host->GetParent(), error_code, | |
124 error_description, validated_url, was_ignored_by_handler); | |
125 } | |
126 | |
127 void WebContentsObserverProxy::DidNavigateMainFrame( | |
128 const LoadCommittedDetails& details, | |
129 const FrameNavigateParams& params) { | |
130 JNIEnv* env = AttachCurrentThread(); | 113 JNIEnv* env = AttachCurrentThread(); |
131 ScopedJavaLocalRef<jobject> obj(java_observer_); | 114 ScopedJavaLocalRef<jobject> obj(java_observer_); |
115 ScopedJavaLocalRef<jstring> jstring_error_description( | |
116 ConvertUTF16ToJavaString(env, error_description)); | |
132 ScopedJavaLocalRef<jstring> jstring_url( | 117 ScopedJavaLocalRef<jstring> jstring_url( |
133 ConvertUTF8ToJavaString(env, params.url.spec())); | 118 ConvertUTF8ToJavaString(env, validated_url.spec())); |
134 ScopedJavaLocalRef<jstring> jstring_base_url( | |
135 ConvertUTF8ToJavaString(env, params.base_url.spec())); | |
136 | 119 |
137 // See http://crbug.com/251330 for why it's determined this way. | 120 Java_WebContentsObserverProxy_didFailLoad( |
138 url::Replacements<char> replacements; | 121 env, obj, !render_frame_host->GetParent(), error_code, |
139 replacements.ClearRef(); | 122 jstring_error_description, jstring_url); |
140 bool urls_same_ignoring_fragment = | |
141 params.url.ReplaceComponents(replacements) == | |
142 details.previous_url.ReplaceComponents(replacements); | |
143 | |
144 // is_fragment_navigation is indicative of the intent of this variable. | |
145 // However, there isn't sufficient information here to determine whether this | |
146 // is actually a fragment navigation, or a history API navigation to a URL | |
147 // that would also be valid for a fragment navigation. | |
148 bool is_fragment_navigation = | |
149 urls_same_ignoring_fragment && details.is_in_page; | |
150 | |
151 Java_WebContentsObserverProxy_didNavigateMainFrame( | |
152 env, obj, jstring_url, jstring_base_url, | |
153 details.is_navigation_to_different_page(), is_fragment_navigation, | |
154 details.http_status_code); | |
155 } | |
156 | |
157 void WebContentsObserverProxy::DidNavigateAnyFrame( | |
158 RenderFrameHost* render_frame_host, | |
159 const LoadCommittedDetails& details, | |
160 const FrameNavigateParams& params) { | |
161 JNIEnv* env = AttachCurrentThread(); | |
162 ScopedJavaLocalRef<jobject> obj(java_observer_); | |
163 ScopedJavaLocalRef<jstring> jstring_url( | |
164 ConvertUTF8ToJavaString(env, params.url.spec())); | |
165 ScopedJavaLocalRef<jstring> jstring_base_url( | |
166 ConvertUTF8ToJavaString(env, params.base_url.spec())); | |
167 jboolean jboolean_is_reload = ui::PageTransitionCoreTypeIs( | |
168 params.transition, ui::PAGE_TRANSITION_RELOAD); | |
169 | |
170 Java_WebContentsObserverProxy_didNavigateAnyFrame( | |
171 env, obj, jstring_url, jstring_base_url, jboolean_is_reload); | |
172 } | 123 } |
173 | 124 |
174 void WebContentsObserverProxy::DocumentAvailableInMainFrame() { | 125 void WebContentsObserverProxy::DocumentAvailableInMainFrame() { |
175 JNIEnv* env = AttachCurrentThread(); | 126 JNIEnv* env = AttachCurrentThread(); |
176 ScopedJavaLocalRef<jobject> obj(java_observer_); | 127 ScopedJavaLocalRef<jobject> obj(java_observer_); |
177 Java_WebContentsObserverProxy_documentAvailableInMainFrame(env, obj); | 128 Java_WebContentsObserverProxy_documentAvailableInMainFrame(env, obj); |
178 } | 129 } |
179 | 130 |
180 void WebContentsObserverProxy::DidStartProvisionalLoadForFrame( | |
181 RenderFrameHost* render_frame_host, | |
182 const GURL& validated_url, | |
183 bool is_error_page) { | |
184 JNIEnv* env = AttachCurrentThread(); | |
185 ScopedJavaLocalRef<jobject> obj(java_observer_); | |
186 ScopedJavaLocalRef<jstring> jstring_url( | |
187 ConvertUTF8ToJavaString(env, validated_url.spec())); | |
188 // TODO(dcheng): Does Java really need the parent frame ID? It doesn't appear | |
189 // to be used at all, and it just adds complexity here. | |
190 Java_WebContentsObserverProxy_didStartProvisionalLoadForFrame( | |
191 env, obj, render_frame_host->GetRoutingID(), | |
192 render_frame_host->GetParent() | |
193 ? render_frame_host->GetParent()->GetRoutingID() | |
194 : -1, | |
195 !render_frame_host->GetParent(), jstring_url, is_error_page); | |
196 } | |
197 | |
198 void WebContentsObserverProxy::DidCommitProvisionalLoadForFrame( | |
199 RenderFrameHost* render_frame_host, | |
200 const GURL& url, | |
201 ui::PageTransition transition_type) { | |
202 JNIEnv* env = AttachCurrentThread(); | |
203 ScopedJavaLocalRef<jobject> obj(java_observer_); | |
204 ScopedJavaLocalRef<jstring> jstring_url( | |
205 ConvertUTF8ToJavaString(env, url.spec())); | |
206 Java_WebContentsObserverProxy_didCommitProvisionalLoadForFrame( | |
207 env, obj, render_frame_host->GetRoutingID(), | |
208 !render_frame_host->GetParent(), jstring_url, transition_type); | |
209 } | |
210 | |
211 void WebContentsObserverProxy::DidStartNavigation( | 131 void WebContentsObserverProxy::DidStartNavigation( |
212 NavigationHandle* navigation_handle) { | 132 NavigationHandle* navigation_handle) { |
213 JNIEnv* env = AttachCurrentThread(); | 133 JNIEnv* env = AttachCurrentThread(); |
214 ScopedJavaLocalRef<jobject> obj(java_observer_); | 134 ScopedJavaLocalRef<jobject> obj(java_observer_); |
215 ScopedJavaLocalRef<jstring> jstring_url( | 135 ScopedJavaLocalRef<jstring> jstring_url( |
216 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec())); | 136 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec())); |
217 Java_WebContentsObserverProxy_didStartNavigation( | 137 Java_WebContentsObserverProxy_didStartNavigation( |
218 env, obj, jstring_url, navigation_handle->IsInMainFrame(), | 138 env, obj, jstring_url, navigation_handle->IsInMainFrame(), |
219 navigation_handle->IsErrorPage()); | 139 navigation_handle->IsErrorPage()); |
220 } | 140 } |
221 | 141 |
222 void WebContentsObserverProxy::DidFinishNavigation( | 142 void WebContentsObserverProxy::DidFinishNavigation( |
223 NavigationHandle* navigation_handle) { | 143 NavigationHandle* navigation_handle) { |
224 JNIEnv* env = AttachCurrentThread(); | 144 JNIEnv* env = AttachCurrentThread(); |
225 ScopedJavaLocalRef<jobject> obj(java_observer_); | 145 ScopedJavaLocalRef<jobject> obj(java_observer_); |
226 ScopedJavaLocalRef<jstring> jstring_url( | 146 ScopedJavaLocalRef<jstring> jstring_url( |
227 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec())); | 147 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec())); |
228 | 148 |
149 CR_DEFINE_STATIC_LOCAL(GURL, last_committed_url, (GURL())); | |
boliu
2017/02/06 15:16:34
why static?
shaktisahu
2017/02/07 07:35:51
I wanted to capture the value of the URL last time
| |
150 | |
151 url::Replacements<char> replacements; | |
152 replacements.ClearRef(); | |
153 bool urls_same_ignoring_fragment = | |
154 navigation_handle->GetURL().ReplaceComponents(replacements) == | |
155 last_committed_url.ReplaceComponents(replacements); | |
156 | |
157 last_committed_url = navigation_handle->GetURL(); | |
158 | |
159 bool is_fragment_navigation = | |
160 urls_same_ignoring_fragment && navigation_handle->IsSamePage(); | |
161 | |
229 Java_WebContentsObserverProxy_didFinishNavigation( | 162 Java_WebContentsObserverProxy_didFinishNavigation( |
230 env, obj, jstring_url, navigation_handle->IsInMainFrame(), | 163 env, obj, jstring_url, navigation_handle->IsInMainFrame(), |
231 navigation_handle->IsErrorPage(), navigation_handle->HasCommitted(), | 164 navigation_handle->IsErrorPage(), navigation_handle->HasCommitted(), |
232 navigation_handle->IsSamePage(), | 165 navigation_handle->IsSamePage(), is_fragment_navigation, |
233 navigation_handle->HasCommitted() ? navigation_handle->GetPageTransition() | 166 navigation_handle->HasCommitted() ? navigation_handle->GetPageTransition() |
234 : -1, | 167 : -1, |
235 navigation_handle->GetNetErrorCode()); | 168 navigation_handle->GetNetErrorCode(), |
169 navigation_handle->GetResponseHeaders() | |
170 ? navigation_handle->GetResponseHeaders()->response_code() | |
171 : 200); | |
236 } | 172 } |
237 | 173 |
238 void WebContentsObserverProxy::DidFinishLoad(RenderFrameHost* render_frame_host, | 174 void WebContentsObserverProxy::DidFinishLoad(RenderFrameHost* render_frame_host, |
239 const GURL& validated_url) { | 175 const GURL& validated_url) { |
240 JNIEnv* env = AttachCurrentThread(); | 176 JNIEnv* env = AttachCurrentThread(); |
241 ScopedJavaLocalRef<jobject> obj(java_observer_); | 177 ScopedJavaLocalRef<jobject> obj(java_observer_); |
242 | 178 |
243 std::string url_string = validated_url.spec(); | 179 std::string url_string = validated_url.spec(); |
244 SetToBaseURLForDataURLIfNeeded(&url_string); | 180 SetToBaseURLForDataURLIfNeeded(&url_string); |
245 | 181 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 ScopedJavaLocalRef<jobject> obj(java_observer_); | 213 ScopedJavaLocalRef<jobject> obj(java_observer_); |
278 Java_WebContentsObserverProxy_didDetachInterstitialPage(env, obj); | 214 Java_WebContentsObserverProxy_didDetachInterstitialPage(env, obj); |
279 } | 215 } |
280 | 216 |
281 void WebContentsObserverProxy::DidChangeThemeColor(SkColor color) { | 217 void WebContentsObserverProxy::DidChangeThemeColor(SkColor color) { |
282 JNIEnv* env = AttachCurrentThread(); | 218 JNIEnv* env = AttachCurrentThread(); |
283 ScopedJavaLocalRef<jobject> obj(java_observer_); | 219 ScopedJavaLocalRef<jobject> obj(java_observer_); |
284 Java_WebContentsObserverProxy_didChangeThemeColor(env, obj, color); | 220 Java_WebContentsObserverProxy_didChangeThemeColor(env, obj, color); |
285 } | 221 } |
286 | 222 |
287 void WebContentsObserverProxy::DidFailLoadInternal( | |
288 bool is_provisional_load, | |
289 bool is_main_frame, | |
290 int error_code, | |
291 const base::string16& description, | |
292 const GURL& url, | |
293 bool was_ignored_by_handler) { | |
294 JNIEnv* env = AttachCurrentThread(); | |
295 ScopedJavaLocalRef<jobject> obj(java_observer_); | |
296 ScopedJavaLocalRef<jstring> jstring_error_description( | |
297 ConvertUTF16ToJavaString(env, description)); | |
298 ScopedJavaLocalRef<jstring> jstring_url( | |
299 ConvertUTF8ToJavaString(env, url.spec())); | |
300 | |
301 Java_WebContentsObserverProxy_didFailLoad( | |
302 env, obj, is_provisional_load, is_main_frame, error_code, | |
303 jstring_error_description, jstring_url, was_ignored_by_handler); | |
304 } | |
305 | |
306 void WebContentsObserverProxy::DidFirstVisuallyNonEmptyPaint() { | 223 void WebContentsObserverProxy::DidFirstVisuallyNonEmptyPaint() { |
307 JNIEnv* env = AttachCurrentThread(); | 224 JNIEnv* env = AttachCurrentThread(); |
308 ScopedJavaLocalRef<jobject> obj(java_observer_); | 225 ScopedJavaLocalRef<jobject> obj(java_observer_); |
309 Java_WebContentsObserverProxy_didFirstVisuallyNonEmptyPaint(env, obj); | 226 Java_WebContentsObserverProxy_didFirstVisuallyNonEmptyPaint(env, obj); |
310 } | 227 } |
311 | 228 |
312 void WebContentsObserverProxy::WasShown() { | 229 void WebContentsObserverProxy::WasShown() { |
313 JNIEnv* env = AttachCurrentThread(); | 230 JNIEnv* env = AttachCurrentThread(); |
314 ScopedJavaLocalRef<jobject> obj(java_observer_); | 231 ScopedJavaLocalRef<jobject> obj(java_observer_); |
315 Java_WebContentsObserverProxy_wasShown(env, obj); | 232 Java_WebContentsObserverProxy_wasShown(env, obj); |
316 } | 233 } |
317 | 234 |
318 void WebContentsObserverProxy::WasHidden() { | 235 void WebContentsObserverProxy::WasHidden() { |
319 JNIEnv* env = AttachCurrentThread(); | 236 JNIEnv* env = AttachCurrentThread(); |
320 ScopedJavaLocalRef<jobject> obj(java_observer_); | 237 ScopedJavaLocalRef<jobject> obj(java_observer_); |
321 Java_WebContentsObserverProxy_wasHidden(env, obj); | 238 Java_WebContentsObserverProxy_wasHidden(env, obj); |
322 } | 239 } |
323 | 240 |
324 void WebContentsObserverProxy::TitleWasSet(NavigationEntry* entry, | 241 void WebContentsObserverProxy::TitleWasSet(NavigationEntry* entry, |
325 bool explicit_set) { | 242 bool explicit_set) { |
326 JNIEnv* env = AttachCurrentThread(); | 243 JNIEnv* env = AttachCurrentThread(); |
327 ScopedJavaLocalRef<jobject> obj(java_observer_); | 244 ScopedJavaLocalRef<jobject> obj(java_observer_); |
328 ScopedJavaLocalRef<jstring> jstring_title = ConvertUTF8ToJavaString( | 245 ScopedJavaLocalRef<jstring> jstring_title = ConvertUTF8ToJavaString( |
329 env, | 246 env, |
330 base::UTF16ToUTF8(web_contents()->GetTitle())); | 247 base::UTF16ToUTF8(web_contents()->GetTitle())); |
331 Java_WebContentsObserverProxy_titleWasSet(env, obj, jstring_title); | 248 Java_WebContentsObserverProxy_titleWasSet(env, obj, jstring_title); |
332 } | 249 } |
333 | 250 |
334 void WebContentsObserverProxy::DidStartNavigationToPendingEntry( | |
335 const GURL& url, | |
336 ReloadType reload_type) { | |
337 JNIEnv* env = AttachCurrentThread(); | |
338 ScopedJavaLocalRef<jobject> obj(java_observer_); | |
339 ScopedJavaLocalRef<jstring> jstring_url( | |
340 ConvertUTF8ToJavaString(env, url.spec())); | |
341 | |
342 Java_WebContentsObserverProxy_didStartNavigationToPendingEntry(env, obj, | |
343 jstring_url); | |
344 } | |
345 | |
346 void WebContentsObserverProxy::SetToBaseURLForDataURLIfNeeded( | 251 void WebContentsObserverProxy::SetToBaseURLForDataURLIfNeeded( |
347 std::string* url) { | 252 std::string* url) { |
348 NavigationEntry* entry = | 253 NavigationEntry* entry = |
349 web_contents()->GetController().GetLastCommittedEntry(); | 254 web_contents()->GetController().GetLastCommittedEntry(); |
350 // Note that GetBaseURLForDataURL is only used by the Android WebView. | 255 // Note that GetBaseURLForDataURL is only used by the Android WebView. |
351 // FIXME: Should we only return valid specs and "about:blank" for invalid | 256 // FIXME: Should we only return valid specs and "about:blank" for invalid |
352 // ones? This may break apps. | 257 // ones? This may break apps. |
353 if (entry && !entry->GetBaseURLForDataURL().is_empty()) { | 258 if (entry && !entry->GetBaseURLForDataURL().is_empty()) { |
354 *url = entry->GetBaseURLForDataURL().possibly_invalid_spec(); | 259 *url = entry->GetBaseURLForDataURL().possibly_invalid_spec(); |
355 } else if (!base_url_of_last_started_data_url_.is_empty()) { | 260 } else if (!base_url_of_last_started_data_url_.is_empty()) { |
356 // NavigationController can lose the pending entry and recreate it without | 261 // NavigationController can lose the pending entry and recreate it without |
357 // a base URL if there has been a loadUrl("javascript:...") after | 262 // a base URL if there has been a loadUrl("javascript:...") after |
358 // loadDataWithBaseUrl. | 263 // loadDataWithBaseUrl. |
359 *url = base_url_of_last_started_data_url_.possibly_invalid_spec(); | 264 *url = base_url_of_last_started_data_url_.possibly_invalid_spec(); |
360 } | 265 } |
361 } | 266 } |
362 | 267 |
363 bool RegisterWebContentsObserverProxy(JNIEnv* env) { | 268 bool RegisterWebContentsObserverProxy(JNIEnv* env) { |
364 return RegisterNativesImpl(env); | 269 return RegisterNativesImpl(env); |
365 } | 270 } |
366 } // namespace content | 271 } // namespace content |
OLD | NEW |