Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / XWalkContentsClient.java
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 package org.xwalk.core.internal;
7
8 import android.content.pm.ActivityInfo;
9 import android.graphics.Bitmap;
10 import android.graphics.Picture;
11 import android.graphics.Rect;
12 import android.graphics.RectF;
13 import android.net.http.SslError;
14 import android.os.Handler;
15 import android.os.Looper;
16 import android.os.Message;
17 import android.util.Log;
18 import android.view.KeyEvent;
19 import android.view.View;
20 import android.webkit.ConsoleMessage;
21 import android.webkit.ValueCallback;
22 import android.webkit.WebResourceResponse;
23
24 import org.chromium.content.browser.ContentViewClient;
25 import org.chromium.content.browser.WebContentsObserver;
26 import org.chromium.content_public.browser.WebContents;
27 import org.chromium.net.NetError;
28
29 /**
30  * Base-class that a XWalkViewContents embedder derives from to receive callbacks.
31  * This extends ContentViewClient, as in many cases we want to pass-thru ContentViewCore
32  * callbacks right to our embedder, and this setup facilities that.
33  * For any other callbacks we need to make transformations of (e.g. adapt parameters
34  * or perform filtering) we can provide final overrides for methods here, and then introduce
35  * new abstract methods that the our own client must implement.
36  * i.e.: all methods in this class should either be final, or abstract.
37  */
38 abstract class XWalkContentsClient extends ContentViewClient {
39
40     private static final String TAG = "XWalkContentsClient";
41     private final XWalkContentsClientCallbackHelper mCallbackHelper =
42         new XWalkContentsClientCallbackHelper(this);
43
44     private XWalkWebContentsObserver mWebContentsObserver;
45
46     private double mDIPScale;
47
48     public class XWalkWebContentsObserver extends WebContentsObserver {
49         public XWalkWebContentsObserver(WebContents webContents) {
50             super(webContents);
51         }
52
53         @Override
54         public void didStopLoading(String url) {
55             onPageFinished(url);
56         }
57
58         @Override
59         public void didFailLoad(boolean isProvisionalLoad,
60                 boolean isMainFrame, int errorCode, String description, String failingUrl) {
61             if (errorCode == NetError.ERR_ABORTED || !isMainFrame) {
62                 // This error code is generated for the following reasons:
63                 // - XWalkViewInternal.stopLoading is called,
64                 // - the navigation is intercepted by the embedder via shouldOverrideNavigation.
65                 //
66                 // The XWalkViewInternal does not notify the embedder of these situations using this
67                 // error code with the XWalkClient.onReceivedError callback. What's more,
68                 // the XWalkViewInternal does not notify the embedder of sub-frame failures.
69                 return;
70             }
71             onReceivedError(ErrorCodeConversionHelper.convertErrorCode(errorCode),
72                     description, failingUrl);
73         }
74
75         @Override
76         public void didNavigateAnyFrame(String url, String baseUrl, boolean isReload) {
77             doUpdateVisitedHistory(url, isReload);
78         }
79
80         @Override
81         public void didFinishLoad(long frameId, String validatedUrl, boolean isMainFrame) {
82             // Both didStopLoading and didFinishLoad will be called once a page is finished
83             // to load, but didStopLoading will also be called when user clicks "X" button
84             // on browser UI to stop loading page.
85             //
86             // So it is safe for Crosswalk to rely on didStopLoading to ensure onPageFinished
87             // can be called.
88         }
89     }
90
91     @Override
92     final public void onUpdateTitle(String title) {
93         onTitleChanged(title);
94     }
95
96     @Override
97     public boolean shouldOverrideKeyEvent(KeyEvent event) {
98         return super.shouldOverrideKeyEvent(event);
99     }
100
101     void installWebContentsObserver(WebContents webContents) {
102         if (mWebContentsObserver != null) {
103             mWebContentsObserver.detachFromWebContents();
104         }
105         mWebContentsObserver = new XWalkWebContentsObserver(webContents);
106     }
107
108     void setDIPScale(double dipScale) {
109         mDIPScale = dipScale;
110     }
111
112     final XWalkContentsClientCallbackHelper getCallbackHelper() {
113         return mCallbackHelper;
114     }
115
116     //--------------------------------------------------------------------------------------------
117     //             XWalkViewInternal specific methods that map directly to XWalkViewClient / XWalkWebChromeClient
118     //--------------------------------------------------------------------------------------------
119
120     public abstract void getVisitedHistory(ValueCallback<String[]> callback);
121
122     public abstract void doUpdateVisitedHistory(String url, boolean isReload);
123
124     public abstract void onProgressChanged(int progress);
125
126     public abstract WebResourceResponse shouldInterceptRequest(String url);
127
128     public abstract void onResourceLoadStarted(String url);
129
130     public abstract void onResourceLoadFinished(String url);
131
132     public abstract void onLoadResource(String url);
133
134     public abstract boolean shouldOverrideUrlLoading(String url);
135
136     public abstract void onUnhandledKeyEvent(KeyEvent event);
137
138     public abstract boolean onConsoleMessage(ConsoleMessage consoleMessage);
139
140     public abstract void onReceivedHttpAuthRequest(XWalkHttpAuthHandler handler,
141             String host, String realm);
142
143     public abstract void onReceivedSslError(ValueCallback<Boolean> callback, SslError error);
144
145     public abstract void onReceivedLoginRequest(String realm, String account, String args);
146
147     public abstract void onFormResubmission(Message dontResend, Message resend);
148
149     public abstract void onDownloadStart(String url, String userAgent, String contentDisposition,
150             String mimeType, long contentLength);
151
152     public abstract void onGeolocationPermissionsShowPrompt(String origin,
153             XWalkGeolocationPermissions.Callback callback);
154
155     public abstract void onGeolocationPermissionsHidePrompt();
156
157     public final void onScaleChanged(float oldScaleFactor, float newScaleFactor) {
158         onScaleChangedScaled((float)(oldScaleFactor * mDIPScale), (float)(newScaleFactor * mDIPScale));
159     }
160
161     public abstract void onScaleChangedScaled(float oldScale, float newScale);
162
163     protected abstract boolean onCreateWindow(boolean isDialog, boolean isUserGesture);
164
165     protected abstract void onCloseWindow();
166
167     public abstract void onReceivedIcon(Bitmap bitmap);
168
169     protected abstract void onRequestFocus();
170
171     public abstract void onPageStarted(String url);
172
173     public abstract void onPageFinished(String url);
174
175     protected abstract void onStopLoading();
176
177     public abstract void onReceivedError(int errorCode, String description, String failingUrl);
178
179     public abstract void onRendererUnresponsive();
180
181     public abstract void onRendererResponsive();
182
183     public abstract void onTitleChanged(String title);
184
185     public abstract void onToggleFullscreen(boolean enterFullscreen);
186
187     public abstract boolean hasEnteredFullscreen();
188
189     public abstract boolean shouldOverrideRunFileChooser(
190             int processId, int renderId, int mode, String acceptTypes, boolean capture);
191
192     // TODO (michaelbai): Remove this method once the same method remove from
193     // XWalkContentsClientAdapter.
194     public abstract void onShowCustomView(View view,
195            int requestedOrientation, XWalkWebChromeClient.CustomViewCallback callback);
196
197     // TODO (michaelbai): This method should be abstract, having empty body here
198     // makes the merge to the Android easy.
199     public void onShowCustomView(View view, XWalkWebChromeClient.CustomViewCallback callback) {
200         onShowCustomView(view, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED, callback);
201     }
202
203     public abstract void onHideCustomView();
204
205     public abstract void didFinishLoad(String url);
206
207     //--------------------------------------------------------------------------------------------
208     //                              Other XWalkViewInternal-specific methods
209     //--------------------------------------------------------------------------------------------
210     //
211     public abstract void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
212             boolean isDoneCounting);
213
214     /**
215      * Called whenever there is a new content picture available.
216      * @param picture New picture.
217      */
218     public abstract void onNewPicture(Picture picture);
219
220     public abstract boolean shouldOpenWithDefaultBrowser(String contentUrl);
221 }