2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2008-2009 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/request_impl.h"
|
2012-03-22 23:08:05 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/http_header_utils.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "net/url_request/url_request.h"
|
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
using WebKit::WebHTTPBody;
|
|
|
|
using WebKit::WebString;
|
|
|
|
using WebKit::WebURL;
|
|
|
|
using WebKit::WebURLRequest;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefRequest> CefRequest::CreateRequest() {
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefRequest> request(new CefRequestImpl());
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRequestImpl::CefRequestImpl()
|
2012-01-10 00:46:23 +01:00
|
|
|
: method_("GET"),
|
|
|
|
flags_(WUR_FLAG_NONE) {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefRequestImpl::GetURL() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return url_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::SetURL(const CefString& url) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
url_ = url;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefRequestImpl::GetMethod() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return method_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::SetMethod(const CefString& method) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
method_ = method;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefPostData> CefRequestImpl::GetPostData() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return postdata_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::SetPostData(CefRefPtr<CefPostData> postData) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
postdata_ = postData;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::GetHeaderMap(HeaderMap& headerMap) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
headerMap = headermap_;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::SetHeaderMap(const HeaderMap& headerMap) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
headermap_ = headerMap;
|
|
|
|
}
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
void CefRequestImpl::Set(const CefString& url,
|
|
|
|
const CefString& method,
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefPostData> postData,
|
2012-01-10 00:46:23 +01:00
|
|
|
const HeaderMap& headerMap) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
url_ = url;
|
|
|
|
method_ = method;
|
|
|
|
postdata_ = postData;
|
|
|
|
headermap_ = headerMap;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::Set(net::URLRequest* request) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
url_ = request->url().spec();
|
|
|
|
method_ = request->method();
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-03-22 23:08:05 +01:00
|
|
|
net::HttpRequestHeaders headers = request->extra_request_headers();
|
|
|
|
|
|
|
|
// Ensure that we do not send username and password fields in the referrer.
|
|
|
|
GURL referrer(request->GetSanitizedReferrer());
|
|
|
|
|
|
|
|
// Strip Referer from request_info_.extra_headers to prevent, e.g., plugins
|
|
|
|
// from overriding headers that are controlled using other means. Otherwise a
|
|
|
|
// plugin could set a referrer although sending the referrer is inhibited.
|
|
|
|
headers.RemoveHeader(net::HttpRequestHeaders::kReferer);
|
|
|
|
|
|
|
|
// Our consumer should have made sure that this is a safe referrer. See for
|
|
|
|
// instance WebCore::FrameLoader::HideReferrer.
|
|
|
|
if (referrer.is_valid())
|
|
|
|
headers.SetHeader(net::HttpRequestHeaders::kReferer, referrer.spec());
|
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
// Transfer request headers
|
2012-03-22 23:08:05 +01:00
|
|
|
GetHeaderMap(headers, headermap_);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Transfer post data, if any
|
|
|
|
net::UploadData* data = request->get_upload();
|
|
|
|
if (data) {
|
2011-02-09 23:04:35 +01:00
|
|
|
postdata_ = CefPostData::CreatePostData();
|
|
|
|
static_cast<CefPostDataImpl*>(postdata_.get())->Set(*data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::Set(const WebKit::WebURLRequest& request) {
|
2011-02-09 23:04:35 +01:00
|
|
|
DCHECK(!request.isNull());
|
|
|
|
AutoLock lock_scope(this);
|
|
|
|
|
|
|
|
url_ = request.url().spec().utf16();
|
|
|
|
method_ = request.httpMethod();
|
|
|
|
|
2011-02-10 02:17:54 +01:00
|
|
|
const WebHTTPBody& body = request.httpBody();
|
2011-02-09 23:04:35 +01:00
|
|
|
if (!body.isNull()) {
|
|
|
|
postdata_ = new CefPostDataImpl();
|
|
|
|
static_cast<CefPostDataImpl*>(postdata_.get())->Set(body);
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (postdata_.get()) {
|
2011-02-09 23:04:35 +01:00
|
|
|
postdata_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
headermap_.clear();
|
|
|
|
GetHeaderMap(request, headermap_);
|
|
|
|
|
|
|
|
int flags = WUR_FLAG_NONE;
|
|
|
|
if (request.cachePolicy() == WebURLRequest::ReloadIgnoringCacheData)
|
|
|
|
flags |= WUR_FLAG_SKIP_CACHE;
|
|
|
|
if (request.allowStoredCredentials())
|
|
|
|
flags |= WUR_FLAG_ALLOW_CACHED_CREDENTIALS;
|
|
|
|
if (request.allowCookies())
|
|
|
|
flags |= WUR_FLAG_ALLOW_COOKIES;
|
|
|
|
if (request.reportUploadProgress())
|
|
|
|
flags |= WUR_FLAG_REPORT_UPLOAD_PROGRESS;
|
|
|
|
if (request.reportLoadTiming())
|
|
|
|
flags |= WUR_FLAG_REPORT_LOAD_TIMING;
|
|
|
|
if (request.reportRawHeaders())
|
|
|
|
flags |= WUR_FLAG_REPORT_RAW_HEADERS;
|
|
|
|
flags_ = static_cast<cef_weburlrequest_flags_t>(flags);
|
|
|
|
|
|
|
|
first_party_for_cookies_ = request.firstPartyForCookies().spec().utf16();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SETBOOLFLAG(obj, flags, method, FLAG) \
|
|
|
|
obj.method((flags & (FLAG)) == (FLAG))
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::Get(WebKit::WebURLRequest& request) {
|
2011-02-09 23:04:35 +01:00
|
|
|
request.initialize();
|
|
|
|
AutoLock lock_scope(this);
|
|
|
|
|
|
|
|
std::string urlStr(url_);
|
|
|
|
GURL gurl = GURL(urlStr);
|
|
|
|
request.setURL(WebURL(gurl));
|
|
|
|
|
|
|
|
std::string method(method_);
|
|
|
|
request.setHTTPMethod(WebString::fromUTF8(method.c_str()));
|
|
|
|
request.setTargetType(WebURLRequest::TargetIsMainFrame);
|
|
|
|
|
|
|
|
WebHTTPBody body;
|
|
|
|
if (postdata_.get()) {
|
|
|
|
body.initialize();
|
|
|
|
static_cast<CefPostDataImpl*>(postdata_.get())->Get(body);
|
|
|
|
request.setHTTPBody(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetHeaderMap(headermap_, request);
|
|
|
|
|
|
|
|
request.setCachePolicy((flags_ & WUR_FLAG_SKIP_CACHE) ?
|
|
|
|
WebURLRequest::ReloadIgnoringCacheData :
|
|
|
|
WebURLRequest::UseProtocolCachePolicy);
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
SETBOOLFLAG(request, flags_, setAllowStoredCredentials,
|
2011-02-09 23:04:35 +01:00
|
|
|
WUR_FLAG_ALLOW_CACHED_CREDENTIALS);
|
2012-01-10 00:46:23 +01:00
|
|
|
SETBOOLFLAG(request, flags_, setAllowCookies,
|
2011-02-09 23:04:35 +01:00
|
|
|
WUR_FLAG_ALLOW_COOKIES);
|
2012-01-10 00:46:23 +01:00
|
|
|
SETBOOLFLAG(request, flags_, setReportUploadProgress,
|
2011-02-09 23:04:35 +01:00
|
|
|
WUR_FLAG_REPORT_UPLOAD_PROGRESS);
|
2012-01-10 00:46:23 +01:00
|
|
|
SETBOOLFLAG(request, flags_, setReportLoadTiming,
|
2011-02-09 23:04:35 +01:00
|
|
|
WUR_FLAG_REPORT_LOAD_TIMING);
|
|
|
|
SETBOOLFLAG(request, flags_, setReportRawHeaders,
|
|
|
|
WUR_FLAG_REPORT_RAW_HEADERS);
|
|
|
|
|
|
|
|
if (!first_party_for_cookies_.empty()) {
|
|
|
|
std::string cookiesStr(first_party_for_cookies_);
|
|
|
|
GURL gurl = GURL(cookiesStr);
|
|
|
|
request.setFirstPartyForCookies(WebURL(gurl));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRequest::RequestFlags CefRequestImpl::GetFlags() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return flags_;
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::SetFlags(RequestFlags flags) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
flags_ = flags;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefRequestImpl::GetFirstPartyForCookies() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return first_party_for_cookies_;
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::SetFirstPartyForCookies(const CefString& url) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
first_party_for_cookies_ = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRequestImpl::GetHeaderMap(const net::HttpRequestHeaders& headers,
|
|
|
|
HeaderMap& map) {
|
2010-10-03 23:04:50 +02:00
|
|
|
net::HttpRequestHeaders::Iterator it(headers);
|
|
|
|
do {
|
2011-10-28 23:31:26 +02:00
|
|
|
map.insert(std::make_pair(it.name(), it.value()));
|
2010-10-03 23:04:50 +02:00
|
|
|
} while (it.GetNext());
|
|
|
|
}
|
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
// static
|
2010-10-03 23:04:50 +02:00
|
|
|
void CefRequestImpl::GetHeaderMap(const WebKit::WebURLRequest& request,
|
2012-01-10 00:46:23 +01:00
|
|
|
HeaderMap& map) {
|
2011-02-09 23:04:35 +01:00
|
|
|
HttpHeaderUtils::HeaderVisitor visitor(&map);
|
2010-10-03 23:04:50 +02:00
|
|
|
request.visitHTTPHeaderFields(&visitor);
|
|
|
|
}
|
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
// static
|
2010-10-03 23:04:50 +02:00
|
|
|
void CefRequestImpl::SetHeaderMap(const HeaderMap& map,
|
2012-01-10 00:46:23 +01:00
|
|
|
WebKit::WebURLRequest& request) {
|
2010-10-03 23:04:50 +02:00
|
|
|
HeaderMap::const_iterator it = map.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != map.end(); ++it)
|
2010-11-22 18:49:46 +01:00
|
|
|
request.setHTTPHeaderField(string16(it->first), string16(it->second));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefPostData> CefPostData::CreatePostData() {
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefPostData> postdata(new CefPostDataImpl());
|
|
|
|
return postdata;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefPostDataImpl::CefPostDataImpl() {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefPostDataImpl::GetElementCount() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return elements_.size();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataImpl::GetElements(ElementVector& elements) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
elements = elements_;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefPostDataImpl::RemoveElement(CefRefPtr<CefPostDataElement> element) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
ElementVector::iterator it = elements_.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != elements_.end(); ++it) {
|
|
|
|
if (it->get() == element.get()) {
|
2010-10-03 23:04:50 +02:00
|
|
|
elements_.erase(it);
|
2011-02-09 23:04:35 +01:00
|
|
|
return true;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
return false;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefPostDataImpl::AddElement(CefRefPtr<CefPostDataElement> element) {
|
2010-10-03 23:04:50 +02:00
|
|
|
bool found = false;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// check that the element isn't already in the list before adding
|
|
|
|
ElementVector::const_iterator it = elements_.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != elements_.end(); ++it) {
|
|
|
|
if (it->get() == element.get()) {
|
2010-10-03 23:04:50 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!found)
|
2010-10-03 23:04:50 +02:00
|
|
|
elements_.push_back(element);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return !found;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataImpl::RemoveElements() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
elements_.clear();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataImpl::Set(net::UploadData& data) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefPostDataElement> postelem;
|
|
|
|
|
|
|
|
std::vector<net::UploadData::Element>* elements = data.elements();
|
|
|
|
std::vector<net::UploadData::Element>::const_iterator it = elements->begin();
|
|
|
|
for (; it != elements->end(); ++it) {
|
|
|
|
postelem = CefPostDataElement::CreatePostDataElement();
|
|
|
|
static_cast<CefPostDataElementImpl*>(postelem.get())->Set(*it);
|
|
|
|
AddElement(postelem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataImpl::Get(net::UploadData& data) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
net::UploadData::Element element;
|
|
|
|
std::vector<net::UploadData::Element> data_elements;
|
|
|
|
ElementVector::iterator it = elements_.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != elements_.end(); ++it) {
|
2010-10-03 23:04:50 +02:00
|
|
|
static_cast<CefPostDataElementImpl*>(it->get())->Get(element);
|
|
|
|
data_elements.push_back(element);
|
|
|
|
}
|
|
|
|
data.SetElements(data_elements);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataImpl::Set(const WebKit::WebHTTPBody& data) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefPostDataElement> postelem;
|
|
|
|
WebKit::WebHTTPBody::Element element;
|
|
|
|
size_t size = data.elementCount();
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
if (data.elementAt(i, element)) {
|
|
|
|
postelem = CefPostDataElement::CreatePostDataElement();
|
|
|
|
static_cast<CefPostDataElementImpl*>(postelem.get())->Set(element);
|
|
|
|
AddElement(postelem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataImpl::Get(WebKit::WebHTTPBody& data) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
WebKit::WebHTTPBody::Element element;
|
|
|
|
ElementVector::iterator it = elements_.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != elements_.end(); ++it) {
|
2010-10-03 23:04:50 +02:00
|
|
|
static_cast<CefPostDataElementImpl*>(it->get())->Get(element);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (element.type == WebKit::WebHTTPBody::Element::TypeData) {
|
2010-10-03 23:04:50 +02:00
|
|
|
data.appendData(element.data);
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (element.type == WebKit::WebHTTPBody::Element::TypeFile) {
|
2010-10-03 23:04:50 +02:00
|
|
|
data.appendFile(element.filePath);
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefPostDataElement> CefPostDataElement::CreatePostDataElement() {
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefPostDataElement> element(new CefPostDataElementImpl());
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefPostDataElementImpl::CefPostDataElementImpl() {
|
2010-10-03 23:04:50 +02:00
|
|
|
type_ = PDE_TYPE_EMPTY;
|
2010-11-22 18:49:46 +01:00
|
|
|
memset(&data_, 0, sizeof(data_));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefPostDataElementImpl::~CefPostDataElementImpl() {
|
2010-10-03 23:04:50 +02:00
|
|
|
SetToEmpty();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataElementImpl::SetToEmpty() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (type_ == PDE_TYPE_BYTES)
|
2010-10-03 23:04:50 +02:00
|
|
|
free(data_.bytes.bytes);
|
2012-01-10 00:46:23 +01:00
|
|
|
else if (type_ == PDE_TYPE_FILE)
|
2010-11-22 18:49:46 +01:00
|
|
|
cef_string_clear(&data_.filename);
|
2010-10-03 23:04:50 +02:00
|
|
|
type_ = PDE_TYPE_EMPTY;
|
2010-11-22 18:49:46 +01:00
|
|
|
memset(&data_, 0, sizeof(data_));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataElementImpl::SetToFile(const CefString& fileName) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
// Clear any data currently in the element
|
|
|
|
SetToEmpty();
|
|
|
|
|
|
|
|
// Assign the new data
|
|
|
|
type_ = PDE_TYPE_FILE;
|
2010-11-22 18:49:46 +01:00
|
|
|
cef_string_copy(fileName.c_str(), fileName.length(), &data_.filename);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataElementImpl::SetToBytes(size_t size, const void* bytes) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
// Clear any data currently in the element
|
|
|
|
SetToEmpty();
|
|
|
|
|
|
|
|
// Assign the new data
|
|
|
|
void* data = malloc(size);
|
|
|
|
DCHECK(data != NULL);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (data == NULL)
|
2010-10-03 23:04:50 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(data, bytes, size);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
type_ = PDE_TYPE_BYTES;
|
|
|
|
data_.bytes.bytes = data;
|
|
|
|
data_.bytes.size = size;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefPostDataElement::Type CefPostDataElementImpl::GetType() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return type_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefPostDataElementImpl::GetFile() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(type_ == PDE_TYPE_FILE);
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString filename;
|
2012-01-10 00:46:23 +01:00
|
|
|
if (type_ == PDE_TYPE_FILE)
|
2010-11-22 18:49:46 +01:00
|
|
|
filename.FromString(data_.filename.str, data_.filename.length, false);
|
2010-10-03 23:04:50 +02:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefPostDataElementImpl::GetBytesCount() {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(type_ == PDE_TYPE_BYTES);
|
|
|
|
size_t size = 0;
|
2012-01-10 00:46:23 +01:00
|
|
|
if (type_ == PDE_TYPE_BYTES)
|
2010-10-03 23:04:50 +02:00
|
|
|
size = data_.bytes.size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefPostDataElementImpl::GetBytes(size_t size, void* bytes) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(type_ == PDE_TYPE_BYTES);
|
|
|
|
size_t rv = 0;
|
2012-01-10 00:46:23 +01:00
|
|
|
if (type_ == PDE_TYPE_BYTES) {
|
2010-10-03 23:04:50 +02:00
|
|
|
rv = (size < data_.bytes.size ? size : data_.bytes.size);
|
|
|
|
memcpy(bytes, data_.bytes.bytes, rv);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataElementImpl::Set(const net::UploadData::Element& element) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
if (element.type() == net::UploadData::TYPE_BYTES) {
|
|
|
|
SetToBytes(element.bytes().size(),
|
|
|
|
static_cast<const void*>(
|
|
|
|
std::string(element.bytes().begin(),
|
|
|
|
element.bytes().end()).c_str()));
|
|
|
|
} else if (element.type() == net::UploadData::TYPE_FILE) {
|
|
|
|
SetToFile(element.file_path().value());
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataElementImpl::Get(net::UploadData::Element& element) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (type_ == PDE_TYPE_BYTES) {
|
2010-10-03 23:04:50 +02:00
|
|
|
element.SetToBytes(static_cast<char*>(data_.bytes.bytes), data_.bytes.size);
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (type_ == PDE_TYPE_FILE) {
|
2010-11-22 18:49:46 +01:00
|
|
|
FilePath path = FilePath(CefString(&data_.filename));
|
|
|
|
element.SetToFilePath(path);
|
2010-10-03 23:04:50 +02:00
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataElementImpl::Set(const WebKit::WebHTTPBody::Element& element) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (element.type == WebKit::WebHTTPBody::Element::TypeData) {
|
2010-10-03 23:04:50 +02:00
|
|
|
SetToBytes(element.data.size(),
|
|
|
|
static_cast<const void*>(element.data.data()));
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (element.type == WebKit::WebHTTPBody::Element::TypeFile) {
|
2010-11-22 18:49:46 +01:00
|
|
|
SetToFile(string16(element.filePath));
|
2010-10-03 23:04:50 +02:00
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefPostDataElementImpl::Get(WebKit::WebHTTPBody::Element& element) {
|
2011-02-09 23:04:35 +01:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (type_ == PDE_TYPE_BYTES) {
|
2010-10-03 23:04:50 +02:00
|
|
|
element.type = WebKit::WebHTTPBody::Element::TypeData;
|
|
|
|
element.data.assign(
|
|
|
|
static_cast<char*>(data_.bytes.bytes), data_.bytes.size);
|
2012-01-10 00:46:23 +01:00
|
|
|
} else if (type_ == PDE_TYPE_FILE) {
|
2010-10-03 23:04:50 +02:00
|
|
|
element.type = WebKit::WebHTTPBody::Element::TypeFile;
|
2010-11-22 18:49:46 +01:00
|
|
|
element.filePath.assign(string16(CefString(&data_.filename)));
|
2010-10-03 23:04:50 +02:00
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|