1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome_frame/external_tab.h"
#include "base/lazy_instance.h"
#include "base/tracked.h"
#include "base/task.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/common/automation_messages.h"
#include "chrome_frame/chrome_frame_delegate.h"
#include "chrome_frame/utils.h"
DISABLE_RUNNABLE_METHOD_REFCOUNT(ExternalTabProxy);
DISABLE_RUNNABLE_METHOD_REFCOUNT(UIDelegate);
namespace {
static base::LazyInstance<ChromeProxyFactory> g_proxy_factory(
base::LINKER_INITIALIZED);
struct UserDataHolder : public SyncMessageContext {
explicit UserDataHolder(void* p) : data(p) {}
void* data;
};
}
ExternalTabProxy::ExternalTabProxy() : state_(NONE), tab_(0), tab_wnd_(NULL),
chrome_wnd_(NULL), proxy_factory_(g_proxy_factory.Pointer()), proxy_(NULL),
ui_delegate_(NULL) {
}
ExternalTabProxy::~ExternalTabProxy() {
Destroy();
}
bool ExternalTabProxy::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ExternalTabProxy, message)
IPC_MESSAGE_HANDLER(AutomationMsg_NavigationStateChanged,
OnNavigationStateChanged)
IPC_MESSAGE_HANDLER(AutomationMsg_UpdateTargetUrl, OnUpdateTargetUrl)
IPC_MESSAGE_HANDLER(AutomationMsg_HandleAccelerator, OnHandleAccelerator)
IPC_MESSAGE_HANDLER(AutomationMsg_TabbedOut, OnTabbedOut)
IPC_MESSAGE_HANDLER(AutomationMsg_OpenURL, OnOpenURL)
IPC_MESSAGE_HANDLER(AutomationMsg_NavigationFailed, OnNavigationFailed)
IPC_MESSAGE_HANDLER(AutomationMsg_DidNavigate, OnDidNavigate)
IPC_MESSAGE_HANDLER(AutomationMsg_TabLoaded, OnTabLoaded)
IPC_MESSAGE_HANDLER(AutomationMsg_MoveWindow, OnMoveWindow)
IPC_MESSAGE_HANDLER(AutomationMsg_ForwardMessageToExternalHost,
OnMessageToHost)
IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuToExternalHost,
OnHandleContextMenu)
IPC_MESSAGE_HANDLER(AutomationMsg_RequestStart, OnNetwork_Start)
IPC_MESSAGE_HANDLER(AutomationMsg_RequestRead, OnNetwork_Read)
IPC_MESSAGE_HANDLER(AutomationMsg_RequestEnd, OnNetwork_End)
IPC_MESSAGE_HANDLER(AutomationMsg_DownloadRequestInHost,
OnNetwork_DownloadInHost)
IPC_MESSAGE_HANDLER(AutomationMsg_GetCookiesFromHost, OnGetCookies)
IPC_MESSAGE_HANDLER(AutomationMsg_SetCookieAsync, OnSetCookie)
IPC_MESSAGE_HANDLER(AutomationMsg_AttachExternalTab, OnAttachTab)
IPC_MESSAGE_HANDLER(AutomationMsg_RequestGoToHistoryEntryOffset,
OnGoToHistoryOffset)
IPC_MESSAGE_HANDLER(AutomationMsg_CloseExternalTab, OnTabClosed)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void ExternalTabProxy::Init() {
if (m_hWnd == NULL) {
// Create a window on the UI thread for marshaling messages back and forth
// from the IPC thread. This window cannot be a message only window as the
// external chrome tab window initially is created as a child of this window
CWindowImpl<ExternalTabProxy>::Create(GetDesktopWindow(), NULL, NULL,
WS_CHILDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_TOOLWINDOW);
DCHECK(m_hWnd != NULL);
ui_.SetWindow(m_hWnd, WM_APP + 6);
}
}
void ExternalTabProxy::Destroy() {
DCHECK(NULL == done_.get());
// TODO(stoyan): Should we release proxy first and then destroy the window
// (parent of the chrome window) or the other way around?
if (state_ != NONE) {
done_.reset(new base::WaitableEvent(true, false));
proxy_factory_->ReleaseProxy(this, tab_params_.proxy_params.profile);
done_->Wait();
done_.reset(NULL);
state_ = NONE;
proxy_ = NULL;
tab_ = 0;
CWindowImpl<ExternalTabProxy>::DestroyWindow();
tab_wnd_ = NULL;
chrome_wnd_ = NULL;
// We shall tell the TaskMarshaller to delete queued tasks.
// ui_.DeleteAll();
}
}
void ExternalTabProxy::CreateTab(const CreateTabParams& create_params,
UIDelegate* delegate) {
DCHECK(ui_delegate_ == NULL);
DCHECK_EQ(NONE, state_);
// Create host window if needed.
Init();
ui_delegate_ = delegate;
// TODO(stoyan): Shall we check the CanNavigate(create_params.url)?
tab_params_ = create_params;
state_ = INIT_IN_PROGRESS;
proxy_factory_->GetProxy(this, create_params.proxy_params);
}
void ExternalTabProxy::Connected(ChromeProxy* proxy) {
// in ipc thread
ui_.PostTask(FROM_HERE, NewRunnableMethod(this,
&ExternalTabProxy::UiConnected, proxy));
}
void ExternalTabProxy::UiConnected(ChromeProxy* proxy) {
proxy_ = proxy;
ExternalTabSettings settings;
settings.parent = m_hWnd;
settings.style = WS_CHILD;
settings.is_incognito = tab_params_.is_incognito;
// TODO(stoyan): FIX this.
settings.load_requests_via_automation = true;
// TODO(stoyan): FIX this.
settings.handle_top_level_requests = true;
settings.initial_url = tab_params_.url;
settings.referrer = tab_params_.referrer;
// Infobars are disabled in widget mode.
settings.infobars_enabled = !tab_params_.is_widget_mode;
// TODO(stoyan): FIX this.
settings.route_all_top_level_navigations = false;
state_ = CREATE_TAB_IN_PROGRESS;
proxy->CreateTab(this, settings);
}
void ExternalTabProxy::Disconnected() {
// in ipc thread
DCHECK(done_.get() != NULL);
done_->Signal();
}
void ExternalTabProxy::PeerLost(ChromeProxy* proxy, DisconnectReason reason) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(this, &ExternalTabProxy::UiPeerLost,
proxy, reason));
}
void ExternalTabProxy::UiPeerLost(ChromeProxy* proxy, DisconnectReason reason) {
// TODO(stoyan):
}
void ExternalTabProxy::Navigate(const std::string& url,
const std::string& referrer,
NavigationConstraints* navigation_constraints) {
// in ui thread
// Catch invalid URLs early. Can we allow this navigation to happen?
GURL parsed_url(url);
if (!CanNavigate(parsed_url, navigation_constraints)) {
DLOG(ERROR) << __FUNCTION__ << " Not allowing navigation to: " << url;
return;
}
GURL parsed_referrer(referrer);
// If we are still establishing channel, simply replace the params
if (state_ == INIT_IN_PROGRESS) {
tab_params_.url = parsed_url;
tab_params_.referrer = parsed_referrer;
}
// Ah! Too late. Wait to get tab handle and then navigate.
if (state_ == CREATE_TAB_IN_PROGRESS) {
pending_navigation_.Set(parsed_url, parsed_referrer);
}
if (state_ == READY) {
proxy_->Tab_Navigate(tab_, parsed_url, parsed_referrer);
}
}
void ExternalTabProxy::ConnectToExternalTab(uint64 external_tab_cookie) {
proxy_->ConnectTab(this, m_hWnd, external_tab_cookie);
}
void ExternalTabProxy::BlockExternalTab(uint64 cookie) {
proxy_->BlockTab(cookie);
}
void ExternalTabProxy::SetZoomLevel(PageZoom::Function zoom_level) {
proxy_->Tab_Zoom(tab_, zoom_level);
}
void ExternalTabProxy::NavigateToIndex(int index) {
CHECK(0);
}
void ExternalTabProxy::ForwardMessageFromExternalHost(
const std::string& message, const std::string& origin,
const std::string& target) {
proxy_->Tab_PostMessage(tab_, message, origin, target);
}
void ExternalTabProxy::ChromeFrameHostMoved() {
proxy_->Tab_OnHostMoved(tab_);
}
//////////////////////////////////////////////////////////////////////////
void ExternalTabProxy::UiCompleted_CreateTab(bool success, HWND chrome_window,
HWND tab_window, int tab_handle,
int session_id) {
if (success) {
state_ = READY;
tab_ = tab_handle;
tab_wnd_ = tab_window;
chrome_wnd_ = chrome_window;
// If a navigation request came while tab creation was in progress -
// go ahead and navigate.
if (pending_navigation_.url.is_valid())
proxy_->Tab_Navigate(tab_, pending_navigation_.url,
pending_navigation_.referrer);
}
}
void ExternalTabProxy::Completed_CreateTab(bool success, HWND chrome_wnd,
HWND tab_window, int tab_handle,
int session_id) {
// in ipc_thread.
ui_.PostTask(FROM_HERE, NewRunnableMethod(this,
&ExternalTabProxy::UiCompleted_CreateTab,
success, chrome_wnd, tab_window, tab_handle, session_id));
}
void ExternalTabProxy::Completed_ConnectToTab(
bool success, HWND chrome_window, HWND tab_window, int tab_handle,
int session_id) {
CHECK(0);
}
void ExternalTabProxy::Completed_Navigate(
bool success, enum AutomationMsg_NavigationResponseValues res) {
// ipc_thread;
CHECK(0);
}
void ExternalTabProxy::OnNavigationStateChanged(
int flags, const NavigationInfo& nav_info) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnNavigationStateChanged, flags, nav_info));
}
void ExternalTabProxy::OnUpdateTargetUrl(const std::wstring& url) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnUpdateTargetUrl, url));
}
void ExternalTabProxy::OnTabLoaded(const GURL& url) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnLoad, url));
}
void ExternalTabProxy::OnMoveWindow(const gfx::Rect& pos) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnMoveWindow, pos));
}
void ExternalTabProxy::OnMessageToHost(const std::string& message,
const std::string& origin,
const std::string& target) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnMessageFromChromeFrame, message, origin, target));
}
void ExternalTabProxy::OnHandleAccelerator(const MSG& accel_message) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnHandleAccelerator, accel_message));
}
void ExternalTabProxy::OnHandleContextMenu(
HANDLE menu_handle,
int align_flags,
const MiniContextMenuParams& params) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnHandleContextMenu, menu_handle, align_flags, params));
}
void ExternalTabProxy::OnTabbedOut(bool reverse) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnTabbedOut, reverse));
}
void ExternalTabProxy::OnGoToHistoryOffset(int offset) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnGoToHistoryOffset, offset));
}
void ExternalTabProxy::OnOpenURL(const GURL& url_to_open, const GURL& referrer,
int open_disposition) {
ui_.PostTask(FROM_HERE, NewRunnableMethod(ui_delegate_,
&UIDelegate::OnOpenURL, url_to_open, referrer, open_disposition));
}
void ExternalTabProxy::OnNavigationFailed(int error_code, const GURL& gurl) {
// TODO(stoyan):
}
void ExternalTabProxy::OnDidNavigate(const NavigationInfo& navigation_info) {
// TODO(stoyan):
}
void ExternalTabProxy::OnNetwork_Start(
int request_id, const AutomationURLRequest& request_info) {
// TODO(stoyan): url_fetcher_.Start();
}
void ExternalTabProxy::OnNetwork_Read(int request_id, int bytes_to_read) {
// TODO(stoyan): url_fetcher_.Read();
}
void ExternalTabProxy::OnNetwork_End(int request_id,
const net::URLRequestStatus& s) {
// TODO(stoyan):
}
void ExternalTabProxy::OnNetwork_DownloadInHost(int request_id) {
// TODO(stoyan):
}
void ExternalTabProxy::OnGetCookies(const GURL& url, int cookie_id) {
// TODO(stoyan):
}
void ExternalTabProxy::OnSetCookie(const GURL& url, const std::string& cookie) {
// TODO(stoyan):
}
void ExternalTabProxy::OnTabClosed() {
// TODO(stoyan):
}
void ExternalTabProxy::OnAttachTab(
const AttachExternalTabParams& attach_params) {
// TODO(stoyan):
}
|