Upstream version 6.35.131.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkWebChromeClient.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.xwalk.core;
18
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.pm.ActivityInfo;
22 import android.graphics.Bitmap;
23 import android.net.Uri;
24 import android.os.Message;
25 import android.view.Gravity;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.webkit.WebStorage;
29 import android.webkit.ConsoleMessage;
30 import android.webkit.ValueCallback;
31 import android.widget.FrameLayout;
32
33 /**
34  * It's an internal legacy class which is to handle kinds of ui related
35  * callback functions. It only handles those which are not exposed to
36  * external users compared to XWalkUIClient.
37  *
38  * @hide
39  */
40 public class XWalkWebChromeClient {
41     private Context mContext;
42     private View mCustomXWalkView;
43     private XWalkView mXWalkView;
44     private XWalkWebChromeClient.CustomViewCallback mCustomViewCallback;
45     private XWalkContentsClient mContentsClient = null;
46     private long XWALK_MAX_QUOTA = 1024 * 1024 * 100;
47
48     public XWalkWebChromeClient(XWalkView view) {
49         mContext = view.getContext();
50         mXWalkView = view;
51     }
52
53     void setContentsClient(XWalkContentsClient client) {
54         mContentsClient = client;
55     }
56
57     /**
58      * Notify the host application of a change in the document title.
59      * @param view The XWalkView that initiated the callback.
60      * @param title A String containing the new title of the document.
61      */
62     public void onReceivedTitle(XWalkView view, String title) {}
63
64     /**
65      * Notify the host application of a new favicon for the current page.
66      * @param view The XWalkView that initiated the callback.
67      * @param icon A Bitmap containing the favicon for the current page.
68      */
69     public void onReceivedIcon(XWalkView view, Bitmap icon) {}
70
71     /**
72      * Notify the host application of the url for an apple-touch-icon.
73      * @param view The XWalkView that initiated the callback.
74      * @param url The icon url.
75      * @param precomposed True if the url is for a precomposed touch icon.
76      */
77     public void onReceivedTouchIconUrl(XWalkView view, String url,
78             boolean precomposed) {}
79
80     /**
81      * A callback interface used by the host application to notify
82      * the current page that its custom view has been dismissed.
83      */
84     public interface CustomViewCallback {
85         /**
86          * Invoked when the host application dismisses the
87          * custom view.
88          */
89         public void onCustomViewHidden();
90     }
91
92     /**
93      * Notify the host application that the current page would
94      * like to show a custom View.
95      * @param view is the View object to be shown.
96      * @param callback is the callback to be invoked if and when the view
97      * is dismissed.
98      */
99     public void onShowCustomView(View view, CustomViewCallback callback) {
100         Activity activity = mXWalkView.getActivity();
101
102         if (mCustomXWalkView != null || activity == null) {
103             callback.onCustomViewHidden();
104             return;
105         }
106
107         mCustomXWalkView = view;
108         mCustomViewCallback = callback;
109
110         if (mContentsClient != null) {
111             mContentsClient.onToggleFullscreen(true);
112         }
113
114         // Add the video view to the activity's ContentView.
115         activity.getWindow().addContentView(view,
116                 new FrameLayout.LayoutParams(
117                         ViewGroup.LayoutParams.MATCH_PARENT,
118                         ViewGroup.LayoutParams.MATCH_PARENT,
119                         Gravity.CENTER));
120     }
121
122     /**
123      * Notify the host application that the current page would
124      * like to show a custom View in a particular orientation.
125      * @param view is the View object to be shown.
126      * @param requestedOrientation An orientation constant as used in
127      * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
128      * @param callback is the callback to be invoked if and when the view
129      * is dismissed.
130      */
131     public void onShowCustomView(View view, int requestedOrientation,
132             CustomViewCallback callback) {};
133
134     /**
135      * Notify the host application that the current page would
136      * like to hide its custom view.
137      */
138     public void onHideCustomView() {
139         Activity activity = mXWalkView.getActivity();
140
141         if (mCustomXWalkView == null || activity == null) return;
142
143         if (mContentsClient != null) {
144             mContentsClient.onToggleFullscreen(true);
145         }
146
147         // Remove video view from activity's ContentView.
148         FrameLayout decor = (FrameLayout) activity.getWindow().getDecorView();
149         decor.removeView(mCustomXWalkView);
150         mCustomViewCallback.onCustomViewHidden();
151
152         mCustomXWalkView = null;
153         mCustomViewCallback = null;
154     }
155
156     /**
157      * Request the host application to create a new window. If the host
158      * application chooses to honor this request, it should return true from
159      * this method, create a new XWalkView to host the window, insert it into the
160      * View system and send the supplied resultMsg message to its target with
161      * the new XWalkView as an argument. If the host application chooses not to
162      * honor the request, it should return false from this method. The default
163      * implementation of this method does nothing and hence returns false.
164      * @param view The XWalkView from which the request for a new window
165      *             originated.
166      * @param isDialog True if the new window should be a dialog, rather than
167      *                 a full-size window.
168      * @param isUserGesture True if the request was initiated by a user gesture,
169      *                      such as the user clicking a link.
170      * @param resultMsg The message to send when once a new XWalkView has been
171      *                  created. resultMsg.obj is a
172      *                  {@link XWalkView.XWalkViewTransport} object. This should be
173      *                  used to transport the new XWalkView, by calling
174      *                  {@link XWalkView.XWalkViewTransport#setXWalkView(XWalkView)
175      *                  XWalkView.XWalkViewTransport.setXWalkView(XWalkView)}.
176      * @return This method should return true if the host application will
177      *         create a new window, in which case resultMsg should be sent to
178      *         its target. Otherwise, this method should return false. Returning
179      *         false from this method but also sending resultMsg will result in
180      *         undefined behavior.
181      */
182     public boolean onCreateWindow(XWalkView view, boolean isDialog,
183             boolean isUserGesture, Message resultMsg) {
184         return false;
185     }
186
187    /**
188     * Tell the client that the quota has been exceeded for the Web SQL Database
189     * API for a particular origin and request a new quota. The client must
190     * respond by invoking the
191     * {@link WebStorage.QuotaUpdater#updateQuota(long) updateQuota(long)}
192     * method of the supplied {@link WebStorage.QuotaUpdater} instance. The
193     * minimum value that can be set for the new quota is the current quota. The
194     * default implementation responds with the current quota, so the quota will
195     * not be increased.
196     * @param url The URL of the page that triggered the notification
197     * @param databaseIdentifier The identifier of the database where the quota
198     *                           was exceeded.
199     * @param quota The quota for the origin, in bytes
200     * @param estimatedDatabaseSize The estimated size of the offending
201     *                              database, in bytes
202     * @param totalQuota The total quota for all origins, in bytes
203     * @param quotaUpdater An instance of {@link WebStorage.QuotaUpdater} which
204     *                     must be used to inform the XWalkView of the new quota.
205     */
206     // Note that the callback must always be executed at some point to ensure
207     // that the sleeping WebCore thread is woken up.
208     // Since the parameter type WebStorage.QuotaUpdater and this API are
209     // deprecated in Android 4.4, while this parameter type and this API
210     // are still used before Android 4.4, no other API and parameter are
211     // to replace them, suppress the compiling warnings for Android 4.4
212     // due to deprecation.
213     @SuppressWarnings("deprecation")
214     public void onExceededDatabaseQuota(String url, String databaseIdentifier,
215             long quota, long estimatedDatabaseSize, long totalQuota,
216             WebStorage.QuotaUpdater quotaUpdater) {
217         // This default implementation passes the current quota back to WebCore.
218         // WebCore will interpret this that new quota was declined.
219         quotaUpdater.updateQuota(XWALK_MAX_QUOTA);
220     }
221
222    /**
223     * Tell the client that the quota has been reached for the Application Cache
224     * API and request a new quota. The client must respond by invoking the
225     * {@link WebStorage.QuotaUpdater#updateQuota(long) updateQuota(long)}
226     * method of the supplied {@link WebStorage.QuotaUpdater} instance. The
227     * minimum value that can be set for the new quota is the current quota. The
228     * default implementation responds with the current quota, so the quota will
229     * not be increased.
230     * @param requiredStorage The amount of storage required by the Application
231     *                        Cache operation that triggered this notification,
232     *                        in bytes.
233     * @param quota The quota, in bytes
234     * @param quotaUpdater An instance of {@link WebStorage.QuotaUpdater} which
235     *                     must be used to inform the XWalkView of the new quota.
236     */
237     // Note that the callback must always be executed at some point to ensure
238     // that the sleeping WebCore thread is woken up.
239     // Since the parameter type WebStorage.QuotaUpdater and this API are
240     // deprecated in Android 4.4, while this parameter type and this API
241     // are still used before Android 4.4, no other API and parameter are
242     // to replace them, suppress the compiling warnings for Android 4.4
243     // due to deprecation.
244     @SuppressWarnings("deprecation")
245     public void onReachedMaxAppCacheSize(long requiredStorage, long quota,
246             WebStorage.QuotaUpdater quotaUpdater) {
247         quotaUpdater.updateQuota(XWALK_MAX_QUOTA);
248     }
249
250     /**
251      * Notify the host application that web content from the specified origin
252      * is attempting to use the Geolocation API, but no permission state is
253      * currently set for that origin. The host application should invoke the
254      * specified callback with the desired permission state. See
255      * {@link GeolocationPermissions} for details.
256      * @param origin The origin of the web content attempting to use the
257      *               Geolocation API.
258      * @param callback The callback to use to set the permission state for the
259      *                 origin.
260      */
261     public void onGeolocationPermissionsShowPrompt(String origin,
262             XWalkGeolocationPermissions.Callback callback) {
263         // Allow all origins for geolocation requests here for Crosswalk.
264         // TODO(yongsheng): Need to define a UI prompt?
265         callback.invoke(origin, true, false);
266     }
267
268     /**
269      * Notify the host application that a request for Geolocation permissions,
270      * made with a previous call to
271      * {@link #onGeolocationPermissionsShowPrompt(String,GeolocationPermissions.Callback) onGeolocationPermissionsShowPrompt()}
272      * has been canceled. Any related UI should therefore be hidden.
273      */
274     public void onGeolocationPermissionsHidePrompt() {}
275
276     /**
277      * Tell the client that a JavaScript execution timeout has occured. And the
278      * client may decide whether or not to interrupt the execution. If the
279      * client returns true, the JavaScript will be interrupted. If the client
280      * returns false, the execution will continue. Note that in the case of
281      * continuing execution, the timeout counter will be reset, and the callback
282      * will continue to occur if the script does not finish at the next check
283      * point.
284      * @return boolean Whether the JavaScript execution should be interrupted.
285      */
286     public boolean onJsTimeout() {
287         return true;
288     }
289
290     /**
291      * Report a JavaScript error message to the host application. The ChromeClient
292      * should override this to process the log message as they see fit.
293      * @param message The error message to report.
294      * @param lineNumber The line number of the error.
295      * @param sourceID The name of the source file that caused the error.
296      * @deprecated Use {@link #onConsoleMessage(ConsoleMessage) onConsoleMessage(ConsoleMessage)}
297      *      instead.
298      */
299     @Deprecated
300     public void onConsoleMessage(String message, int lineNumber, String sourceID) { }
301
302     /**
303      * Report a JavaScript console message to the host application. The ChromeClient
304      * should override this to process the log message as they see fit.
305      * @param consoleMessage Object containing details of the console message.
306      * @return true if the message is handled by the client.
307      */
308     public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
309         // Call the old version of this function for backwards compatability.
310         onConsoleMessage(consoleMessage.message(), consoleMessage.lineNumber(),
311                 consoleMessage.sourceId());
312         return false;
313     }
314
315     /**
316      * When not playing, video elements are represented by a 'poster' image. The
317      * image to use can be specified by the poster attribute of the video tag in
318      * HTML. If the attribute is absent, then a default poster will be used. This
319      * method allows the ChromeClient to provide that default image.
320      *
321      * @return Bitmap The image to use as a default poster, or null if no such image is
322      * available.
323      */
324     public Bitmap getDefaultVideoPoster() {
325         return null;
326     }
327
328     /**
329      * When the user starts to playback a video element, it may take time for enough
330      * data to be buffered before the first frames can be rendered. While this buffering
331      * is taking place, the ChromeClient can use this function to provide a View to be
332      * displayed. For example, the ChromeClient could show a spinner animation.
333      *
334      * @return View The View to be displayed whilst the video is loading.
335      */
336     public View getVideoLoadingProgressView() {
337         return null;
338     }
339
340     /** Obtains a list of all visited history items, used for link coloring
341      */
342     public void getVisitedHistory(ValueCallback<String[]> callback) {
343     }
344
345     /**
346      * Tell the client that the page being viewed is web app capable,
347      * i.e. has specified the fullscreen-web-app-capable meta tag.
348      * @hide
349      */
350     public void setInstallableWebApp() { }
351
352     /**
353      * Tell the client that the page being viewed has an autofillable
354      * form and the user would like to set a profile up.
355      * @param msg A Message to send once the user has successfully
356      *      set up a profile and to inform the WebTextView it should
357      *      now autofill using that new profile.
358      * @hide
359      */
360     public void setupAutoFill(Message msg) { }
361 }