1 /* 2 * Copyright (C) 2007 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 com.android.dumprendertree; 18 19 import android.os.Bundle; 20 import android.os.Handler; 21 import android.os.Message; 22 import android.webkit.WebStorage; 23 24 import java.util.HashMap; 25 26 public class CallbackProxy extends Handler implements EventSender, LayoutTestController { 27 28 private EventSender mEventSender; 29 private LayoutTestController mLayoutTestController; 30 31 private static final int EVENT_DOM_LOG = 1; 32 private static final int EVENT_FIRE_KBD = 2; 33 private static final int EVENT_KEY_DOWN_1 = 3; 34 private static final int EVENT_KEY_DOWN_2 = 4; 35 private static final int EVENT_LEAP = 5; 36 private static final int EVENT_MOUSE_CLICK = 6; 37 private static final int EVENT_MOUSE_DOWN = 7; 38 private static final int EVENT_MOUSE_MOVE = 8; 39 private static final int EVENT_MOUSE_UP = 9; 40 private static final int EVENT_TOUCH_START = 10; 41 private static final int EVENT_TOUCH_MOVE = 11; 42 private static final int EVENT_TOUCH_END = 12; 43 private static final int EVENT_TOUCH_CANCEL = 13; 44 private static final int EVENT_ADD_TOUCH_POINT = 14; 45 private static final int EVENT_UPDATE_TOUCH_POINT = 15; 46 private static final int EVENT_RELEASE_TOUCH_POINT = 16; 47 private static final int EVENT_CLEAR_TOUCH_POINTS = 17; 48 private static final int EVENT_CANCEL_TOUCH_POINT = 18; 49 private static final int EVENT_SET_TOUCH_MODIFIER = 19; 50 private static final int LAYOUT_CLEAR_LIST = 20; 51 private static final int LAYOUT_DISPLAY = 21; 52 private static final int LAYOUT_DUMP_TEXT = 22; 53 private static final int LAYOUT_DUMP_HISTORY = 23; 54 private static final int LAYOUT_DUMP_CHILD_SCROLL = 24; 55 private static final int LAYOUT_DUMP_EDIT_CB = 25; 56 private static final int LAYOUT_DUMP_SEL_RECT = 26; 57 private static final int LAYOUT_DUMP_TITLE_CHANGES = 27; 58 private static final int LAYOUT_KEEP_WEB_HISTORY = 28; 59 private static final int LAYOUT_NOTIFY_DONE = 29; 60 private static final int LAYOUT_QUEUE_BACK_NAV = 30; 61 private static final int LAYOUT_QUEUE_FWD_NAV = 31; 62 private static final int LAYOUT_QUEUE_LOAD = 32; 63 private static final int LAYOUT_QUEUE_RELOAD = 33; 64 private static final int LAYOUT_QUEUE_SCRIPT = 34; 65 private static final int LAYOUT_REPAINT_HORZ = 35; 66 private static final int LAYOUT_SET_ACCEPT_EDIT = 36; 67 private static final int LAYOUT_MAIN_FIRST_RESP = 37; 68 private static final int LAYOUT_SET_WINDOW_KEY = 38; 69 private static final int LAYOUT_TEST_REPAINT = 39; 70 private static final int LAYOUT_WAIT_UNTIL_DONE = 40; 71 private static final int LAYOUT_DUMP_DATABASE_CALLBACKS = 41; 72 private static final int LAYOUT_SET_CAN_OPEN_WINDOWS = 42; 73 private static final int OVERRIDE_PREFERENCE = 43; 74 private static final int LAYOUT_DUMP_CHILD_FRAMES_TEXT = 44; 75 private static final int SET_XSS_AUDITOR_ENABLED = 45; 76 CallbackProxy(EventSender eventSender, LayoutTestController layoutTestController)77 CallbackProxy(EventSender eventSender, 78 LayoutTestController layoutTestController) { 79 mEventSender = eventSender; 80 mLayoutTestController = layoutTestController; 81 } 82 handleMessage(Message msg)83 public void handleMessage(Message msg) { 84 switch (msg.what) { 85 case EVENT_DOM_LOG: 86 mEventSender.enableDOMUIEventLogging(msg.arg1); 87 break; 88 case EVENT_FIRE_KBD: 89 mEventSender.fireKeyboardEventsToElement(msg.arg1); 90 break; 91 case EVENT_KEY_DOWN_1: 92 HashMap map = (HashMap) msg.obj; 93 mEventSender.keyDown((String) map.get("character"), 94 (String[]) map.get("withModifiers")); 95 break; 96 97 case EVENT_KEY_DOWN_2: 98 mEventSender.keyDown((String)msg.obj); 99 break; 100 101 case EVENT_LEAP: 102 mEventSender.leapForward(msg.arg1); 103 break; 104 105 case EVENT_MOUSE_CLICK: 106 mEventSender.mouseClick(); 107 break; 108 109 case EVENT_MOUSE_DOWN: 110 mEventSender.mouseDown(); 111 break; 112 113 case EVENT_MOUSE_MOVE: 114 mEventSender.mouseMoveTo(msg.arg1, msg.arg2); 115 break; 116 117 case EVENT_MOUSE_UP: 118 mEventSender.mouseUp(); 119 break; 120 121 case EVENT_TOUCH_START: 122 mEventSender.touchStart(); 123 break; 124 125 case EVENT_TOUCH_MOVE: 126 mEventSender.touchMove(); 127 break; 128 129 case EVENT_TOUCH_END: 130 mEventSender.touchEnd(); 131 break; 132 133 case EVENT_TOUCH_CANCEL: 134 mEventSender.touchCancel(); 135 break; 136 137 case EVENT_ADD_TOUCH_POINT: 138 mEventSender.addTouchPoint(msg.arg1, msg.arg2); 139 break; 140 141 case EVENT_UPDATE_TOUCH_POINT: 142 Bundle args = (Bundle) msg.obj; 143 int x = args.getInt("x"); 144 int y = args.getInt("y"); 145 int id = args.getInt("id"); 146 mEventSender.updateTouchPoint(id, x, y); 147 break; 148 149 case EVENT_SET_TOUCH_MODIFIER: 150 Bundle modifierArgs = (Bundle) msg.obj; 151 String modifier = modifierArgs.getString("modifier"); 152 boolean enabled = modifierArgs.getBoolean("enabled"); 153 mEventSender.setTouchModifier(modifier, enabled); 154 break; 155 156 case EVENT_RELEASE_TOUCH_POINT: 157 mEventSender.releaseTouchPoint(msg.arg1); 158 break; 159 160 case EVENT_CLEAR_TOUCH_POINTS: 161 mEventSender.clearTouchPoints(); 162 break; 163 164 case EVENT_CANCEL_TOUCH_POINT: 165 mEventSender.cancelTouchPoint(msg.arg1); 166 break; 167 168 case LAYOUT_CLEAR_LIST: 169 mLayoutTestController.clearBackForwardList(); 170 break; 171 172 case LAYOUT_DISPLAY: 173 mLayoutTestController.display(); 174 break; 175 176 case LAYOUT_DUMP_TEXT: 177 mLayoutTestController.dumpAsText(msg.arg1 == 1); 178 break; 179 180 case LAYOUT_DUMP_CHILD_FRAMES_TEXT: 181 mLayoutTestController.dumpChildFramesAsText(); 182 break; 183 184 case LAYOUT_DUMP_HISTORY: 185 mLayoutTestController.dumpBackForwardList(); 186 break; 187 188 case LAYOUT_DUMP_CHILD_SCROLL: 189 mLayoutTestController.dumpChildFrameScrollPositions(); 190 break; 191 192 case LAYOUT_DUMP_EDIT_CB: 193 mLayoutTestController.dumpEditingCallbacks(); 194 break; 195 196 case LAYOUT_DUMP_SEL_RECT: 197 mLayoutTestController.dumpSelectionRect(); 198 break; 199 200 case LAYOUT_DUMP_TITLE_CHANGES: 201 mLayoutTestController.dumpTitleChanges(); 202 break; 203 204 case LAYOUT_KEEP_WEB_HISTORY: 205 mLayoutTestController.keepWebHistory(); 206 break; 207 208 case LAYOUT_NOTIFY_DONE: 209 mLayoutTestController.notifyDone(); 210 break; 211 212 case LAYOUT_QUEUE_BACK_NAV: 213 mLayoutTestController.queueBackNavigation(msg.arg1); 214 break; 215 216 case LAYOUT_QUEUE_FWD_NAV: 217 mLayoutTestController.queueForwardNavigation(msg.arg1); 218 break; 219 220 case LAYOUT_QUEUE_LOAD: 221 HashMap<String, String> loadMap = 222 (HashMap<String, String>) msg.obj; 223 mLayoutTestController.queueLoad(loadMap.get("Url"), 224 loadMap.get("frameTarget")); 225 break; 226 227 case LAYOUT_QUEUE_RELOAD: 228 mLayoutTestController.queueReload(); 229 break; 230 231 case LAYOUT_QUEUE_SCRIPT: 232 mLayoutTestController.queueScript((String)msg.obj); 233 break; 234 235 case LAYOUT_REPAINT_HORZ: 236 mLayoutTestController.repaintSweepHorizontally(); 237 break; 238 239 case LAYOUT_SET_ACCEPT_EDIT: 240 mLayoutTestController.setAcceptsEditing( 241 msg.arg1 == 1 ? true : false); 242 break; 243 case LAYOUT_MAIN_FIRST_RESP: 244 mLayoutTestController.setMainFrameIsFirstResponder( 245 msg.arg1 == 1 ? true : false); 246 break; 247 248 case LAYOUT_SET_WINDOW_KEY: 249 mLayoutTestController.setWindowIsKey( 250 msg.arg1 == 1 ? true : false); 251 break; 252 253 case LAYOUT_TEST_REPAINT: 254 mLayoutTestController.testRepaint(); 255 break; 256 257 case LAYOUT_WAIT_UNTIL_DONE: 258 mLayoutTestController.waitUntilDone(); 259 break; 260 261 case LAYOUT_DUMP_DATABASE_CALLBACKS: 262 mLayoutTestController.dumpDatabaseCallbacks(); 263 break; 264 265 case LAYOUT_SET_CAN_OPEN_WINDOWS: 266 mLayoutTestController.setCanOpenWindows(); 267 break; 268 269 case OVERRIDE_PREFERENCE: 270 String key = msg.getData().getString("key"); 271 boolean value = msg.getData().getBoolean("value"); 272 mLayoutTestController.overridePreference(key, value); 273 break; 274 275 case SET_XSS_AUDITOR_ENABLED: 276 mLayoutTestController.setXSSAuditorEnabled(msg.arg1 == 1); 277 break; 278 } 279 } 280 281 // EventSender Methods 282 enableDOMUIEventLogging(int DOMNode)283 public void enableDOMUIEventLogging(int DOMNode) { 284 obtainMessage(EVENT_DOM_LOG, DOMNode, 0).sendToTarget(); 285 } 286 fireKeyboardEventsToElement(int DOMNode)287 public void fireKeyboardEventsToElement(int DOMNode) { 288 obtainMessage(EVENT_FIRE_KBD, DOMNode, 0).sendToTarget(); 289 } 290 keyDown(String character, String[] withModifiers)291 public void keyDown(String character, String[] withModifiers) { 292 // TODO Auto-generated method stub 293 HashMap map = new HashMap(); 294 map.put("character", character); 295 map.put("withModifiers", withModifiers); 296 obtainMessage(EVENT_KEY_DOWN_1, map).sendToTarget(); 297 } 298 keyDown(String character)299 public void keyDown(String character) { 300 obtainMessage(EVENT_KEY_DOWN_2, character).sendToTarget(); 301 } 302 leapForward(int milliseconds)303 public void leapForward(int milliseconds) { 304 obtainMessage(EVENT_LEAP, milliseconds, 0).sendToTarget(); 305 } 306 mouseClick()307 public void mouseClick() { 308 obtainMessage(EVENT_MOUSE_CLICK).sendToTarget(); 309 } 310 mouseDown()311 public void mouseDown() { 312 obtainMessage(EVENT_MOUSE_DOWN).sendToTarget(); 313 } 314 mouseMoveTo(int X, int Y)315 public void mouseMoveTo(int X, int Y) { 316 obtainMessage(EVENT_MOUSE_MOVE, X, Y).sendToTarget(); 317 } 318 mouseUp()319 public void mouseUp() { 320 obtainMessage(EVENT_MOUSE_UP).sendToTarget(); 321 } 322 touchStart()323 public void touchStart() { 324 obtainMessage(EVENT_TOUCH_START).sendToTarget(); 325 } 326 addTouchPoint(int x, int y)327 public void addTouchPoint(int x, int y) { 328 obtainMessage(EVENT_ADD_TOUCH_POINT, x, y).sendToTarget(); 329 } 330 updateTouchPoint(int id, int x, int y)331 public void updateTouchPoint(int id, int x, int y) { 332 Bundle map = new Bundle(); 333 map.putInt("x", x); 334 map.putInt("y", y); 335 map.putInt("id", id); 336 obtainMessage(EVENT_UPDATE_TOUCH_POINT, map).sendToTarget(); 337 } 338 setTouchModifier(String modifier, boolean enabled)339 public void setTouchModifier(String modifier, boolean enabled) { 340 Bundle map = new Bundle(); 341 map.putString("modifier", modifier); 342 map.putBoolean("enabled", enabled); 343 obtainMessage(EVENT_SET_TOUCH_MODIFIER, map).sendToTarget(); 344 } 345 touchMove()346 public void touchMove() { 347 obtainMessage(EVENT_TOUCH_MOVE).sendToTarget(); 348 } 349 releaseTouchPoint(int id)350 public void releaseTouchPoint(int id) { 351 obtainMessage(EVENT_RELEASE_TOUCH_POINT, id, 0).sendToTarget(); 352 } 353 touchEnd()354 public void touchEnd() { 355 obtainMessage(EVENT_TOUCH_END).sendToTarget(); 356 } 357 touchCancel()358 public void touchCancel() { 359 obtainMessage(EVENT_TOUCH_CANCEL).sendToTarget(); 360 } 361 362 clearTouchPoints()363 public void clearTouchPoints() { 364 obtainMessage(EVENT_CLEAR_TOUCH_POINTS).sendToTarget(); 365 } 366 cancelTouchPoint(int id)367 public void cancelTouchPoint(int id) { 368 obtainMessage(EVENT_CANCEL_TOUCH_POINT, id, 0).sendToTarget(); 369 } 370 371 // LayoutTestController Methods 372 clearBackForwardList()373 public void clearBackForwardList() { 374 obtainMessage(LAYOUT_CLEAR_LIST).sendToTarget(); 375 } 376 display()377 public void display() { 378 obtainMessage(LAYOUT_DISPLAY).sendToTarget(); 379 } 380 dumpAsText()381 public void dumpAsText() { 382 obtainMessage(LAYOUT_DUMP_TEXT, 0).sendToTarget(); 383 } 384 dumpAsText(boolean enablePixelTests)385 public void dumpAsText(boolean enablePixelTests) { 386 obtainMessage(LAYOUT_DUMP_TEXT, enablePixelTests ? 1 : 0).sendToTarget(); 387 } 388 dumpChildFramesAsText()389 public void dumpChildFramesAsText() { 390 obtainMessage(LAYOUT_DUMP_CHILD_FRAMES_TEXT).sendToTarget(); 391 } 392 dumpBackForwardList()393 public void dumpBackForwardList() { 394 obtainMessage(LAYOUT_DUMP_HISTORY).sendToTarget(); 395 } 396 dumpChildFrameScrollPositions()397 public void dumpChildFrameScrollPositions() { 398 obtainMessage(LAYOUT_DUMP_CHILD_SCROLL).sendToTarget(); 399 } 400 dumpEditingCallbacks()401 public void dumpEditingCallbacks() { 402 obtainMessage(LAYOUT_DUMP_EDIT_CB).sendToTarget(); 403 } 404 dumpSelectionRect()405 public void dumpSelectionRect() { 406 obtainMessage(LAYOUT_DUMP_SEL_RECT).sendToTarget(); 407 } 408 dumpTitleChanges()409 public void dumpTitleChanges() { 410 obtainMessage(LAYOUT_DUMP_TITLE_CHANGES).sendToTarget(); 411 } 412 keepWebHistory()413 public void keepWebHistory() { 414 obtainMessage(LAYOUT_KEEP_WEB_HISTORY).sendToTarget(); 415 } 416 notifyDone()417 public void notifyDone() { 418 obtainMessage(LAYOUT_NOTIFY_DONE).sendToTarget(); 419 } 420 queueBackNavigation(int howfar)421 public void queueBackNavigation(int howfar) { 422 obtainMessage(LAYOUT_QUEUE_BACK_NAV, howfar, 0).sendToTarget(); 423 } 424 queueForwardNavigation(int howfar)425 public void queueForwardNavigation(int howfar) { 426 obtainMessage(LAYOUT_QUEUE_FWD_NAV, howfar, 0).sendToTarget(); 427 } 428 queueLoad(String Url, String frameTarget)429 public void queueLoad(String Url, String frameTarget) { 430 HashMap <String, String>map = new HashMap<String, String>(); 431 map.put("Url", Url); 432 map.put("frameTarget", frameTarget); 433 obtainMessage(LAYOUT_QUEUE_LOAD, map).sendToTarget(); 434 } 435 queueReload()436 public void queueReload() { 437 obtainMessage(LAYOUT_QUEUE_RELOAD).sendToTarget(); 438 } 439 queueScript(String scriptToRunInCurrentContext)440 public void queueScript(String scriptToRunInCurrentContext) { 441 obtainMessage(LAYOUT_QUEUE_SCRIPT, 442 scriptToRunInCurrentContext).sendToTarget(); 443 } 444 repaintSweepHorizontally()445 public void repaintSweepHorizontally() { 446 obtainMessage(LAYOUT_REPAINT_HORZ).sendToTarget(); 447 } 448 setAcceptsEditing(boolean b)449 public void setAcceptsEditing(boolean b) { 450 obtainMessage(LAYOUT_SET_ACCEPT_EDIT, b ? 1 : 0, 0).sendToTarget(); 451 } 452 setMainFrameIsFirstResponder(boolean b)453 public void setMainFrameIsFirstResponder(boolean b) { 454 obtainMessage(LAYOUT_MAIN_FIRST_RESP, b ? 1 : 0, 0).sendToTarget(); 455 } 456 setWindowIsKey(boolean b)457 public void setWindowIsKey(boolean b) { 458 obtainMessage(LAYOUT_SET_WINDOW_KEY, b ? 1 : 0, 0).sendToTarget(); 459 } 460 testRepaint()461 public void testRepaint() { 462 obtainMessage(LAYOUT_TEST_REPAINT).sendToTarget(); 463 } 464 waitUntilDone()465 public void waitUntilDone() { 466 obtainMessage(LAYOUT_WAIT_UNTIL_DONE).sendToTarget(); 467 } 468 dumpDatabaseCallbacks()469 public void dumpDatabaseCallbacks() { 470 obtainMessage(LAYOUT_DUMP_DATABASE_CALLBACKS).sendToTarget(); 471 } 472 clearAllDatabases()473 public void clearAllDatabases() { 474 WebStorage.getInstance().deleteAllData(); 475 } 476 setDatabaseQuota(long quota)477 public void setDatabaseQuota(long quota) { 478 WebStorage.getInstance().setQuotaForOrigin("file://", quota); 479 } 480 setAppCacheMaximumSize(long size)481 public void setAppCacheMaximumSize(long size) { 482 android.webkit.WebStorageClassic.getInstance().setAppCacheMaximumSize(size); 483 } 484 setCanOpenWindows()485 public void setCanOpenWindows() { 486 obtainMessage(LAYOUT_SET_CAN_OPEN_WINDOWS).sendToTarget(); 487 } 488 setMockGeolocationPosition(double latitude, double longitude, double accuracy)489 public void setMockGeolocationPosition(double latitude, 490 double longitude, 491 double accuracy) { 492 // Configuration is in WebKit, so stay on WebCore thread, but go via the TestShellActivity 493 // as we need access to the Webview. 494 mLayoutTestController.setMockGeolocationPosition(latitude, 495 longitude, 496 accuracy); 497 } 498 setMockGeolocationError(int code, String message)499 public void setMockGeolocationError(int code, String message) { 500 // Configuration is in WebKit, so stay on WebCore thread, but go via the TestShellActivity 501 // as we need access to the Webview. 502 mLayoutTestController.setMockGeolocationError(code, message); 503 } 504 setGeolocationPermission(boolean allow)505 public void setGeolocationPermission(boolean allow) { 506 // Configuration is in WebKit, so stay on WebCore thread, but go via the TestShellActivity 507 // as we need access to the Webview. 508 mLayoutTestController.setGeolocationPermission(allow); 509 } 510 setMockDeviceOrientation(boolean canProvideAlpha, double alpha, boolean canProvideBeta, double beta, boolean canProvideGamma, double gamma)511 public void setMockDeviceOrientation(boolean canProvideAlpha, double alpha, 512 boolean canProvideBeta, double beta, boolean canProvideGamma, double gamma) { 513 // Configuration is in WebKit, so stay on WebCore thread, but go via the TestShellActivity 514 // as we need access to the Webview. 515 mLayoutTestController.setMockDeviceOrientation(canProvideAlpha, alpha, canProvideBeta, beta, 516 canProvideGamma, gamma); 517 } 518 overridePreference(String key, boolean value)519 public void overridePreference(String key, boolean value) { 520 Message message = obtainMessage(OVERRIDE_PREFERENCE); 521 message.getData().putString("key", key); 522 message.getData().putBoolean("value", value); 523 message.sendToTarget(); 524 } 525 setXSSAuditorEnabled(boolean flag)526 public void setXSSAuditorEnabled(boolean flag) { 527 obtainMessage(SET_XSS_AUDITOR_ENABLED, flag ? 1 : 0, 0).sendToTarget(); 528 } 529 } 530