a52d61f9eae2d7717046b89a1afcb6abb20a7cf4
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / 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 // TODO(yongsheng): remove public modifier.
39 public abstract class XWalkContentsClient extends ContentViewClient {
40
41     private static final String TAG = "XWalkContentsClient";
42     private final XWalkContentsClientCallbackHelper mCallbackHelper =
43         new XWalkContentsClientCallbackHelper(this);
44
45     private XWalkWebContentsObserver mWebContentsObserver;
46
47     private double mDIPScale;
48
49     public class XWalkWebContentsObserver extends WebContentsObserverAndroid {
50         public XWalkWebContentsObserver(ContentViewCore contentViewCore) {
51             super(contentViewCore);
52         }
53
54         @Override
55         public void didStopLoading(String url) {
56             onPageFinished(url);
57         }
58
59         @Override
60         public void didFailLoad(boolean isProvisionalLoad,
61                 boolean isMainFrame, int errorCode, String description, String failingUrl) {
62             if (errorCode == NetError.ERR_ABORTED || !isMainFrame) {
63                 // This error code is generated for the following reasons:
64                 // - XWalkView.stopLoading is called,
65                 // - the navigation is intercepted by the embedder via shouldOverrideNavigation.
66                 //
67                 // The XWalkView does not notify the embedder of these situations using this
68                 // error code with the XWalkClient.onReceivedError callback. What's more,
69                 // the XWalkView does not notify the embedder of sub-frame failures.
70                 return;
71             }
72             onReceivedError(errorCode, 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     void installWebContentsObserver(ContentViewCore contentViewCore) {
92         if (mWebContentsObserver != null) {
93             mWebContentsObserver.detachFromWebContents();
94         }
95         mWebContentsObserver = new XWalkWebContentsObserver(contentViewCore);
96     }
97
98     void setDIPScale(double dipScale) {
99         mDIPScale = dipScale;
100     }
101
102     final XWalkContentsClientCallbackHelper getCallbackHelper() {
103         return mCallbackHelper;
104     }
105
106     //--------------------------------------------------------------------------------------------
107     //             XWalkView specific methods that map directly to XWalkViewClient / XWalkWebChromeClient
108     //--------------------------------------------------------------------------------------------
109
110     public abstract void getVisitedHistory(ValueCallback<String[]> callback);
111
112     public abstract void doUpdateVisitedHistory(String url, boolean isReload);
113
114     public abstract void onProgressChanged(int progress);
115
116     public abstract WebResourceResponse shouldInterceptRequest(String url);
117
118     public abstract void onLoadResource(String url);
119
120     public abstract boolean shouldOverrideUrlLoading(String url);
121
122     public abstract void onUnhandledKeyEvent(KeyEvent event);
123
124     public abstract boolean onConsoleMessage(ConsoleMessage consoleMessage);
125
126     public abstract void onReceivedHttpAuthRequest(XWalkHttpAuthHandler handler,
127             String host, String realm);
128
129     public abstract void onReceivedSslError(SslErrorHandler handler, SslError error);
130
131     public abstract void onReceivedLoginRequest(String realm, String account, String args);
132
133     public abstract void onFormResubmission(Message dontResend, Message resend);
134
135     public abstract void onDownloadStart(String url, String userAgent, String contentDisposition,
136             String mimeType, long contentLength);
137
138     public abstract void onGeolocationPermissionsShowPrompt(String origin,
139             XWalkGeolocationPermissions.Callback callback);
140
141     public abstract void onGeolocationPermissionsHidePrompt();
142
143     public final void onScaleChanged(float oldScale, float newScale) {
144         onScaleChangedScaled((float)(oldScale * mDIPScale), (float)(newScale * mDIPScale));
145     }
146
147     public abstract void onScaleChangedScaled(float oldScale, float newScale);
148
149     protected abstract void handleJsAlert(String url, String message, JsResult result);
150
151     protected abstract void handleJsBeforeUnload(String url, String message,
152             JsResult result);
153
154     protected abstract void handleJsConfirm(String url, String message, JsResult result);
155
156     protected abstract void handleJsPrompt(String url, String message, String defaultValue,
157             JsPromptResult result);
158
159     protected abstract boolean onCreateWindow(boolean isDialog, boolean isUserGesture);
160
161     protected abstract void onCloseWindow();
162
163     public abstract void onReceivedTouchIconUrl(String url, boolean precomposed);
164
165     public abstract void onReceivedIcon(Bitmap bitmap);
166
167     protected abstract void onRequestFocus();
168
169     protected abstract View getVideoLoadingProgressView();
170
171     public abstract void onPageStarted(String url);
172
173     public abstract void onPageFinished(String url);
174
175     public abstract void onReceivedError(int errorCode, String description, String failingUrl);
176
177     public abstract void onRendererUnresponsive();
178
179     public abstract void onRendererResponsive();
180
181     final public void onUpdateTitle(String title) {
182         onTitleChanged(title);
183     }
184
185     public abstract void onTitleChanged(String title);
186
187     public abstract void onToggleFullscreen(boolean enterFullscreen);
188
189     public abstract boolean isFullscreen();
190
191     // TODO (michaelbai): Remove this method once the same method remove from
192     // XWalkContentsClientAdapter.
193     public void onShowCustomView(View view,
194            int requestedOrientation, XWalkWebChromeClient.CustomViewCallback callback) {
195     }
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 Bitmap getDefaultVideoPoster();
206
207     public abstract void didFinishLoad(String url);
208
209     //--------------------------------------------------------------------------------------------
210     //                              Other XWalkView-specific methods
211     //--------------------------------------------------------------------------------------------
212     //
213     public abstract void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
214             boolean isDoneCounting);
215
216     /**
217      * Called whenever there is a new content picture available.
218      * @param picture New picture.
219      */
220     public abstract void onNewPicture(Picture picture);
221 }