Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / 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.internal;
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 XWalkUIClientInternal.
37  *
38  * @hide
39  */
40 public class XWalkWebChromeClient {
41     private Context mContext;
42     private View mCustomXWalkView;
43     private XWalkViewInternal mXWalkView;
44     private XWalkWebChromeClient.CustomViewCallback mCustomViewCallback;
45     private XWalkContentsClient mContentsClient = null;
46     private long XWALK_MAX_QUOTA = 1024 * 1024 * 100;
47     private final int INVALID_ORIENTATION = -2;
48     private int mPreOrientation = INVALID_ORIENTATION;
49
50     public XWalkWebChromeClient(XWalkViewInternal view) {
51         mContext = view.getContext();
52         mXWalkView = view;
53     }
54
55     void setContentsClient(XWalkContentsClient client) {
56         mContentsClient = client;
57     }
58
59     /**
60      * Notify the host application of a new favicon for the current page.
61      * @param view The XWalkViewInternal that initiated the callback.
62      * @param icon A Bitmap containing the favicon for the current page.
63      */
64     public void onReceivedIcon(XWalkViewInternal view, Bitmap icon) {}
65
66     /**
67      * A callback interface used by the host application to notify
68      * the current page that its custom view has been dismissed.
69      */
70     public interface CustomViewCallback {
71         /**
72          * Invoked when the host application dismisses the
73          * custom view.
74          */
75         public void onCustomViewHidden();
76     }
77
78     private Activity addContentView(View view, CustomViewCallback callback) {
79         Activity activity = mXWalkView.getActivity();
80
81         if (mCustomXWalkView != null || activity == null) {
82             callback.onCustomViewHidden();
83             return null;
84         }
85
86         mCustomXWalkView = view;
87         mCustomViewCallback = callback;
88
89         if (mContentsClient != null) {
90             mContentsClient.onToggleFullscreen(true);
91         }
92
93         // Add the video view to the activity's DecorView.
94         FrameLayout decor = (FrameLayout) activity.getWindow().getDecorView();
95         decor.addView(mCustomXWalkView, 0,
96                 new FrameLayout.LayoutParams(
97                         ViewGroup.LayoutParams.MATCH_PARENT,
98                         ViewGroup.LayoutParams.MATCH_PARENT,
99                         Gravity.CENTER));
100         return activity;
101     }
102
103     /**
104      * Notify the host application that the current page would
105      * like to show a custom View.
106      * @param view is the View object to be shown.
107      * @param callback is the callback to be invoked if and when the view
108      * is dismissed.
109      */
110     public void onShowCustomView(View view, CustomViewCallback callback) {
111         addContentView(view, callback);
112     }
113
114     /**
115      * Notify the host application that the current page would
116      * like to show a custom View in a particular orientation.
117      * @param view is the View object to be shown.
118      * @param requestedOrientation An orientation constant as used in
119      * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
120      * @param callback is the callback to be invoked if and when the view
121      * is dismissed.
122      */
123     public void onShowCustomView(View view, int requestedOrientation,
124             CustomViewCallback callback) {
125         Activity activity = addContentView(view, callback);
126         if (activity == null) return;
127
128         final int orientation = activity.getResources().getConfiguration().orientation;
129
130         if (requestedOrientation != orientation &&
131                 requestedOrientation >= ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED &&
132                 requestedOrientation <= ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
133             mPreOrientation = orientation;
134             activity.setRequestedOrientation(requestedOrientation);
135         }
136     };
137
138     /**
139      * Notify the host application that the current page would
140      * like to hide its custom view.
141      */
142     public void onHideCustomView() {
143         Activity activity = mXWalkView.getActivity();
144
145         if (mCustomXWalkView == null || activity == null) return;
146
147         if (mContentsClient != null) {
148             mContentsClient.onToggleFullscreen(false);
149         }
150
151         // Remove video view from activity's ContentView.
152         FrameLayout decor = (FrameLayout) activity.getWindow().getDecorView();
153         decor.removeView(mCustomXWalkView);
154         mCustomViewCallback.onCustomViewHidden();
155
156         if (mPreOrientation != INVALID_ORIENTATION &&
157                 mPreOrientation >= ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED &&
158                 mPreOrientation <= ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
159             activity.setRequestedOrientation(mPreOrientation);
160             mPreOrientation = INVALID_ORIENTATION;
161         }
162
163         mCustomXWalkView = null;
164         mCustomViewCallback = null;
165     }
166
167    /**
168     * Tell the client that the quota has been exceeded for the Web SQL Database
169     * API for a particular origin and request a new quota. The client must
170     * respond by invoking the
171     * {@link WebStorage.QuotaUpdater#updateQuota(long) updateQuota(long)}
172     * method of the supplied {@link WebStorage.QuotaUpdater} instance. The
173     * minimum value that can be set for the new quota is the current quota. The
174     * default implementation responds with the current quota, so the quota will
175     * not be increased.
176     * @param url The URL of the page that triggered the notification
177     * @param databaseIdentifier The identifier of the database where the quota
178     *                           was exceeded.
179     * @param quota The quota for the origin, in bytes
180     * @param estimatedDatabaseSize The estimated size of the offending
181     *                              database, in bytes
182     * @param totalQuota The total quota for all origins, in bytes
183     * @param quotaUpdater An instance of {@link WebStorage.QuotaUpdater} which
184     *                     must be used to inform the XWalkViewInternal of the new quota.
185     */
186     // Note that the callback must always be executed at some point to ensure
187     // that the sleeping WebCore thread is woken up.
188     // Since the parameter type WebStorage.QuotaUpdater and this API are
189     // deprecated in Android 4.4, while this parameter type and this API
190     // are still used before Android 4.4, no other API and parameter are
191     // to replace them, suppress the compiling warnings for Android 4.4
192     // due to deprecation.
193     @SuppressWarnings("deprecation")
194     public void onExceededDatabaseQuota(String url, String databaseIdentifier,
195             long quota, long estimatedDatabaseSize, long totalQuota,
196             WebStorage.QuotaUpdater quotaUpdater) {
197         // This default implementation passes the current quota back to WebCore.
198         // WebCore will interpret this that new quota was declined.
199         quotaUpdater.updateQuota(XWALK_MAX_QUOTA);
200     }
201
202    /**
203     * Tell the client that the quota has been reached for the Application Cache
204     * API and request a new quota. The client must respond by invoking the
205     * {@link WebStorage.QuotaUpdater#updateQuota(long) updateQuota(long)}
206     * method of the supplied {@link WebStorage.QuotaUpdater} instance. The
207     * minimum value that can be set for the new quota is the current quota. The
208     * default implementation responds with the current quota, so the quota will
209     * not be increased.
210     * @param requiredStorage The amount of storage required by the Application
211     *                        Cache operation that triggered this notification,
212     *                        in bytes.
213     * @param quota The quota, in bytes
214     * @param quotaUpdater An instance of {@link WebStorage.QuotaUpdater} which
215     *                     must be used to inform the XWalkViewInternal of the new quota.
216     */
217     // Note that the callback must always be executed at some point to ensure
218     // that the sleeping WebCore thread is woken up.
219     // Since the parameter type WebStorage.QuotaUpdater and this API are
220     // deprecated in Android 4.4, while this parameter type and this API
221     // are still used before Android 4.4, no other API and parameter are
222     // to replace them, suppress the compiling warnings for Android 4.4
223     // due to deprecation.
224     @SuppressWarnings("deprecation")
225     public void onReachedMaxAppCacheSize(long requiredStorage, long quota,
226             WebStorage.QuotaUpdater quotaUpdater) {
227         quotaUpdater.updateQuota(XWALK_MAX_QUOTA);
228     }
229
230     /**
231      * Notify the host application that web content from the specified origin
232      * is attempting to use the Geolocation API, but no permission state is
233      * currently set for that origin. The host application should invoke the
234      * specified callback with the desired permission state. See
235      * {@link GeolocationPermissions} for details.
236      * @param origin The origin of the web content attempting to use the
237      *               Geolocation API.
238      * @param callback The callback to use to set the permission state for the
239      *                 origin.
240      */
241     public void onGeolocationPermissionsShowPrompt(String origin,
242             XWalkGeolocationPermissions.Callback callback) {
243         // Allow all origins for geolocation requests here for Crosswalk.
244         // TODO(yongsheng): Need to define a UI prompt?
245         callback.invoke(origin, true, false);
246     }
247
248     /**
249      * Notify the host application that a request for Geolocation permissions,
250      * made with a previous call to
251      * {@link #onGeolocationPermissionsShowPrompt(String,GeolocationPermissions.Callback) onGeolocationPermissionsShowPrompt()}
252      * has been canceled. Any related UI should therefore be hidden.
253      */
254     public void onGeolocationPermissionsHidePrompt() {}
255
256     /**
257      * Tell the client that a JavaScript execution timeout has occured. And the
258      * client may decide whether or not to interrupt the execution. If the
259      * client returns true, the JavaScript will be interrupted. If the client
260      * returns false, the execution will continue. Note that in the case of
261      * continuing execution, the timeout counter will be reset, and the callback
262      * will continue to occur if the script does not finish at the next check
263      * point.
264      * @return boolean Whether the JavaScript execution should be interrupted.
265      */
266     public boolean onJsTimeout() {
267         return true;
268     }
269
270     /**
271      * Report a JavaScript error message to the host application. The ChromeClient
272      * should override this to process the log message as they see fit.
273      * @param message The error message to report.
274      * @param lineNumber The line number of the error.
275      * @param sourceID The name of the source file that caused the error.
276      * @deprecated Use {@link #onConsoleMessage(ConsoleMessage) onConsoleMessage(ConsoleMessage)}
277      *      instead.
278      */
279     @Deprecated
280     public void onConsoleMessage(String message, int lineNumber, String sourceID) { }
281
282     /**
283      * Report a JavaScript console message to the host application. The ChromeClient
284      * should override this to process the log message as they see fit.
285      * @param consoleMessage Object containing details of the console message.
286      * @return true if the message is handled by the client.
287      */
288     public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
289         // Call the old version of this function for backwards compatability.
290         onConsoleMessage(consoleMessage.message(), consoleMessage.lineNumber(),
291                 consoleMessage.sourceId());
292         return false;
293     }
294
295     /** Obtains a list of all visited history items, used for link coloring
296      */
297     public void getVisitedHistory(ValueCallback<String[]> callback) {
298     }
299
300     /**
301      * Tell the client that the page being viewed is web app capable,
302      * i.e. has specified the fullscreen-web-app-capable meta tag.
303      * @hide
304      */
305     public void setInstallableWebApp() { }
306
307     /**
308      * Tell the client that the page being viewed has an autofillable
309      * form and the user would like to set a profile up.
310      * @param msg A Message to send once the user has successfully
311      *      set up a profile and to inform the WebTextView it should
312      *      now autofill using that new profile.
313      * @hide
314      */
315     public void setupAutoFill(Message msg) { }
316 }