9c7f16649d95de1b9c4bbafcf4646fb6ed39f355
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / java / src / org / xwalk / core / 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;
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.ContentViewCore;
26 import org.chromium.content.browser.WebContentsObserverAndroid;
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 public 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 WebContentsObserverAndroid {
49         public XWalkWebContentsObserver(ContentViewCore contentViewCore) {
50             super(contentViewCore);
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                 // - XWalkView.stopLoading is called,
64                 // - the navigation is intercepted by the embedder via shouldOverrideNavigation.
65                 //
66                 // The XWalkView does not notify the embedder of these situations using this
67                 // error code with the XWalkClient.onReceivedError callback. What's more,
68                 // the XWalkView does not notify the embedder of sub-frame failures.
69                 return;
70             }
71             onReceivedError(errorCode, description, failingUrl);
72         }
73
74         @Override
75         public void didNavigateAnyFrame(String url, String baseUrl, boolean isReload) {
76             doUpdateVisitedHistory(url, isReload);
77         }
78
79         @Override
80         public void didFinishLoad(long frameId, String validatedUrl, boolean isMainFrame) {
81             // Both didStopLoading and didFinishLoad will be called once a page is finished
82             // to load, but didStopLoading will also be called when user clicks "X" button
83             // on browser UI to stop loading page.
84             //
85             // So it is safe for Crosswalk to rely on didStopLoading to ensure onPageFinished
86             // can be called.
87         }
88     }
89
90     void installWebContentsObserver(ContentViewCore contentViewCore) {
91         if (mWebContentsObserver != null) {
92             mWebContentsObserver.detachFromWebContents();
93         }
94         mWebContentsObserver = new XWalkWebContentsObserver(contentViewCore);
95     }
96
97     void setDIPScale(double dipScale) {
98         mDIPScale = dipScale;
99     }
100
101     final XWalkContentsClientCallbackHelper getCallbackHelper() {
102         return mCallbackHelper;
103     }
104
105     //--------------------------------------------------------------------------------------------
106     //             XWalkView specific methods that map directly to XWalkViewClient / XWalkWebChromeClient
107     //--------------------------------------------------------------------------------------------
108
109     public abstract void getVisitedHistory(ValueCallback<String[]> callback);
110
111     public abstract void doUpdateVisitedHistory(String url, boolean isReload);
112
113     public abstract void onProgressChanged(int progress);
114
115     public abstract WebResourceResponse shouldInterceptRequest(String url);
116
117     public abstract void onLoadResource(String url);
118
119     public abstract boolean shouldOverrideUrlLoading(String url);
120
121     public abstract void onUnhandledKeyEvent(KeyEvent event);
122
123     public abstract boolean onConsoleMessage(ConsoleMessage consoleMessage);
124
125     public abstract void onReceivedHttpAuthRequest(XWalkHttpAuthHandler handler,
126             String host, String realm);
127
128     public abstract void onReceivedSslError(SslErrorHandler handler, SslError error);
129
130     public abstract void onReceivedLoginRequest(String realm, String account, String args);
131
132     public abstract void onFormResubmission(Message dontResend, Message resend);
133
134     public abstract void onDownloadStart(String url, String userAgent, String contentDisposition,
135             String mimeType, long contentLength);
136
137     public abstract void onGeolocationPermissionsShowPrompt(String origin,
138             XWalkGeolocationPermissions.Callback callback);
139
140     public abstract void onGeolocationPermissionsHidePrompt();
141
142     public final void onScaleChanged(float oldScale, float newScale) {
143         onScaleChangedScaled((float)(oldScale * mDIPScale), (float)(newScale * mDIPScale));
144     }
145
146     public abstract void onScaleChangedScaled(float oldScale, float newScale);
147
148     protected abstract void handleJsAlert(String url, String message, JsResult result);
149
150     protected abstract void handleJsBeforeUnload(String url, String message,
151             JsResult result);
152
153     protected abstract void handleJsConfirm(String url, String message, JsResult result);
154
155     protected abstract void handleJsPrompt(String url, String message, String defaultValue,
156             JsPromptResult result);
157
158     protected abstract boolean onCreateWindow(boolean isDialog, boolean isUserGesture);
159
160     protected abstract void onCloseWindow();
161
162     public abstract void onReceivedTouchIconUrl(String url, boolean precomposed);
163
164     public abstract void onReceivedIcon(Bitmap bitmap);
165
166     protected abstract void onRequestFocus();
167
168     protected abstract View getVideoLoadingProgressView();
169
170     public abstract void onPageStarted(String url);
171
172     public abstract void onPageFinished(String url);
173
174     public abstract void onReceivedError(int errorCode, String description, String failingUrl);
175
176     public abstract void onRendererUnresponsive();
177
178     public abstract void onRendererResponsive();
179
180     final public void onUpdateTitle(String title) {
181         onTitleChanged(title);
182     }
183
184     public abstract void onTitleChanged(String title);
185
186     public abstract void onToggleFullscreen(boolean enterFullscreen);
187
188     public abstract boolean isFullscreen();
189
190     // TODO (michaelbai): Remove this method once the same method remove from
191     // XWalkContentsClientAdapter.
192     public void onShowCustomView(View view,
193            int requestedOrientation, XWalkWebChromeClient.CustomViewCallback callback) {
194     }
195
196     // TODO (michaelbai): This method should be abstract, having empty body here
197     // makes the merge to the Android easy.
198     public void onShowCustomView(View view, XWalkWebChromeClient.CustomViewCallback callback) {
199         onShowCustomView(view, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED, callback);
200     }
201
202     public abstract void onHideCustomView();
203
204     public abstract Bitmap getDefaultVideoPoster();
205
206     public abstract void didFinishLoad(String url);
207
208     //--------------------------------------------------------------------------------------------
209     //                              Other XWalkView-specific methods
210     //--------------------------------------------------------------------------------------------
211     //
212     public abstract void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
213             boolean isDoneCounting);
214
215     /**
216      * Called whenever there is a new content picture available.
217      * @param picture New picture.
218      */
219     public abstract void onNewPicture(Picture picture);
220 }