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;
47 private final int INVALID_ORIENTATION = -2;
48 private int mPreOrientation = INVALID_ORIENTATION;
50 public XWalkWebChromeClient(XWalkViewInternal view) {
51 mContext = view.getContext();
55 void setContentsClient(XWalkContentsClient client) {
56 mContentsClient = client;
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.
64 public void onReceivedIcon(XWalkViewInternal view, Bitmap icon) {}
67 * A callback interface used by the host application to notify
68 * the current page that its custom view has been dismissed.
70 public interface CustomViewCallback {
72 * Invoked when the host application dismisses the
75 public void onCustomViewHidden();
78 private Activity addContentView(View view, CustomViewCallback callback) {
79 Activity activity = mXWalkView.getActivity();
81 if (mCustomXWalkView != null || activity == null) {
82 callback.onCustomViewHidden();
86 mCustomXWalkView = view;
87 mCustomViewCallback = callback;
89 if (mContentsClient != null) {
90 mContentsClient.onToggleFullscreen(true);
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,
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
110 public void onShowCustomView(View view, CustomViewCallback callback) {
111 addContentView(view, callback);
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
123 public void onShowCustomView(View view, int requestedOrientation,
124 CustomViewCallback callback) {
125 Activity activity = addContentView(view, callback);
126 if (activity == null) return;
128 final int orientation = activity.getResources().getConfiguration().orientation;
130 if (requestedOrientation != orientation &&
131 requestedOrientation >= ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED &&
132 requestedOrientation <= ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
133 mPreOrientation = orientation;
134 activity.setRequestedOrientation(requestedOrientation);
139 * Notify the host application that the current page would
140 * like to hide its custom view.
142 public void onHideCustomView() {
143 Activity activity = mXWalkView.getActivity();
145 if (mCustomXWalkView == null || activity == null) return;
147 if (mContentsClient != null) {
148 mContentsClient.onToggleFullscreen(false);
151 // Remove video view from activity's ContentView.
152 FrameLayout decor = (FrameLayout) activity.getWindow().getDecorView();
153 decor.removeView(mCustomXWalkView);
154 mCustomViewCallback.onCustomViewHidden();
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;
163 mCustomXWalkView = null;
164 mCustomViewCallback = null;
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
176 * @param url The URL of the page that triggered the notification
177 * @param databaseIdentifier The identifier of the database where the quota
179 * @param quota The quota for the origin, in bytes
180 * @param estimatedDatabaseSize The estimated size of the offending
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.
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);
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
210 * @param requiredStorage The amount of storage required by the Application
211 * Cache operation that triggered this notification,
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.
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);
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
238 * @param callback The callback to use to set the permission state for the
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);
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.
254 public void onGeolocationPermissionsHidePrompt() {}
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
264 * @return boolean Whether the JavaScript execution should be interrupted.
266 public boolean onJsTimeout() {
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)}
280 public void onConsoleMessage(String message, int lineNumber, String sourceID) { }
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.
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());
295 /** Obtains a list of all visited history items, used for link coloring
297 public void getVisitedHistory(ValueCallback<String[]> callback) {
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.
305 public void setInstallableWebApp() { }
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.
315 public void setupAutoFill(Message msg) { }