embedded_browser.cpp
3.76 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
#include "stdafx.h"
#include "etradeclient/browser/embedded_browser.h"
#include <mutex>
#include <condition_variable>
#include "include/cef_request.h"
#include "include/cef_cookie.h"
#include "include/wrapper/cef_helpers.h"
#include "include/wrapper/cef_closure_task.h"
namespace
{
class SetCookieProxy : public CefSetCookieCallback
{
public:
SetCookieProxy(std::mutex& mtx, std::condition_variable& cv) :
m_mtx(mtx), m_cv(cv), m_set_cookie_done(false), m_set_cookie_res(false)
{}
void SetCookie(const std::string& url, const CefCookie& cookie)
{
CEF_REQUIRE_IO_THREAD();
CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(NULL);
if (!manager->SetCookie(url, cookie, CefRefPtr<CefSetCookieCallback>(this)))
{
m_set_cookie_res = false; // Set cookie failed.
m_set_cookie_done = true;
m_cv.notify_one(); // Notify the other thread when setting cookie failed.
}
}
void OnComplete(bool res)
{
CEF_REQUIRE_IO_THREAD();
{
std::lock_guard<std::mutex> lg(m_mtx);
m_set_cookie_res = res;
m_set_cookie_done = true;
}
m_cv.notify_one();
}
private:
std::mutex& m_mtx;
std::condition_variable& m_cv;
bool m_set_cookie_done;
bool m_set_cookie_res;
private:
friend class EmbeddedBrower;
IMPLEMENT_REFCOUNTING(SetCookieProxy);
DISALLOW_COPY_AND_ASSIGN(SetCookieProxy);
};
}
bool EmbeddedBrower::SetCookie(const std::string& url, const CefCookie& cookie)
{
std::mutex mtx;
std::condition_variable cv;
CefRefPtr<SetCookieProxy> set_cookie_proxy = new SetCookieProxy(mtx, cv);
CefPostTask(TID_IO, base::Bind(&SetCookieProxy::SetCookie, set_cookie_proxy, url, cookie));
std::unique_lock<std::mutex> lk(mtx);
cv.wait(lk, [&]
{
return set_cookie_proxy->m_set_cookie_done;
});
return set_cookie_proxy->m_set_cookie_res;
}
EmbeddedBrower::EmbeddedBrower() : m_cef_browser(nullptr)
{}
EmbeddedBrower::~EmbeddedBrower()
{
if (m_cef_browser) // release browser if still active
m_cef_browser = nullptr;
}
void EmbeddedBrower::AttachCEFBrowser(CefRefPtr<CefBrowser> cef_browser)
{
m_cef_browser = cef_browser;
}
void EmbeddedBrower::DetachCEFBrowser(CefRefPtr<CefBrowser> cef_browser)
{
if (nullptr != m_cef_browser && m_cef_browser->IsSame(cef_browser))
m_cef_browser = nullptr; // Release reference of browser instance.
}
bool EmbeddedBrower::CEFBrowserAttached() const
{
return (nullptr != m_cef_browser);
}
void EmbeddedBrower::Close(bool force_close)
{
// Notify the browser window that we would like to close it. This
// will result in a call to MainViewBrowserHandler::DoClose() if the
// JavaScript 'onbeforeunload' event handler allows it.
m_cef_browser->GetHost()->CloseBrowser(force_close);
}
void EmbeddedBrower::Resize(const RECT& rect)
{
// cef stretch to to full view
CefWindowHandle hwnd = m_cef_browser->GetHost()->GetWindowHandle();
if (hwnd)
{
HDWP hdwp = BeginDeferWindowPos(1);
hdwp = DeferWindowPos(hdwp, hwnd, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER);
EndDeferWindowPos(hdwp);
}
}
void EmbeddedBrower::Copy()
{
m_cef_browser->GetMainFrame()->Copy();
}
void EmbeddedBrower::Cut()
{
m_cef_browser->GetMainFrame()->Cut();
}
void EmbeddedBrower::Paste()
{
m_cef_browser->GetMainFrame()->Paste();
}
void EmbeddedBrower::NavigateTo(const std::string& url) const
{
m_cef_browser->GetMainFrame()->LoadURL(url);
}
void EmbeddedBrower::LoadRequest(const CefRefPtr<CefRequest>& req) const
{
m_cef_browser->GetMainFrame()->LoadRequest(req);
}
void EmbeddedBrower::GoBack() const
{
m_cef_browser->GoBack();
}
void EmbeddedBrower::GoForward() const
{
m_cef_browser->GoForward();
}
void EmbeddedBrower::Reload() const
{
m_cef_browser->Reload();
}
void EmbeddedBrower::SetFocus(bool focus)
{
m_cef_browser->GetHost()->SetFocus(true);
}