Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / android_webview / java / src / org / chromium / android_webview / AwWebContentsObserver.java
1 // Copyright 2014 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.
4
5 package org.chromium.android_webview;
6
7 import org.chromium.content.browser.WebContentsObserverAndroid;
8 import org.chromium.content_public.browser.WebContents;
9 import org.chromium.net.NetError;
10
11 /**
12  * Routes notifications from WebContents to AwContentsClient and other listeners.
13  */
14 public class AwWebContentsObserver extends WebContentsObserverAndroid {
15     private final AwContentsClient mAwContentsClient;
16
17     public AwWebContentsObserver(WebContents webContents, AwContentsClient awContentsClient) {
18         super(webContents);
19         mAwContentsClient = awContentsClient;
20     }
21
22     @Override
23     public void didFinishLoad(long frameId, String validatedUrl, boolean isMainFrame) {
24         String unreachableWebDataUrl = AwContentsStatics.getUnreachableWebDataUrl();
25         boolean isErrorUrl =
26                 unreachableWebDataUrl != null && unreachableWebDataUrl.equals(validatedUrl);
27         if (isMainFrame && !isErrorUrl) {
28             mAwContentsClient.onPageFinished(validatedUrl);
29         }
30     }
31
32     @Override
33     public void didFailLoad(boolean isProvisionalLoad,
34             boolean isMainFrame, int errorCode, String description, String failingUrl) {
35         String unreachableWebDataUrl = AwContentsStatics.getUnreachableWebDataUrl();
36         boolean isErrorUrl =
37                 unreachableWebDataUrl != null && unreachableWebDataUrl.equals(failingUrl);
38         if (isMainFrame && !isErrorUrl) {
39             if (errorCode != NetError.ERR_ABORTED) {
40                 // This error code is generated for the following reasons:
41                 // - WebView.stopLoading is called,
42                 // - the navigation is intercepted by the embedder via shouldOverrideNavigation.
43                 //
44                 // The Android WebView does not notify the embedder of these situations using
45                 // this error code with the WebViewClient.onReceivedError callback.
46                 mAwContentsClient.onReceivedError(
47                         ErrorCodeConversionHelper.convertErrorCode(errorCode), description,
48                                 failingUrl);
49             }
50             // Need to call onPageFinished after onReceivedError (if there is an error) for
51             // backwards compatibility with the classic webview.
52             mAwContentsClient.onPageFinished(failingUrl);
53         }
54     }
55
56     @Override
57     public void didNavigateMainFrame(String url, String baseUrl,
58             boolean isNavigationToDifferentPage, boolean isFragmentNavigation) {
59         // This is here to emulate the Classic WebView firing onPageFinished for main frame
60         // navigations where only the hash fragment changes.
61         if (isFragmentNavigation) {
62             mAwContentsClient.onPageFinished(url);
63         }
64     }
65
66     @Override
67     public void didNavigateAnyFrame(String url, String baseUrl, boolean isReload) {
68         mAwContentsClient.doUpdateVisitedHistory(url, isReload);
69     }
70 }