PopupBrowserDlgView.cpp
4.95 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
#include "stdafx.h"
#include "etradeclient/mfc_ui/PopupBrowserDlgView.h"
#include "etradeclient/mfc_ui/LoginDialog.h"
#include "etradeclient/browser/popup_browser_handler.h"
#include "etradeclient/browser/session.h"
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/url_config.h"
#include "etradeclient/utility/win_msg_define.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)
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 0;
}
LRESULT CPopupBrowserView::OnSessionExpired(WPARAM wParam, LPARAM lParam)
{
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;
}
CDialog::OnClose();
}
// -----------------------------------------------
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
{
auto& url_cfg = URLConfig::Instance();
return url_cfg.FullHost() + url_cfg.PwdModificationPath();
}
afx_msg LRESULT CPopupBrowserView::OnCefJsCammandCloseWnd(WPARAM wParam, LPARAM lParam)
{
::PostMessage(GetSafeHwnd(), WM_CLOSE, NULL, NULL);
return 0;
}