1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.content.ActivityNotFoundException;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.util.Log;
11 import android.view.ActionMode;
12 import android.view.KeyEvent;
14 import org.chromium.content.browser.SelectActionModeCallback.ActionHandler;
16 import java.net.URISyntaxException;
19 * Main callback class used by ContentView.
21 * This contains the superset of callbacks required to implement the browser UI and the callbacks
22 * required to implement the WebView API.
23 * The memory and reference ownership of this class is unusual - see the .cc file and ContentView
26 * TODO(mkosiba): Rid this guy of default implementations. This class is used by both WebView and
27 * the browser and we don't want a the browser-specific default implementation to accidentally leak
30 public class ContentViewClient {
31 // Tag used for logging.
32 private static final String TAG = "ContentViewClient";
34 public void onUpdateTitle(String title) {
38 * Called whenever the background color of the page changes as notified by WebKit.
39 * @param color The new ARGB color of the page background.
41 public void onBackgroundColorChanged(int color) {
45 * Notifies the client that the position of the top controls has changed.
46 * @param topControlsOffsetYPix The Y offset of the top controls in physical pixels.
47 * @param contentOffsetYPix The Y offset of the content in physical pixels.
48 * @param overdrawBottomHeightPix The overdraw height.
50 public void onOffsetsForFullscreenChanged(
51 float topControlsOffsetYPix, float contentOffsetYPix, float overdrawBottomHeightPix) {
54 public boolean shouldOverrideKeyEvent(KeyEvent event) {
55 int keyCode = event.getKeyCode();
57 if (!shouldPropagateKey(keyCode)) return true;
59 // We also have to intercept some shortcuts before we send them to the ContentView.
60 if (event.isCtrlPressed() && (
61 keyCode == KeyEvent.KEYCODE_TAB ||
62 keyCode == KeyEvent.KEYCODE_W ||
63 keyCode == KeyEvent.KEYCODE_F4)) {
71 * Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
74 public void onImeEvent() {
78 * Notified when a change to the IME was requested.
80 * @param requestShow Whether the IME was requested to be shown (may already be showing
83 public void onImeStateChangeRequested(boolean requestShow) {
87 * Returns an ActionMode.Callback for in-page selection.
89 public ActionMode.Callback getSelectActionModeCallback(
90 Context context, ActionHandler actionHandler, boolean incognito) {
91 return new SelectActionModeCallback(context, actionHandler, incognito);
95 * Called when the contextual ActionBar is shown.
97 public void onContextualActionBarShown() {
101 * Called when the contextual ActionBar is hidden.
103 public void onContextualActionBarHidden() {
107 * Perform a search on {@code searchQuery}. This method is only called if
108 * {@link #doesPerformWebSearch()} returns {@code true}.
109 * @param searchQuery The string to search for.
111 public void performWebSearch(String searchQuery) {
115 * If this returns {@code true} contextual web search attempts will be forwarded to
116 * {@link #performWebSearch(String)}.
117 * @return {@code true} iff this {@link ContentViewClient} wants to consume web search queries
118 * and override the default intent behavior.
120 public boolean doesPerformWebSearch() {
125 * Notification that the selection has changed.
126 * TODO(donnd): Remove this and instead expose a ContextualSearchClient, crbug.com/403001.
127 * @param selection The newly established selection.
129 public void onSelectionChanged(String selection) {
133 * Notification that a selection or insertion-related event has occurred.
134 * TODO(pedrosimonetti): remove this method once downstream code has been updated to use
135 * the other signature.
136 * @param eventType The selection event type, see {@link SelectionEventType}.
138 public void onSelectionEvent(int eventType) {
142 * Notification that a selection or insertion-related event has occurred.
143 * TODO(donnd): Remove this and instead expose a ContextualSearchClient, crbug.com/403001.
144 * @param eventType The selection event type, see {@link SelectionEventType}.
145 * @param posXPix The x coordinate of the selection start handle.
146 * @param posYPix The y coordinate of the selection start handle.
148 public void onSelectionEvent(int eventType, float posXPix, float posYPix) {
149 onSelectionEvent(eventType);
153 * Called when a new content intent is requested to be started.
155 public void onStartContentIntent(Context context, String intentUrl) {
157 // Perform generic parsing of the URI to turn it into an Intent.
159 intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
160 } catch (URISyntaxException ex) {
161 Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
166 context.startActivity(intent);
167 } catch (ActivityNotFoundException ex) {
168 Log.w(TAG, "No application can handle " + intentUrl);
172 public ContentVideoViewClient getContentVideoViewClient() {
177 * Called when BrowserMediaPlayerManager wants to load a media resource.
178 * @param url the URL of media resource to load.
179 * @return true to prevent the resource from being loaded.
181 public boolean shouldBlockMediaRequest(String url) {
186 * Check whether a key should be propagated to the embedder or not.
187 * We need to send almost every key to Blink. However:
188 * 1. We don't want to block the device on the renderer for
189 * some keys like menu, home, call.
190 * 2. There are no WebKit equivalents for some of these keys
191 * (see app/keyboard_codes_win.h)
192 * Note that these are not the same set as KeyEvent.isSystemKey:
193 * for instance, AKEYCODE_MEDIA_* will be dispatched to webkit*.
195 public static boolean shouldPropagateKey(int keyCode) {
196 if (keyCode == KeyEvent.KEYCODE_MENU ||
197 keyCode == KeyEvent.KEYCODE_HOME ||
198 keyCode == KeyEvent.KEYCODE_BACK ||
199 keyCode == KeyEvent.KEYCODE_CALL ||
200 keyCode == KeyEvent.KEYCODE_ENDCALL ||
201 keyCode == KeyEvent.KEYCODE_POWER ||
202 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
203 keyCode == KeyEvent.KEYCODE_CAMERA ||
204 keyCode == KeyEvent.KEYCODE_FOCUS ||
205 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
206 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
207 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {