Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: chrome/test/automation/automation_proxy.cc

Issue 113722: Make automation proxy objects to ref_counted. That allows to process async no... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 <sstream> 5 #include <sstream>
6 6
7 #include "chrome/test/automation/automation_proxy.h" 7 #include "chrome/test/automation/automation_proxy.h"
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/file_version_info.h" 10 #include "base/file_version_info.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 LOG(ERROR) << "Pre-versioning protocol detected in automation provider."; 68 LOG(ERROR) << "Pre-versioning protocol detected in automation provider.";
69 } 69 }
70 70
71 server_->SignalAppLaunch(server_version); 71 server_->SignalAppLaunch(server_version);
72 } 72 }
73 73
74 private: 74 private:
75 AutomationProxy* server_; 75 AutomationProxy* server_;
76 }; 76 };
77 77
78 class TabProxyNotificationMessageFilter
79 : public IPC::ChannelProxy::MessageFilter {
80 public:
81 TabProxyNotificationMessageFilter(AutomationHandleTracker* tracker)
82 : tracker_ (tracker) {
83 }
84
85 virtual bool OnMessageReceived(const IPC::Message& message) {
86 if (message.is_sync())
87 return false;
88
89 if (message.is_reply())
90 return false;
91
92 bool tab_message = IsTabNotifyMessage(message);
93 if (tab_message == false)
94 return false;
95
96 // Read tab handle from the message.
97 int tab_handle = 0;
98 void* iter = NULL;
99 if (!message.ReadInt(&iter, &tab_handle))
100 return false;
101
102 // Get AddRef-ed pointer to corresponding TabProxy object
103 TabProxy* tab = static_cast<TabProxy*>(tracker_->GetResource(tab_handle));
104 if (tab) {
105 tab->OnMessageReceived(message);
106 tab->Release();
107 }
108 return true;
109 }
110
111 static bool IsTabNotifyMessage(const IPC::Message& message) {
112 bool tab_message = true;
113 IPC_BEGIN_MESSAGE_MAP(TabProxyNotificationMessageFilter, message)
114 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_NavigationStateChanged, )
115 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_UpdateTargetUrl, )
116 #if defined(OS_WIN)
117 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_HandleAccelerator, )
118 #endif
119 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_TabbedOut, )
120 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_OpenURL, )
121 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_DidNavigate, )
122 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_NavigationFailed, )
123 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_TabLoaded, )
124 IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_ForwardMessageToExternalHost, )
125
126 IPC_MESSAGE_UNHANDLED(tab_message = false);
127 IPC_END_MESSAGE_MAP()
128 return tab_message;
129 }
130
131 private:
132 AutomationHandleTracker* tracker_;
133 };
78 } // anonymous namespace 134 } // anonymous namespace
79 135
80 136
81 AutomationProxy::AutomationProxy(int command_execution_timeout_ms) 137 AutomationProxy::AutomationProxy(int command_execution_timeout_ms)
82 : app_launched_(true, false), 138 : app_launched_(true, false),
83 initial_loads_complete_(true, false), 139 initial_loads_complete_(true, false),
84 new_tab_ui_load_complete_(true, false), 140 new_tab_ui_load_complete_(true, false),
85 shutdown_event_(new base::WaitableEvent(true, false)), 141 shutdown_event_(new base::WaitableEvent(true, false)),
86 app_launch_signaled_(0), 142 app_launch_signaled_(0),
87 perform_version_check_(false), 143 perform_version_check_(false),
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 388
333 bool AutomationProxy::WaitForURLDisplayed(GURL url, int wait_timeout) { 389 bool AutomationProxy::WaitForURLDisplayed(GURL url, int wait_timeout) {
334 const TimeTicks start = TimeTicks::Now(); 390 const TimeTicks start = TimeTicks::Now();
335 const TimeDelta timeout = TimeDelta::FromMilliseconds(wait_timeout); 391 const TimeDelta timeout = TimeDelta::FromMilliseconds(wait_timeout);
336 while (TimeTicks::Now() - start < timeout) { 392 while (TimeTicks::Now() - start < timeout) {
337 int window_count; 393 int window_count;
338 if (!GetBrowserWindowCount(&window_count)) 394 if (!GetBrowserWindowCount(&window_count))
339 return false; 395 return false;
340 396
341 for (int i = 0; i < window_count; i++) { 397 for (int i = 0; i < window_count; i++) {
342 BrowserProxy* window = GetBrowserWindow(i); 398 scoped_refptr<BrowserProxy> window = GetBrowserWindow(i);
343 if (!window) 399 if (!window.get())
344 break; 400 break;
345 401
346 int tab_count; 402 int tab_count;
347 if (!window->GetTabCount(&tab_count)) 403 if (!window->GetTabCount(&tab_count))
348 continue; 404 continue;
349 405
350 for (int j = 0; j < tab_count; j++) { 406 for (int j = 0; j < tab_count; j++) {
351 TabProxy* tab = window->GetTab(j); 407 scoped_refptr<TabProxy> tab = window->GetTab(j);
352 if (!tab) 408 if (!tab.get())
353 break; 409 break;
354 410
355 GURL tab_url; 411 GURL tab_url;
356 if (!tab->GetCurrentURL(&tab_url)) 412 if (!tab->GetCurrentURL(&tab_url))
357 continue; 413 continue;
358 414
359 if (tab_url == url) 415 if (tab_url == url)
360 return true; 416 return true;
361 } 417 }
362 } 418 }
(...skipping 18 matching lines...) Expand all
381 void AutomationProxy::OnMessageReceived(const IPC::Message& msg) { 437 void AutomationProxy::OnMessageReceived(const IPC::Message& msg) {
382 // This won't get called unless AutomationProxy is run from 438 // This won't get called unless AutomationProxy is run from
383 // inside a message loop. 439 // inside a message loop.
384 NOTREACHED(); 440 NOTREACHED();
385 } 441 }
386 442
387 void AutomationProxy::OnChannelError() { 443 void AutomationProxy::OnChannelError() {
388 DLOG(ERROR) << "Channel error in AutomationProxy."; 444 DLOG(ERROR) << "Channel error in AutomationProxy.";
389 } 445 }
390 446
391 WindowProxy* AutomationProxy::GetActiveWindow() { 447 scoped_refptr<WindowProxy> AutomationProxy::GetActiveWindow() {
392 int handle = 0; 448 int handle = 0;
393 449
394 if (!SendWithTimeout(new AutomationMsg_ActiveWindow(0, &handle), 450 if (!SendWithTimeout(new AutomationMsg_ActiveWindow(0, &handle),
395 command_execution_timeout_ms(), NULL)) { 451 command_execution_timeout_ms(), NULL)) {
396 return NULL; 452 return NULL;
397 } 453 }
398 454
399 return new WindowProxy(this, tracker_.get(), handle); 455 return ProxyObjectFromHandle<WindowProxy>(handle);
400 } 456 }
401 457
402 BrowserProxy* AutomationProxy::GetBrowserWindow(int window_index) { 458 scoped_refptr<BrowserProxy> AutomationProxy::GetBrowserWindow(
459 int window_index) {
403 int handle = 0; 460 int handle = 0;
404 461
405 if (!SendWithTimeout(new AutomationMsg_BrowserWindow(0, window_index, 462 if (!SendWithTimeout(new AutomationMsg_BrowserWindow(0, window_index,
406 &handle), 463 &handle),
407 command_execution_timeout_ms(), NULL)) { 464 command_execution_timeout_ms(), NULL)) {
408 DLOG(ERROR) << "GetBrowserWindow did not complete in a timely fashion"; 465 DLOG(ERROR) << "GetBrowserWindow did not complete in a timely fashion";
409 return NULL; 466 return NULL;
410 } 467 }
411 468
412 if (handle == 0) { 469 return ProxyObjectFromHandle<BrowserProxy>(handle);
413 return NULL;
414 }
415
416 return new BrowserProxy(this, tracker_.get(), handle);
417 } 470 }
418 471
419 BrowserProxy* AutomationProxy::FindNormalBrowserWindow() { 472 scoped_refptr<BrowserProxy> AutomationProxy::FindNormalBrowserWindow() {
420 int handle = 0; 473 int handle = 0;
421 474
422 if (!SendWithTimeout(new AutomationMsg_FindNormalBrowserWindow(0, &handle), 475 if (!SendWithTimeout(new AutomationMsg_FindNormalBrowserWindow(0, &handle),
423 command_execution_timeout_ms(), NULL)) { 476 command_execution_timeout_ms(), NULL)) {
424 return NULL; 477 return NULL;
425 } 478 }
426 479
427 if (handle == 0) { 480 return ProxyObjectFromHandle<BrowserProxy>(handle);
428 return NULL;
429 }
430
431 return new BrowserProxy(this, tracker_.get(), handle);
432 } 481 }
433 482
434 BrowserProxy* AutomationProxy::GetLastActiveBrowserWindow() { 483 scoped_refptr<BrowserProxy> AutomationProxy::GetLastActiveBrowserWindow() {
435 int handle = 0; 484 int handle = 0;
436 485
437 if (!SendWithTimeout(new AutomationMsg_LastActiveBrowserWindow( 486 if (!SendWithTimeout(new AutomationMsg_LastActiveBrowserWindow(
438 0, &handle), command_execution_timeout_ms(), NULL)) { 487 0, &handle), command_execution_timeout_ms(), NULL)) {
439 DLOG(ERROR) << 488 DLOG(ERROR) <<
440 "GetLastActiveBrowserWindow did not complete in a timely fashion"; 489 "GetLastActiveBrowserWindow did not complete in a timely fashion";
441 return NULL; 490 return NULL;
442 } 491 }
443 492
444 return new BrowserProxy(this, tracker_.get(), handle); 493 return ProxyObjectFromHandle<BrowserProxy>(handle);
445 } 494 }
446 495
447 #if defined(OS_POSIX) 496 #if defined(OS_POSIX)
448 base::file_handle_mapping_vector AutomationProxy::fds_to_map() const { 497 base::file_handle_mapping_vector AutomationProxy::fds_to_map() const {
449 base::file_handle_mapping_vector map; 498 base::file_handle_mapping_vector map;
450 int src_fd = -1, dest_fd = -1; 499 int src_fd = -1, dest_fd = -1;
451 channel_->GetClientFileDescriptorMapping(&src_fd, &dest_fd); 500 channel_->GetClientFileDescriptorMapping(&src_fd, &dest_fd);
452 if (src_fd > -1) 501 if (src_fd > -1)
453 map.push_back(std::make_pair(src_fd, dest_fd)); 502 map.push_back(std::make_pair(src_fd, dest_fd));
454 return map; 503 return map;
(...skipping 29 matching lines...) Expand all
484 tracker_->InvalidateHandle(handle); 533 tracker_->InvalidateHandle(handle);
485 } 534 }
486 } 535 }
487 536
488 bool AutomationProxy::OpenNewBrowserWindow(bool show) { 537 bool AutomationProxy::OpenNewBrowserWindow(bool show) {
489 return Send(new AutomationMsg_OpenNewBrowserWindow(0, show)); 538 return Send(new AutomationMsg_OpenNewBrowserWindow(0, show));
490 } 539 }
491 540
492 #if defined(OS_WIN) 541 #if defined(OS_WIN)
493 // TODO(port): Replace HWNDs. 542 // TODO(port): Replace HWNDs.
494 TabProxy* AutomationProxy::CreateExternalTab(HWND parent, 543 scoped_refptr<TabProxy> AutomationProxy::CreateExternalTab(HWND parent,
495 const gfx::Rect& dimensions, 544 const gfx::Rect& dimensions, unsigned int style, bool incognito,
496 unsigned int style, 545 HWND* external_tab_container) {
497 bool incognito,
498 HWND* external_tab_container) {
499 IPC::Message* response = NULL; 546 IPC::Message* response = NULL;
500 int handle = 0; 547 int handle = 0;
501 548
502 bool succeeded = 549 bool succeeded =
503 Send(new AutomationMsg_CreateExternalTab(0, parent, dimensions, style, 550 Send(new AutomationMsg_CreateExternalTab(0, parent, dimensions, style,
504 incognito, 551 incognito,
505 external_tab_container, 552 external_tab_container,
506 &handle)); 553 &handle));
507 if (!succeeded) { 554 if (!succeeded) {
508 return NULL; 555 return NULL;
509 } 556 }
510 557
511 DCHECK(IsWindow(*external_tab_container)); 558 DCHECK(IsWindow(*external_tab_container));
512 559 DCHECK(tracker_->GetResource(handle) == NULL);
513 return new TabProxy(this, tracker_.get(), handle); 560 return new TabProxy(this, tracker_.get(), handle);
514 } 561 }
515 #endif // defined(OS_WIN) 562 #endif // defined(OS_WIN)
563
564 template <class T> scoped_refptr<T> AutomationProxy::ProxyObjectFromHandle(
565 int handle) {
566 if (!handle)
567 return NULL;
568
569 // Get AddRef-ed pointer to the object if handle is already seen.
570 T* p = static_cast<T*>(tracker_->GetResource(handle));
571 if (!p) {
572 p = new T(this, tracker_.get(), handle);
573 p->AddRef();
574 }
575
576 // Since there is no scoped_refptr::attach.
577 scoped_refptr<T> result;
578 result.swap(&p);
579 return result;
580 }
OLDNEW
« no previous file with comments | « chrome/test/automation/automation_proxy.h ('k') | chrome/test/automation/automation_proxy_uitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698