2012-04-03 03:34:16 +02:00
|
|
|
// 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 "libcef/browser/url_request_interceptor.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
|
|
|
#include "libcef/browser/resource_request_job.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "libcef/common/request_impl.h"
|
|
|
|
|
|
|
|
#include "net/url_request/url_request_job_manager.h"
|
|
|
|
#include "net/url_request/url_request_redirect_job.h"
|
|
|
|
|
|
|
|
CefRequestInterceptor::CefRequestInterceptor() {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
net::URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRequestInterceptor::~CefRequestInterceptor() {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
net::URLRequestJobManager::GetInstance()->
|
|
|
|
UnregisterRequestInterceptor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestJob* CefRequestInterceptor::MaybeIntercept(
|
2012-08-29 00:26:35 +02:00
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser =
|
|
|
|
CefBrowserHostImpl::GetBrowserForRequest(request);
|
|
|
|
if (browser.get()) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefRequestHandler> handler = client->GetRequestHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefFrame> frame = browser->GetFrameForRequest(request);
|
|
|
|
|
|
|
|
// Populate the request data.
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefRequest> req(CefRequest::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
static_cast<CefRequestImpl*>(req.get())->Set(request);
|
|
|
|
|
|
|
|
// Give the client an opportunity to replace the request.
|
|
|
|
CefRefPtr<CefResourceHandler> resourceHandler =
|
|
|
|
handler->GetResourceHandler(browser.get(), frame, req);
|
|
|
|
if (resourceHandler.get())
|
2012-08-29 00:26:35 +02:00
|
|
|
return new CefResourceRequestJob(request, network_delegate,
|
|
|
|
resourceHandler);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestJob* CefRequestInterceptor::MaybeInterceptRedirect(
|
2012-08-29 00:26:35 +02:00
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate,
|
|
|
|
const GURL& location) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser =
|
|
|
|
CefBrowserHostImpl::GetBrowserForRequest(request);
|
|
|
|
if (browser.get()) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefRequestHandler> handler = client->GetRequestHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefFrame> frame = browser->GetFrameForRequest(request);
|
|
|
|
|
|
|
|
// Give the client an opportunity to redirect the request.
|
|
|
|
CefString newUrlStr = location.spec();
|
|
|
|
handler->OnResourceRedirect(browser.get(), frame, request->url().spec(),
|
|
|
|
newUrlStr);
|
|
|
|
if (newUrlStr != location.spec()) {
|
|
|
|
GURL new_url = GURL(std::string(newUrlStr));
|
2012-11-30 20:08:20 +01:00
|
|
|
if (!new_url.is_empty() && new_url.is_valid()) {
|
|
|
|
return new net::URLRequestRedirectJob(
|
|
|
|
request, network_delegate, new_url,
|
|
|
|
net::URLRequestRedirectJob::REDIRECT_302_FOUND);
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestJob* CefRequestInterceptor::MaybeInterceptResponse(
|
2012-08-29 00:26:35 +02:00
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return NULL;
|
|
|
|
}
|