1 // Copyright (c) 2012 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.
5 #ifndef CHROME_FRAME_CHROME_FRAME_AUTOMATION_H_
6 #define CHROME_FRAME_CHROME_FRAME_AUTOMATION_H_
14 #include "base/containers/stack_container.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_handle.h"
17 #include "base/synchronization/lock.h"
18 #include "base/threading/thread.h"
19 #include "base/timer.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/plugin_url_request.h"
24 #include "chrome_frame/sync_msg_reply_dispatcher.h"
25 #include "content/public/common/page_zoom.h"
27 // By a convoluated route, this timeout also winds up being the sync automation
28 // message timeout. See the ChromeFrameAutomationProxyImpl ctor and the
29 // AutomationProxy ctor for details.
30 const unsigned long kCommandExecutionTimeout
= 60000; // NOLINT, 60 seconds
33 class NavigationConstraints
;
34 enum AutomationPageFontSize
;
36 struct DECLSPEC_NOVTABLE ChromeFrameAutomationProxy
{ // NOLINT
37 virtual bool Send(IPC::Message
* msg
) = 0;
39 virtual void SendAsAsync(
40 IPC::SyncMessage
* msg
,
41 SyncMessageReplyDispatcher::SyncMessageCallContext
* context
,
43 virtual void CancelAsync(void* key
) = 0;
44 virtual scoped_refptr
<TabProxy
> CreateTabProxy(int handle
) = 0;
45 virtual void ReleaseTabProxy(AutomationHandle handle
) = 0;
46 virtual std::string
server_version() = 0;
48 virtual void SendProxyConfig(const std::string
&) = 0;
50 virtual ~ChromeFrameAutomationProxy() {}
53 // Forward declarations.
56 // We extend the AutomationProxy class to handle our custom
58 class ChromeFrameAutomationProxyImpl
59 : public ChromeFrameAutomationProxy
,
60 // We have to derive from automationproxy since we want access to some
61 // members (tracker_ & channel_) - simple aggregation wont work;
62 // .. and non-public inheritance is verboten.
63 public AutomationProxy
{
65 ~ChromeFrameAutomationProxyImpl();
66 virtual void SendAsAsync(
67 IPC::SyncMessage
* msg
,
68 SyncMessageReplyDispatcher::SyncMessageCallContext
* context
,
71 // Called on the worker thread.
72 virtual void OnChannelError();
74 virtual void CancelAsync(void* key
);
76 virtual scoped_refptr
<TabProxy
> CreateTabProxy(int handle
);
77 virtual void ReleaseTabProxy(AutomationHandle handle
);
79 virtual std::string
server_version() {
80 return AutomationProxy::server_version();
83 virtual bool Send(IPC::Message
* msg
) {
84 return AutomationProxy::Send(msg
);
87 virtual void SendProxyConfig(const std::string
& p
) {
88 AutomationProxy::SendProxyConfig(p
);
92 friend class AutomationProxyCacheEntry
;
93 ChromeFrameAutomationProxyImpl(AutomationProxyCacheEntry
* entry
,
94 std::string channel_id
,
95 base::TimeDelta launch_timeout
);
97 class CFMsgDispatcher
;
98 class TabProxyNotificationMessageFilter
;
100 scoped_refptr
<CFMsgDispatcher
> sync_
;
101 scoped_refptr
<TabProxyNotificationMessageFilter
> message_filter_
;
102 AutomationProxyCacheEntry
* proxy_entry_
;
105 // This class contains information used for launching chrome.
106 class ChromeFrameLaunchParams
: // NOLINT
107 public base::RefCountedThreadSafe
<ChromeFrameLaunchParams
> {
109 ChromeFrameLaunchParams(const GURL
& url
, const GURL
& referrer
,
110 const base::FilePath
& profile_path
,
111 const std::wstring
& profile_name
,
112 const std::wstring
& language
,
113 bool incognito
, bool widget_mode
,
114 bool route_all_top_level_navigations
)
115 : launch_timeout_(kCommandExecutionTimeout
), url_(url
),
116 referrer_(referrer
), profile_path_(profile_path
),
117 profile_name_(profile_name
), language_(language
),
118 version_check_(true), incognito_mode_(incognito
),
119 is_widget_mode_(widget_mode
),
120 route_all_top_level_navigations_(route_all_top_level_navigations
) {
123 ~ChromeFrameLaunchParams() {
126 void set_launch_timeout(int timeout
) {
127 launch_timeout_
= timeout
;
130 int launch_timeout() const {
131 return launch_timeout_
;
134 const GURL
& url() const {
138 void set_url(const GURL
& url
) {
142 const GURL
& referrer() const {
146 void set_referrer(const GURL
& referrer
) {
147 referrer_
= referrer
;
150 const base::FilePath
& profile_path() const {
151 return profile_path_
;
154 const std::wstring
& profile_name() const {
155 return profile_name_
;
158 const std::wstring
& language() const {
162 bool version_check() const {
163 return version_check_
;
166 void set_version_check(bool check
) {
167 version_check_
= check
;
170 bool incognito() const {
171 return incognito_mode_
;
174 bool widget_mode() const {
175 return is_widget_mode_
;
178 void set_route_all_top_level_navigations(
179 bool route_all_top_level_navigations
) {
180 route_all_top_level_navigations_
= route_all_top_level_navigations
;
183 bool route_all_top_level_navigations() const {
184 return route_all_top_level_navigations_
;
191 base::FilePath profile_path_
;
192 std::wstring profile_name_
;
193 std::wstring language_
;
195 bool incognito_mode_
;
196 bool is_widget_mode_
;
197 bool route_all_top_level_navigations_
;
200 DISALLOW_COPY_AND_ASSIGN(ChromeFrameLaunchParams
);
203 // Callback when chrome process launch is complete and automation handshake
204 // (Hello message) is established. All methods are invoked on the automation
205 // proxy's worker thread.
206 struct DECLSPEC_NOVTABLE LaunchDelegate
{ // NOLINT
207 virtual void LaunchComplete(ChromeFrameAutomationProxy
* proxy
,
208 AutomationLaunchResult result
) = 0;
209 virtual void AutomationServerDied() = 0;
212 // Manages a cached ChromeFrameAutomationProxyImpl entry and holds
213 // reference-less pointers to LaunchDelegate(s) to be notified in case
214 // of automation server process changes.
215 class AutomationProxyCacheEntry
216 : public base::RefCounted
<AutomationProxyCacheEntry
> {
218 AutomationProxyCacheEntry(ChromeFrameLaunchParams
* params
,
219 LaunchDelegate
* delegate
);
221 ~AutomationProxyCacheEntry();
223 void AddDelegate(LaunchDelegate
* delegate
);
224 void RemoveDelegate(LaunchDelegate
* delegate
, base::WaitableEvent
* done
,
225 bool* was_last_delegate
);
227 DWORD
WaitForThread(DWORD timeout
) { // NOLINT
228 DCHECK(thread_
.get());
229 return ::WaitForSingleObject(thread_
->thread_handle().platform_handle(),
233 bool IsSameProfile(const std::wstring
& name
) const {
234 return lstrcmpiW(name
.c_str(), profile_name
.c_str()) == 0;
237 base::Thread
* thread() const {
238 return thread_
.get();
241 base::MessageLoop
* message_loop() const {
242 return thread_
->message_loop();
245 bool IsSameThread(base::PlatformThreadId id
) const {
246 return thread_
->thread_id() == id
;
249 ChromeFrameAutomationProxyImpl
* proxy() const {
250 DCHECK(IsSameThread(base::PlatformThread::CurrentId()));
254 // Called by the proxy when the automation server has unexpectedly gone away.
255 void OnChannelError();
258 void CreateProxy(ChromeFrameLaunchParams
* params
,
259 LaunchDelegate
* delegate
);
262 std::wstring profile_name
;
263 scoped_ptr
<base::Thread
> thread_
;
264 scoped_ptr
<ChromeFrameAutomationProxyImpl
> proxy_
;
265 AutomationLaunchResult launch_result_
;
266 typedef std::vector
<LaunchDelegate
*> LaunchDelegates
;
267 LaunchDelegates launch_delegates_
;
268 // Used for UMA histogram logging to measure the time for the chrome
269 // automation server to start;
270 base::TimeTicks automation_server_launch_start_time_
;
273 // We must create and destroy automation proxy in a thread with a message loop.
274 // Hence thread cannot be a member of the proxy.
278 virtual ~ProxyFactory();
280 // Fetches or creates a new automation server instance.
281 // delegate may be NULL. If non-null, a pointer to the delegate will
282 // be stored for the lifetime of the automation process or until
283 // ReleaseAutomationServer is called.
284 virtual void GetAutomationServer(LaunchDelegate
* delegate
,
285 ChromeFrameLaunchParams
* params
,
286 void** automation_server_id
);
287 virtual bool ReleaseAutomationServer(void* server_id
,
288 LaunchDelegate
* delegate
);
291 typedef base::StackVector
<scoped_refptr
<AutomationProxyCacheEntry
>, 4> Vector
;
293 // Lock if we are going to call GetAutomationServer from more than one thread.
297 // Handles all automation requests initiated from the chrome frame objects.
298 // These include the chrome tab/chrome frame activex plugin objects.
299 class ChromeFrameAutomationClient
300 : public CWindowImpl
<ChromeFrameAutomationClient
>,
301 public TaskMarshallerThroughWindowsMessages
<ChromeFrameAutomationClient
>,
302 public base::RefCountedThreadSafe
<ChromeFrameAutomationClient
>,
303 public PluginUrlRequestDelegate
,
304 public TabProxy::TabProxyDelegate
,
305 public LaunchDelegate
{
307 ChromeFrameAutomationClient();
308 ~ChromeFrameAutomationClient();
310 // Called from UI thread.
311 virtual bool Initialize(ChromeFrameDelegate
* chrome_frame_delegate
,
312 ChromeFrameLaunchParams
* chrome_launch_params
);
314 void NotifyAndUninitialize();
316 virtual bool InitiateNavigation(
317 const std::string
& url
,
318 const std::string
& referrer
,
319 NavigationConstraints
* navigation_constraints
);
321 virtual bool NavigateToIndex(int index
);
322 bool ForwardMessageFromExternalHost(const std::string
& message
,
323 const std::string
& origin
,
324 const std::string
& target
);
325 bool SetProxySettings(const std::string
& json_encoded_proxy_settings
);
327 void FindInPage(const std::wstring
& search_string
,
328 FindInPageDirection forward
,
329 FindInPageCase match_case
,
332 virtual void OnChromeFrameHostMoved();
334 TabProxy
* tab() const { return tab_
.get(); }
336 BEGIN_MSG_MAP(ChromeFrameAutomationClient
)
338 TaskMarshallerThroughWindowsMessages
<ChromeFrameAutomationClient
>)
341 // Resizes the hosted chrome window. This is brokered to the chrome
342 // automation instance as the host browser could be running under low IL,
343 // which would cause the SetWindowPos call to fail.
344 void Resize(int width
, int height
, int flags
);
346 // Sets the passed in window as the parent of the external tab.
347 void SetParentWindow(HWND parent_window
);
349 void SendContextMenuCommandToChromeFrame(int selected_command
);
351 HWND
tab_window() const {
355 void ReleaseAutomationServer();
357 // Returns the version number of plugin dll.
358 std::wstring
GetVersion() const;
360 // BitBlts the contents of the chrome window to the print dc.
361 void Print(HDC print_dc
, const RECT
& print_bounds
);
363 // Called in full tab mode and indicates a request to chrome to print
367 void set_use_chrome_network(bool use_chrome_network
) {
368 use_chrome_network_
= use_chrome_network
;
371 bool use_chrome_network() const {
372 return use_chrome_network_
;
376 void set_proxy_factory(ProxyFactory
* factory
) {
377 proxy_factory_
= factory
;
381 void set_handle_top_level_requests(bool handle_top_level_requests
) {
382 handle_top_level_requests_
= handle_top_level_requests
;
385 // Url request manager set up.
386 void SetUrlFetcher(PluginUrlRequestManager
* url_fetcher
);
388 // Attaches an existing external tab to this automation client instance.
389 void AttachExternalTab(uint64 external_tab_cookie
);
390 void BlockExternalTab(uint64 cookie
);
392 void SetPageFontSize(enum AutomationPageFontSize
);
394 // For IDeleteBrowsingHistorySupport
395 void RemoveBrowsingData(int remove_mask
);
397 // Sets the current zoom level on the tab.
398 void SetZoomLevel(content::PageZoom zoom_level
);
400 // Fires before unload and unload handlers on the page if any. Allows the
401 // the website to put up a confirmation dialog on unload.
402 void OnUnload(bool* should_unload
);
405 // ChromeFrameAutomationProxy::LaunchDelegate implementation.
406 virtual void LaunchComplete(ChromeFrameAutomationProxy
* proxy
,
407 AutomationLaunchResult result
);
408 virtual void AutomationServerDied();
410 // TabProxyDelegate implementation
411 virtual bool OnMessageReceived(TabProxy
* tab
, const IPC::Message
& msg
);
412 virtual void OnChannelError(TabProxy
* tab
);
414 void CreateExternalTab();
415 AutomationLaunchResult
CreateExternalTabComplete(HWND chrome_window
,
419 // Called in UI thread. Here we fire event to the client notifying for
420 // the result of Initialize() method call.
421 void InitializeComplete(AutomationLaunchResult result
);
423 virtual void OnFinalMessage(HWND wnd
) {
427 scoped_refptr
<ChromeFrameLaunchParams
> launch_params() {
428 return chrome_launch_params_
;
432 void OnMessageReceivedUIThread(const IPC::Message
& msg
);
433 void OnChannelErrorUIThread();
435 HWND
chrome_window() const { return chrome_window_
; }
436 void BeginNavigate();
437 void BeginNavigateCompleted(AutomationMsg_NavigationResponseValues result
);
440 void ReportNavigationError(AutomationMsg_NavigationResponseValues error_code
,
441 const std::string
& url
);
443 bool ProcessUrlRequestMessage(TabProxy
* tab
, const IPC::Message
& msg
,
446 // PluginUrlRequestDelegate implementation. Simply adds tab's handle
447 // as parameter and forwards to Chrome via IPC.
448 virtual void OnResponseStarted(
449 int request_id
, const char* mime_type
, const char* headers
, int size
,
450 base::Time last_modified
, const std::string
& redirect_url
,
451 int redirect_status
, const net::HostPortPair
& socket_address
,
453 virtual void OnReadComplete(int request_id
, const std::string
& data
);
454 virtual void OnResponseEnd(int request_id
,
455 const net::URLRequestStatus
& status
);
456 virtual void OnCookiesRetrieved(bool success
, const GURL
& url
,
457 const std::string
& cookie_string
, int cookie_id
);
459 bool is_initialized() const {
460 return init_state_
== INITIALIZED
;
464 base::PlatformThreadId ui_thread_id_
;
466 void* automation_server_id_
;
467 ChromeFrameAutomationProxy
* automation_server_
;
470 scoped_refptr
<TabProxy
> tab_
;
471 ChromeFrameDelegate
* chrome_frame_delegate_
;
473 // Handle to the underlying chrome window. This is a child of the external
477 // Keeps track of the version of Chrome we're talking to.
478 std::string automation_server_version_
;
480 typedef enum InitializationState
{
487 InitializationState init_state_
;
488 bool use_chrome_network_
;
489 bool handle_top_level_requests_
;
490 ProxyFactory
* proxy_factory_
;
492 // The SessionId used by Chrome as the id in the Javascript Tab object.
494 // Only used if we attach to an existing tab.
495 uint64 external_tab_cookie_
;
497 // Set to true if we received a navigation request prior to the automation
498 // server being initialized.
499 bool navigate_after_initialization_
;
501 scoped_refptr
<ChromeFrameLaunchParams
> chrome_launch_params_
;
503 // Cache security manager for URL zone checking
504 base::win::ScopedComPtr
<IInternetSecurityManager
> security_manager_
;
506 // When host network stack is used, this object is in charge of
507 // handling network requests.
508 PluginUrlRequestManager
* url_fetcher_
;
509 PluginUrlRequestManager::ThreadSafeFlags url_fetcher_flags_
;
511 // set to true if the host needs to get notified of all top level navigations
512 // in this page. This typically applies to hosts which would render the new
513 // page without chrome frame. Defaults to false.
514 bool route_all_top_level_navigations_
;
516 friend class BeginNavigateContext
;
517 friend class CreateExternalTabContext
;
520 #endif // CHROME_FRAME_CHROME_FRAME_AUTOMATION_H_