1 // Copyright (c) 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. 4 5 #ifndef CHROME_TEST_AUTOMATION_AUTOMATION_JSON_REQUESTS_H_ 6 #define CHROME_TEST_AUTOMATION_AUTOMATION_JSON_REQUESTS_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/strings/string_number_conversions.h" 15 #include "chrome/common/automation_constants.h" 16 #include "chrome/common/automation_id.h" 17 #include "ui/base/keycodes/keyboard_codes.h" 18 19 class AutomationMessageSender; 20 21 namespace base { 22 class DictionaryValue; 23 class FilePath; 24 class ListValue; 25 class Value; 26 } 27 28 struct WebKeyEvent { 29 WebKeyEvent(automation::KeyEventTypes type, 30 ui::KeyboardCode key_code, 31 const std::string& unmodified_text, 32 const std::string& modified_text, 33 int modifiers); 34 35 automation::KeyEventTypes type; 36 ui::KeyboardCode key_code; 37 std::string unmodified_text; 38 std::string modified_text; 39 int modifiers; 40 }; 41 42 struct WebMouseEvent { 43 WebMouseEvent(automation::MouseEventType type, 44 automation::MouseButton button, 45 int x, 46 int y, 47 int click_count, 48 int modifiers); 49 50 automation::MouseEventType type; 51 automation::MouseButton button; 52 int x; 53 int y; 54 int click_count; 55 int modifiers; 56 }; 57 58 // Uniquely identifies a particular WebView. 59 // This is needed because Chrome used to accept just tab IDs, while 60 // now it accepts IDs for other types of WebViews. 61 // TOOD(kkania): Remove this abstraction once Chrome 16 is unsupported. 62 class WebViewId { 63 public: 64 // Creates an ID for the given view ID. 65 static WebViewId ForView(const AutomationId& view_id); 66 67 // Creates an ID for the given tab ID. 68 static WebViewId ForOldStyleTab(int tab_id); 69 70 // Creates an invalid ID. 71 WebViewId(); 72 73 // Updates the given dictionary to include this ID. If the ID refers to a 74 // view ID, |view_id_key| will be the key modified in the dictionary. 75 void UpdateDictionary(base::DictionaryValue* dictionary, 76 const std::string& view_id_key) const; 77 78 // Returns whether this ID is valid. Even if it is valid, the object it 79 // refers to may not exist. 80 bool IsValid() const; 81 82 // Returns an |AutomationId| made from this ID. 83 AutomationId GetId() const; 84 85 // Returns whether this ID refers to a tab. 86 bool IsTab() const; 87 88 int tab_id() const; 89 90 // The old style is to use a single integer ID for a tab. The new style is 91 // to use an automation ID which may refer to a number of different object 92 // types. 93 bool old_style() const; 94 95 private: 96 // Whether this ID is an old-style integer tab ID. 97 bool old_style_; 98 99 AutomationId id_; 100 int tab_id_; 101 }; 102 103 // Used to locate a WebView. The same locator may locate different WebViews 104 // at different times. This is needed because Chrome used to only accept 105 // browser/tab indices, while the new Chrome accepts a unique ID. 106 // TOOD(kkania): Simplify this once Chrome 16 is unsupported. 107 class WebViewLocator { 108 public: 109 // Creates a locator for locating the given tab. 110 static WebViewLocator ForIndexPair(int browser_index, int tab_index); 111 112 // Creates a locator for locating the given view. 113 static WebViewLocator ForViewId(const AutomationId& view_id); 114 115 // Creates an invalid locator. 116 WebViewLocator(); 117 ~WebViewLocator(); 118 119 // Updates the given dictionary to include the given locator information. 120 // If this locator is a view ID, |view_id_key| will be the name of the key 121 // to update. 122 void UpdateDictionary(base::DictionaryValue* dict, 123 const std::string& view_id_key) const; 124 125 int browser_index() const; 126 int tab_index() const; 127 128 private: 129 enum Type { 130 kTypeIndexPair, 131 kTypeViewId, 132 }; 133 134 struct IndexPair { 135 int browser_index; 136 int tab_index; 137 }; 138 139 struct Locator { 140 Locator(); 141 ~Locator(); 142 143 IndexPair index_pair; 144 AutomationId view_id; 145 }; 146 147 Type type_; 148 Locator locator_; 149 }; 150 151 // Collection of info about a given WebView. 152 struct WebViewInfo { 153 WebViewInfo(const WebViewId& view_id, 154 const std::string& extension_id); 155 ~WebViewInfo(); 156 157 // The view's unique ID. 158 WebViewId view_id; 159 160 // If this view belongs to an extension, this ID will be set to it. 161 std::string extension_id; 162 }; 163 164 // Sends a JSON request to the chrome automation provider. Returns true 165 // if the JSON request was successfully sent and the reply was received. 166 // If true, |success| will be set to whether the JSON request was 167 // completed successfully by the automation provider. 168 bool SendAutomationJSONRequest(AutomationMessageSender* sender, 169 const std::string& request, 170 int timeout_ms, 171 std::string* reply, 172 bool* success) WARN_UNUSED_RESULT; 173 174 // Same as above, but uses the given |AutomationMessageSender|'s default timeout 175 // value. 176 bool SendAutomationJSONRequestWithDefaultTimeout( 177 AutomationMessageSender* sender, 178 const std::string& request, 179 std::string* reply, 180 bool* success); 181 182 // Requests the current browser and tab indices for the given tab ID. 183 // Returns true on success. 184 bool SendGetIndicesFromTabIdJSONRequest( 185 AutomationMessageSender* sender, 186 int tab_id, 187 int* browser_index, 188 int* tab_index, 189 automation::Error* error) WARN_UNUSED_RESULT; 190 191 // Requests the current browser and tab indices for the given |TabProxy| 192 // handle. Returns true on success. 193 bool SendGetIndicesFromTabHandleJSONRequest( 194 AutomationMessageSender* sender, 195 int tab_proxy_handle, 196 int* browser_index, 197 int* tab_index, 198 automation::Error* error) WARN_UNUSED_RESULT; 199 200 // Requests to navigate to the given url and wait for the given number of 201 // navigations to complete. Returns true on success. 202 bool SendNavigateToURLJSONRequest( 203 AutomationMessageSender* sender, 204 const WebViewLocator& locator, 205 const std::string& url, 206 int navigation_count, 207 AutomationMsg_NavigationResponseValues* nav_response, 208 automation::Error* error) WARN_UNUSED_RESULT; 209 210 // Requests the given javascript to be executed in the frame specified by the 211 // given xpath. Returns true on success. If true, |result| will be reset to the 212 // result of the execution. 213 bool SendExecuteJavascriptJSONRequest( 214 AutomationMessageSender* sender, 215 const WebViewLocator& locator, 216 const std::string& frame_xpath, 217 const std::string& javascript, 218 scoped_ptr<base::Value>* result, 219 automation::Error* error) WARN_UNUSED_RESULT; 220 221 // Requests the specified view to go forward. Waits for the load to complete. 222 // Returns true on success. 223 bool SendGoForwardJSONRequest( 224 AutomationMessageSender* sender, 225 const WebViewLocator& locator, 226 automation::Error* error) WARN_UNUSED_RESULT; 227 228 // Requests the specified view to go back. Waits for the load to complete. 229 // Returns true on success. 230 bool SendGoBackJSONRequest( 231 AutomationMessageSender* sender, 232 const WebViewLocator& locator, 233 automation::Error* error) WARN_UNUSED_RESULT; 234 235 // Requests the specified view to reload. Waits for the load to complete. 236 // Returns true on success. 237 bool SendReloadJSONRequest( 238 AutomationMessageSender* sender, 239 const WebViewLocator& locator, 240 automation::Error* error) WARN_UNUSED_RESULT; 241 242 // Deprecated, no longer works with Chrome 29+. 243 // Requests a snapshot of the entire page to be saved to the given path 244 // in PNG format. 245 // Returns true on success. 246 bool SendCaptureEntirePageJSONRequestDeprecated( 247 AutomationMessageSender* sender, 248 const WebViewLocator& locator, 249 const base::FilePath& path, 250 automation::Error* error) WARN_UNUSED_RESULT; 251 252 #if !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) 253 // Deprecated, no longer works with Chrome 29+. 254 // Requests a heap profile dump. 255 // Returns true on success. 256 bool SendHeapProfilerDumpJSONRequestDeprecated( 257 AutomationMessageSender* sender, 258 const WebViewLocator& locator, 259 const std::string& reason, 260 automation::Error* error) WARN_UNUSED_RESULT; 261 #endif // !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) 262 263 // Requests all the cookies for the given URL. On success returns true and 264 // caller takes ownership of |cookies|, which is a list of all the cookies in 265 // dictionary format. 266 bool SendGetCookiesJSONRequest( 267 AutomationMessageSender* sender, 268 const std::string& url, 269 scoped_ptr<base::ListValue>* cookies, 270 automation::Error* error) WARN_UNUSED_RESULT; 271 272 // Requests deletion of the cookie with the given name and URL. Returns true 273 // on success. 274 bool SendDeleteCookieJSONRequest( 275 AutomationMessageSender* sender, 276 const std::string& url, 277 const std::string& cookie_name, 278 automation::Error* error) WARN_UNUSED_RESULT; 279 280 // Requests setting the given cookie for the given URL. Returns true on 281 // success. The caller retains ownership of |cookie_dict|. 282 bool SendSetCookieJSONRequest( 283 AutomationMessageSender* sender, 284 const std::string& url, 285 base::DictionaryValue* cookie_dict, 286 automation::Error* error) WARN_UNUSED_RESULT; 287 288 // Requests the IDs for all open tabs. Returns true on success. 289 bool SendGetTabIdsJSONRequest( 290 AutomationMessageSender* sender, 291 std::vector<WebViewInfo>* views, 292 automation::Error* error) WARN_UNUSED_RESULT; 293 294 // Requests info for all open views. Returns true on success. 295 bool SendGetWebViewsJSONRequest( 296 AutomationMessageSender* sender, 297 std::vector<WebViewInfo>* views, 298 automation::Error* error) WARN_UNUSED_RESULT; 299 300 // Requests whether the given tab ID is valid. Returns true on success. 301 bool SendIsTabIdValidJSONRequest( 302 AutomationMessageSender* sender, 303 const WebViewId& view_id, 304 bool* is_valid, 305 automation::Error* error) WARN_UNUSED_RESULT; 306 307 // Requests whether the given automation ID refers to an actual automation 308 // object. Returns true on success. 309 bool SendDoesAutomationObjectExistJSONRequest( 310 AutomationMessageSender* sender, 311 const WebViewId& view_id, 312 bool* does_exist, 313 automation::Error* error) WARN_UNUSED_RESULT; 314 315 // Requests to close the given view. Returns true on success. 316 bool SendCloseViewJSONRequest( 317 AutomationMessageSender* sender, 318 const WebViewLocator& locator, 319 automation::Error* error) WARN_UNUSED_RESULT; 320 321 // Requests to send the WebKit event for a mouse move to the given 322 // coordinate in the specified view. Returns true on success. 323 // Deprecated. TODO(kkania): Remove when chrome 17 is unsupported. 324 bool SendMouseMoveJSONRequestDeprecated( 325 AutomationMessageSender* sender, 326 const WebViewLocator& locator, 327 int x, 328 int y, 329 automation::Error* error) WARN_UNUSED_RESULT; 330 331 // Requests to send the WebKit events for a mouse click at the given 332 // coordinate in the specified view. Returns true on success. 333 // Deprecated. TODO(kkania): Remove when chrome 17 is unsupported. 334 bool SendMouseClickJSONRequestDeprecated( 335 AutomationMessageSender* sender, 336 const WebViewLocator& locator, 337 automation::MouseButton button, 338 int x, 339 int y, 340 automation::Error* error) WARN_UNUSED_RESULT; 341 342 // Requests to send the WebKit events for a mouse drag from the start to end 343 // coordinates given in the specified view. Returns true on success. 344 // Deprecated. TODO(kkania): Remove when chrome 17 is unsupported. 345 bool SendMouseDragJSONRequestDeprecated( 346 AutomationMessageSender* sender, 347 const WebViewLocator& locator, 348 int start_x, 349 int start_y, 350 int end_x, 351 int end_y, 352 automation::Error* error) WARN_UNUSED_RESULT; 353 354 // Requests to send the WebKit event for a mouse button down at the given 355 // coordinate in the specified view. Returns true on success. 356 // Deprecated. TODO(kkania): Remove when chrome 17 is unsupported. 357 bool SendMouseButtonDownJSONRequestDeprecated( 358 AutomationMessageSender* sender, 359 const WebViewLocator& locator, 360 int x, 361 int y, 362 automation::Error* error) WARN_UNUSED_RESULT; 363 364 // Requests to send the WebKit event for a mouse button up at the given 365 // coordinate in the specified view. Returns true on success. 366 // Deprecated. TODO(kkania): Remove when chrome 17 is unsupported. 367 bool SendMouseButtonUpJSONRequestDeprecated( 368 AutomationMessageSender* sender, 369 const WebViewLocator& locator, 370 int x, 371 int y, 372 automation::Error* error) WARN_UNUSED_RESULT; 373 374 // Requests to send the WebKit event for a mouse double click at the given 375 // coordinate in the specified view. Returns true on success. 376 // Deprecated. TODO(kkania): Remove when chrome 17 is unsupported. 377 bool SendMouseDoubleClickJSONRequestDeprecated( 378 AutomationMessageSender* sender, 379 const WebViewLocator& locator, 380 int x, 381 int y, 382 automation::Error* error) WARN_UNUSED_RESULT; 383 384 // Requests to send the WebKit event for the given |WebKeyEvent| in a 385 // specified view. Returns true on success. 386 bool SendWebKeyEventJSONRequest( 387 AutomationMessageSender* sender, 388 const WebViewLocator& locator, 389 const WebKeyEvent& key_event, 390 automation::Error* error) WARN_UNUSED_RESULT; 391 392 // Requests to send the key event for the given keycode+modifiers to a 393 // browser window containing the specified view. Returns true on success. 394 bool SendNativeKeyEventJSONRequest( 395 AutomationMessageSender* sender, 396 const WebViewLocator& locator, 397 ui::KeyboardCode key_code, 398 int modifiers, 399 automation::Error* error) WARN_UNUSED_RESULT; 400 401 // Deprecated, no longer works with Chrome 29+. 402 // Requests to send the WebKit event for the given |WebMouseEvent| in a 403 // specified view. Returns true on success. 404 bool SendWebMouseEventJSONRequestDeprecated( 405 AutomationMessageSender* sender, 406 const WebViewLocator& locator, 407 const WebMouseEvent& mouse_event, 408 automation::Error* error) WARN_UNUSED_RESULT; 409 410 // Requests to drag and drop the file paths at the given coordinate in the 411 // specified view. Returns true on success. 412 bool SendDragAndDropFilePathsJSONRequest( 413 AutomationMessageSender* sender, 414 const WebViewLocator& locator, 415 int x, 416 int y, 417 const std::vector<base::FilePath::StringType>& paths, 418 automation::Error* error) WARN_UNUSED_RESULT; 419 420 // Requests to set the given view's bounds. Returns true on success. 421 bool SendSetViewBoundsJSONRequest( 422 AutomationMessageSender* sender, 423 const WebViewId& id, 424 int x, 425 int y, 426 int width, 427 int height, 428 automation::Error* error) WARN_UNUSED_RESULT; 429 430 // Requests to maximize the given view. Returns true on success. 431 bool SendMaximizeJSONRequest( 432 AutomationMessageSender* sender, 433 const WebViewId& id, 434 automation::Error* error) WARN_UNUSED_RESULT; 435 436 // Requests to get the active JavaScript modal dialog's message. Returns true 437 // on success. 438 bool SendGetAppModalDialogMessageJSONRequest( 439 AutomationMessageSender* sender, 440 std::string* message, 441 automation::Error* error) WARN_UNUSED_RESULT; 442 443 // Requests to accept or dismiss the active JavaScript modal dialog. 444 // Returns true on success. 445 bool SendAcceptOrDismissAppModalDialogJSONRequest( 446 AutomationMessageSender* sender, 447 bool accept, 448 automation::Error* error) WARN_UNUSED_RESULT; 449 450 // Requests to accept the active JavaScript modal dialog with the given prompt 451 // text. Returns true on success. 452 bool SendAcceptPromptAppModalDialogJSONRequest( 453 AutomationMessageSender* sender, 454 const std::string& prompt_text, 455 automation::Error* error) WARN_UNUSED_RESULT; 456 457 // Deprecated, no longer works with Chrome 29+. 458 // Requests to wait for all views to stop loading. Returns true on success. 459 bool SendWaitForAllViewsToStopLoadingJSONRequestDeprecated( 460 AutomationMessageSender* sender, 461 automation::Error* error) WARN_UNUSED_RESULT; 462 463 // Requests the version of ChromeDriver automation supported by the automation 464 // server. Returns true on success. 465 bool SendGetChromeDriverAutomationVersion( 466 AutomationMessageSender* sender, 467 int* version, 468 automation::Error* error) WARN_UNUSED_RESULT; 469 470 // Requests that the given extension be installed. If |with_ui| is false, 471 // the extension will be installed silently. Returns true on success. 472 bool SendInstallExtensionJSONRequest( 473 AutomationMessageSender* sender, 474 const base::FilePath& path, 475 bool with_ui, 476 std::string* extension_id, 477 automation::Error* error) WARN_UNUSED_RESULT; 478 479 // Requests info about all installed extensions. Returns true on success. 480 bool SendGetExtensionsInfoJSONRequest( 481 AutomationMessageSender* sender, 482 base::ListValue* extensions_list, 483 automation::Error* error) WARN_UNUSED_RESULT; 484 485 // Requests whether the given extension's page action is visible in the 486 // given tab. 487 bool SendIsPageActionVisibleJSONRequest( 488 AutomationMessageSender* sender, 489 const WebViewId& tab_id, 490 const std::string& extension_id, 491 bool* is_visible, 492 automation::Error* error) WARN_UNUSED_RESULT; 493 494 // Requests a modification of the given extension state. Returns true on 495 // success. 496 bool SendSetExtensionStateJSONRequest( 497 AutomationMessageSender* sender, 498 const std::string& extension_id, 499 bool enable, 500 bool allow_in_incognito, 501 automation::Error* error) WARN_UNUSED_RESULT; 502 503 // Requests the given extension's action button be pressed. Returns true on 504 // success. 505 bool SendClickExtensionButtonJSONRequest( 506 AutomationMessageSender* sender, 507 const std::string& extension_id, 508 bool browser_action, 509 automation::Error* error) WARN_UNUSED_RESULT; 510 511 // Requests the given extension be uninstalled. Returns true on success. 512 bool SendUninstallExtensionJSONRequest( 513 AutomationMessageSender* sender, 514 const std::string& extension_id, 515 automation::Error* error) WARN_UNUSED_RESULT; 516 517 // Requests the local state preference to be set to the given value. 518 // Ownership of |value| is taken by this function. Returns true on success. 519 bool SendSetLocalStatePreferenceJSONRequest( 520 AutomationMessageSender* sender, 521 const std::string& pref, 522 base::Value* value, 523 automation::Error* error) WARN_UNUSED_RESULT; 524 525 // Requests the user preference to be set to the given value. 526 // Ownership of |value| is taken by this function. Returns true on success. 527 bool SendSetPreferenceJSONRequest( 528 AutomationMessageSender* sender, 529 const std::string& pref, 530 base::Value* value, 531 automation::Error* error) WARN_UNUSED_RESULT; 532 533 // Requests to override the user's geolocation. Returns true on success. 534 bool SendOverrideGeolocationJSONRequest( 535 AutomationMessageSender* sender, 536 const base::DictionaryValue* geolocation, 537 automation::Error* error) WARN_UNUSED_RESULT; 538 539 #endif // CHROME_TEST_AUTOMATION_AUTOMATION_JSON_REQUESTS_H_ 540