| Index: chrome/test/automation/automation_proxy.cc
|
| ===================================================================
|
| --- chrome/test/automation/automation_proxy.cc (revision 17078)
|
| +++ chrome/test/automation/automation_proxy.cc (working copy)
|
| @@ -75,6 +75,62 @@
|
| AutomationProxy* server_;
|
| };
|
|
|
| +class TabProxyNotificationMessageFilter
|
| + : public IPC::ChannelProxy::MessageFilter {
|
| + public:
|
| + TabProxyNotificationMessageFilter(AutomationHandleTracker* tracker)
|
| + : tracker_ (tracker) {
|
| + }
|
| +
|
| + virtual bool OnMessageReceived(const IPC::Message& message) {
|
| + if (message.is_sync())
|
| + return false;
|
| +
|
| + if (message.is_reply())
|
| + return false;
|
| +
|
| + bool tab_message = IsTabNotifyMessage(message);
|
| + if (tab_message == false)
|
| + return false;
|
| +
|
| + // Read tab handle from the message.
|
| + int tab_handle = 0;
|
| + void* iter = NULL;
|
| + if (!message.ReadInt(&iter, &tab_handle))
|
| + return false;
|
| +
|
| + // Get AddRef-ed pointer to corresponding TabProxy object
|
| + TabProxy* tab = static_cast<TabProxy*>(tracker_->GetResource(tab_handle));
|
| + if (tab) {
|
| + tab->OnMessageReceived(message);
|
| + tab->Release();
|
| + }
|
| + return true;
|
| + }
|
| +
|
| + static bool IsTabNotifyMessage(const IPC::Message& message) {
|
| + bool tab_message = true;
|
| + IPC_BEGIN_MESSAGE_MAP(TabProxyNotificationMessageFilter, message)
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_NavigationStateChanged, )
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_UpdateTargetUrl, )
|
| +#if defined(OS_WIN)
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_HandleAccelerator, )
|
| +#endif
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_TabbedOut, )
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_OpenURL, )
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_DidNavigate, )
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_NavigationFailed, )
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_TabLoaded, )
|
| + IPC_MESSAGE_HANDLER_GENERIC(AutomationMsg_ForwardMessageToExternalHost, )
|
| +
|
| + IPC_MESSAGE_UNHANDLED(tab_message = false);
|
| + IPC_END_MESSAGE_MAP()
|
| + return tab_message;
|
| + }
|
| +
|
| + private:
|
| + AutomationHandleTracker* tracker_;
|
| +};
|
| } // anonymous namespace
|
|
|
|
|
| @@ -339,8 +395,8 @@
|
| return false;
|
|
|
| for (int i = 0; i < window_count; i++) {
|
| - BrowserProxy* window = GetBrowserWindow(i);
|
| - if (!window)
|
| + scoped_refptr<BrowserProxy> window = GetBrowserWindow(i);
|
| + if (!window.get())
|
| break;
|
|
|
| int tab_count;
|
| @@ -348,8 +404,8 @@
|
| continue;
|
|
|
| for (int j = 0; j < tab_count; j++) {
|
| - TabProxy* tab = window->GetTab(j);
|
| - if (!tab)
|
| + scoped_refptr<TabProxy> tab = window->GetTab(j);
|
| + if (!tab.get())
|
| break;
|
|
|
| GURL tab_url;
|
| @@ -388,7 +444,7 @@
|
| DLOG(ERROR) << "Channel error in AutomationProxy.";
|
| }
|
|
|
| -WindowProxy* AutomationProxy::GetActiveWindow() {
|
| +scoped_refptr<WindowProxy> AutomationProxy::GetActiveWindow() {
|
| int handle = 0;
|
|
|
| if (!SendWithTimeout(new AutomationMsg_ActiveWindow(0, &handle),
|
| @@ -396,10 +452,11 @@
|
| return NULL;
|
| }
|
|
|
| - return new WindowProxy(this, tracker_.get(), handle);
|
| + return ProxyObjectFromHandle<WindowProxy>(handle);
|
| }
|
|
|
| -BrowserProxy* AutomationProxy::GetBrowserWindow(int window_index) {
|
| +scoped_refptr<BrowserProxy> AutomationProxy::GetBrowserWindow(
|
| + int window_index) {
|
| int handle = 0;
|
|
|
| if (!SendWithTimeout(new AutomationMsg_BrowserWindow(0, window_index,
|
| @@ -409,14 +466,10 @@
|
| return NULL;
|
| }
|
|
|
| - if (handle == 0) {
|
| - return NULL;
|
| - }
|
| -
|
| - return new BrowserProxy(this, tracker_.get(), handle);
|
| + return ProxyObjectFromHandle<BrowserProxy>(handle);
|
| }
|
|
|
| -BrowserProxy* AutomationProxy::FindNormalBrowserWindow() {
|
| +scoped_refptr<BrowserProxy> AutomationProxy::FindNormalBrowserWindow() {
|
| int handle = 0;
|
|
|
| if (!SendWithTimeout(new AutomationMsg_FindNormalBrowserWindow(0, &handle),
|
| @@ -424,14 +477,10 @@
|
| return NULL;
|
| }
|
|
|
| - if (handle == 0) {
|
| - return NULL;
|
| - }
|
| -
|
| - return new BrowserProxy(this, tracker_.get(), handle);
|
| + return ProxyObjectFromHandle<BrowserProxy>(handle);
|
| }
|
|
|
| -BrowserProxy* AutomationProxy::GetLastActiveBrowserWindow() {
|
| +scoped_refptr<BrowserProxy> AutomationProxy::GetLastActiveBrowserWindow() {
|
| int handle = 0;
|
|
|
| if (!SendWithTimeout(new AutomationMsg_LastActiveBrowserWindow(
|
| @@ -441,7 +490,7 @@
|
| return NULL;
|
| }
|
|
|
| - return new BrowserProxy(this, tracker_.get(), handle);
|
| + return ProxyObjectFromHandle<BrowserProxy>(handle);
|
| }
|
|
|
| #if defined(OS_POSIX)
|
| @@ -491,11 +540,9 @@
|
|
|
| #if defined(OS_WIN)
|
| // TODO(port): Replace HWNDs.
|
| -TabProxy* AutomationProxy::CreateExternalTab(HWND parent,
|
| - const gfx::Rect& dimensions,
|
| - unsigned int style,
|
| - bool incognito,
|
| - HWND* external_tab_container) {
|
| +scoped_refptr<TabProxy> AutomationProxy::CreateExternalTab(HWND parent,
|
| + const gfx::Rect& dimensions, unsigned int style, bool incognito,
|
| + HWND* external_tab_container) {
|
| IPC::Message* response = NULL;
|
| int handle = 0;
|
|
|
| @@ -509,7 +556,25 @@
|
| }
|
|
|
| DCHECK(IsWindow(*external_tab_container));
|
| -
|
| + DCHECK(tracker_->GetResource(handle) == NULL);
|
| return new TabProxy(this, tracker_.get(), handle);
|
| }
|
| #endif // defined(OS_WIN)
|
| +
|
| +template <class T> scoped_refptr<T> AutomationProxy::ProxyObjectFromHandle(
|
| + int handle) {
|
| + if (!handle)
|
| + return NULL;
|
| +
|
| + // Get AddRef-ed pointer to the object if handle is already seen.
|
| + T* p = static_cast<T*>(tracker_->GetResource(handle));
|
| + if (!p) {
|
| + p = new T(this, tracker_.get(), handle);
|
| + p->AddRef();
|
| + }
|
| +
|
| + // Since there is no scoped_refptr::attach.
|
| + scoped_refptr<T> result;
|
| + result.swap(&p);
|
| + return result;
|
| +}
|
|
|