Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1072)

Side by Side Diff: content/browser/android/web_contents_observer_proxy.cc

Issue 2642303002: PlzNavigate: Chrome UI changes for new methods of WebContentsObserver (Closed)
Patch Set: errorDescription in WCOProxy Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/android/scoped_java_ref.h" 11 #include "base/android/scoped_java_ref.h"
12 #include "base/optional.h" 12 #include "base/optional.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "components/error_page/common/localized_error.h"
Bernhard Bauer 2017/02/10 16:49:42 Okay, you were calling into components/ from conte
14 #include "content/browser/renderer_host/render_widget_host_impl.h" 15 #include "content/browser/renderer_host/render_widget_host_impl.h"
15 #include "content/browser/web_contents/web_contents_impl.h" 16 #include "content/browser/web_contents/web_contents_impl.h"
16 #include "content/common/android/media_metadata_android.h" 17 #include "content/common/android/media_metadata_android.h"
17 #include "content/public/browser/navigation_details.h" 18 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/navigation_entry.h" 19 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/navigation_handle.h" 20 #include "content/public/browser/navigation_handle.h"
20 #include "content/public/common/media_metadata.h" 21 #include "content/public/common/media_metadata.h"
21 #include "jni/WebContentsObserverProxy_jni.h" 22 #include "jni/WebContentsObserverProxy_jni.h"
23 #include "net/base/net_errors.h"
22 24
23 using base::android::AttachCurrentThread; 25 using base::android::AttachCurrentThread;
24 using base::android::JavaParamRef; 26 using base::android::JavaParamRef;
25 using base::android::ScopedJavaLocalRef; 27 using base::android::ScopedJavaLocalRef;
26 using base::android::ConvertUTF8ToJavaString; 28 using base::android::ConvertUTF8ToJavaString;
27 using base::android::ConvertUTF16ToJavaString; 29 using base::android::ConvertUTF16ToJavaString;
28 30
29 namespace content { 31 namespace content {
30 32
31 // TODO(dcheng): File a bug. This class incorrectly passes just a frame ID, 33 // TODO(dcheng): File a bug. This class incorrectly passes just a frame ID,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 ScopedJavaLocalRef<jobject> obj(java_observer_); 99 ScopedJavaLocalRef<jobject> obj(java_observer_);
98 std::string url_string = web_contents()->GetLastCommittedURL().spec(); 100 std::string url_string = web_contents()->GetLastCommittedURL().spec();
99 SetToBaseURLForDataURLIfNeeded(&url_string); 101 SetToBaseURLForDataURLIfNeeded(&url_string);
100 // DidStopLoading is the last event we should get. 102 // DidStopLoading is the last event we should get.
101 base_url_of_last_started_data_url_ = GURL::EmptyGURL(); 103 base_url_of_last_started_data_url_ = GURL::EmptyGURL();
102 ScopedJavaLocalRef<jstring> jstring_url(ConvertUTF8ToJavaString( 104 ScopedJavaLocalRef<jstring> jstring_url(ConvertUTF8ToJavaString(
103 env, url_string)); 105 env, url_string));
104 Java_WebContentsObserverProxy_didStopLoading(env, obj, jstring_url); 106 Java_WebContentsObserverProxy_didStopLoading(env, obj, jstring_url);
105 } 107 }
106 108
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( 109 void WebContentsObserverProxy::DidFailLoad(
118 RenderFrameHost* render_frame_host, 110 RenderFrameHost* render_frame_host,
119 const GURL& validated_url, 111 const GURL& validated_url,
120 int error_code, 112 int error_code,
121 const base::string16& error_description, 113 const base::string16& error_description,
122 bool was_ignored_by_handler) { 114 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(); 115 JNIEnv* env = AttachCurrentThread();
131 ScopedJavaLocalRef<jobject> obj(java_observer_); 116 ScopedJavaLocalRef<jobject> obj(java_observer_);
117 ScopedJavaLocalRef<jstring> jstring_error_description(
118 ConvertUTF16ToJavaString(env, error_description));
132 ScopedJavaLocalRef<jstring> jstring_url( 119 ScopedJavaLocalRef<jstring> jstring_url(
133 ConvertUTF8ToJavaString(env, params.url.spec())); 120 ConvertUTF8ToJavaString(env, validated_url.spec()));
134 ScopedJavaLocalRef<jstring> jstring_base_url(
135 ConvertUTF8ToJavaString(env, params.base_url.spec()));
136 121
137 // See http://crbug.com/251330 for why it's determined this way. 122 Java_WebContentsObserverProxy_didFailLoad(
138 url::Replacements<char> replacements; 123 env, obj, !render_frame_host->GetParent(), error_code,
139 replacements.ClearRef(); 124 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 } 125 }
173 126
174 void WebContentsObserverProxy::DocumentAvailableInMainFrame() { 127 void WebContentsObserverProxy::DocumentAvailableInMainFrame() {
175 JNIEnv* env = AttachCurrentThread(); 128 JNIEnv* env = AttachCurrentThread();
176 ScopedJavaLocalRef<jobject> obj(java_observer_); 129 ScopedJavaLocalRef<jobject> obj(java_observer_);
177 Java_WebContentsObserverProxy_documentAvailableInMainFrame(env, obj); 130 Java_WebContentsObserverProxy_documentAvailableInMainFrame(env, obj);
178 } 131 }
179 132
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( 133 void WebContentsObserverProxy::DidStartNavigation(
212 NavigationHandle* navigation_handle) { 134 NavigationHandle* navigation_handle) {
213 JNIEnv* env = AttachCurrentThread(); 135 JNIEnv* env = AttachCurrentThread();
214 ScopedJavaLocalRef<jobject> obj(java_observer_); 136 ScopedJavaLocalRef<jobject> obj(java_observer_);
215 ScopedJavaLocalRef<jstring> jstring_url( 137 ScopedJavaLocalRef<jstring> jstring_url(
216 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec())); 138 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec()));
217 Java_WebContentsObserverProxy_didStartNavigation( 139 Java_WebContentsObserverProxy_didStartNavigation(
218 env, obj, jstring_url, navigation_handle->IsInMainFrame(), 140 env, obj, jstring_url, navigation_handle->IsInMainFrame(),
219 navigation_handle->IsErrorPage()); 141 navigation_handle->IsSamePage(), navigation_handle->IsErrorPage());
220 } 142 }
221 143
222 void WebContentsObserverProxy::DidFinishNavigation( 144 void WebContentsObserverProxy::DidFinishNavigation(
223 NavigationHandle* navigation_handle) { 145 NavigationHandle* navigation_handle) {
224 JNIEnv* env = AttachCurrentThread(); 146 JNIEnv* env = AttachCurrentThread();
225 ScopedJavaLocalRef<jobject> obj(java_observer_); 147 ScopedJavaLocalRef<jobject> obj(java_observer_);
226 ScopedJavaLocalRef<jstring> jstring_url( 148 ScopedJavaLocalRef<jstring> jstring_url(
227 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec())); 149 ConvertUTF8ToJavaString(env, navigation_handle->GetURL().spec()));
228 150
151 bool is_fragment_navigation = navigation_handle->IsSamePage();
152
153 if (navigation_handle->HasCommitted()) {
154 // See http://crbug.com/251330 for why it's determined this way.
155 url::Replacements<char> replacements;
156 replacements.ClearRef();
157 bool urls_same_ignoring_fragment =
158 navigation_handle->GetURL().ReplaceComponents(replacements) ==
159 navigation_handle->GetPreviousURL().ReplaceComponents(replacements);
160 is_fragment_navigation &= urls_same_ignoring_fragment;
161 }
162
163 base::string16 error_description =
164 navigation_handle->GetNetErrorCode() == net::OK
165 ? base::string16()
166 : error_page::LocalizedError::GetErrorDetails(
167 net::kErrorDomain, navigation_handle->GetNetErrorCode(),
168 navigation_handle->IsPost());
169
170 ScopedJavaLocalRef<jstring> jerror_description(
171 ConvertUTF16ToJavaString(env, error_description));
229 Java_WebContentsObserverProxy_didFinishNavigation( 172 Java_WebContentsObserverProxy_didFinishNavigation(
230 env, obj, jstring_url, navigation_handle->IsInMainFrame(), 173 env, obj, jstring_url, navigation_handle->IsInMainFrame(),
231 navigation_handle->IsErrorPage(), navigation_handle->HasCommitted(), 174 navigation_handle->IsErrorPage(), navigation_handle->HasCommitted(),
232 navigation_handle->IsSamePage(), 175 navigation_handle->IsSamePage(), is_fragment_navigation,
233 navigation_handle->HasCommitted() ? navigation_handle->GetPageTransition() 176 navigation_handle->HasCommitted() ? navigation_handle->GetPageTransition()
234 : -1, 177 : -1,
235 navigation_handle->GetNetErrorCode()); 178 navigation_handle->GetNetErrorCode(), jerror_description,
179 // TODO(shaktisahu): Change default status to -1 after fixing
180 // crbug/690041.
181 navigation_handle->GetResponseHeaders()
182 ? navigation_handle->GetResponseHeaders()->response_code()
183 : 200);
236 } 184 }
237 185
238 void WebContentsObserverProxy::DidFinishLoad(RenderFrameHost* render_frame_host, 186 void WebContentsObserverProxy::DidFinishLoad(RenderFrameHost* render_frame_host,
239 const GURL& validated_url) { 187 const GURL& validated_url) {
240 JNIEnv* env = AttachCurrentThread(); 188 JNIEnv* env = AttachCurrentThread();
241 ScopedJavaLocalRef<jobject> obj(java_observer_); 189 ScopedJavaLocalRef<jobject> obj(java_observer_);
242 190
243 std::string url_string = validated_url.spec(); 191 std::string url_string = validated_url.spec();
244 SetToBaseURLForDataURLIfNeeded(&url_string); 192 SetToBaseURLForDataURLIfNeeded(&url_string);
245 193
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 ScopedJavaLocalRef<jobject> obj(java_observer_); 225 ScopedJavaLocalRef<jobject> obj(java_observer_);
278 Java_WebContentsObserverProxy_didDetachInterstitialPage(env, obj); 226 Java_WebContentsObserverProxy_didDetachInterstitialPage(env, obj);
279 } 227 }
280 228
281 void WebContentsObserverProxy::DidChangeThemeColor(SkColor color) { 229 void WebContentsObserverProxy::DidChangeThemeColor(SkColor color) {
282 JNIEnv* env = AttachCurrentThread(); 230 JNIEnv* env = AttachCurrentThread();
283 ScopedJavaLocalRef<jobject> obj(java_observer_); 231 ScopedJavaLocalRef<jobject> obj(java_observer_);
284 Java_WebContentsObserverProxy_didChangeThemeColor(env, obj, color); 232 Java_WebContentsObserverProxy_didChangeThemeColor(env, obj, color);
285 } 233 }
286 234
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() { 235 void WebContentsObserverProxy::DidFirstVisuallyNonEmptyPaint() {
307 JNIEnv* env = AttachCurrentThread(); 236 JNIEnv* env = AttachCurrentThread();
308 ScopedJavaLocalRef<jobject> obj(java_observer_); 237 ScopedJavaLocalRef<jobject> obj(java_observer_);
309 Java_WebContentsObserverProxy_didFirstVisuallyNonEmptyPaint(env, obj); 238 Java_WebContentsObserverProxy_didFirstVisuallyNonEmptyPaint(env, obj);
310 } 239 }
311 240
312 void WebContentsObserverProxy::WasShown() { 241 void WebContentsObserverProxy::WasShown() {
313 JNIEnv* env = AttachCurrentThread(); 242 JNIEnv* env = AttachCurrentThread();
314 ScopedJavaLocalRef<jobject> obj(java_observer_); 243 ScopedJavaLocalRef<jobject> obj(java_observer_);
315 Java_WebContentsObserverProxy_wasShown(env, obj); 244 Java_WebContentsObserverProxy_wasShown(env, obj);
316 } 245 }
317 246
318 void WebContentsObserverProxy::WasHidden() { 247 void WebContentsObserverProxy::WasHidden() {
319 JNIEnv* env = AttachCurrentThread(); 248 JNIEnv* env = AttachCurrentThread();
320 ScopedJavaLocalRef<jobject> obj(java_observer_); 249 ScopedJavaLocalRef<jobject> obj(java_observer_);
321 Java_WebContentsObserverProxy_wasHidden(env, obj); 250 Java_WebContentsObserverProxy_wasHidden(env, obj);
322 } 251 }
323 252
324 void WebContentsObserverProxy::TitleWasSet(NavigationEntry* entry, 253 void WebContentsObserverProxy::TitleWasSet(NavigationEntry* entry,
325 bool explicit_set) { 254 bool explicit_set) {
326 JNIEnv* env = AttachCurrentThread(); 255 JNIEnv* env = AttachCurrentThread();
327 ScopedJavaLocalRef<jobject> obj(java_observer_); 256 ScopedJavaLocalRef<jobject> obj(java_observer_);
328 ScopedJavaLocalRef<jstring> jstring_title = ConvertUTF8ToJavaString( 257 ScopedJavaLocalRef<jstring> jstring_title = ConvertUTF8ToJavaString(
329 env, 258 env,
330 base::UTF16ToUTF8(web_contents()->GetTitle())); 259 base::UTF16ToUTF8(web_contents()->GetTitle()));
331 Java_WebContentsObserverProxy_titleWasSet(env, obj, jstring_title); 260 Java_WebContentsObserverProxy_titleWasSet(env, obj, jstring_title);
332 } 261 }
333 262
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( 263 void WebContentsObserverProxy::SetToBaseURLForDataURLIfNeeded(
347 std::string* url) { 264 std::string* url) {
348 NavigationEntry* entry = 265 NavigationEntry* entry =
349 web_contents()->GetController().GetLastCommittedEntry(); 266 web_contents()->GetController().GetLastCommittedEntry();
350 // Note that GetBaseURLForDataURL is only used by the Android WebView. 267 // Note that GetBaseURLForDataURL is only used by the Android WebView.
351 // FIXME: Should we only return valid specs and "about:blank" for invalid 268 // FIXME: Should we only return valid specs and "about:blank" for invalid
352 // ones? This may break apps. 269 // ones? This may break apps.
353 if (entry && !entry->GetBaseURLForDataURL().is_empty()) { 270 if (entry && !entry->GetBaseURLForDataURL().is_empty()) {
354 *url = entry->GetBaseURLForDataURL().possibly_invalid_spec(); 271 *url = entry->GetBaseURLForDataURL().possibly_invalid_spec();
355 } else if (!base_url_of_last_started_data_url_.is_empty()) { 272 } else if (!base_url_of_last_started_data_url_.is_empty()) {
356 // NavigationController can lose the pending entry and recreate it without 273 // NavigationController can lose the pending entry and recreate it without
357 // a base URL if there has been a loadUrl("javascript:...") after 274 // a base URL if there has been a loadUrl("javascript:...") after
358 // loadDataWithBaseUrl. 275 // loadDataWithBaseUrl.
359 *url = base_url_of_last_started_data_url_.possibly_invalid_spec(); 276 *url = base_url_of_last_started_data_url_.possibly_invalid_spec();
360 } 277 }
361 } 278 }
362 279
363 bool RegisterWebContentsObserverProxy(JNIEnv* env) { 280 bool RegisterWebContentsObserverProxy(JNIEnv* env) {
364 return RegisterNativesImpl(env); 281 return RegisterNativesImpl(env);
365 } 282 }
366 } // namespace content 283 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698