[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 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_FRAME_EXTERNAL_TAB_H_ |
| 6 | #define CHROME_FRAME_EXTERNAL_TAB_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <windows.h> |
| 10 | #include <atlbase.h> |
| 11 | #include <atlwin.h> |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 12 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 13 | #include <string> |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 17 | #include "base/time.h" |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 18 | #include "base/win/scoped_comptr.h" |
[email protected] | a8ba636 | 2010-11-10 20:02:25 | [diff] [blame] | 19 | #include "chrome/common/automation_constants.h" |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 20 | #include "chrome_frame/cfproxy.h" |
| 21 | #include "chrome_frame/task_marshaller.h" |
[email protected] | 4dd5793 | 2011-03-17 06:06:12 | [diff] [blame] | 22 | #include "content/common/page_zoom.h" |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 23 | #include "googleurl/src/gurl.h" |
| 24 | |
| 25 | class Task; |
| 26 | class CancelableTask; |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 27 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 28 | namespace base { |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 29 | class TimeDelta; |
| 30 | class WaitableEvent; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | namespace IPC { |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 34 | struct NavigationInfo; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 35 | } |
| 36 | |
[email protected] | 326d3b7 | 2011-03-29 20:38:24 | [diff] [blame] | 37 | namespace gfx { |
| 38 | class Rect; |
| 39 | } |
| 40 | |
| 41 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 42 | // This is the delegate/callback interface that has to be implemented |
| 43 | // by the customers of ExternalTabProxy class. |
| 44 | class UIDelegate { |
| 45 | public: |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 46 | virtual void OnNavigationStateChanged( |
| 47 | int flags, const NavigationInfo& nav_info) = 0; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 48 | virtual void OnUpdateTargetUrl(const std::wstring& new_target_url) = 0; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 49 | virtual void OnLoad(const GURL& url) = 0; |
[email protected] | 326d3b7 | 2011-03-29 20:38:24 | [diff] [blame] | 50 | virtual void OnMoveWindow(const gfx::Rect& pos) = 0; |
| 51 | |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 52 | virtual void OnMessageFromChromeFrame( |
| 53 | const std::string& message, const std::string& origin, |
| 54 | const std::string& target) = 0; |
| 55 | virtual void OnHandleContextMenu( |
[email protected] | b516e2d | 2011-07-12 16:54:12 | [diff] [blame^] | 56 | const ContextMenuModel& context_menu_model, int align_flags, |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 57 | const MiniContextMenuParams& params) = 0; |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 58 | virtual void OnHandleAccelerator(const MSG& accel_message) = 0; |
| 59 | virtual void OnTabbedOut(bool reverse) = 0; |
| 60 | virtual void OnGoToHistoryOffset(int offset) = 0; |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 61 | virtual void OnOpenURL( |
| 62 | const GURL& url_to_open, const GURL& referrer, int open_disposition) = 0; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 63 | protected: |
| 64 | ~UIDelegate() {} |
| 65 | }; |
| 66 | |
| 67 | struct CreateTabParams { |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 68 | struct ProxyParams proxy_params; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 69 | bool is_incognito; |
| 70 | bool is_widget_mode; |
| 71 | GURL url; |
| 72 | GURL referrer; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 73 | }; |
| 74 | |
[email protected] | 354bcba | 2010-12-14 04:34:43 | [diff] [blame] | 75 | class NavigationConstraints; |
| 76 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 77 | ///////////////////////////////////////////////////////////////////////// |
[email protected] | 99d81e5b | 2011-06-29 02:29:53 | [diff] [blame] | 78 | // ExternalTabProxy is a mediator between ChromeProxy (which runs mostly in the |
| 79 | // background IPC-channel thread) and the UI object (ActiveX, ActiveDocument). |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 80 | // The lifetime of ExternalTabProxy is determined by the UI object. |
| 81 | // |
| 82 | // When ExternalTabProxy dies: |
| 83 | // 1. Remove itself as a ChromeProxyDelegate. This blocks until _Disconnected() |
| 84 | // is received. |
| 85 | // 2. Kills all posted tasks to the UI thread. |
| 86 | // 3. Stop all network requests |
| 87 | // => It does not have to (and should not) be a refcount-ed object. |
| 88 | |
| 89 | // Non-public inheritance is not allowed by the style-guide. |
| 90 | class ExternalTabProxy : public CWindowImpl<ExternalTabProxy>, |
| 91 | public ChromeProxyDelegate { |
| 92 | public: |
| 93 | ExternalTabProxy(); |
| 94 | ~ExternalTabProxy(); |
| 95 | |
| 96 | #ifdef UNIT_TEST |
| 97 | void set_proxy_factory(ChromeProxyFactory* factory) { |
| 98 | proxy_factory_ = factory; |
| 99 | } |
| 100 | #endif |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 101 | |
| 102 | // IPC::Channel::Listener implementation. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 103 | bool OnMessageReceived(const IPC::Message& message); |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 104 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 105 | // |
| 106 | virtual void CreateTab(const CreateTabParams& create_params, |
| 107 | UIDelegate* delegate); |
| 108 | virtual void Navigate(const std::string& url, const std::string& referrer, |
[email protected] | 354bcba | 2010-12-14 04:34:43 | [diff] [blame] | 109 | NavigationConstraints* navigation_constraints); |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 110 | virtual void NavigateToIndex(int index); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 111 | virtual void ForwardMessageFromExternalHost(const std::string& message, |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 112 | const std::string& origin, const std::string& target); |
| 113 | virtual void ChromeFrameHostMoved(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 114 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 115 | // Attaches an existing external tab to this automation client instance. |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 116 | virtual void ConnectToExternalTab(uint64 external_tab_cookie); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 117 | virtual void BlockExternalTab(uint64 cookie); |
| 118 | |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 119 | void SetZoomLevel(PageZoom::Function zoom_level); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 120 | |
| 121 | private: |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 122 | BEGIN_MSG_MAP(ExternalTabProxy) |
| 123 | CHAIN_MSG_MAP_MEMBER(ui_); |
| 124 | END_MSG_MAP() |
| 125 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 126 | ////////////////////////////////////////////////////////////////////////// |
| 127 | // ChromeProxyDelegate implementation |
| 128 | virtual int tab_handle() { |
| 129 | return tab_; |
| 130 | } |
| 131 | virtual void Connected(ChromeProxy* proxy); |
| 132 | virtual void PeerLost(ChromeProxy* proxy, DisconnectReason reason); |
| 133 | virtual void Disconnected(); |
| 134 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 135 | // Sync message responses. |
| 136 | virtual void Completed_CreateTab(bool success, HWND chrome_wnd, |
[email protected] | 751bf4b | 2010-11-05 22:06:31 | [diff] [blame] | 137 | HWND tab_window, int tab_handle, int session_id); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 138 | virtual void Completed_ConnectToTab(bool success, HWND chrome_window, |
[email protected] | 751bf4b | 2010-11-05 22:06:31 | [diff] [blame] | 139 | HWND tab_window, int tab_handle, int session_id); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 140 | virtual void Completed_Navigate(bool success, |
| 141 | enum AutomationMsg_NavigationResponseValues res); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 142 | |
| 143 | // Network requests from Chrome. |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 144 | virtual void OnNetwork_Start( |
| 145 | int request_id, const AutomationURLRequest& request_info); |
| 146 | virtual void OnNetwork_Read(int request_id, int bytes_to_read); |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 147 | virtual void OnNetwork_End(int request_id, const net::URLRequestStatus& s); |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 148 | virtual void OnNetwork_DownloadInHost(int request_id); |
| 149 | virtual void OnGetCookies(const GURL& url, int cookie_id); |
| 150 | virtual void OnSetCookie(const GURL& url, const std::string& cookie); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 151 | |
| 152 | // Navigation progress notifications. |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 153 | virtual void OnNavigationStateChanged( |
| 154 | int flags, const NavigationInfo& nav_info); |
| 155 | virtual void OnUpdateTargetUrl(const std::wstring& url); |
| 156 | virtual void OnNavigationFailed(int error_code, const GURL& gurl); |
| 157 | virtual void OnDidNavigate(const NavigationInfo& navigation_info); |
| 158 | virtual void OnTabLoaded(const GURL& url); |
[email protected] | 326d3b7 | 2011-03-29 20:38:24 | [diff] [blame] | 159 | virtual void OnMoveWindow(const gfx::Rect& pos); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 160 | |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 161 | virtual void OnOpenURL(const GURL& url_to_open, const GURL& referrer, |
| 162 | int open_disposition); |
| 163 | virtual void OnGoToHistoryOffset(int offset); |
| 164 | virtual void OnMessageToHost( |
| 165 | const std::string& message, const std::string& origin, |
| 166 | const std::string& target); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 167 | |
| 168 | // Misc. UI. |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 169 | virtual void OnHandleAccelerator(const MSG& accel_message); |
[email protected] | b516e2d | 2011-07-12 16:54:12 | [diff] [blame^] | 170 | virtual void OnHandleContextMenu(const ContextMenuModel& context_menu_model, |
| 171 | int align_flags, |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 172 | const MiniContextMenuParams& params); |
| 173 | virtual void OnTabbedOut(bool reverse); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 174 | |
| 175 | // Other |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 176 | virtual void OnTabClosed(); |
| 177 | virtual void OnAttachTab(const AttachExternalTabParams& attach_params); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 178 | |
| 179 | // end of ChromeProxyDelegate methods |
| 180 | ////////////////////////////////////////////////////////////////////////// |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 181 | void Init(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 182 | void Destroy(); |
| 183 | |
| 184 | // The UiXXXX are the ChromeProxyDelegate methods but on UI thread. |
| 185 | void UiConnected(ChromeProxy* proxy); |
| 186 | void UiPeerLost(ChromeProxy* proxy, DisconnectReason reason); |
[email protected] | 314a0696 | 2010-10-20 21:32:32 | [diff] [blame] | 187 | void UiCompleted_CreateTab(bool success, HWND chrome_window, |
[email protected] | 751bf4b | 2010-11-05 22:06:31 | [diff] [blame] | 188 | HWND tab_window, int tab_handle, int session_id); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 189 | |
| 190 | // With the present state of affairs the only response we can possibly handle |
| 191 | // in the background IPC thread is Completed_CreateTab() where we can |
| 192 | // initiate a navigation (if there is a pending one). |
| 193 | // To simplify - will handle Completed_CreateTab in UI thread and avoid |
| 194 | // the need of lock when accessing members. |
| 195 | enum { |
| 196 | NONE, |
| 197 | INIT_IN_PROGRESS, |
| 198 | CREATE_TAB_IN_PROGRESS, |
| 199 | READY, |
| 200 | RELEASE_CF_PROXY_IN_PROGRESS |
| 201 | } state_; |
| 202 | int tab_; |
[email protected] | 314a0696 | 2010-10-20 21:32:32 | [diff] [blame] | 203 | HWND tab_wnd_; |
| 204 | HWND chrome_wnd_; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 205 | ChromeProxyFactory* proxy_factory_; |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 206 | // Accessed only in the UI thread for simplicity. |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 207 | ChromeProxy* proxy_; |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 208 | // Accessed from ipc thread as well. It's safe if the object goes away |
| 209 | // because this should be preceded by destruction of the window and |
| 210 | // therefore all queued tasks should be destroyed. |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 211 | UIDelegate* ui_delegate_; |
| 212 | TaskMarshallerThroughMessageQueue ui_; |
| 213 | |
| 214 | scoped_ptr<base::WaitableEvent> done_; |
| 215 | |
| 216 | CreateTabParams tab_params_; |
| 217 | struct PendingNavigation { |
| 218 | GURL url; |
| 219 | GURL referrer; |
| 220 | void Set(const GURL& gurl, const GURL& ref) { |
| 221 | url = gurl; |
| 222 | referrer = ref; |
| 223 | } |
| 224 | } pending_navigation_; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | #endif // CHROME_FRAME_EXTERNAL_TAB_H_ |