| 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 bool is_fragment_navigation = navigation_handle->IsSamePage(); |
| 150 |
| 151 if (navigation_handle->HasCommitted()) { |
| 152 // See http://crbug.com/251330 for why it's determined this way. |
| 153 url::Replacements<char> replacements; |
| 154 replacements.ClearRef(); |
| 155 bool urls_same_ignoring_fragment = |
| 156 navigation_handle->GetURL().ReplaceComponents(replacements) == |
| 157 navigation_handle->GetPreviousURL().ReplaceComponents(replacements); |
| 158 is_fragment_navigation &= urls_same_ignoring_fragment; |
| 159 } |
| 160 |
| 229 Java_WebContentsObserverProxy_didFinishNavigation( | 161 Java_WebContentsObserverProxy_didFinishNavigation( |
| 230 env, obj, jstring_url, navigation_handle->IsInMainFrame(), | 162 env, obj, jstring_url, navigation_handle->IsInMainFrame(), |
| 231 navigation_handle->IsErrorPage(), navigation_handle->HasCommitted(), | 163 navigation_handle->IsErrorPage(), navigation_handle->HasCommitted(), |
| 232 navigation_handle->IsSamePage(), | 164 navigation_handle->IsSamePage(), is_fragment_navigation, |
| 233 navigation_handle->HasCommitted() ? navigation_handle->GetPageTransition() | 165 navigation_handle->HasCommitted() ? navigation_handle->GetPageTransition() |
| 234 : -1, | 166 : -1, |
| 235 navigation_handle->GetNetErrorCode()); | 167 navigation_handle->GetNetErrorCode(), |
| 168 navigation_handle->GetResponseHeaders() |
| 169 ? navigation_handle->GetResponseHeaders()->response_code() |
| 170 : 200); |
| 236 } | 171 } |
| 237 | 172 |
| 238 void WebContentsObserverProxy::DidFinishLoad(RenderFrameHost* render_frame_host, | 173 void WebContentsObserverProxy::DidFinishLoad(RenderFrameHost* render_frame_host, |
| 239 const GURL& validated_url) { | 174 const GURL& validated_url) { |
| 240 JNIEnv* env = AttachCurrentThread(); | 175 JNIEnv* env = AttachCurrentThread(); |
| 241 ScopedJavaLocalRef<jobject> obj(java_observer_); | 176 ScopedJavaLocalRef<jobject> obj(java_observer_); |
| 242 | 177 |
| 243 std::string url_string = validated_url.spec(); | 178 std::string url_string = validated_url.spec(); |
| 244 SetToBaseURLForDataURLIfNeeded(&url_string); | 179 SetToBaseURLForDataURLIfNeeded(&url_string); |
| 245 | 180 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 ScopedJavaLocalRef<jobject> obj(java_observer_); | 212 ScopedJavaLocalRef<jobject> obj(java_observer_); |
| 278 Java_WebContentsObserverProxy_didDetachInterstitialPage(env, obj); | 213 Java_WebContentsObserverProxy_didDetachInterstitialPage(env, obj); |
| 279 } | 214 } |
| 280 | 215 |
| 281 void WebContentsObserverProxy::DidChangeThemeColor(SkColor color) { | 216 void WebContentsObserverProxy::DidChangeThemeColor(SkColor color) { |
| 282 JNIEnv* env = AttachCurrentThread(); | 217 JNIEnv* env = AttachCurrentThread(); |
| 283 ScopedJavaLocalRef<jobject> obj(java_observer_); | 218 ScopedJavaLocalRef<jobject> obj(java_observer_); |
| 284 Java_WebContentsObserverProxy_didChangeThemeColor(env, obj, color); | 219 Java_WebContentsObserverProxy_didChangeThemeColor(env, obj, color); |
| 285 } | 220 } |
| 286 | 221 |
| 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() { | 222 void WebContentsObserverProxy::DidFirstVisuallyNonEmptyPaint() { |
| 307 JNIEnv* env = AttachCurrentThread(); | 223 JNIEnv* env = AttachCurrentThread(); |
| 308 ScopedJavaLocalRef<jobject> obj(java_observer_); | 224 ScopedJavaLocalRef<jobject> obj(java_observer_); |
| 309 Java_WebContentsObserverProxy_didFirstVisuallyNonEmptyPaint(env, obj); | 225 Java_WebContentsObserverProxy_didFirstVisuallyNonEmptyPaint(env, obj); |
| 310 } | 226 } |
| 311 | 227 |
| 312 void WebContentsObserverProxy::WasShown() { | 228 void WebContentsObserverProxy::WasShown() { |
| 313 JNIEnv* env = AttachCurrentThread(); | 229 JNIEnv* env = AttachCurrentThread(); |
| 314 ScopedJavaLocalRef<jobject> obj(java_observer_); | 230 ScopedJavaLocalRef<jobject> obj(java_observer_); |
| 315 Java_WebContentsObserverProxy_wasShown(env, obj); | 231 Java_WebContentsObserverProxy_wasShown(env, obj); |
| 316 } | 232 } |
| 317 | 233 |
| 318 void WebContentsObserverProxy::WasHidden() { | 234 void WebContentsObserverProxy::WasHidden() { |
| 319 JNIEnv* env = AttachCurrentThread(); | 235 JNIEnv* env = AttachCurrentThread(); |
| 320 ScopedJavaLocalRef<jobject> obj(java_observer_); | 236 ScopedJavaLocalRef<jobject> obj(java_observer_); |
| 321 Java_WebContentsObserverProxy_wasHidden(env, obj); | 237 Java_WebContentsObserverProxy_wasHidden(env, obj); |
| 322 } | 238 } |
| 323 | 239 |
| 324 void WebContentsObserverProxy::TitleWasSet(NavigationEntry* entry, | 240 void WebContentsObserverProxy::TitleWasSet(NavigationEntry* entry, |
| 325 bool explicit_set) { | 241 bool explicit_set) { |
| 326 JNIEnv* env = AttachCurrentThread(); | 242 JNIEnv* env = AttachCurrentThread(); |
| 327 ScopedJavaLocalRef<jobject> obj(java_observer_); | 243 ScopedJavaLocalRef<jobject> obj(java_observer_); |
| 328 ScopedJavaLocalRef<jstring> jstring_title = ConvertUTF8ToJavaString( | 244 ScopedJavaLocalRef<jstring> jstring_title = ConvertUTF8ToJavaString( |
| 329 env, | 245 env, |
| 330 base::UTF16ToUTF8(web_contents()->GetTitle())); | 246 base::UTF16ToUTF8(web_contents()->GetTitle())); |
| 331 Java_WebContentsObserverProxy_titleWasSet(env, obj, jstring_title); | 247 Java_WebContentsObserverProxy_titleWasSet(env, obj, jstring_title); |
| 332 } | 248 } |
| 333 | 249 |
| 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( | 250 void WebContentsObserverProxy::SetToBaseURLForDataURLIfNeeded( |
| 347 std::string* url) { | 251 std::string* url) { |
| 348 NavigationEntry* entry = | 252 NavigationEntry* entry = |
| 349 web_contents()->GetController().GetLastCommittedEntry(); | 253 web_contents()->GetController().GetLastCommittedEntry(); |
| 350 // Note that GetBaseURLForDataURL is only used by the Android WebView. | 254 // Note that GetBaseURLForDataURL is only used by the Android WebView. |
| 351 // FIXME: Should we only return valid specs and "about:blank" for invalid | 255 // FIXME: Should we only return valid specs and "about:blank" for invalid |
| 352 // ones? This may break apps. | 256 // ones? This may break apps. |
| 353 if (entry && !entry->GetBaseURLForDataURL().is_empty()) { | 257 if (entry && !entry->GetBaseURLForDataURL().is_empty()) { |
| 354 *url = entry->GetBaseURLForDataURL().possibly_invalid_spec(); | 258 *url = entry->GetBaseURLForDataURL().possibly_invalid_spec(); |
| 355 } else if (!base_url_of_last_started_data_url_.is_empty()) { | 259 } else if (!base_url_of_last_started_data_url_.is_empty()) { |
| 356 // NavigationController can lose the pending entry and recreate it without | 260 // NavigationController can lose the pending entry and recreate it without |
| 357 // a base URL if there has been a loadUrl("javascript:...") after | 261 // a base URL if there has been a loadUrl("javascript:...") after |
| 358 // loadDataWithBaseUrl. | 262 // loadDataWithBaseUrl. |
| 359 *url = base_url_of_last_started_data_url_.possibly_invalid_spec(); | 263 *url = base_url_of_last_started_data_url_.possibly_invalid_spec(); |
| 360 } | 264 } |
| 361 } | 265 } |
| 362 | 266 |
| 363 bool RegisterWebContentsObserverProxy(JNIEnv* env) { | 267 bool RegisterWebContentsObserverProxy(JNIEnv* env) { |
| 364 return RegisterNativesImpl(env); | 268 return RegisterNativesImpl(env); |
| 365 } | 269 } |
| 366 } // namespace content | 270 } // namespace content |
| OLD | NEW |