1 // Copyright (c) 2011 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_CFPROXY_PRIVATE_H_
6 #define CHROME_FRAME_CFPROXY_PRIVATE_H_
14 #include "chrome_frame/cfproxy.h"
15 #include "base/threading/thread.h"
16 // Since we can't forward declare IPC::Message::Sender or IPC::Channel::Listener
17 #include "ipc/ipc_message.h"
18 #include "ipc/ipc_channel.h"
20 typedef std::map
<int, ChromeProxyDelegate
*> TabsMap
;
22 // This is the functions needed by CFProxy implementation.
23 // Extracted in separate class so we can mock it.
26 virtual IPC::Message::Sender
* CreateChannel(const std::string
& id
,
27 IPC::Channel::Listener
* l
);
28 virtual void CloseChannel(IPC::Message::Sender
* s
);
29 virtual bool LaunchApp(const std::wstring
& cmd_line
);
32 // Holds a queue of sent synchronous IPC messages. Knows how to dispatch
36 explicit SyncMsgSender(TabsMap
* tab2delegate
);
37 void QueueSyncMessage(const IPC::SyncMessage
* msg
,
38 ChromeProxyDelegate
* delegate
, SyncMessageContext
* ctx
);
39 bool OnReplyReceived(const IPC::Message
* reply_msg
);
40 void OnChannelClosed();
41 void Cancel(ChromeProxyDelegate
* delegate
);
43 // sync_message_id -> (message_type, delegate, context)
44 struct SingleSentMessage
{
45 SingleSentMessage() : type_(0), ctx_(NULL
), delegate_(NULL
) {}
46 SingleSentMessage(uint32 type
,
47 ChromeProxyDelegate
* delegate
,
48 SyncMessageContext
* ctx
)
49 : type_(type
), ctx_(ctx
), delegate_(delegate
) {}
50 ~SingleSentMessage() { delete ctx_
; }
52 SyncMessageContext
* ctx_
;
53 ChromeProxyDelegate
* delegate_
;
56 SingleSentMessage
* RemoveMessage(int id
);
57 typedef std::map
<int, SingleSentMessage
*> SentMessages
;
58 SentMessages messages_
;
59 base::Lock messages_lock_
;
60 TabsMap
* tab2delegate_
;
63 // Converts method call to an IPC message and then send it over Message::Sender
64 class Interface2IPCMessage
: public ChromeProxy
{
66 Interface2IPCMessage() {}
69 virtual void RemoveBrowsingData(int mask
);
70 virtual void SetProxyConfig(const std::string
& json_encoded_proxy_cfg
);
72 virtual void Tab_PostMessage(int tab
, const std::string
& message
,
73 const std::string
& origin
, const std::string
& target
);
74 virtual void Tab_Reload(int tab
);
75 virtual void Tab_Stop(int tab
);
76 virtual void Tab_SaveAs(int tab
);
77 virtual void Tab_Print(int tab
);
78 virtual void Tab_Cut(int tab
);
79 virtual void Tab_Copy(int tab
);
80 virtual void Tab_Paste(int tab
);
81 virtual void Tab_SelectAll(int tab
);
82 virtual void Tab_MenuCommand(int tab
, int selected_command
);
83 virtual void Tab_Zoom(int tab
, content::PageZoom zoom_level
);
84 virtual void Tab_FontSize(int tab
, enum AutomationPageFontSize font_size
);
85 virtual void Tab_SetInitialFocus(int tab
, bool reverse
,
86 bool restore_focus_to_view
);
87 virtual void Tab_SetParentWindow(int tab
);
88 virtual void Tab_Resize(int tab
);
89 virtual void Tab_ProcessAccelerator(int tab
, const MSG
& msg
);
92 virtual void Tab_OnHostMoved(int tab
);
94 ~Interface2IPCMessage() {}
96 IPC::Message::Sender
* sender_
;
99 // Simple class to keep a list of pointers to ChromeProxyDelegate for a
100 // specific proxy as well as mapping between tab_id -> ChromeProxyDelegate.
101 class DelegateHolder
{
106 void AddDelegate(ChromeProxyDelegate
* p
);
107 void RemoveDelegate(ChromeProxyDelegate
* p
);
109 ChromeProxyDelegate
* Tab2Delegate(int tab_handle
);
110 TabsMap tab2delegate_
;
111 typedef std::set
<ChromeProxyDelegate
*> DelegateList
;
112 DelegateList delegate_list_
;
115 // ChromeFrame Automation Proxy implementation.
116 class CFProxy
: public Interface2IPCMessage
,
117 public IPC::Channel::Listener
,
118 public DelegateHolder
{
120 explicit CFProxy(CFProxyTraits
* api
);
124 virtual void Init(const ProxyParams
& params
);
125 virtual int AddDelegate(ChromeProxyDelegate
* p
);
126 virtual int RemoveDelegate(ChromeProxyDelegate
* p
);
128 // Executed in IPC thread.
129 void AddDelegateOnIoThread(ChromeProxyDelegate
* p
);
130 void RemoveDelegateOnIoThread(ChromeProxyDelegate
* p
);
131 // Initialization that has to be mede in IPC thread.
132 void InitInIoThread(const ProxyParams
& params
);
133 // Cleanup that has to be made in IPC thread.
134 void CleanupOnIoThread();
135 // IPC connection was not established in timely manner
136 void LaunchTimeOut();
137 // Close channel, inform delegates.
138 void OnPeerLost(ChromeProxyDelegate::DisconnectReason reason
);
139 // Queues message to be send in IPC thread.
140 void SendIpcMessage(IPC::Message
* m
);
141 // Same but in IPC thread.
142 void SendIpcMessageOnIoThread(IPC::Message
* m
);
144 //////////////////////////////////////////////////////////////////////////
146 virtual void Tab_Find(int tab
, const string16
& search_string
,
147 FindInPageDirection forward
, FindInPageCase match_case
, bool find_next
);
148 virtual void Tab_OverrideEncoding(int tab
, const char* encoding
);
149 virtual void Tab_Navigate(int tab
, const GURL
& url
, const GURL
& referrer
);
150 virtual void CreateTab(ChromeProxyDelegate
* delegate
,
151 const ExternalTabSettings
& p
);
152 virtual void ConnectTab(ChromeProxyDelegate
* delegate
, HWND hwnd
,
154 virtual void BlockTab(uint64 cookie
);
155 virtual void Tab_RunUnloadHandlers(int tab
);
157 //////////////////////////////////////////////////////////////////////////
158 // IPC::Channel::Listener implementation.
159 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
160 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
161 virtual void OnChannelError() OVERRIDE
;
163 bool CalledOnIpcThread() const {
164 return base::PlatformThread::CurrentId() == ipc_thread_
.thread_id();
167 base::Thread ipc_thread_
;
168 SyncMsgSender sync_dispatcher_
;
169 IPC::Message::Sender
* ipc_sender_
;
175 // Support functions.
176 std::string
GenerateChannelId();
177 std::wstring
BuildCmdLine(const std::string
& channel_id
,
178 const FilePath
& profile_path
,
179 const std::wstring
& extra_args
);
180 #endif // CHROME_FRAME_CFPROXY_PRIVATE_H_