OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/webdriver/webdriver_session.h" | 5 #include "chrome/test/webdriver/webdriver_session.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 FrameId::FrameId() {} | 46 FrameId::FrameId() {} |
47 | 47 |
48 FrameId::FrameId(const WebViewId& view_id, const FramePath& frame_path) | 48 FrameId::FrameId(const WebViewId& view_id, const FramePath& frame_path) |
49 : view_id(view_id), | 49 : view_id(view_id), |
50 frame_path(frame_path) { | 50 frame_path(frame_path) { |
51 } | 51 } |
52 | 52 |
53 Session::Options::Options() | 53 Session::Options::Options() |
54 : use_native_events(false), | 54 : use_native_events(false), |
55 load_async(false) { | 55 load_async(false), |
| 56 no_website_testing_defaults(false) { |
56 } | 57 } |
57 | 58 |
58 Session::Options::~Options() { | 59 Session::Options::~Options() { |
59 } | 60 } |
60 | 61 |
61 Session::Session(const Options& options) | 62 Session::Session(const Options& options) |
62 : id_(GenerateRandomID()), | 63 : id_(GenerateRandomID()), |
63 current_target_(FrameId(WebViewId(), FramePath())), | 64 current_target_(FrameId(WebViewId(), FramePath())), |
64 thread_(id_.c_str()), | 65 thread_(id_.c_str()), |
65 async_script_timeout_(0), | 66 async_script_timeout_(0), |
66 implicit_wait_(0), | 67 implicit_wait_(0), |
67 has_alert_prompt_text_(false), | 68 has_alert_prompt_text_(false), |
68 options_(options) { | 69 options_(options) { |
69 SessionManager::GetInstance()->Add(this); | 70 SessionManager::GetInstance()->Add(this); |
70 } | 71 } |
71 | 72 |
72 Session::~Session() { | 73 Session::~Session() { |
73 SessionManager::GetInstance()->Remove(id_); | 74 SessionManager::GetInstance()->Remove(id_); |
74 } | 75 } |
75 | 76 |
76 Error* Session::Init(const Automation::BrowserOptions& options) { | 77 Error* Session::Init(const Automation::BrowserOptions& options) { |
77 if (!thread_.Start()) { | 78 if (!thread_.Start()) { |
78 delete this; | 79 delete this; |
79 return new Error(kUnknownError, "Cannot start session thread"); | 80 return new Error(kUnknownError, "Cannot start session thread"); |
80 } | 81 } |
81 | 82 |
82 Error* error = NULL; | 83 Error* error = NULL; |
83 RunSessionTask(NewRunnableMethod( | 84 RunSessionTask(base::Bind( |
84 this, | |
85 &Session::InitOnSessionThread, | 85 &Session::InitOnSessionThread, |
| 86 base::Unretained(this), |
86 options, | 87 options, |
87 &error)); | 88 &error)); |
| 89 |
| 90 if (!error) |
| 91 error = PostBrowserStartInit(); |
| 92 |
88 if (error) | 93 if (error) |
89 Terminate(); | 94 Terminate(); |
90 return error; | 95 return error; |
91 } | 96 } |
92 | 97 |
93 Error* Session::BeforeExecuteCommand() { | 98 Error* Session::BeforeExecuteCommand() { |
94 Error* error = AfterExecuteCommand(); | 99 Error* error = AfterExecuteCommand(); |
95 if (!error) { | 100 if (!error) { |
96 scoped_ptr<Error> switch_error(SwitchToTopFrameIfCurrentFrameInvalid()); | 101 scoped_ptr<Error> switch_error(SwitchToTopFrameIfCurrentFrameInvalid()); |
97 if (switch_error.get()) { | 102 if (switch_error.get()) { |
(...skipping 13 matching lines...) Expand all Loading... |
111 Error* error = NULL; | 116 Error* error = NULL; |
112 if (!options_.load_async) { | 117 if (!options_.load_async) { |
113 LOG(INFO) << "Waiting for the page to stop loading"; | 118 LOG(INFO) << "Waiting for the page to stop loading"; |
114 error = WaitForAllViewsToStopLoading(); | 119 error = WaitForAllViewsToStopLoading(); |
115 LOG(INFO) << "Done waiting for the page to stop loading"; | 120 LOG(INFO) << "Done waiting for the page to stop loading"; |
116 } | 121 } |
117 return error; | 122 return error; |
118 } | 123 } |
119 | 124 |
120 void Session::Terminate() { | 125 void Session::Terminate() { |
121 RunSessionTask(NewRunnableMethod( | 126 RunSessionTask(base::Bind( |
122 this, | 127 &Session::TerminateOnSessionThread, |
123 &Session::TerminateOnSessionThread)); | 128 base::Unretained(this))); |
124 delete this; | 129 delete this; |
125 } | 130 } |
126 | 131 |
127 Error* Session::ExecuteScript(const FrameId& frame_id, | 132 Error* Session::ExecuteScript(const FrameId& frame_id, |
128 const std::string& script, | 133 const std::string& script, |
129 const ListValue* const args, | 134 const ListValue* const args, |
130 Value** value) { | 135 Value** value) { |
131 std::string args_as_json; | 136 std::string args_as_json; |
132 base::JSONWriter::Write(static_cast<const Value* const>(args), | 137 base::JSONWriter::Write(static_cast<const Value* const>(args), |
133 /*pretty_print=*/false, | 138 /*pretty_print=*/false, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 " throw new Error('Failed to send keys because cannot focus element');" | 256 " throw new Error('Failed to send keys because cannot focus element');" |
252 "}"; | 257 "}"; |
253 error = ExecuteScriptAndParse(current_target_, | 258 error = ExecuteScriptAndParse(current_target_, |
254 kFocusScript, | 259 kFocusScript, |
255 "focusElement", | 260 "focusElement", |
256 CreateListValueFrom(element), | 261 CreateListValueFrom(element), |
257 CreateDirectValueParser(kSkipParsing)); | 262 CreateDirectValueParser(kSkipParsing)); |
258 if (error) | 263 if (error) |
259 return error; | 264 return error; |
260 | 265 |
261 RunSessionTask(NewRunnableMethod( | 266 RunSessionTask(base::Bind( |
262 this, | |
263 &Session::SendKeysOnSessionThread, | 267 &Session::SendKeysOnSessionThread, |
| 268 base::Unretained(this), |
264 keys, | 269 keys, |
265 &error)); | 270 &error)); |
266 return error; | 271 return error; |
267 } | 272 } |
268 | 273 |
269 Error* Session::DragAndDropFilePaths( | 274 Error* Session::DragAndDropFilePaths( |
270 const Point& location, | 275 const Point& location, |
271 const std::vector<FilePath::StringType>& paths) { | 276 const std::vector<FilePath::StringType>& paths) { |
272 Error* error = NULL; | 277 Error* error = NULL; |
273 RunSessionTask(NewRunnableMethod( | 278 RunSessionTask(base::Bind( |
274 automation_.get(), | |
275 &Automation::DragAndDropFilePaths, | 279 &Automation::DragAndDropFilePaths, |
| 280 base::Unretained(automation_.get()), |
276 current_target_.view_id, | 281 current_target_.view_id, |
277 location, | 282 location, |
278 paths, | 283 paths, |
279 &error)); | 284 &error)); |
280 return error; | 285 return error; |
281 } | 286 } |
282 | 287 |
283 Error* Session::NavigateToURL(const std::string& url) { | 288 Error* Session::NavigateToURL(const std::string& url) { |
284 if (!current_target_.view_id.IsTab()) { | 289 if (!current_target_.view_id.IsTab()) { |
285 return new Error(kUnknownError, | 290 return new Error(kUnknownError, |
286 "The current target does not support navigation"); | 291 "The current target does not support navigation"); |
287 } | 292 } |
288 Error* error = NULL; | 293 Error* error = NULL; |
289 if (options_.load_async) { | 294 if (options_.load_async) { |
290 RunSessionTask(NewRunnableMethod( | 295 RunSessionTask(base::Bind( |
291 automation_.get(), | |
292 &Automation::NavigateToURLAsync, | 296 &Automation::NavigateToURLAsync, |
| 297 base::Unretained(automation_.get()), |
293 current_target_.view_id, | 298 current_target_.view_id, |
294 url, | 299 url, |
295 &error)); | 300 &error)); |
296 } else { | 301 } else { |
297 RunSessionTask(NewRunnableMethod( | 302 RunSessionTask(base::Bind( |
298 automation_.get(), | |
299 &Automation::NavigateToURL, | 303 &Automation::NavigateToURL, |
| 304 base::Unretained(automation_.get()), |
300 current_target_.view_id, | 305 current_target_.view_id, |
301 url, | 306 url, |
302 &error)); | 307 &error)); |
303 } | 308 } |
304 return error; | 309 return error; |
305 } | 310 } |
306 | 311 |
307 Error* Session::GoForward() { | 312 Error* Session::GoForward() { |
308 if (!current_target_.view_id.IsTab()) { | 313 if (!current_target_.view_id.IsTab()) { |
309 return new Error(kUnknownError, | 314 return new Error(kUnknownError, |
310 "The current target does not support navigation"); | 315 "The current target does not support navigation"); |
311 } | 316 } |
312 Error* error = NULL; | 317 Error* error = NULL; |
313 RunSessionTask(NewRunnableMethod( | 318 RunSessionTask(base::Bind( |
314 automation_.get(), | |
315 &Automation::GoForward, | 319 &Automation::GoForward, |
| 320 base::Unretained(automation_.get()), |
316 current_target_.view_id, | 321 current_target_.view_id, |
317 &error)); | 322 &error)); |
318 return error; | 323 return error; |
319 } | 324 } |
320 | 325 |
321 Error* Session::GoBack() { | 326 Error* Session::GoBack() { |
322 if (!current_target_.view_id.IsTab()) { | 327 if (!current_target_.view_id.IsTab()) { |
323 return new Error(kUnknownError, | 328 return new Error(kUnknownError, |
324 "The current target does not support navigation"); | 329 "The current target does not support navigation"); |
325 } | 330 } |
326 Error* error = NULL; | 331 Error* error = NULL; |
327 RunSessionTask(NewRunnableMethod( | 332 RunSessionTask(base::Bind( |
328 automation_.get(), | |
329 &Automation::GoBack, | 333 &Automation::GoBack, |
| 334 base::Unretained(automation_.get()), |
330 current_target_.view_id, | 335 current_target_.view_id, |
331 &error)); | 336 &error)); |
332 return error; | 337 return error; |
333 } | 338 } |
334 | 339 |
335 Error* Session::Reload() { | 340 Error* Session::Reload() { |
336 if (!current_target_.view_id.IsTab()) { | 341 if (!current_target_.view_id.IsTab()) { |
337 return new Error(kUnknownError, | 342 return new Error(kUnknownError, |
338 "The current target does not support navigation"); | 343 "The current target does not support navigation"); |
339 } | 344 } |
340 Error* error = NULL; | 345 Error* error = NULL; |
341 RunSessionTask(NewRunnableMethod( | 346 RunSessionTask(base::Bind( |
342 automation_.get(), | |
343 &Automation::Reload, | 347 &Automation::Reload, |
| 348 base::Unretained(automation_.get()), |
344 current_target_.view_id, | 349 current_target_.view_id, |
345 &error)); | 350 &error)); |
346 return error; | 351 return error; |
347 } | 352 } |
348 | 353 |
349 Error* Session::GetURL(std::string* url) { | 354 Error* Session::GetURL(std::string* url) { |
350 return ExecuteScriptAndParse(current_target_, | 355 return ExecuteScriptAndParse(current_target_, |
351 "function() { return document.URL }", | 356 "function() { return document.URL }", |
352 "getUrl", | 357 "getUrl", |
353 new ListValue(), | 358 new ListValue(), |
(...skipping 11 matching lines...) Expand all Loading... |
365 return ExecuteScriptAndParse(FrameId(current_target_.view_id, FramePath()), | 370 return ExecuteScriptAndParse(FrameId(current_target_.view_id, FramePath()), |
366 kGetTitleScript, | 371 kGetTitleScript, |
367 "getTitle", | 372 "getTitle", |
368 new ListValue(), | 373 new ListValue(), |
369 CreateDirectValueParser(tab_title)); | 374 CreateDirectValueParser(tab_title)); |
370 } | 375 } |
371 | 376 |
372 Error* Session::MouseMoveAndClick(const Point& location, | 377 Error* Session::MouseMoveAndClick(const Point& location, |
373 automation::MouseButton button) { | 378 automation::MouseButton button) { |
374 Error* error = NULL; | 379 Error* error = NULL; |
375 RunSessionTask(NewRunnableMethod( | 380 RunSessionTask(base::Bind( |
376 automation_.get(), | |
377 &Automation::MouseClick, | 381 &Automation::MouseClick, |
| 382 base::Unretained(automation_.get()), |
378 current_target_.view_id, | 383 current_target_.view_id, |
379 location, | 384 location, |
380 button, | 385 button, |
381 &error)); | 386 &error)); |
382 if (!error) | 387 if (!error) |
383 mouse_position_ = location; | 388 mouse_position_ = location; |
384 return error; | 389 return error; |
385 } | 390 } |
386 | 391 |
387 Error* Session::MouseMove(const Point& location) { | 392 Error* Session::MouseMove(const Point& location) { |
388 Error* error = NULL; | 393 Error* error = NULL; |
389 RunSessionTask(NewRunnableMethod( | 394 RunSessionTask(base::Bind( |
390 automation_.get(), | |
391 &Automation::MouseMove, | 395 &Automation::MouseMove, |
| 396 base::Unretained(automation_.get()), |
392 current_target_.view_id, | 397 current_target_.view_id, |
393 location, | 398 location, |
394 &error)); | 399 &error)); |
395 if (!error) | 400 if (!error) |
396 mouse_position_ = location; | 401 mouse_position_ = location; |
397 return error; | 402 return error; |
398 } | 403 } |
399 | 404 |
400 Error* Session::MouseDrag(const Point& start, | 405 Error* Session::MouseDrag(const Point& start, |
401 const Point& end) { | 406 const Point& end) { |
402 Error* error = NULL; | 407 Error* error = NULL; |
403 RunSessionTask(NewRunnableMethod( | 408 RunSessionTask(base::Bind( |
404 automation_.get(), | |
405 &Automation::MouseDrag, | 409 &Automation::MouseDrag, |
| 410 base::Unretained(automation_.get()), |
406 current_target_.view_id, | 411 current_target_.view_id, |
407 start, | 412 start, |
408 end, | 413 end, |
409 &error)); | 414 &error)); |
410 if (!error) | 415 if (!error) |
411 mouse_position_ = end; | 416 mouse_position_ = end; |
412 return error; | 417 return error; |
413 } | 418 } |
414 | 419 |
415 Error* Session::MouseClick(automation::MouseButton button) { | 420 Error* Session::MouseClick(automation::MouseButton button) { |
416 return MouseMoveAndClick(mouse_position_, button); | 421 return MouseMoveAndClick(mouse_position_, button); |
417 } | 422 } |
418 | 423 |
419 Error* Session::MouseButtonDown() { | 424 Error* Session::MouseButtonDown() { |
420 Error* error = NULL; | 425 Error* error = NULL; |
421 RunSessionTask(NewRunnableMethod( | 426 RunSessionTask(base::Bind( |
422 automation_.get(), | |
423 &Automation::MouseButtonDown, | 427 &Automation::MouseButtonDown, |
| 428 base::Unretained(automation_.get()), |
424 current_target_.view_id, | 429 current_target_.view_id, |
425 mouse_position_, | 430 mouse_position_, |
426 &error)); | 431 &error)); |
427 return error; | 432 return error; |
428 } | 433 } |
429 | 434 |
430 Error* Session::MouseButtonUp() { | 435 Error* Session::MouseButtonUp() { |
431 Error* error = NULL; | 436 Error* error = NULL; |
432 RunSessionTask(NewRunnableMethod( | 437 RunSessionTask(base::Bind( |
433 automation_.get(), | |
434 &Automation::MouseButtonUp, | 438 &Automation::MouseButtonUp, |
| 439 base::Unretained(automation_.get()), |
435 current_target_.view_id, | 440 current_target_.view_id, |
436 mouse_position_, | 441 mouse_position_, |
437 &error)); | 442 &error)); |
438 return error; | 443 return error; |
439 } | 444 } |
440 | 445 |
441 Error* Session::MouseDoubleClick() { | 446 Error* Session::MouseDoubleClick() { |
442 Error* error = NULL; | 447 Error* error = NULL; |
443 RunSessionTask(NewRunnableMethod( | 448 RunSessionTask(base::Bind( |
444 automation_.get(), | |
445 &Automation::MouseDoubleClick, | 449 &Automation::MouseDoubleClick, |
| 450 base::Unretained(automation_.get()), |
446 current_target_.view_id, | 451 current_target_.view_id, |
447 mouse_position_, | 452 mouse_position_, |
448 &error)); | 453 &error)); |
449 return error; | 454 return error; |
450 } | 455 } |
451 | 456 |
452 Error* Session::GetCookies(const std::string& url, ListValue** cookies) { | 457 Error* Session::GetCookies(const std::string& url, ListValue** cookies) { |
453 Error* error = NULL; | 458 Error* error = NULL; |
454 RunSessionTask(NewRunnableMethod( | 459 RunSessionTask(base::Bind( |
455 automation_.get(), | |
456 &Automation::GetCookies, | 460 &Automation::GetCookies, |
| 461 base::Unretained(automation_.get()), |
457 url, | 462 url, |
458 cookies, | 463 cookies, |
459 &error)); | 464 &error)); |
460 return error; | 465 return error; |
461 } | 466 } |
462 | 467 |
463 Error* Session::DeleteCookie(const std::string& url, | 468 Error* Session::DeleteCookie(const std::string& url, |
464 const std::string& cookie_name) { | 469 const std::string& cookie_name) { |
465 Error* error = NULL; | 470 Error* error = NULL; |
466 RunSessionTask(NewRunnableMethod( | 471 RunSessionTask(base::Bind( |
467 automation_.get(), | |
468 &Automation::DeleteCookie, | 472 &Automation::DeleteCookie, |
| 473 base::Unretained(automation_.get()), |
469 url, | 474 url, |
470 cookie_name, | 475 cookie_name, |
471 &error)); | 476 &error)); |
472 return error; | 477 return error; |
473 } | 478 } |
474 | 479 |
475 Error* Session::SetCookie(const std::string& url, | 480 Error* Session::SetCookie(const std::string& url, |
476 DictionaryValue* cookie_dict) { | 481 DictionaryValue* cookie_dict) { |
477 Error* error = NULL; | 482 Error* error = NULL; |
478 RunSessionTask(NewRunnableMethod( | 483 RunSessionTask(base::Bind( |
479 automation_.get(), | |
480 &Automation::SetCookie, | 484 &Automation::SetCookie, |
| 485 base::Unretained(automation_.get()), |
481 url, | 486 url, |
482 cookie_dict, | 487 cookie_dict, |
483 &error)); | 488 &error)); |
484 return error; | 489 return error; |
485 } | 490 } |
486 | 491 |
487 Error* Session::GetViews(std::vector<WebViewInfo>* views) { | 492 Error* Session::GetViews(std::vector<WebViewInfo>* views) { |
488 Error* error = NULL; | 493 Error* error = NULL; |
489 RunSessionTask(NewRunnableMethod( | 494 RunSessionTask(base::Bind( |
490 automation_.get(), | |
491 &Automation::GetViews, | 495 &Automation::GetViews, |
| 496 base::Unretained(automation_.get()), |
492 views, | 497 views, |
493 &error)); | 498 &error)); |
494 return error; | 499 return error; |
495 } | 500 } |
496 | 501 |
497 Error* Session::SwitchToView(const std::string& id_or_name) { | 502 Error* Session::SwitchToView(const std::string& id_or_name) { |
498 Error* error = NULL; | 503 Error* error = NULL; |
499 bool does_exist = false; | 504 bool does_exist = false; |
500 | 505 |
501 WebViewId new_view; | 506 WebViewId new_view; |
502 StringToWebViewId(id_or_name, &new_view); | 507 StringToWebViewId(id_or_name, &new_view); |
503 if (new_view.IsValid()) { | 508 if (new_view.IsValid()) { |
504 RunSessionTask(NewRunnableMethod( | 509 RunSessionTask(base::Bind( |
505 automation_.get(), | |
506 &Automation::DoesViewExist, | 510 &Automation::DoesViewExist, |
| 511 base::Unretained(automation_.get()), |
507 new_view, | 512 new_view, |
508 &does_exist, | 513 &does_exist, |
509 &error)); | 514 &error)); |
510 if (error) | 515 if (error) |
511 return error; | 516 return error; |
512 } | 517 } |
513 | 518 |
514 if (!does_exist) { | 519 if (!does_exist) { |
515 // See if any of the tab window names match |name|. | 520 // See if any of the tab window names match |name|. |
516 std::vector<WebViewInfo> views; | 521 std::vector<WebViewInfo> views; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 } else if (error.get()) { | 642 } else if (error.get()) { |
638 return error.release(); | 643 return error.release(); |
639 } | 644 } |
640 frame_path = frame_path.Append(components[i]); | 645 frame_path = frame_path.Append(components[i]); |
641 } | 646 } |
642 return NULL; | 647 return NULL; |
643 } | 648 } |
644 | 649 |
645 Error* Session::CloseWindow() { | 650 Error* Session::CloseWindow() { |
646 Error* error = NULL; | 651 Error* error = NULL; |
647 RunSessionTask(NewRunnableMethod( | 652 RunSessionTask(base::Bind( |
648 automation_.get(), | |
649 &Automation::CloseView, | 653 &Automation::CloseView, |
| 654 base::Unretained(automation_.get()), |
650 current_target_.view_id, | 655 current_target_.view_id, |
651 &error)); | 656 &error)); |
652 | 657 |
653 if (!error) { | 658 if (!error) { |
654 std::vector<WebViewInfo> views; | 659 std::vector<WebViewInfo> views; |
655 scoped_ptr<Error> error(GetViews(&views)); | 660 scoped_ptr<Error> error(GetViews(&views)); |
656 if (error.get() || views.empty()) { | 661 if (error.get() || views.empty()) { |
657 // The automation connection will soon be closed, if not already, | 662 // The automation connection will soon be closed, if not already, |
658 // because we supposedly just closed the last window. Terminate the | 663 // because we supposedly just closed the last window. Terminate the |
659 // session. | 664 // session. |
660 // TODO(kkania): This will cause us problems if GetWindowIds fails for a | 665 // TODO(kkania): This will cause us problems if GetWindowIds fails for a |
661 // reason other than the channel is disconnected. Look into having | 666 // reason other than the channel is disconnected. Look into having |
662 // |GetWindowIds| tell us if it just closed the last window. | 667 // |GetWindowIds| tell us if it just closed the last window. |
663 Terminate(); | 668 Terminate(); |
664 } | 669 } |
665 } | 670 } |
666 return error; | 671 return error; |
667 } | 672 } |
668 | 673 |
669 Error* Session::GetAlertMessage(std::string* text) { | 674 Error* Session::GetAlertMessage(std::string* text) { |
670 Error* error = NULL; | 675 Error* error = NULL; |
671 RunSessionTask(NewRunnableMethod( | 676 RunSessionTask(base::Bind( |
672 automation_.get(), | |
673 &Automation::GetAppModalDialogMessage, | 677 &Automation::GetAppModalDialogMessage, |
| 678 base::Unretained(automation_.get()), |
674 text, | 679 text, |
675 &error)); | 680 &error)); |
676 return error; | 681 return error; |
677 } | 682 } |
678 | 683 |
679 Error* Session::SetAlertPromptText(const std::string& alert_prompt_text) { | 684 Error* Session::SetAlertPromptText(const std::string& alert_prompt_text) { |
680 std::string message_text; | 685 std::string message_text; |
681 // Only set the alert prompt text if an alert is actually active. | 686 // Only set the alert prompt text if an alert is actually active. |
682 Error* error = GetAlertMessage(&message_text); | 687 Error* error = GetAlertMessage(&message_text); |
683 if (!error) { | 688 if (!error) { |
684 has_alert_prompt_text_ = true; | 689 has_alert_prompt_text_ = true; |
685 alert_prompt_text_ = alert_prompt_text; | 690 alert_prompt_text_ = alert_prompt_text; |
686 } | 691 } |
687 return error; | 692 return error; |
688 } | 693 } |
689 | 694 |
690 Error* Session::AcceptOrDismissAlert(bool accept) { | 695 Error* Session::AcceptOrDismissAlert(bool accept) { |
691 Error* error = NULL; | 696 Error* error = NULL; |
692 if (accept && has_alert_prompt_text_) { | 697 if (accept && has_alert_prompt_text_) { |
693 RunSessionTask(NewRunnableMethod( | 698 RunSessionTask(base::Bind( |
694 automation_.get(), | |
695 &Automation::AcceptPromptAppModalDialog, | 699 &Automation::AcceptPromptAppModalDialog, |
| 700 base::Unretained(automation_.get()), |
696 alert_prompt_text_, | 701 alert_prompt_text_, |
697 &error)); | 702 &error)); |
698 } else { | 703 } else { |
699 RunSessionTask(NewRunnableMethod( | 704 RunSessionTask(base::Bind( |
700 automation_.get(), | |
701 &Automation::AcceptOrDismissAppModalDialog, | 705 &Automation::AcceptOrDismissAppModalDialog, |
| 706 base::Unretained(automation_.get()), |
702 accept, | 707 accept, |
703 &error)); | 708 &error)); |
704 } | 709 } |
705 has_alert_prompt_text_ = false; | 710 has_alert_prompt_text_ = false; |
706 return error; | 711 return error; |
707 } | 712 } |
708 | 713 |
709 std::string Session::GetBrowserVersion() { | 714 std::string Session::GetBrowserVersion() { |
710 std::string version; | 715 std::string version; |
711 RunSessionTask(NewRunnableMethod( | 716 RunSessionTask(base::Bind( |
712 automation_.get(), | |
713 &Automation::GetBrowserVersion, | 717 &Automation::GetBrowserVersion, |
| 718 base::Unretained(automation_.get()), |
714 &version)); | 719 &version)); |
715 return version; | 720 return version; |
716 } | 721 } |
717 | 722 |
718 Error* Session::CompareBrowserVersion(int client_build_no, | 723 Error* Session::CompareBrowserVersion(int client_build_no, |
719 int client_patch_no, | 724 int client_patch_no, |
720 bool* is_newer_or_equal) { | 725 bool* is_newer_or_equal) { |
721 std::string version = GetBrowserVersion(); | 726 std::string version = GetBrowserVersion(); |
722 std::vector<std::string> split_version; | 727 std::vector<std::string> split_version; |
723 base::SplitString(version, '.', &split_version); | 728 base::SplitString(version, '.', &split_version); |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
999 atoms::asString(atoms::GET_ATTRIBUTE), | 1004 atoms::asString(atoms::GET_ATTRIBUTE), |
1000 "getAttribute", | 1005 "getAttribute", |
1001 CreateListValueFrom(element, key), | 1006 CreateListValueFrom(element, key), |
1002 CreateDirectValueParser(value)); | 1007 CreateDirectValueParser(value)); |
1003 } | 1008 } |
1004 | 1009 |
1005 Error* Session::WaitForAllViewsToStopLoading() { | 1010 Error* Session::WaitForAllViewsToStopLoading() { |
1006 if (!automation_.get()) | 1011 if (!automation_.get()) |
1007 return NULL; | 1012 return NULL; |
1008 Error* error = NULL; | 1013 Error* error = NULL; |
1009 RunSessionTask(NewRunnableMethod( | 1014 RunSessionTask(base::Bind( |
1010 automation_.get(), | |
1011 &Automation::WaitForAllViewsToStopLoading, | 1015 &Automation::WaitForAllViewsToStopLoading, |
| 1016 base::Unretained(automation_.get()), |
1012 &error)); | 1017 &error)); |
1013 return error; | 1018 return error; |
1014 } | 1019 } |
1015 | 1020 |
1016 Error* Session::InstallExtensionDeprecated(const FilePath& path) { | 1021 Error* Session::InstallExtensionDeprecated(const FilePath& path) { |
1017 Error* error = NULL; | 1022 Error* error = NULL; |
1018 RunSessionTask(NewRunnableMethod( | 1023 RunSessionTask(base::Bind( |
1019 automation_.get(), | |
1020 &Automation::InstallExtensionDeprecated, | 1024 &Automation::InstallExtensionDeprecated, |
| 1025 base::Unretained(automation_.get()), |
1021 path, | 1026 path, |
1022 &error)); | 1027 &error)); |
1023 return error; | 1028 return error; |
1024 } | 1029 } |
1025 | 1030 |
1026 Error* Session::InstallExtension( | 1031 Error* Session::InstallExtension( |
1027 const FilePath& path, std::string* extension_id) { | 1032 const FilePath& path, std::string* extension_id) { |
1028 Error* error = NULL; | 1033 Error* error = NULL; |
1029 RunSessionTask(base::Bind( | 1034 RunSessionTask(base::Bind( |
1030 &Automation::InstallExtension, | 1035 &Automation::InstallExtension, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1092 Error* Session::UninstallExtension(const std::string& extension_id) { | 1097 Error* Session::UninstallExtension(const std::string& extension_id) { |
1093 Error* error = NULL; | 1098 Error* error = NULL; |
1094 RunSessionTask(base::Bind( | 1099 RunSessionTask(base::Bind( |
1095 &Automation::UninstallExtension, | 1100 &Automation::UninstallExtension, |
1096 base::Unretained(automation_.get()), | 1101 base::Unretained(automation_.get()), |
1097 extension_id, | 1102 extension_id, |
1098 &error)); | 1103 &error)); |
1099 return error; | 1104 return error; |
1100 } | 1105 } |
1101 | 1106 |
| 1107 Error* Session::SetPreference( |
| 1108 const std::string& pref, |
| 1109 bool is_user_pref, |
| 1110 base::Value* value) { |
| 1111 Error* error = NULL; |
| 1112 if (is_user_pref) { |
| 1113 RunSessionTask(base::Bind( |
| 1114 &Automation::SetPreference, |
| 1115 base::Unretained(automation_.get()), |
| 1116 pref, |
| 1117 value, |
| 1118 &error)); |
| 1119 } else { |
| 1120 RunSessionTask(base::Bind( |
| 1121 &Automation::SetLocalStatePreference, |
| 1122 base::Unretained(automation_.get()), |
| 1123 pref, |
| 1124 value, |
| 1125 &error)); |
| 1126 } |
| 1127 return error; |
| 1128 } |
| 1129 |
1102 const std::string& Session::id() const { | 1130 const std::string& Session::id() const { |
1103 return id_; | 1131 return id_; |
1104 } | 1132 } |
1105 | 1133 |
1106 const FrameId& Session::current_target() const { | 1134 const FrameId& Session::current_target() const { |
1107 return current_target_; | 1135 return current_target_; |
1108 } | 1136 } |
1109 | 1137 |
1110 void Session::set_async_script_timeout(int timeout_ms) { | 1138 void Session::set_async_script_timeout(int timeout_ms) { |
1111 async_script_timeout_ = timeout_ms; | 1139 async_script_timeout_ = timeout_ms; |
(...skipping 14 matching lines...) Expand all Loading... |
1126 const Point& Session::get_mouse_position() const { | 1154 const Point& Session::get_mouse_position() const { |
1127 return mouse_position_; | 1155 return mouse_position_; |
1128 } | 1156 } |
1129 | 1157 |
1130 const Session::Options& Session::options() const { | 1158 const Session::Options& Session::options() const { |
1131 return options_; | 1159 return options_; |
1132 } | 1160 } |
1133 | 1161 |
1134 void Session::RunSessionTask(Task* task) { | 1162 void Session::RunSessionTask(Task* task) { |
1135 base::WaitableEvent done_event(false, false); | 1163 base::WaitableEvent done_event(false, false); |
1136 thread_.message_loop_proxy()->PostTask(FROM_HERE, NewRunnableMethod( | 1164 thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind( |
1137 this, | |
1138 &Session::RunSessionTaskOnSessionThread, | 1165 &Session::RunSessionTaskOnSessionThread, |
| 1166 base::Unretained(this), |
1139 task, | 1167 task, |
1140 &done_event)); | 1168 &done_event)); |
1141 done_event.Wait(); | 1169 done_event.Wait(); |
1142 } | 1170 } |
1143 | 1171 |
1144 void Session::RunSessionTaskOnSessionThread(Task* task, | 1172 void Session::RunSessionTaskOnSessionThread(Task* task, |
1145 base::WaitableEvent* done_event) { | 1173 base::WaitableEvent* done_event) { |
1146 task->Run(); | 1174 task->Run(); |
1147 delete task; | 1175 delete task; |
1148 done_event->Signal(); | 1176 done_event->Signal(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1186 if (automation_.get()) | 1214 if (automation_.get()) |
1187 automation_->Terminate(); | 1215 automation_->Terminate(); |
1188 automation_.reset(); | 1216 automation_.reset(); |
1189 } | 1217 } |
1190 | 1218 |
1191 Error* Session::ExecuteScriptAndParseValue(const FrameId& frame_id, | 1219 Error* Session::ExecuteScriptAndParseValue(const FrameId& frame_id, |
1192 const std::string& script, | 1220 const std::string& script, |
1193 Value** script_result) { | 1221 Value** script_result) { |
1194 std::string response_json; | 1222 std::string response_json; |
1195 Error* error = NULL; | 1223 Error* error = NULL; |
1196 RunSessionTask(NewRunnableMethod( | 1224 RunSessionTask(base::Bind( |
1197 automation_.get(), | |
1198 &Automation::ExecuteScript, | 1225 &Automation::ExecuteScript, |
| 1226 base::Unretained(automation_.get()), |
1199 frame_id.view_id, | 1227 frame_id.view_id, |
1200 frame_id.frame_path, | 1228 frame_id.frame_path, |
1201 script, | 1229 script, |
1202 &response_json, | 1230 &response_json, |
1203 &error)); | 1231 &error)); |
1204 if (error) | 1232 if (error) |
1205 return error; | 1233 return error; |
1206 | 1234 |
1207 scoped_ptr<Value> value(base::JSONReader::ReadAndReturnError( | 1235 scoped_ptr<Value> value(base::JSONReader::ReadAndReturnError( |
1208 response_json, true, NULL, NULL)); | 1236 response_json, true, NULL, NULL)); |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1510 "The current target does not support screenshot"); | 1538 "The current target does not support screenshot"); |
1511 } | 1539 } |
1512 Error* error = NULL; | 1540 Error* error = NULL; |
1513 ScopedTempDir screenshots_dir; | 1541 ScopedTempDir screenshots_dir; |
1514 if (!screenshots_dir.CreateUniqueTempDir()) { | 1542 if (!screenshots_dir.CreateUniqueTempDir()) { |
1515 return new Error(kUnknownError, | 1543 return new Error(kUnknownError, |
1516 "Could not create temp directory for screenshot"); | 1544 "Could not create temp directory for screenshot"); |
1517 } | 1545 } |
1518 | 1546 |
1519 FilePath path = screenshots_dir.path().AppendASCII("screen"); | 1547 FilePath path = screenshots_dir.path().AppendASCII("screen"); |
1520 RunSessionTask(NewRunnableMethod( | 1548 RunSessionTask(base::Bind( |
1521 automation_.get(), | |
1522 &Automation::CaptureEntirePageAsPNG, | 1549 &Automation::CaptureEntirePageAsPNG, |
| 1550 base::Unretained(automation_.get()), |
1523 current_target_.view_id, | 1551 current_target_.view_id, |
1524 path, | 1552 path, |
1525 &error)); | 1553 &error)); |
1526 if (error) | 1554 if (error) |
1527 return error; | 1555 return error; |
1528 if (!file_util::ReadFileToString(path, png)) | 1556 if (!file_util::ReadFileToString(path, png)) |
1529 return new Error(kUnknownError, "Could not read screenshot file"); | 1557 return new Error(kUnknownError, "Could not read screenshot file"); |
1530 return NULL; | 1558 return NULL; |
1531 } | 1559 } |
1532 | 1560 |
1533 Error* Session::GetBrowserConnectionState(bool* online) { | 1561 Error* Session::GetBrowserConnectionState(bool* online) { |
1534 return ExecuteScriptAndParse( | 1562 return ExecuteScriptAndParse( |
1535 current_target_, | 1563 current_target_, |
1536 atoms::asString(atoms::IS_ONLINE), | 1564 atoms::asString(atoms::IS_ONLINE), |
1537 "isOnline", | 1565 "isOnline", |
1538 new ListValue(), | 1566 new ListValue(), |
1539 CreateDirectValueParser(online)); | 1567 CreateDirectValueParser(online)); |
1540 } | 1568 } |
1541 | 1569 |
1542 Error* Session::GetAppCacheStatus(int* status) { | 1570 Error* Session::GetAppCacheStatus(int* status) { |
1543 return ExecuteScriptAndParse( | 1571 return ExecuteScriptAndParse( |
1544 current_target_, | 1572 current_target_, |
1545 atoms::asString(atoms::GET_APPCACHE_STATUS), | 1573 atoms::asString(atoms::GET_APPCACHE_STATUS), |
1546 "getAppcacheStatus", | 1574 "getAppcacheStatus", |
1547 new ListValue(), | 1575 new ListValue(), |
1548 CreateDirectValueParser(status)); | 1576 CreateDirectValueParser(status)); |
1549 } | 1577 } |
1550 | 1578 |
| 1579 Error* Session::PostBrowserStartInit() { |
| 1580 Error* error = NULL; |
| 1581 if (!options_.no_website_testing_defaults) |
| 1582 error = InitForWebsiteTesting(); |
| 1583 if (error) |
| 1584 return error; |
| 1585 |
| 1586 // Install extensions. |
| 1587 for (size_t i = 0; i < options_.extensions.size(); ++i) { |
| 1588 error = InstallExtensionDeprecated(options_.extensions[i]); |
| 1589 if (error) |
| 1590 return error; |
| 1591 } |
| 1592 return NULL; |
| 1593 } |
| 1594 |
| 1595 Error* Session::InitForWebsiteTesting() { |
| 1596 // Disable checking for SSL certificate revocation. |
| 1597 Error* error = SetPreference( |
| 1598 "ssl.rev_checking.enabled", |
| 1599 false /* is_user_pref */, |
| 1600 Value::CreateBooleanValue(false)); |
| 1601 if (error) |
| 1602 return error; |
| 1603 |
| 1604 // Allow certain content by default. |
| 1605 const int kAllowContent = 1; |
| 1606 DictionaryValue* default_content_settings = new DictionaryValue(); |
| 1607 default_content_settings->SetInteger("geolocation", kAllowContent); |
| 1608 default_content_settings->SetInteger("notifications", kAllowContent); |
| 1609 return SetPreference( |
| 1610 "profile.default_content_settings", |
| 1611 true /* is_user_pref */, |
| 1612 default_content_settings); |
| 1613 } |
| 1614 |
1551 } // namespace webdriver | 1615 } // namespace webdriver |
OLD | NEW |