2011-05-27 18:20:32 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors.
|
2010-10-03 23:04:50 +02:00
|
|
|
// Portions copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
#include "include/cef.h"
|
|
|
|
#include "cef_context.h"
|
|
|
|
#include "request_impl.h"
|
|
|
|
#include "response_impl.h"
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "base/lazy_instance.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/message_loop.h"
|
2011-05-27 18:20:32 +02:00
|
|
|
#include "base/string_util.h"
|
|
|
|
#include "base/synchronization/lock.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "googleurl/src/url_util.h"
|
|
|
|
#include "net/base/completion_callback.h"
|
|
|
|
#include "net/base/io_buffer.h"
|
|
|
|
#include "net/base/upload_data.h"
|
2011-04-21 18:46:16 +02:00
|
|
|
#include "net/http/http_response_headers.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "net/http/http_util.h"
|
|
|
|
#include "net/url_request/url_request.h"
|
2011-05-27 18:20:32 +02:00
|
|
|
#include "net/url_request/url_request_about_job.h"
|
|
|
|
#include "net/url_request/url_request_data_job.h"
|
|
|
|
#include "net/url_request/url_request_error_job.h"
|
|
|
|
#include "net/url_request/url_request_file_job.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "net/url_request/url_request_filter.h"
|
2011-05-27 18:20:32 +02:00
|
|
|
#include "net/url_request/url_request_ftp_job.h"
|
|
|
|
#include "net/url_request/url_request_http_job.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "net/url_request/url_request_job.h"
|
2011-05-27 18:20:32 +02:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
using WebKit::WebSecurityPolicy;
|
|
|
|
using WebKit::WebString;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
namespace {
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
bool IsStandardScheme(const std::string& scheme)
|
|
|
|
{
|
|
|
|
url_parse::Component scheme_comp(0, scheme.length());
|
|
|
|
return url_util::IsStandard(scheme.c_str(), scheme_comp);
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
void RegisterStandardScheme(const std::string& scheme)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-05-27 18:20:32 +02:00
|
|
|
REQUIRE_UIT();
|
|
|
|
url_parse::Component scheme_comp(0, scheme.length());
|
|
|
|
if (!url_util::IsStandard(scheme.c_str(), scheme_comp))
|
|
|
|
url_util::AddStandardScheme(scheme.c_str());
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Copied from net/url_request/url_request_job_manager.cc.
|
|
|
|
struct SchemeToFactory {
|
|
|
|
const char* scheme;
|
|
|
|
net::URLRequest::ProtocolFactory* factory;
|
|
|
|
};
|
|
|
|
static const SchemeToFactory kBuiltinFactories[] = {
|
|
|
|
{ "http", net::URLRequestHttpJob::Factory },
|
|
|
|
{ "https", net::URLRequestHttpJob::Factory },
|
|
|
|
{ "file", net::URLRequestFileJob::Factory },
|
|
|
|
{ "ftp", net::URLRequestFtpJob::Factory },
|
|
|
|
{ "about", net::URLRequestAboutJob::Factory },
|
|
|
|
{ "data", net::URLRequestDataJob::Factory },
|
2010-10-03 23:04:50 +02:00
|
|
|
};
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
bool IsBuiltinScheme(const std::string& scheme)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-05-27 18:20:32 +02:00
|
|
|
for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i)
|
|
|
|
if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme))
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
net::URLRequestJob* GetBuiltinSchemeRequestJob(net::URLRequest* request,
|
|
|
|
const std::string& scheme)
|
|
|
|
{
|
|
|
|
// See if the request should be handled by a built-in protocol factory.
|
|
|
|
for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) {
|
|
|
|
if (scheme == kBuiltinFactories[i].scheme) {
|
|
|
|
net::URLRequestJob* job = (kBuiltinFactories[i].factory)(request, scheme);
|
|
|
|
DCHECK(job); // The built-in factories are not expected to fail!
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToLower(const std::string& str)
|
|
|
|
{
|
|
|
|
std::string str_lower = str;
|
|
|
|
std::transform(str_lower.begin(), str_lower.end(), str_lower.begin(),
|
|
|
|
towlower);
|
|
|
|
return str;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// net::URLRequestJob implementation.
|
2010-12-16 22:54:42 +01:00
|
|
|
class CefUrlRequestJob : public net::URLRequestJob {
|
2010-10-03 23:04:50 +02:00
|
|
|
public:
|
2011-05-27 18:20:32 +02:00
|
|
|
CefUrlRequestJob(net::URLRequest* request,
|
|
|
|
CefRefPtr<CefSchemeHandler> handler)
|
2010-12-16 22:54:42 +01:00
|
|
|
: net::URLRequestJob(request),
|
2010-10-03 23:04:50 +02:00
|
|
|
handler_(handler),
|
|
|
|
response_length_(0),
|
2010-10-10 02:16:24 +02:00
|
|
|
url_(request->url()),
|
2010-10-03 23:04:50 +02:00
|
|
|
remaining_bytes_(0) { }
|
|
|
|
|
|
|
|
virtual ~CefUrlRequestJob(){}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void Start() OVERRIDE
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
handler_->Cancel();
|
|
|
|
// Continue asynchronously.
|
|
|
|
DCHECK(!async_resolver_);
|
2011-04-21 18:46:16 +02:00
|
|
|
response_ = new CefResponseImpl();
|
2010-10-03 23:04:50 +02:00
|
|
|
async_resolver_ = new AsyncResolver(this);
|
|
|
|
CefThread::PostTask(CefThread::IO, FROM_HERE, NewRunnableMethod(
|
|
|
|
async_resolver_.get(), &AsyncResolver::Resolve, url_));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void Kill() OVERRIDE
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
if (async_resolver_) {
|
|
|
|
async_resolver_->Cancel();
|
|
|
|
async_resolver_ = NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
net::URLRequestJob::Kill();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool ReadRawData(net::IOBuffer* dest, int dest_size, int *bytes_read)
|
2011-05-20 16:42:25 +02:00
|
|
|
OVERRIDE
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
DCHECK_NE(dest_size, 0);
|
|
|
|
DCHECK(bytes_read);
|
|
|
|
|
|
|
|
// When remaining_bytes_>=0, it means the handler knows the content size
|
|
|
|
// before hand. We continue to read until
|
|
|
|
if (remaining_bytes_>=0) {
|
|
|
|
if (remaining_bytes_ < dest_size)
|
|
|
|
dest_size = static_cast<int>(remaining_bytes_);
|
|
|
|
|
|
|
|
// If we should copy zero bytes because |remaining_bytes_| is zero, short
|
|
|
|
// circuit here.
|
|
|
|
if (!dest_size) {
|
|
|
|
*bytes_read = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remaining_bytes > 0
|
|
|
|
bool rv = handler_->ReadResponse(dest->data(), dest_size, bytes_read);
|
|
|
|
remaining_bytes_ -= *bytes_read;
|
|
|
|
if (!rv) {
|
|
|
|
// handler indicated no further data to read
|
|
|
|
*bytes_read = 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// The handler returns -1 for GetResponseLength, this means the handler
|
|
|
|
// doesn't know the content size before hand. We do basically the same
|
|
|
|
// thing, except for checking the return value for handler_->ReadResponse,
|
|
|
|
// which is an indicator for no further data to be read.
|
|
|
|
bool rv = handler_->ReadResponse(dest->data(), dest_size, bytes_read);
|
|
|
|
if (!rv)
|
|
|
|
// handler indicated no further data to read
|
|
|
|
*bytes_read = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE {
|
2011-04-21 18:46:16 +02:00
|
|
|
CefResponseImpl* responseImpl =
|
|
|
|
static_cast<CefResponseImpl*>(response_.get());
|
2011-05-26 20:11:56 +02:00
|
|
|
info->headers = responseImpl->GetResponseHeaders();
|
2011-04-21 18:46:16 +02:00
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
virtual bool IsRedirectResponse(GURL* location, int* http_status_code)
|
2011-05-20 16:42:25 +02:00
|
|
|
OVERRIDE
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-04-21 20:17:46 +02:00
|
|
|
if (redirect_url_.is_valid()) {
|
|
|
|
// Redirect to the new URL.
|
|
|
|
*http_status_code = 303;
|
|
|
|
location->Swap(&redirect_url_);
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual bool GetMimeType(std::string* mime_type) const OVERRIDE
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
DCHECK(request_);
|
|
|
|
// call handler to get mime type
|
2011-04-21 18:46:16 +02:00
|
|
|
*mime_type = response_->GetMimeType();
|
2010-10-03 23:04:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefSchemeHandler> handler_;
|
2011-04-21 18:46:16 +02:00
|
|
|
CefRefPtr<CefResponse> response_;
|
2010-10-03 23:04:50 +02:00
|
|
|
int response_length_;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GURL url_;
|
2011-04-21 20:17:46 +02:00
|
|
|
GURL redirect_url_;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void DidResolve(const GURL& url)
|
|
|
|
{
|
|
|
|
async_resolver_ = NULL;
|
|
|
|
|
|
|
|
// We may have been orphaned...
|
|
|
|
if (!request_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
remaining_bytes_ = response_length_;
|
|
|
|
if (remaining_bytes_>0)
|
|
|
|
set_expected_content_size(remaining_bytes_);
|
|
|
|
NotifyHeadersComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 remaining_bytes_;
|
|
|
|
std::string m_response;
|
|
|
|
|
|
|
|
class AsyncResolver :
|
|
|
|
public base::RefCountedThreadSafe<AsyncResolver> {
|
|
|
|
public:
|
|
|
|
explicit AsyncResolver(CefUrlRequestJob* owner)
|
|
|
|
: owner_(owner), owner_loop_(MessageLoop::current()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resolve(const GURL& url) {
|
2011-02-15 19:07:24 +01:00
|
|
|
base::AutoLock locked(lock_);
|
2010-10-03 23:04:50 +02:00
|
|
|
if (!owner_ || !owner_loop_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// safe to perform long operation here
|
|
|
|
CefRefPtr<CefRequest> req(CefRequest::CreateRequest());
|
|
|
|
|
|
|
|
// populate the request data
|
|
|
|
static_cast<CefRequestImpl*>(req.get())->Set(owner_->request());
|
|
|
|
|
|
|
|
owner_->handler_->Cancel();
|
2011-04-21 20:17:46 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
int response_length = 0;
|
2011-04-21 20:17:46 +02:00
|
|
|
CefString redirectUrl;
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// handler should complete content generation in ProcessRequest
|
2011-04-21 20:17:46 +02:00
|
|
|
bool res = owner_->handler_->ProcessRequest(req, redirectUrl,
|
|
|
|
owner_->response_, &response_length);
|
|
|
|
if (res) {
|
|
|
|
if (!redirectUrl.empty()) {
|
|
|
|
// Treat the request as a redirect.
|
|
|
|
std::string redirectUrlStr = redirectUrl;
|
|
|
|
owner_->redirect_url_ = GURL(redirectUrlStr);
|
|
|
|
owner_->response_length_ = 0;
|
|
|
|
} else {
|
|
|
|
owner_->response_length_ = response_length;
|
|
|
|
}
|
|
|
|
}
|
2011-04-21 18:46:16 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
if (owner_loop_) {
|
|
|
|
owner_loop_->PostTask(FROM_HERE, NewRunnableMethod(
|
|
|
|
this, &AsyncResolver::ReturnResults, url));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() {
|
|
|
|
owner_->handler_->Cancel();
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
base::AutoLock locked(lock_);
|
2010-10-03 23:04:50 +02:00
|
|
|
owner_ = NULL;
|
|
|
|
owner_loop_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ReturnResults(const GURL& url) {
|
|
|
|
if (owner_)
|
|
|
|
owner_->DidResolve(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefUrlRequestJob* owner_;
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
base::Lock lock_;
|
2010-10-03 23:04:50 +02:00
|
|
|
MessageLoop* owner_loop_;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class AsyncResolver;
|
|
|
|
scoped_refptr<AsyncResolver> async_resolver_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefUrlRequestJob);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Class that manages the CefSchemeHandlerFactory instances.
|
|
|
|
class CefUrlRequestManager {
|
2010-10-03 23:04:50 +02:00
|
|
|
public:
|
2011-05-27 18:20:32 +02:00
|
|
|
CefUrlRequestManager() {}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Retrieve the singleton instance.
|
|
|
|
static CefUrlRequestManager* GetInstance();
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
bool AddFactory(const std::string& scheme,
|
|
|
|
const std::string& domain,
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> factory)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-05-27 18:20:32 +02:00
|
|
|
if (!factory.get()) {
|
|
|
|
RemoveFactory(scheme, domain);
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
REQUIRE_IOT();
|
|
|
|
|
|
|
|
std::string scheme_lower = ToLower(scheme);
|
|
|
|
std::string domain_lower = ToLower(domain);
|
|
|
|
|
|
|
|
// Hostname is only supported for standard schemes.
|
|
|
|
if (!IsStandardScheme(scheme_lower))
|
|
|
|
domain_lower.clear();
|
|
|
|
|
|
|
|
handler_map_[make_pair(scheme_lower, domain_lower)] = factory;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Register with the ProtocolFactory.
|
2011-05-27 18:20:32 +02:00
|
|
|
net::URLRequest::RegisterProtocolFactory(scheme_lower,
|
|
|
|
&CefUrlRequestManager::Factory);
|
|
|
|
|
|
|
|
return true;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
void RemoveFactory(const std::string& scheme,
|
|
|
|
const std::string& domain)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-05-27 18:20:32 +02:00
|
|
|
REQUIRE_IOT();
|
|
|
|
|
|
|
|
std::string scheme_lower = ToLower(scheme);
|
|
|
|
std::string domain_lower = ToLower(domain);
|
|
|
|
|
|
|
|
// Hostname is only supported for standard schemes.
|
|
|
|
if (!IsStandardScheme(scheme_lower))
|
|
|
|
domain_lower.clear();
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
HandlerMap::iterator iter =
|
|
|
|
handler_map_.find(make_pair(scheme_lower, domain_lower));
|
|
|
|
if (iter != handler_map_.end())
|
|
|
|
handler_map_.erase(iter);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Clear all the existing URL handlers and unregister the ProtocolFactory.
|
|
|
|
void ClearFactories()
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-05-27 18:20:32 +02:00
|
|
|
REQUIRE_IOT();
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Unregister with the ProtocolFactory.
|
|
|
|
std::set<std::string> schemes;
|
|
|
|
for (HandlerMap::const_iterator i = handler_map_.begin();
|
|
|
|
i != handler_map_.end(); ++i) {
|
|
|
|
schemes.insert(i->first.first);
|
|
|
|
}
|
|
|
|
for (std::set<std::string>::const_iterator scheme = schemes.begin();
|
|
|
|
scheme != schemes.end(); ++scheme) {
|
2010-12-16 22:54:42 +01:00
|
|
|
net::URLRequest::RegisterProtocolFactory(*scheme, NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handler_map_.clear();
|
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Check if a scheme has already been registered.
|
|
|
|
bool HasRegisteredScheme(const std::string& scheme)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-05-27 18:20:32 +02:00
|
|
|
std::string scheme_lower = ToLower(scheme);
|
|
|
|
|
|
|
|
// Don't register builtin schemes.
|
|
|
|
if (IsBuiltinScheme(scheme_lower))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
scheme_set_lock_.Acquire();
|
|
|
|
bool registered = (scheme_set_.find(scheme_lower) != scheme_set_.end());
|
|
|
|
scheme_set_lock_.Release();
|
|
|
|
return registered;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a scheme.
|
|
|
|
bool RegisterScheme(const std::string& scheme,
|
|
|
|
bool is_standard,
|
|
|
|
bool is_local,
|
|
|
|
bool is_display_isolated)
|
|
|
|
{
|
|
|
|
if (HasRegisteredScheme(scheme)) {
|
|
|
|
NOTREACHED() << "Scheme already registered: " << scheme;
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
std::string scheme_lower = ToLower(scheme);
|
|
|
|
|
|
|
|
scheme_set_lock_.Acquire();
|
|
|
|
scheme_set_.insert(scheme_lower);
|
|
|
|
scheme_set_lock_.Release();
|
|
|
|
|
|
|
|
if (is_standard)
|
|
|
|
RegisterStandardScheme(scheme_lower);
|
|
|
|
if (is_local) {
|
|
|
|
WebSecurityPolicy::registerURLSchemeAsLocal(
|
|
|
|
WebString::fromUTF8(scheme_lower));
|
|
|
|
}
|
|
|
|
if (is_display_isolated) {
|
|
|
|
WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
|
|
|
|
WebString::fromUTF8(scheme_lower));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Retrieve the matching handler factory, if any. |scheme| will already be in
|
|
|
|
// lower case.
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> GetHandlerFactory(
|
|
|
|
net::URLRequest* request, const std::string& scheme)
|
|
|
|
{
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> factory;
|
|
|
|
|
|
|
|
if (request->url().is_valid() && IsStandardScheme(scheme)) {
|
|
|
|
// Check for a match with a domain first.
|
|
|
|
const std::string& domain = request->url().host();
|
|
|
|
|
|
|
|
HandlerMap::iterator i = handler_map_.find(make_pair(scheme, domain));
|
2010-10-03 23:04:50 +02:00
|
|
|
if (i != handler_map_.end())
|
|
|
|
factory = i->second;
|
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
if (!factory.get()) {
|
|
|
|
// Check for a match with no specified domain.
|
2010-10-03 23:04:50 +02:00
|
|
|
HandlerMap::iterator i =
|
|
|
|
handler_map_.find(make_pair(scheme, std::string()));
|
|
|
|
if (i != handler_map_.end())
|
|
|
|
factory = i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return factory;
|
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Create the job that will handle the request. |scheme| will already be in
|
|
|
|
// lower case.
|
|
|
|
net::URLRequestJob* GetRequestJob(net::URLRequest* request,
|
|
|
|
const std::string& scheme)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2010-12-16 22:54:42 +01:00
|
|
|
net::URLRequestJob* job = NULL;
|
2011-05-27 18:20:32 +02:00
|
|
|
CefRefPtr<CefSchemeHandlerFactory> factory =
|
|
|
|
GetHandlerFactory(request, scheme);
|
2010-10-03 23:04:50 +02:00
|
|
|
if (factory) {
|
2011-05-27 18:20:32 +02:00
|
|
|
// Call the handler factory to create the handler for the request.
|
|
|
|
CefRefPtr<CefRequest> requestPtr(new CefRequestImpl());
|
|
|
|
static_cast<CefRequestImpl*>(requestPtr.get())->Set(request);
|
|
|
|
CefRefPtr<CefSchemeHandler> handler = factory->Create(scheme, requestPtr);
|
2010-10-03 23:04:50 +02:00
|
|
|
if (handler.get())
|
|
|
|
job = new CefUrlRequestJob(request, handler);
|
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
if (!job && IsBuiltinScheme(scheme)) {
|
|
|
|
// Give the built-in scheme handler a chance to handle the request.
|
|
|
|
job = GetBuiltinSchemeRequestJob(request, scheme);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2011-05-27 18:20:32 +02:00
|
|
|
|
|
|
|
if (job)
|
|
|
|
DLOG(INFO) << "CefUrlRequestManager hit for " << request->url().spec();
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Factory method called by the ProtocolFactory. |scheme| will already be in
|
|
|
|
// lower case.
|
|
|
|
static net::URLRequestJob* Factory(net::URLRequest* request,
|
|
|
|
const std::string& scheme)
|
|
|
|
{
|
|
|
|
REQUIRE_IOT();
|
|
|
|
return GetInstance()->GetRequestJob(request, scheme);
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Map (scheme, domain) to factories. This map will only be accessed on the IO
|
|
|
|
// thread.
|
|
|
|
typedef std::map<std::pair<std::string, std::string>,
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> > HandlerMap;
|
|
|
|
HandlerMap handler_map_;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
// Set of registered schemes. This set may be accessed from multiple threads.
|
|
|
|
typedef std::set<std::string> SchemeSet;
|
|
|
|
SchemeSet scheme_set_;
|
|
|
|
base::Lock scheme_set_lock_;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
DISALLOW_EVIL_CONSTRUCTORS(CefUrlRequestManager);
|
2010-10-03 23:04:50 +02:00
|
|
|
};
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
base::LazyInstance<CefUrlRequestManager> g_manager(base::LINKER_INITIALIZED);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
CefUrlRequestManager* CefUrlRequestManager::GetInstance()
|
|
|
|
{
|
|
|
|
return g_manager.Pointer();
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
} // anonymous
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
bool CefRegisterCustomScheme(const CefString& scheme_name,
|
|
|
|
bool is_standard,
|
|
|
|
bool is_local,
|
|
|
|
bool is_display_isolated)
|
|
|
|
{
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
if (CefThread::CurrentlyOn(CefThread::UI)) {
|
|
|
|
// Must be executed on the UI thread because it calls WebKit APIs.
|
|
|
|
return CefUrlRequestManager::GetInstance()->RegisterScheme(scheme_name,
|
|
|
|
is_standard, is_local, is_display_isolated);
|
|
|
|
} else {
|
|
|
|
// Verify that the scheme has not already been registered.
|
|
|
|
if (CefUrlRequestManager::GetInstance()->HasRegisteredScheme(scheme_name)) {
|
|
|
|
NOTREACHED() << "Scheme already registered: " << scheme_name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefThread::PostTask(CefThread::UI, FROM_HERE,
|
|
|
|
NewRunnableFunction(&CefRegisterCustomScheme, scheme_name, is_standard,
|
|
|
|
is_local, is_display_isolated));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
bool CefRegisterSchemeHandlerFactory(const CefString& scheme_name,
|
|
|
|
const CefString& domain_name,
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> factory)
|
|
|
|
{
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
if (CefThread::CurrentlyOn(CefThread::IO)) {
|
|
|
|
return CefUrlRequestManager::GetInstance()->AddFactory(scheme_name,
|
|
|
|
domain_name,
|
|
|
|
factory);
|
|
|
|
} else {
|
|
|
|
CefThread::PostTask(CefThread::IO, FROM_HERE,
|
|
|
|
NewRunnableFunction(&CefRegisterSchemeHandlerFactory, scheme_name,
|
|
|
|
domain_name, factory));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
bool CefClearSchemeHandlerFactories()
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-01-07 02:06:10 +01:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED();
|
2010-10-03 23:04:50 +02:00
|
|
|
return false;
|
2011-01-07 02:06:10 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-27 18:20:32 +02:00
|
|
|
if (CefThread::CurrentlyOn(CefThread::IO)) {
|
|
|
|
CefUrlRequestManager::GetInstance()->ClearFactories();
|
|
|
|
} else {
|
|
|
|
CefThread::PostTask(CefThread::IO, FROM_HERE,
|
|
|
|
NewRunnableFunction(&CefClearSchemeHandlerFactories));
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|