OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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_FRAME_CHROME_FRAME_AUTOMATION_H_ |
| 6 #define CHROME_FRAME_CHROME_FRAME_AUTOMATION_H_ |
| 7 |
| 8 #include <atlbase.h> |
| 9 #include <atlwin.h> |
| 10 #include <string> |
| 11 #include <map> |
| 12 |
| 13 #include "base/lock.h" |
| 14 #include "base/ref_counted.h" |
| 15 #include "base/scoped_handle.h" |
| 16 #include "base/stack_container.h" |
| 17 #include "base/task.h" |
| 18 #include "base/timer.h" |
| 19 #include "base/thread.h" |
| 20 #include "chrome/test/automation/automation_proxy.h" |
| 21 #include "chrome/test/automation/tab_proxy.h" |
| 22 #include "chrome_frame/chrome_frame_delegate.h" |
| 23 #include "chrome_frame/chrome_frame_histograms.h" |
| 24 #include "chrome_frame/plugin_url_request.h" |
| 25 |
| 26 const unsigned long kCommandExecutionTimeout = 4000; // NOLINT, 4 seconds |
| 27 |
| 28 class ProxyFactory; |
| 29 |
| 30 struct DECLSPEC_NOVTABLE ChromeFrameAutomationProxy { |
| 31 virtual bool Send(IPC::Message* msg) = 0; |
| 32 |
| 33 virtual void SendAsAsync(IPC::SyncMessage* msg, void* callback, |
| 34 void* key) = 0; |
| 35 virtual void CancelAsync(void* key) = 0; |
| 36 virtual scoped_refptr<TabProxy> CreateTabProxy(int handle) = 0; |
| 37 virtual std::string server_version() = 0; |
| 38 |
| 39 virtual void SendProxyConfig(const std::string&) = 0; |
| 40 virtual void SetEnableExtensionAutomation(bool enable) = 0; |
| 41 protected: |
| 42 ~ChromeFrameAutomationProxy() {} |
| 43 }; |
| 44 |
| 45 // We extend the AutomationProxy class to handle our custom |
| 46 // IPC messages |
| 47 class ChromeFrameAutomationProxyImpl : public ChromeFrameAutomationProxy, |
| 48 // We have to derive from automationproxy since we want access to some members |
| 49 // (tracker_ & channel_) - simple aggregation wont work; |
| 50 // .. and non-public inheritance is verboten. |
| 51 public AutomationProxy { |
| 52 public: |
| 53 virtual void SendAsAsync(IPC::SyncMessage* msg, void* callback, void* key); |
| 54 |
| 55 virtual void CancelAsync(void* key); |
| 56 |
| 57 virtual scoped_refptr<TabProxy> CreateTabProxy(int handle); |
| 58 virtual std::string server_version() { |
| 59 return AutomationProxy::server_version(); |
| 60 } |
| 61 |
| 62 |
| 63 virtual bool Send(IPC::Message* msg) { |
| 64 return AutomationProxy::Send(msg); |
| 65 } |
| 66 |
| 67 virtual void SendProxyConfig(const std::string& p) { |
| 68 AutomationProxy::SendProxyConfig(p); |
| 69 } |
| 70 |
| 71 virtual void SetEnableExtensionAutomation(bool e) { |
| 72 AutomationProxy::SetEnableExtensionAutomation(e); |
| 73 } |
| 74 |
| 75 protected: |
| 76 explicit ChromeFrameAutomationProxyImpl(int launch_timeout); |
| 77 ~ChromeFrameAutomationProxyImpl(); |
| 78 class CFMsgDispatcher; |
| 79 scoped_refptr<CFMsgDispatcher> sync_; |
| 80 friend class ProxyFactory; |
| 81 }; |
| 82 |
| 83 // We must create and destroy automation proxy in a thread with a message loop. |
| 84 // Hence thread cannot be a member of the proxy. |
| 85 class ProxyFactory { |
| 86 public: |
| 87 // Callback when chrome process launch is complete and automation handshake |
| 88 // (Hello message) is established. |
| 89 struct DECLSPEC_NOVTABLE LaunchDelegate { |
| 90 virtual void LaunchComplete(ChromeFrameAutomationProxy* proxy, |
| 91 AutomationLaunchResult result) = 0; |
| 92 }; |
| 93 |
| 94 ProxyFactory(); |
| 95 ~ProxyFactory(); |
| 96 // FIXME: we should pass the result as output parameter, not as return value |
| 97 // since, LaunchDelegate can be invoked before this function returns. |
| 98 virtual void* GetAutomationServer(int launch_timeout, |
| 99 const std::wstring& profile_name, |
| 100 // Extra command line argument when launching Chrome |
| 101 const std::wstring& extra_argument, |
| 102 bool perform_version_check, |
| 103 LaunchDelegate* delegate); |
| 104 virtual bool ReleaseAutomationServer(void* server_id); |
| 105 |
| 106 private: |
| 107 struct ProxyCacheEntry { |
| 108 std::wstring profile_name; |
| 109 int ref_count; |
| 110 scoped_ptr<base::Thread> thread; |
| 111 ChromeFrameAutomationProxyImpl* proxy; |
| 112 AutomationLaunchResult launch_result; |
| 113 explicit ProxyCacheEntry(const std::wstring& profile); |
| 114 }; |
| 115 |
| 116 void CreateProxy(ProxyCacheEntry* entry, |
| 117 int launch_timeout, |
| 118 const std::wstring& extra_chrome_arguments, |
| 119 bool perform_version_check, |
| 120 LaunchDelegate* delegate); |
| 121 void DestroyProxy(ProxyCacheEntry* entry); |
| 122 |
| 123 void SendUMAData(ProxyCacheEntry* proxy_entry); |
| 124 |
| 125 typedef StackVector<ProxyCacheEntry*, 4> Vector; |
| 126 Vector proxies_; |
| 127 // Lock if we are going to call GetAutomationServer from more than one thread. |
| 128 Lock lock_; |
| 129 |
| 130 // Used for UMA histogram logging to measure the time for the chrome |
| 131 // automation server to start; |
| 132 base::TimeTicks automation_server_launch_start_time_; |
| 133 |
| 134 // Gathers histograms to be sent to Chrome. |
| 135 ChromeFrameHistogramSnapshots chrome_frame_histograms_; |
| 136 |
| 137 // Interval for sending UMA data |
| 138 int uma_send_interval_; |
| 139 }; |
| 140 |
| 141 // T is expected to be something CWindowImpl derived, or at least to have |
| 142 // PostMessage(UINT, WPARAM) method. Do not forget to CHAIN_MSG_MAP |
| 143 template <class T> class TaskMarshallerThroughWindowsMessages { |
| 144 public: |
| 145 void PostTask(const tracked_objects::Location& from_here, Task* task) { |
| 146 task->SetBirthPlace(from_here); |
| 147 T* this_ptr = static_cast<T*>(this); |
| 148 if (this_ptr->IsWindow()) { |
| 149 this_ptr->PostMessage(MSG_EXECUTE_TASK, reinterpret_cast<WPARAM>(task)); |
| 150 } else { |
| 151 DLOG(INFO) << "Dropping MSG_EXECUTE_TASK message for destroyed window."; |
| 152 } |
| 153 } |
| 154 |
| 155 BEGIN_MSG_MAP(PostMessageMarshaller) |
| 156 MESSAGE_HANDLER(MSG_EXECUTE_TASK, ExecuteTask) |
| 157 END_MSG_MAP() |
| 158 |
| 159 private: |
| 160 enum { MSG_EXECUTE_TASK = WM_APP + 6 }; |
| 161 inline LRESULT ExecuteTask(UINT, WPARAM wparam, LPARAM, |
| 162 BOOL& handled) { // NOLINT |
| 163 Task* task = reinterpret_cast<Task*>(wparam); |
| 164 task->Run(); |
| 165 delete task; |
| 166 return 0; |
| 167 } |
| 168 }; |
| 169 |
| 170 // Handles all automation requests initiated from the chrome frame objects. |
| 171 // These include the chrome tab/chrome frame activex/chrome frame npapi |
| 172 // plugin objects. |
| 173 class ChromeFrameAutomationClient |
| 174 : public CWindowImpl<ChromeFrameAutomationClient>, |
| 175 public TaskMarshallerThroughWindowsMessages<ChromeFrameAutomationClient>, |
| 176 public PluginRequestHandler, |
| 177 public TabProxy::TabProxyDelegate, |
| 178 public ProxyFactory::LaunchDelegate { |
| 179 public: |
| 180 ChromeFrameAutomationClient(); |
| 181 ~ChromeFrameAutomationClient(); |
| 182 |
| 183 // Called from UI thread. |
| 184 virtual bool Initialize(ChromeFrameDelegate* chrome_frame_delegate, |
| 185 int automation_server_launch_timeout, |
| 186 bool perform_version_check, |
| 187 const std::wstring& profile_name, |
| 188 const std::wstring& extra_chrome_arguments, |
| 189 bool incognito); |
| 190 void Uninitialize(); |
| 191 |
| 192 virtual bool InitiateNavigation(const std::string& url); |
| 193 virtual bool NavigateToIndex(int index); |
| 194 bool ForwardMessageFromExternalHost(const std::string& message, |
| 195 const std::string& origin, |
| 196 const std::string& target); |
| 197 bool SetProxySettings(const std::string& json_encoded_proxy_settings); |
| 198 |
| 199 virtual void SetEnableExtensionAutomation(bool enable_automation); |
| 200 |
| 201 void FindInPage(const std::wstring& search_string, |
| 202 FindInPageDirection forward, |
| 203 FindInPageCase match_case, |
| 204 bool find_next); |
| 205 |
| 206 TabProxy* tab() const { return tab_.get(); } |
| 207 |
| 208 BEGIN_MSG_MAP(ChromeFrameAutomationClient) |
| 209 CHAIN_MSG_MAP( |
| 210 TaskMarshallerThroughWindowsMessages<ChromeFrameAutomationClient>) |
| 211 END_MSG_MAP() |
| 212 |
| 213 void set_delegate(ChromeFrameDelegate* d) { |
| 214 chrome_frame_delegate_ = d; |
| 215 } |
| 216 |
| 217 // Resizes the hosted chrome window. This is brokered to the chrome |
| 218 // automation instance as the host browser could be running under low IL, |
| 219 // which would cause the SetWindowPos call to fail. |
| 220 void Resize(int width, int height, int flags); |
| 221 |
| 222 // Sets the passed in window as the parent of the external tab. |
| 223 void SetParentWindow(HWND parent_window); |
| 224 |
| 225 void SendContextMenuCommandToChromeFrame(int selected_command); |
| 226 |
| 227 HWND tab_window() const { |
| 228 return tab_window_; |
| 229 } |
| 230 |
| 231 void ReleaseAutomationServer(); |
| 232 |
| 233 // Returns the version number of plugin dll. |
| 234 std::wstring GetVersion() const; |
| 235 |
| 236 // BitBlts the contents of the chrome window to the print dc. |
| 237 void Print(HDC print_dc, const RECT& print_bounds); |
| 238 |
| 239 // Called in full tab mode and indicates a request to chrome to print |
| 240 // the whole tab. |
| 241 void PrintTab(); |
| 242 |
| 243 // PluginRequestHandler |
| 244 bool AddRequest(PluginUrlRequest* request); |
| 245 void RemoveRequest(PluginUrlRequest* request); |
| 246 virtual bool Send(IPC::Message* msg); |
| 247 |
| 248 // URL request related |
| 249 bool ReadRequest(int request_id, int bytes_to_read); |
| 250 void RemoveRequest(int request_id, int reason, bool abort); |
| 251 PluginUrlRequest* LookupRequest(int request_id) const; |
| 252 bool IsValidRequest(PluginUrlRequest* request) const; |
| 253 void CleanupRequests(); |
| 254 |
| 255 void set_use_chrome_network(bool use_chrome_network) { |
| 256 use_chrome_network_ = use_chrome_network; |
| 257 } |
| 258 bool use_chrome_network() const { |
| 259 return use_chrome_network_; |
| 260 } |
| 261 |
| 262 #ifdef UNIT_TEST |
| 263 void set_proxy_factory(ProxyFactory* factory) { |
| 264 proxy_factory_ = factory; |
| 265 } |
| 266 #endif |
| 267 |
| 268 void set_handle_top_level_requests(bool handle_top_level_requests) { |
| 269 handle_top_level_requests_ = handle_top_level_requests; |
| 270 } |
| 271 |
| 272 // Called if the same instance of the ChromeFrameAutomationClient object |
| 273 // is reused. |
| 274 bool Reinitialize(ChromeFrameDelegate* chrome_frame_delegate); |
| 275 |
| 276 // Attaches an existing external tab to this automation client instance. |
| 277 void AttachExternalTab(intptr_t external_tab_cookie); |
| 278 |
| 279 protected: |
| 280 // ChromeFrameAutomationProxy::LaunchDelegate implementation. |
| 281 virtual void LaunchComplete(ChromeFrameAutomationProxy* proxy, |
| 282 AutomationLaunchResult result); |
| 283 // TabProxyDelegate implementation |
| 284 virtual void OnMessageReceived(TabProxy* tab, const IPC::Message& msg); |
| 285 |
| 286 void CreateExternalTab(); |
| 287 void CreateExternalTabComplete(HWND chrome_window, HWND tab_window, |
| 288 int tab_handle); |
| 289 // Called in UI thread. Here we fire event to the client notifying for |
| 290 // the result of Initialize() method call. |
| 291 void InitializeComplete(AutomationLaunchResult result); |
| 292 |
| 293 private: |
| 294 typedef std::map<int, scoped_refptr<PluginUrlRequest> > RequestMap; |
| 295 |
| 296 // Usage: From bkgnd thread invoke: |
| 297 // CallDelegate(FROM_HERE, NewRunnableMethod(chrome_frame_delegate_, |
| 298 // ChromeFrameDelegate::Something, |
| 299 // param1, |
| 300 // param2)); |
| 301 void CallDelegate(const tracked_objects::Location& from_here, |
| 302 Task* delegate_task); |
| 303 // The workhorse method called in main/GUI thread which is going to |
| 304 // execute ChromeFrameDelegate method encapsulated in delegate_task. |
| 305 void CallDelegateImpl(Task* delegate_task); |
| 306 |
| 307 HWND chrome_window() const { return chrome_window_; } |
| 308 void BeginNavigate(const GURL& url); |
| 309 void BeginNavigateCompleted(AutomationMsg_NavigationResponseValues result); |
| 310 |
| 311 // Helpers |
| 312 void ReportNavigationError(AutomationMsg_NavigationResponseValues error_code, |
| 313 const std::string& url); |
| 314 |
| 315 bool is_initialized() const { |
| 316 return init_state_ == INITIALIZED; |
| 317 } |
| 318 |
| 319 bool incognito_; |
| 320 HWND parent_window_; |
| 321 PlatformThreadId ui_thread_id_; |
| 322 |
| 323 void* automation_server_id_; |
| 324 ChromeFrameAutomationProxy* automation_server_; |
| 325 HWND chrome_window_; |
| 326 scoped_refptr<TabProxy> tab_; |
| 327 ChromeFrameDelegate* chrome_frame_delegate_; |
| 328 GURL url_; |
| 329 |
| 330 // Handle to the underlying chrome window. This is a child of the external |
| 331 // tab window. |
| 332 HWND tab_window_; |
| 333 |
| 334 // Keeps track of the version of Chrome we're talking to. |
| 335 std::string automation_server_version_; |
| 336 |
| 337 // Map of outstanding requests |
| 338 RequestMap request_map_; |
| 339 |
| 340 typedef enum InitializationState { |
| 341 UNINITIALIZED = 0, |
| 342 INITIALIZING, |
| 343 INITIALIZED, |
| 344 UNINITIALIZING, |
| 345 }; |
| 346 |
| 347 InitializationState init_state_; |
| 348 bool use_chrome_network_; |
| 349 bool handle_top_level_requests_; |
| 350 ProxyFactory* proxy_factory_; |
| 351 int tab_handle_; |
| 352 // Only used if we attach to an existing tab. |
| 353 intptr_t external_tab_cookie_; |
| 354 }; |
| 355 |
| 356 #endif // CHROME_FRAME_CHROME_FRAME_AUTOMATION_H_ |
OLD | NEW |