- Add CefURLRequest support (issue #517).

- Add CefBrowserProcessHandler interface (issue #650).
- Internally re-register supported schemes with CefCookieManager after changing the storage path (issue #651).
- Add CefResourceHandler callbacks for blocking cookie loading and saving (issue #652).
- Allow custom scheme handlers for requests that do not originate from browser content (issue #653).
- Use 'int' instead of 'RequestFlags' for CefRequest::GetFlags and SetFlags (issue #654).
- Rename cef_request.h CreateObject methods to Create (issue #655).
- Add #ifdef guards to cef_tuple.h to allow the use of both cef_runnable.h and base/bind.h in the same unit test source file.
- Retrieve cookieable schemes as part of ClientApp::RegisterCustomSchemes and register with the global cookie manager.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@697 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2012-06-19 16:29:49 +00:00
parent 7e6932cd00
commit 9df142f832
91 changed files with 4881 additions and 392 deletions

View File

@@ -109,8 +109,7 @@ class CefUIThread : public base::Thread {
// Use our own browser process runner.
browser_runner_.reset(content::BrowserMainRunner::Create());
// Initialize browser process state. Results in a call to
// CefBrowserMain::GetMainMessageLoop().
// Initialize browser process state. Uses the current thread's mesage loop.
int exit_code = browser_runner_->Initialize(main_function_params_);
CHECK_EQ(exit_code, -1);
}
@@ -257,7 +256,8 @@ int CefMainDelegate::RunProcess(
browser_runner_.reset(content::BrowserMainRunner::Create());
// Initialize browser process state. Results in a call to
// CefBrowserMain::GetMainMessageLoop().
// CefBrowserMain::PreMainMessageLoopStart() which creates the UI message
// loop.
int exit_code = browser_runner_->Initialize(main_function_params);
if (exit_code >= 0)
return exit_code;

View File

@@ -10,15 +10,46 @@
#include "base/logging.h"
#include "net/url_request/url_request.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebHTTPHeaderVisitor.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRequest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.h"
CefRefPtr<CefRequest> CefRequest::CreateRequest() {
#define CHECK_READONLY_RETURN(val) \
if (read_only_) { \
NOTREACHED() << "object is read only"; \
return val; \
}
#define CHECK_READONLY_RETURN_VOID() \
if (read_only_) { \
NOTREACHED() << "object is read only"; \
return; \
}
// CefRequest -----------------------------------------------------------------
// static
CefRefPtr<CefRequest> CefRequest::Create() {
CefRefPtr<CefRequest> request(new CefRequestImpl());
return request;
}
// CefRequestImpl -------------------------------------------------------------
CefRequestImpl::CefRequestImpl()
: method_("GET"),
flags_(WUR_FLAG_NONE) {
flags_(UR_FLAG_NONE),
read_only_(false) {
}
bool CefRequestImpl::IsReadOnly() {
AutoLock lock_scope(this);
return read_only_;
}
CefString CefRequestImpl::GetURL() {
@@ -28,6 +59,7 @@ CefString CefRequestImpl::GetURL() {
void CefRequestImpl::SetURL(const CefString& url) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
url_ = url;
}
@@ -38,6 +70,7 @@ CefString CefRequestImpl::GetMethod() {
void CefRequestImpl::SetMethod(const CefString& method) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
method_ = method;
}
@@ -48,6 +81,7 @@ CefRefPtr<CefPostData> CefRequestImpl::GetPostData() {
void CefRequestImpl::SetPostData(CefRefPtr<CefPostData> postData) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
postdata_ = postData;
}
@@ -58,6 +92,7 @@ void CefRequestImpl::GetHeaderMap(HeaderMap& headerMap) {
void CefRequestImpl::SetHeaderMap(const HeaderMap& headerMap) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
headermap_ = headerMap;
}
@@ -66,21 +101,20 @@ void CefRequestImpl::Set(const CefString& url,
CefRefPtr<CefPostData> postData,
const HeaderMap& headerMap) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
url_ = url;
method_ = method;
postdata_ = postData;
headermap_ = headerMap;
}
#define SETBOOLFLAG(obj, flags, method, FLAG) \
obj.method((flags & (FLAG)) == (FLAG))
CefRequest::RequestFlags CefRequestImpl::GetFlags() {
int CefRequestImpl::GetFlags() {
AutoLock lock_scope(this);
return flags_;
}
void CefRequestImpl::SetFlags(RequestFlags flags) {
void CefRequestImpl::SetFlags(int flags) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
flags_ = flags;
}
@@ -90,11 +124,13 @@ CefString CefRequestImpl::GetFirstPartyForCookies() {
}
void CefRequestImpl::SetFirstPartyForCookies(const CefString& url) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
first_party_for_cookies_ = url;
}
void CefRequestImpl::Set(net::URLRequest* request) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
url_ = request->url().spec();
method_ = request->method();
@@ -121,7 +157,7 @@ void CefRequestImpl::Set(net::URLRequest* request) {
// Transfer post data, if any
net::UploadData* data = request->get_upload();
if (data) {
postdata_ = CefPostData::CreatePostData();
postdata_ = CefPostData::Create();
static_cast<CefPostDataImpl*>(postdata_.get())->Set(*data);
} else if (postdata_.get()) {
postdata_ = NULL;
@@ -160,6 +196,98 @@ void CefRequestImpl::Get(net::URLRequest* request) {
}
}
void CefRequestImpl::Set(const WebKit::WebURLRequest& request) {
DCHECK(!request.isNull());
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
url_ = request.url().spec().utf16();
method_ = request.httpMethod();
const WebKit::WebHTTPBody& body = request.httpBody();
if (!body.isNull()) {
postdata_ = new CefPostDataImpl();
static_cast<CefPostDataImpl*>(postdata_.get())->Set(body);
} else if (postdata_.get()) {
postdata_ = NULL;
}
headermap_.clear();
GetHeaderMap(request, headermap_);
flags_ = UR_FLAG_NONE;
if (request.cachePolicy() == WebKit::WebURLRequest::ReloadIgnoringCacheData)
flags_ |= UR_FLAG_SKIP_CACHE;
if (request.allowStoredCredentials())
flags_ |= UR_FLAG_ALLOW_CACHED_CREDENTIALS;
if (request.allowCookies())
flags_ |= UR_FLAG_ALLOW_COOKIES;
if (request.reportUploadProgress())
flags_ |= UR_FLAG_REPORT_UPLOAD_PROGRESS;
if (request.reportLoadTiming())
flags_ |= UR_FLAG_REPORT_LOAD_TIMING;
if (request.reportRawHeaders())
flags_ |= UR_FLAG_REPORT_RAW_HEADERS;
first_party_for_cookies_ = request.firstPartyForCookies().spec().utf16();
}
void CefRequestImpl::Get(WebKit::WebURLRequest& request) {
request.initialize();
AutoLock lock_scope(this);
GURL gurl = GURL(url_.ToString());
request.setURL(WebKit::WebURL(gurl));
std::string method(method_);
request.setHTTPMethod(WebKit::WebString::fromUTF8(method.c_str()));
request.setTargetType(WebKit::WebURLRequest::TargetIsMainFrame);
WebKit::WebHTTPBody body;
if (postdata_.get()) {
body.initialize();
static_cast<CefPostDataImpl*>(postdata_.get())->Get(body);
request.setHTTPBody(body);
}
SetHeaderMap(headermap_, request);
request.setCachePolicy((flags_ & UR_FLAG_SKIP_CACHE) ?
WebKit::WebURLRequest::ReloadIgnoringCacheData :
WebKit::WebURLRequest::UseProtocolCachePolicy);
#define SETBOOLFLAG(obj, flags, method, FLAG) \
obj.method((flags & (FLAG)) == (FLAG))
SETBOOLFLAG(request, flags_, setAllowStoredCredentials,
UR_FLAG_ALLOW_CACHED_CREDENTIALS);
SETBOOLFLAG(request, flags_, setAllowCookies,
UR_FLAG_ALLOW_COOKIES);
SETBOOLFLAG(request, flags_, setReportUploadProgress,
UR_FLAG_REPORT_UPLOAD_PROGRESS);
SETBOOLFLAG(request, flags_, setReportLoadTiming,
UR_FLAG_REPORT_LOAD_TIMING);
SETBOOLFLAG(request, flags_, setReportRawHeaders,
UR_FLAG_REPORT_RAW_HEADERS);
if (!first_party_for_cookies_.empty()) {
GURL gurl = GURL(first_party_for_cookies_.ToString());
request.setFirstPartyForCookies(WebKit::WebURL(gurl));
}
}
void CefRequestImpl::SetReadOnly(bool read_only) {
AutoLock lock_scope(this);
if (read_only_ == read_only)
return;
read_only_ = read_only;
if (postdata_.get())
static_cast<CefPostDataImpl*>(postdata_.get())->SetReadOnly(read_only);
}
// static
void CefRequestImpl::GetHeaderMap(const net::HttpRequestHeaders& headers,
HeaderMap& map) {
@@ -172,12 +300,53 @@ void CefRequestImpl::GetHeaderMap(const net::HttpRequestHeaders& headers,
} while (it.GetNext());
}
CefRefPtr<CefPostData> CefPostData::CreatePostData() {
// static
void CefRequestImpl::GetHeaderMap(const WebKit::WebURLRequest& request,
HeaderMap& map) {
class HeaderVisitor : public WebKit::WebHTTPHeaderVisitor {
public:
explicit HeaderVisitor(HeaderMap* map) : map_(map) {}
virtual void visitHeader(const WebKit::WebString& name,
const WebKit::WebString& value) {
map_->insert(std::make_pair(string16(name), string16(value)));
}
private:
HeaderMap* map_;
};
HeaderVisitor visitor(&map);
request.visitHTTPHeaderFields(&visitor);
}
// static
void CefRequestImpl::SetHeaderMap(const HeaderMap& map,
WebKit::WebURLRequest& request) {
HeaderMap::const_iterator it = map.begin();
for (; it != map.end(); ++it)
request.setHTTPHeaderField(string16(it->first), string16(it->second));
}
// CefPostData ----------------------------------------------------------------
// static
CefRefPtr<CefPostData> CefPostData::Create() {
CefRefPtr<CefPostData> postdata(new CefPostDataImpl());
return postdata;
}
CefPostDataImpl::CefPostDataImpl() {
// CefPostDataImpl ------------------------------------------------------------
CefPostDataImpl::CefPostDataImpl()
: read_only_(false) {
}
bool CefPostDataImpl::IsReadOnly() {
AutoLock lock_scope(this);
return read_only_;
}
size_t CefPostDataImpl::GetElementCount() {
@@ -192,6 +361,7 @@ void CefPostDataImpl::GetElements(ElementVector& elements) {
bool CefPostDataImpl::RemoveElement(CefRefPtr<CefPostDataElement> element) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN(false);
ElementVector::iterator it = elements_.begin();
for (; it != elements_.end(); ++it) {
@@ -208,6 +378,7 @@ bool CefPostDataImpl::AddElement(CefRefPtr<CefPostDataElement> element) {
bool found = false;
AutoLock lock_scope(this);
CHECK_READONLY_RETURN(false);
// check that the element isn't already in the list before adding
ElementVector::const_iterator it = elements_.begin();
@@ -226,18 +397,20 @@ bool CefPostDataImpl::AddElement(CefRefPtr<CefPostDataElement> element) {
void CefPostDataImpl::RemoveElements() {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
elements_.clear();
}
void CefPostDataImpl::Set(net::UploadData& data) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
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();
postelem = CefPostDataElement::Create();
static_cast<CefPostDataElementImpl*>(postelem.get())->Set(*it);
AddElement(postelem);
}
@@ -248,7 +421,7 @@ void CefPostDataImpl::Get(net::UploadData& data) {
net::UploadData::Element element;
std::vector<net::UploadData::Element> data_elements;
ElementVector::iterator it = elements_.begin();
ElementVector::const_iterator it = elements_.begin();
for (; it != elements_.end(); ++it) {
static_cast<CefPostDataElementImpl*>(it->get())->Get(element);
data_elements.push_back(element);
@@ -256,32 +429,89 @@ void CefPostDataImpl::Get(net::UploadData& data) {
data.SetElements(data_elements);
}
CefRefPtr<CefPostDataElement> CefPostDataElement::CreatePostDataElement() {
void CefPostDataImpl::Set(const WebKit::WebHTTPBody& data) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
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::Create();
static_cast<CefPostDataElementImpl*>(postelem.get())->Set(element);
AddElement(postelem);
}
}
}
void CefPostDataImpl::Get(WebKit::WebHTTPBody& data) {
AutoLock lock_scope(this);
WebKit::WebHTTPBody::Element element;
ElementVector::iterator it = elements_.begin();
for (; it != elements_.end(); ++it) {
static_cast<CefPostDataElementImpl*>(it->get())->Get(element);
if (element.type == WebKit::WebHTTPBody::Element::TypeData) {
data.appendData(element.data);
} else if (element.type == WebKit::WebHTTPBody::Element::TypeFile) {
data.appendFile(element.filePath);
} else {
NOTREACHED();
}
}
}
void CefPostDataImpl::SetReadOnly(bool read_only) {
AutoLock lock_scope(this);
if (read_only_ == read_only)
return;
read_only_ = read_only;
ElementVector::const_iterator it = elements_.begin();
for (; it != elements_.end(); ++it) {
static_cast<CefPostDataElementImpl*>(it->get())->SetReadOnly(read_only);
}
}
// CefPostDataElement ---------------------------------------------------------
// static
CefRefPtr<CefPostDataElement> CefPostDataElement::Create() {
CefRefPtr<CefPostDataElement> element(new CefPostDataElementImpl());
return element;
}
CefPostDataElementImpl::CefPostDataElementImpl() {
type_ = PDE_TYPE_EMPTY;
// CefPostDataElementImpl -----------------------------------------------------
CefPostDataElementImpl::CefPostDataElementImpl()
: type_(PDE_TYPE_EMPTY),
read_only_(false) {
memset(&data_, 0, sizeof(data_));
}
CefPostDataElementImpl::~CefPostDataElementImpl() {
SetToEmpty();
Cleanup();
}
bool CefPostDataElementImpl::IsReadOnly() {
AutoLock lock_scope(this);
return read_only_;
}
void CefPostDataElementImpl::SetToEmpty() {
AutoLock lock_scope(this);
if (type_ == PDE_TYPE_BYTES)
free(data_.bytes.bytes);
else if (type_ == PDE_TYPE_FILE)
cef_string_clear(&data_.filename);
type_ = PDE_TYPE_EMPTY;
memset(&data_, 0, sizeof(data_));
CHECK_READONLY_RETURN_VOID();
Cleanup();
}
void CefPostDataElementImpl::SetToFile(const CefString& fileName) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
// Clear any data currently in the element
SetToEmpty();
@@ -292,6 +522,8 @@ void CefPostDataElementImpl::SetToFile(const CefString& fileName) {
void CefPostDataElementImpl::SetToBytes(size_t size, const void* bytes) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
// Clear any data currently in the element
SetToEmpty();
@@ -344,6 +576,7 @@ size_t CefPostDataElementImpl::GetBytes(size_t size, void* bytes) {
void CefPostDataElementImpl::Set(const net::UploadData::Element& element) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
if (element.type() == net::UploadData::TYPE_BYTES) {
SetToBytes(element.bytes().size(),
@@ -369,3 +602,52 @@ void CefPostDataElementImpl::Get(net::UploadData::Element& element) {
NOTREACHED();
}
}
void CefPostDataElementImpl::Set(const WebKit::WebHTTPBody::Element& element) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
if (element.type == WebKit::WebHTTPBody::Element::TypeData) {
SetToBytes(element.data.size(),
static_cast<const void*>(element.data.data()));
} else if (element.type == WebKit::WebHTTPBody::Element::TypeFile) {
SetToFile(string16(element.filePath));
} else {
NOTREACHED();
}
}
void CefPostDataElementImpl::Get(WebKit::WebHTTPBody::Element& element) {
AutoLock lock_scope(this);
if (type_ == PDE_TYPE_BYTES) {
element.type = WebKit::WebHTTPBody::Element::TypeData;
element.data.assign(
static_cast<char*>(data_.bytes.bytes), data_.bytes.size);
} else if (type_ == PDE_TYPE_FILE) {
element.type = WebKit::WebHTTPBody::Element::TypeFile;
element.filePath.assign(string16(CefString(&data_.filename)));
} else {
NOTREACHED();
}
}
void CefPostDataElementImpl::SetReadOnly(bool read_only) {
AutoLock lock_scope(this);
if (read_only_ == read_only)
return;
read_only_ = read_only;
}
void CefPostDataElementImpl::Cleanup() {
if (type_ == PDE_TYPE_EMPTY)
return;
if (type_ == PDE_TYPE_BYTES)
free(data_.bytes.bytes);
else if (type_ == PDE_TYPE_FILE)
cef_string_clear(&data_.filename);
type_ = PDE_TYPE_EMPTY;
memset(&data_, 0, sizeof(data_));
}

View File

@@ -9,17 +9,23 @@
#include "include/cef_request.h"
#include "net/base/upload_data.h"
#include "net/http/http_request_headers.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebHTTPBody.h"
namespace net {
class URLRequest;
};
namespace WebKit {
class WebURLRequest;
}
// Implementation of CefRequest
class CefRequestImpl : public CefRequest {
public:
CefRequestImpl();
~CefRequestImpl() {}
virtual bool IsReadOnly() OVERRIDE;
virtual CefString GetURL() OVERRIDE;
virtual void SetURL(const CefString& url) OVERRIDE;
virtual CefString GetMethod() OVERRIDE;
@@ -32,8 +38,8 @@ class CefRequestImpl : public CefRequest {
const CefString& method,
CefRefPtr<CefPostData> postData,
const HeaderMap& headerMap) OVERRIDE;
virtual RequestFlags GetFlags() OVERRIDE;
virtual void SetFlags(RequestFlags flags) OVERRIDE;
virtual int GetFlags() OVERRIDE;
virtual void SetFlags(int flags) OVERRIDE;
virtual CefString GetFirstPartyForCookies() OVERRIDE;
virtual void SetFirstPartyForCookies(const CefString& url) OVERRIDE;
@@ -43,8 +49,20 @@ class CefRequestImpl : public CefRequest {
// Populate the URLRequest object from this object.
void Get(net::URLRequest* request);
// Populate this object from a WebURLRequest object.
void Set(const WebKit::WebURLRequest& request);
// Populate the WebURLRequest object from this object.
void Get(WebKit::WebURLRequest& request);
void SetReadOnly(bool read_only);
static void GetHeaderMap(const net::HttpRequestHeaders& headers,
HeaderMap& map);
static void GetHeaderMap(const WebKit::WebURLRequest& request,
HeaderMap& map);
static void SetHeaderMap(const HeaderMap& map,
WebKit::WebURLRequest& request);
protected:
CefString url_;
@@ -52,10 +70,13 @@ class CefRequestImpl : public CefRequest {
CefRefPtr<CefPostData> postdata_;
HeaderMap headermap_;
// The below methods are used by WebURLRequest.
RequestFlags flags_;
// The below members are used by CefURLRequest.
int flags_;
CefString first_party_for_cookies_;
// True if this object is read-only.
bool read_only_;
IMPLEMENT_REFCOUNTING(CefRequestImpl);
IMPLEMENT_LOCKING(CefRequestImpl);
};
@@ -66,6 +87,7 @@ class CefPostDataImpl : public CefPostData {
CefPostDataImpl();
~CefPostDataImpl() {}
virtual bool IsReadOnly() OVERRIDE;
virtual size_t GetElementCount() OVERRIDE;
virtual void GetElements(ElementVector& elements) OVERRIDE;
virtual bool RemoveElement(CefRefPtr<CefPostDataElement> element) OVERRIDE;
@@ -74,10 +96,17 @@ class CefPostDataImpl : public CefPostData {
void Set(net::UploadData& data);
void Get(net::UploadData& data);
void Set(const WebKit::WebHTTPBody& data);
void Get(WebKit::WebHTTPBody& data);
void SetReadOnly(bool read_only);
protected:
ElementVector elements_;
// True if this object is read-only.
bool read_only_;
IMPLEMENT_REFCOUNTING(CefPostDataImpl);
IMPLEMENT_LOCKING(CefPostDataImpl);
};
@@ -88,6 +117,7 @@ class CefPostDataElementImpl : public CefPostDataElement {
CefPostDataElementImpl();
~CefPostDataElementImpl();
virtual bool IsReadOnly() OVERRIDE;
virtual void SetToEmpty() OVERRIDE;
virtual void SetToFile(const CefString& fileName) OVERRIDE;
virtual void SetToBytes(size_t size, const void* bytes) OVERRIDE;
@@ -100,8 +130,14 @@ class CefPostDataElementImpl : public CefPostDataElement {
void Set(const net::UploadData::Element& element);
void Get(net::UploadData::Element& element);
void Set(const WebKit::WebHTTPBody::Element& element);
void Get(WebKit::WebHTTPBody::Element& element);
void SetReadOnly(bool read_only);
protected:
void Cleanup();
Type type_;
union {
struct {
@@ -111,6 +147,9 @@ class CefPostDataElementImpl : public CefPostDataElement {
cef_string_t filename;
} data_;
// True if this object is read-only.
bool read_only_;
IMPLEMENT_REFCOUNTING(CefPostDataElementImpl);
IMPLEMENT_LOCKING(CefPostDataElementImpl);
};

View File

@@ -4,12 +4,43 @@
#include "libcef/common/response_impl.h"
#include <string>
#include "base/logging.h"
#include "base/stringprintf.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebHTTPHeaderVisitor.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLResponse.h"
#define CHECK_READONLY_RETURN_VOID() \
if (read_only_) { \
NOTREACHED() << "object is read only"; \
return; \
}
// CefResponse ----------------------------------------------------------------
// static
CefRefPtr<CefResponse> CefResponse::Create() {
CefRefPtr<CefResponse> response(new CefResponseImpl());
return response;
}
// CefResponseImpl ------------------------------------------------------------
CefResponseImpl::CefResponseImpl()
: status_code_(0) {
: status_code_(0),
read_only_(false) {
}
bool CefResponseImpl::IsReadOnly() {
AutoLock lock_scope(this);
return read_only_;
}
int CefResponseImpl::GetStatus() {
@@ -19,6 +50,7 @@ int CefResponseImpl::GetStatus() {
void CefResponseImpl::SetStatus(int status) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
status_code_ = status;
}
@@ -29,6 +61,7 @@ CefString CefResponseImpl::GetStatusText() {
void CefResponseImpl::SetStatusText(const CefString& statusText) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
status_text_ = statusText;
}
@@ -39,6 +72,7 @@ CefString CefResponseImpl::GetMimeType() {
void CefResponseImpl::SetMimeType(const CefString& mimeType) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
mime_type_ = mimeType;
}
@@ -61,6 +95,7 @@ void CefResponseImpl::GetHeaderMap(HeaderMap& map) {
void CefResponseImpl::SetHeaderMap(const HeaderMap& headerMap) {
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
header_map_ = headerMap;
}
@@ -69,11 +104,12 @@ net::HttpResponseHeaders* CefResponseImpl::GetResponseHeaders() {
std::string response;
std::string status_text;
bool has_content_type_header = false;
if (status_text_.empty())
status_text = (status_code_ == 200)?"OK":"ERROR";
else
if (!status_text_.empty())
status_text = status_text_;
else
status_text = (status_code_ == 200)?"OK":"ERROR";
base::SStringPrintf(&response, "HTTP/1.1 %d %s", status_code_,
status_text.c_str());
@@ -90,9 +126,79 @@ net::HttpResponseHeaders* CefResponseImpl::GetResponseHeaders() {
std::string value_str(value);
base::StringAppendF(&response, "%c%s: %s", '\0', key_str.c_str(),
value_str.c_str());
if (!has_content_type_header &&
key_str == net::HttpRequestHeaders::kContentType) {
has_content_type_header = true;
}
}
}
}
if (!has_content_type_header) {
std::string mime_type;
if (!mime_type_.empty())
mime_type = mime_type_;
else
mime_type = "text/html";
base::StringAppendF(&response, "%c%s: %s", '\0',
net::HttpRequestHeaders::kContentType, mime_type.c_str());
}
return new net::HttpResponseHeaders(response);
}
void CefResponseImpl::SetResponseHeaders(
const net::HttpResponseHeaders& headers) {
AutoLock lock_scope(this);
header_map_.empty();
void* iter = NULL;
std::string name, value;
while (headers.EnumerateHeaderLines(&iter, &name, &value))
header_map_.insert(std::make_pair(name, value));
status_code_ = headers.response_code();
status_text_ = headers.GetStatusText();
std::string mime_type;
if (headers.GetMimeType(&mime_type))
mime_type_ = mime_type;
}
void CefResponseImpl::Set(const WebKit::WebURLResponse& response) {
DCHECK(!response.isNull());
AutoLock lock_scope(this);
CHECK_READONLY_RETURN_VOID();
WebKit::WebString str;
status_code_ = response.httpStatusCode();
str = response.httpStatusText();
status_text_ = CefString(str);
str = response.mimeType();
mime_type_ = CefString(str);
class HeaderVisitor : public WebKit::WebHTTPHeaderVisitor {
public:
explicit HeaderVisitor(HeaderMap* map) : map_(map) {}
virtual void visitHeader(const WebKit::WebString& name,
const WebKit::WebString& value) {
map_->insert(std::make_pair(string16(name), string16(value)));
}
private:
HeaderMap* map_;
};
HeaderVisitor visitor(&header_map_);
response.visitHTTPHeaderFields(&visitor);
}
void CefResponseImpl::SetReadOnly(bool read_only) {
AutoLock lock_scope(this);
read_only_ = read_only;
}

View File

@@ -12,30 +12,41 @@ namespace net {
class HttpResponseHeaders;
}
namespace WebKit {
class WebURLResponse;
}
// Implementation of CefResponse.
class CefResponseImpl : public CefResponse {
public:
CefResponseImpl();
~CefResponseImpl() {}
// CefResponse API
virtual int GetStatus();
virtual void SetStatus(int status);
virtual CefString GetStatusText();
virtual void SetStatusText(const CefString& statusText);
virtual CefString GetMimeType();
virtual void SetMimeType(const CefString& mimeType);
virtual CefString GetHeader(const CefString& name);
virtual void GetHeaderMap(HeaderMap& headerMap);
virtual void SetHeaderMap(const HeaderMap& headerMap);
// CefResponse methods.
virtual bool IsReadOnly() OVERRIDE;
virtual int GetStatus() OVERRIDE;
virtual void SetStatus(int status) OVERRIDE;
virtual CefString GetStatusText() OVERRIDE;
virtual void SetStatusText(const CefString& statusText) OVERRIDE;
virtual CefString GetMimeType() OVERRIDE;
virtual void SetMimeType(const CefString& mimeType) OVERRIDE;
virtual CefString GetHeader(const CefString& name) OVERRIDE;
virtual void GetHeaderMap(HeaderMap& headerMap) OVERRIDE;
virtual void SetHeaderMap(const HeaderMap& headerMap) OVERRIDE;
net::HttpResponseHeaders* GetResponseHeaders();
void SetResponseHeaders(const net::HttpResponseHeaders& headers);
void Set(const WebKit::WebURLResponse& response);
void SetReadOnly(bool read_only);
protected:
int status_code_;
CefString status_text_;
CefString mime_type_;
HeaderMap header_map_;
bool read_only_;
IMPLEMENT_REFCOUNTING(CefResponseImpl);
IMPLEMENT_LOCKING(CefResponseImpl);

View File

@@ -0,0 +1,45 @@
// Copyright (c) 2012 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.
#include "include/cef_urlrequest.h"
#include "libcef/browser/browser_urlrequest_impl.h"
#include "libcef/renderer/render_urlrequest_impl.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "content/public/common/content_client.h"
// static
CefRefPtr<CefURLRequest> CefURLRequest::Create(
CefRefPtr<CefRequest> request,
CefRefPtr<CefURLRequestClient> client) {
if (!request.get() || !client.get()) {
NOTREACHED() << "called with invalid parameters";
return NULL;
}
if (!MessageLoop::current()) {
NOTREACHED() << "called on invalid thread";
return NULL;
}
if (content::GetContentClient()->browser()) {
// In the browser process.
CefRefPtr<CefBrowserURLRequest> impl =
new CefBrowserURLRequest(request, client);
if (impl->Start())
return impl.get();
return NULL;
} else if (content::GetContentClient()->renderer()) {
// In the render process.
CefRefPtr<CefRenderURLRequest> impl =
new CefRenderURLRequest(request, client);
if (impl->Start())
return impl.get();
return NULL;
} else {
NOTREACHED() << "called in unsupported process";
return NULL;
}
}