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