PopupBrowserDlgView.cpp
7.64 KB
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
#include "stdafx.h"
#include "etradeclient/mfc_ui/PopupBrowserDlgView.h"
#include "etradeclient/mfc_ui/LoginDialog.h"
#include "etradeclient/browser/popup_browser_handler.h"
#include "etradeclient/utility/session.h"
#include "etradeclient/utility/logon_mgr.h"
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/url_config.h"
#include "etradeclient/utility/win_msg_define.h"
#include "etradeclient/utility/app_mgr.h"
BEGIN_MESSAGE_MAP(CPopupBrowserView, CDialog)
// System message.
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_CLOSE()
// Customized CEF message handlers.
ON_MESSAGE(WM_CEF_MSG_VIEW_NEW_BROWSER, &CPopupBrowserView::OnAttachCEFBrowser)
ON_MESSAGE(WM_CEF_MSG_VIEW_CLOSE_BROWSER, &CPopupBrowserView::OnDetachCEFBrowser)
ON_MESSAGE(WM_CEF_SESSION_EXPIRED, &CPopupBrowserView::OnSessionExpired)
ON_MESSAGE(WM_CEF_TITLE_CHANGE, &CPopupBrowserView::OnTitleChange)
ON_MESSAGE(WM_CEF_LOAD_END,&CPopupBrowserView::OnLoadEnd)
ON_MESSAGE(WM_CEF_JS_CAMMAND_CLOSE_WND, &CPopupBrowserView::OnCefJsCammandCloseWnd)
ON_MESSAGE(WM_CEF_JS_CAMMAND_USER_RELOGIN, &CPopupBrowserView::OnCefJsCammandUserRelogin)
ON_MESSAGE(WM_CEF_JS_CAMMAND_APP_EXIT, &CPopupBrowserView::OnCefJsCammandAppExit)
ON_MESSAGE(WM_CEF_JS_CAMMAND_APP_RELAUNCH, &CPopupBrowserView::OnCefJsCammandAppRelaunch)
END_MESSAGE_MAP()
CPopupBrowserView::CPopupBrowserView(const RECT& main_wnd_rect, CWnd* pParent /*= NULL*/)
: CDialog(CPopupBrowserView::IDD, pParent), m_rect(main_wnd_rect), m_browser_handler(nullptr)
{}
CPopupBrowserView::~CPopupBrowserView()
{}
int CPopupBrowserView::OnCreate(LPCREATESTRUCT create_struct)
{
if (CDialog::OnCreate(create_struct) == -1)
return -1;
m_browser_handler = new PopupBrowserHandler();
return 0;
}
BOOL CPopupBrowserView::OnInitDialog()
{
CDialog::OnInitDialog();
MoveWindow(&m_rect, FALSE);
// MoveWindow 需在CreateBrowser之前,避免创建的浏览器根据默认的窗口RECT来显示页面。
CRect browser_rect;
GetClientRect(browser_rect);
CefBrowserSettings settings;
CefWindowInfo info;
info.SetAsChild(GetSafeHwnd(), browser_rect);
if (!CefBrowserHost::CreateBrowser(info, m_browser_handler, URL(), settings, NULL))
{
LOG_ERROR(L"创建通知消息浏览器失败!");
return FALSE;
}
return TRUE;
}
void CPopupBrowserView::OnSize(UINT type, int cx, int cy)
{
CDialog::OnSize(type, cx, cy);
if (!m_browser.CEFBrowserAttached())
return;
RECT rect;
GetClientRect(&rect);
m_browser.Resize(rect);
}
LRESULT CPopupBrowserView::OnAttachCEFBrowser(WPARAM wParam, LPARAM lParam)
{
m_browser.AttachCEFBrowser((CefBrowser*)wParam);
return 0;
}
LRESULT CPopupBrowserView::OnDetachCEFBrowser(WPARAM wParam, LPARAM lParam)
{
m_browser.DetachCEFBrowser((CefBrowser*)wParam);
SendMessage(WM_CLOSE);
return FALSE;
}
LRESULT CPopupBrowserView::OnSessionExpired(WPARAM wParam, LPARAM lParam)
{
LOG_TRACE(_T("弹出界面登录超时!"));
this->ShowWindow(SW_HIDE); // Hide window first since we nee to show the login dialog.
// AfxGetMainWnd() will return NULL if called from an other thread (has to do with thread local storage).
// That's why we use AfxGetApp()->GetMainWnd() instead of AfxGetMainWnd().
CWnd* main_frm = AfxGetApp()->GetMainWnd();
if (!CLoginDialog(L"当前连接已过期,请重新登录!", main_frm).Launch())
{
::PostMessage(main_frm->GetSafeHwnd(), WM_CLOSE, NULL, NULL);
SendMessage(WM_CLOSE);
return 0;
}
Session::Instance().OnValid(); // If login succeeded, recover the session state to be "not expired".
m_browser.Reload();
this->ShowWindow(SW_SHOW); // Show window again after relogged in.
return 0;
}
LRESULT CPopupBrowserView::OnLoadEnd(WPARAM wParam, LPARAM lParam)
{
m_browser.SetFocus(TRUE);
return 0;
}
LRESULT CPopupBrowserView::OnTitleChange(WPARAM wParam, LPARAM lParam)
{
SetWindowText((LPCTSTR)wParam);
return S_OK;
}
void CPopupBrowserView::OnClose()
{
if (m_browser.CEFBrowserAttached())
{
m_browser.Close();
return;// Browser会再次触发该接口
}
CDialog::OnClose();
}
afx_msg LRESULT CPopupBrowserView::OnCefJsCammandCloseWnd(WPARAM wParam, LPARAM lParam)
{
::PostMessage(GetSafeHwnd(), WM_CLOSE, NULL, NULL);
return 0;
}
afx_msg LRESULT CPopupBrowserView::OnCefJsCammandUserRelogin(WPARAM wParam, LPARAM lParam)
{
Session::Instance().OnExpired();
// AfxGetMainWnd() will return NULL if called from an other thread (has to do with thread local storage).
// That's why we use AfxGetApp()->GetMainWnd() instead of AfxGetMainWnd().
CWnd* main_frm = AfxGetApp()->GetMainWnd();
::PostMessage(main_frm->GetSafeHwnd(), WM_CEF_SESSION_EXPIRED, NULL, NULL);
::SendMessage(GetSafeHwnd(), WM_CLOSE, NULL, NULL);
return 0;
}
afx_msg LRESULT CPopupBrowserView::OnCefJsCammandAppExit(WPARAM wParam, LPARAM lParam)
{
::SendMessage(GetSafeHwnd(), WM_CLOSE, NULL, NULL);
AppMgr::Instance().Exit(true);
return 0;
}
afx_msg LRESULT CPopupBrowserView::OnCefJsCammandAppRelaunch(WPARAM wParam, LPARAM lParam)
{
::SendMessage(GetSafeHwnd(), WM_CLOSE, NULL, NULL);
AppMgr::Instance().Relauch();
return 0;
}
// -----------------------------------------------
CUserMsgView::CUserMsgView(const RECT& main_wnd_rect, CWnd* pParent /*= NULL*/) : CPopupBrowserView(main_wnd_rect,pParent)
{
const int kWidth = 240, kHorizontalMargin = 16, kHeight = 220, kVerticalMargin = 39, kToolbarHeight = 40;
m_rect.left = m_rect.right - kWidth - kHorizontalMargin;
m_rect.top += kToolbarHeight;
m_rect.bottom = m_rect.top + kHeight + kVerticalMargin;
}
std::string CUserMsgView::URL() const
{
auto& url_cfg = URLConfig::Instance();
return url_cfg.FullHost() + url_cfg.UserMsgPath();
}
// -----------------------------------------------
CModifyPwdView::CModifyPwdView(const RECT& main_wnd_rect, CWnd* pParent /*= NULL*/) : CPopupBrowserView(main_wnd_rect, pParent)
{
const int kWndWidth = 770, kHorizontalMargin = 20, kWndHeight = 350, kVerticalMargin = 40;
const int kCenterX = m_rect.left + (m_rect.right - m_rect.left) / 2, kCenterY = m_rect.top + (m_rect.bottom - m_rect.top) / 2;
m_rect.left = kCenterX - (kWndWidth + kHorizontalMargin) / 2;
m_rect.right = kCenterX + (kWndWidth + kHorizontalMargin) / 2;
m_rect.top = kCenterY - (kWndHeight + kVerticalMargin) / 2;
m_rect.bottom = kCenterY + (kWndHeight + kVerticalMargin) / 2;
}
std::string CModifyPwdView::URL() const
{
#if 1
auto& url_cfg = URLConfig::Instance();
return url_cfg.FullHost() + url_cfg.PwdModificationPath();
#else
return "D:\\ApplicationWorkspace\\VisualStudio2013\\ETtradeClient\\hardware_test_html\\async_js_callbak_handler_test.html";
#endif
}
CCreateMerchantView::CCreateMerchantView(const RECT& main_wnd_rect, CWnd* pParent /*= NULL*/) : CPopupBrowserView(main_wnd_rect, pParent)
{
const int kWndWidth = 770, kHorizontalMargin = 20, kWndHeight = 350, kVerticalMargin = 40;
const int kCenterX = m_rect.left + (m_rect.right - m_rect.left) / 2, kCenterY = m_rect.top + (m_rect.bottom - m_rect.top) / 2;
m_rect.left = kCenterX - (kWndWidth + kHorizontalMargin) / 2;
m_rect.right = kCenterX + (kWndWidth + kHorizontalMargin) / 2;
m_rect.top = kCenterY - (kWndHeight + kVerticalMargin) / 2;
m_rect.bottom = kCenterY + (kWndHeight + kVerticalMargin) / 2;
}
std::string CCreateMerchantView::URL() const
{
#if 1
auto& url_cfg = URLConfig::Instance();
return url_cfg.FullHost() + url_cfg.CreateMerchantPath();
#else
return "D:\\ApplicationWorkspace\\VisualStudio2013\\ETtradeClient\\hardware_test_html\\async_js_callbak_handler_test.html";
#endif
}
BOOL CModifyPwdView::OnInitDialog()
{
BOOL ret = CPopupBrowserView::OnInitDialog();
SetWindowLong(GetSafeHwnd(), GWL_STYLE, GetWindowLong(GetSafeHwnd(), GWL_STYLE) & ~WS_SYSMENU);
return ret;
}
BOOL CCreateMerchantView::OnInitDialog()
{
BOOL ret = CPopupBrowserView::OnInitDialog();
SetWindowLong(GetSafeHwnd(), GWL_STYLE, GetWindowLong(GetSafeHwnd(), GWL_STYLE) & ~WS_SYSMENU);
return ret;
}