url_request_proxy.cpp
3.35 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
#include "stdafx.h"
#include "etradeclient/browser/url_request_proxy.h"
#include <functional>
#include "include/wrapper/cef_helpers.h"
#include "include/wrapper/cef_closure_task.h"
#include "etradeclient/browser/url_request.h"
#include "etradeclient/utility/logging.h"
URLRequestProxy::URLRequestProxyImpl::URLRequestProxyImpl(std::mutex& mtx, std::condition_variable& cv) :
m_mtx(mtx), m_cv(cv), m_req_sent(false), m_res(false), m_response_str("")
{}
void URLRequestProxy::URLRequestProxyImpl::Get(const std::string& request_url, int flags)
{
CEF_REQUIRE_UI_THREAD();
CefRefPtr<CefRequest> request = CefRequest::Create();
request->Set(request_url, "GET", nullptr, CefRequest::HeaderMap());
request->SetFlags(flags);
SendRequest(request);
}
void URLRequestProxy::URLRequestProxyImpl::Post(const std::string& request_url, const std::string& request_param, int flags)
{
CEF_REQUIRE_UI_THREAD();
CefRefPtr<CefRequest> request = CefRequest::Create();
CefRefPtr<CefPostData> postData = CefPostData::Create();
CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
element->SetToBytes(request_param.size(), request_param.c_str());
postData->AddElement(element);
request->Set(request_url, "POST", postData, CefRequest::HeaderMap());
request->SetFlags(flags);
SendRequest(request);
}
void URLRequestProxy::URLRequestProxyImpl::Cancel()
{
CEF_REQUIRE_UI_THREAD();
if (nullptr != m_url_request.get())
m_url_request->CancelPendingRequest();
m_url_request.reset(nullptr);
m_cv.notify_one();
}
bool URLRequestProxy::URLRequestProxyImpl::GotResponse() const
{
return m_req_sent;
}
bool URLRequestProxy::URLRequestProxyImpl::Result() const
{
return m_res;
}
const std::string& URLRequestProxy::URLRequestProxyImpl::ResponseString() const
{
return m_response_str;
}
void URLRequestProxy::URLRequestProxyImpl::SendRequest(CefRefPtr<CefRequest> request)
{
m_req_sent = false;
m_res = false;
m_response_str.swap(std::string(""));
if (nullptr == m_url_request.get())
m_url_request.reset(new URLRequest());
m_url_request->Send(request,
std::bind(&URLRequestProxyImpl::HandleResponse, this, std::placeholders::_1, std::placeholders::_2));
}
void URLRequestProxy::URLRequestProxyImpl::HandleResponse(CefURLRequest::ErrorCode error_code, const std::string& response_str)
{
CEF_REQUIRE_UI_THREAD();
{
std::lock_guard<std::mutex> lg(m_mtx);
m_req_sent = true;
m_res = (ERR_NONE == error_code);
m_response_str = response_str;
if (!m_res)
LOG_ERROR(L"CEF网络请求失败!错误代码: " + std::to_wstring(error_code));
m_url_request.reset(nullptr);
}
m_cv.notify_one();
}
URLRequestProxy::URLRequestProxy() : m_pimpl(new URLRequestProxyImpl(m_mtx, m_cv))
{}
bool URLRequestProxy::Get(const std::string& request_url, int flags)
{
CefPostTask(TID_UI, base::Bind(&URLRequestProxyImpl::Get, m_pimpl, request_url, flags));
std::unique_lock<std::mutex> lk(m_mtx);
m_cv.wait(lk, [&]
{
return m_pimpl->GotResponse();
});
return m_pimpl->Result();
}
bool URLRequestProxy::Post(const std::string& request_url, const std::string& request_param, int flags)
{
CefPostTask(TID_UI, base::Bind(&URLRequestProxyImpl::Post, m_pimpl, request_url, request_param, flags));
std::unique_lock<std::mutex> lk(m_mtx);
m_cv.wait(lk, [&]
{
return m_pimpl->GotResponse();
});
return m_pimpl->Result();
}
const std::string& URLRequestProxy::ResponseString() const
{
return m_pimpl->ResponseString();
}