2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.xwalk.core.internal;
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;
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.
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;
48 public XWalkWebChromeClient(XWalkViewInternal view) {
49 mContext = view.getContext();
53 void setContentsClient(XWalkContentsClient client) {
54 mContentsClient = client;
58 * Notify the host application of a change in the document title.
59 * @param view The XWalkViewInternal that initiated the callback.
60 * @param title A String containing the new title of the document.
62 public void onReceivedTitle(XWalkViewInternal view, String title) {}
65 * Notify the host application of a new favicon for the current page.
66 * @param view The XWalkViewInternal that initiated the callback.
67 * @param icon A Bitmap containing the favicon for the current page.
69 public void onReceivedIcon(XWalkViewInternal view, Bitmap icon) {}
72 * Notify the host application of the url for an apple-touch-icon.
73 * @param view The XWalkViewInternal that initiated the callback.
74 * @param url The icon url.
75 * @param precomposed True if the url is for a precomposed touch icon.
77 public void onReceivedTouchIconUrl(XWalkViewInternal view, String url,
78 boolean precomposed) {}
81 * A callback interface used by the host application to notify
82 * the current page that its custom view has been dismissed.
84 public interface CustomViewCallback {
86 * Invoked when the host application dismisses the
89 public void onCustomViewHidden();
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
99 public void onShowCustomView(View view, CustomViewCallback callback) {
100 Activity activity = mXWalkView.getActivity();
102 if (mCustomXWalkView != null || activity == null) {
103 callback.onCustomViewHidden();
107 mCustomXWalkView = view;
108 mCustomViewCallback = callback;
110 if (mContentsClient != null) {
111 mContentsClient.onToggleFullscreen(true);
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,
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
131 public void onShowCustomView(View view, int requestedOrientation,
132 CustomViewCallback callback) {};
135 * Notify the host application that the current page would
136 * like to hide its custom view.
138 public void onHideCustomView() {
139 Activity activity = mXWalkView.getActivity();
141 if (mCustomXWalkView == null || activity == null) return;
143 if (mContentsClient != null) {
144 mContentsClient.onToggleFullscreen(true);
147 // Remove video view from activity's ContentView.
148 FrameLayout decor = (FrameLayout) activity.getWindow().getDecorView();
149 decor.removeView(mCustomXWalkView);
150 mCustomViewCallback.onCustomViewHidden();
152 mCustomXWalkView = null;
153 mCustomViewCallback = null;
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 XWalkViewInternal to host the window, insert it into the
160 * View system and send the supplied resultMsg message to its target with
161 * the new XWalkViewInternal 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 XWalkViewInternal from which the request for a new window
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 XWalkViewInternal has been
171 * created. resultMsg.obj is a
172 * {@link XWalkViewInternal.XWalkViewTransport} object. This should be
173 * used to transport the new XWalkViewInternal, by calling
174 * {@link XWalkViewInternal.XWalkViewTransport#setXWalkView(XWalkViewInternal)
175 * XWalkViewInternal.XWalkViewTransport.setXWalkView(XWalkViewInternal)}.
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.
182 public boolean onCreateWindow(XWalkViewInternal view, boolean isDialog,
183 boolean isUserGesture, Message resultMsg) {
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
196 * @param url The URL of the page that triggered the notification
197 * @param databaseIdentifier The identifier of the database where the quota
199 * @param quota The quota for the origin, in bytes
200 * @param estimatedDatabaseSize The estimated size of the offending
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 XWalkViewInternal of the new quota.
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);
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
230 * @param requiredStorage The amount of storage required by the Application
231 * Cache operation that triggered this notification,
233 * @param quota The quota, in bytes
234 * @param quotaUpdater An instance of {@link WebStorage.QuotaUpdater} which
235 * must be used to inform the XWalkViewInternal of the new quota.
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);
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
258 * @param callback The callback to use to set the permission state for the
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);
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.
274 public void onGeolocationPermissionsHidePrompt() {}
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
284 * @return boolean Whether the JavaScript execution should be interrupted.
286 public boolean onJsTimeout() {
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)}
300 public void onConsoleMessage(String message, int lineNumber, String sourceID) { }
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.
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());
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.
321 * @return Bitmap The image to use as a default poster, or null if no such image is
324 public Bitmap getDefaultVideoPoster() {
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.
334 * @return View The View to be displayed whilst the video is loading.
336 public View getVideoLoadingProgressView() {
340 /** Obtains a list of all visited history items, used for link coloring
342 public void getVisitedHistory(ValueCallback<String[]> callback) {
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.
350 public void setInstallableWebApp() { }
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.
360 public void setupAutoFill(Message msg) { }