This source file includes following definitions.
- SetCacheControlFlag
- render_process_id
- render_frame_id
- request_
- GetNameForLogging
- WillStartRequest
- WillRedirectRequest
- OnIoThreadClientReady
- MaybeBlockRequest
- ShouldBlockRequest
- ResourceDispatcherHostCreated
- RequestBeginning
- OnRequestRedirected
- DownloadStarting
- CreateLoginDelegate
- HandleExternalProtocol
- OnResponseStarted
- RemovePendingThrottleOnIoThread
- OnIoThreadClientReady
- AddPendingThrottle
- AddPendingThrottleOnIoThread
- OnIoThreadClientReadyInternal
- AddExtraHeadersIfNeeded
#include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
#include <string>
#include "android_webview/browser/aw_contents_io_thread_client.h"
#include "android_webview/browser/aw_login_delegate.h"
#include "android_webview/browser/aw_resource_context.h"
#include "android_webview/common/url_constants.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "components/auto_login_parser/auto_login_parser.h"
#include "components/navigation_interception/intercept_navigation_delegate.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_controller.h"
#include "content/public/browser/resource_dispatcher_host.h"
#include "content/public/browser/resource_dispatcher_host_login_delegate.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/resource_throttle.h"
#include "content/public/common/url_constants.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
using android_webview::AwContentsIoThreadClient;
using content::BrowserThread;
using navigation_interception::InterceptNavigationDelegate;
namespace {
base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate>
g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER;
void SetCacheControlFlag(
net::URLRequest* request, int flag) {
const int all_cache_control_flags = net::LOAD_BYPASS_CACHE |
net::LOAD_VALIDATE_CACHE |
net::LOAD_PREFERRING_CACHE |
net::LOAD_ONLY_FROM_CACHE;
DCHECK((flag & all_cache_control_flags) == flag);
int load_flags = request->load_flags();
load_flags &= ~all_cache_control_flags;
load_flags |= flag;
request->SetLoadFlags(load_flags);
}
}
namespace android_webview {
class IoThreadClientThrottle : public content::ResourceThrottle {
public:
IoThreadClientThrottle(int render_process_id,
int render_frame_id,
net::URLRequest* request);
virtual ~IoThreadClientThrottle();
virtual void WillStartRequest(bool* defer) OVERRIDE;
virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
virtual const char* GetNameForLogging() const OVERRIDE;
void OnIoThreadClientReady(int new_render_process_id,
int new_render_frame_id);
bool MaybeBlockRequest();
bool ShouldBlockRequest();
int render_process_id() const { return render_process_id_; }
int render_frame_id() const { return render_frame_id_; }
private:
int render_process_id_;
int render_frame_id_;
net::URLRequest* request_;
};
IoThreadClientThrottle::IoThreadClientThrottle(int render_process_id,
int render_frame_id,
net::URLRequest* request)
: render_process_id_(render_process_id),
render_frame_id_(render_frame_id),
request_(request) { }
IoThreadClientThrottle::~IoThreadClientThrottle() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
g_webview_resource_dispatcher_host_delegate.Get().
RemovePendingThrottleOnIoThread(this);
}
const char* IoThreadClientThrottle::GetNameForLogging() const {
return "IoThreadClientThrottle";
}
void IoThreadClientThrottle::WillStartRequest(bool* defer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (render_frame_id_ < 1) {
DCHECK_EQ("OPTIONS", request_->method());
return;
}
DCHECK(render_process_id_);
*defer = false;
scoped_ptr<AwContentsIoThreadClient> io_client =
AwContentsIoThreadClient::FromID(render_process_id_, render_frame_id_);
if (io_client && io_client->PendingAssociation()) {
*defer = true;
AwResourceDispatcherHostDelegate::AddPendingThrottle(
render_process_id_, render_frame_id_, this);
} else {
MaybeBlockRequest();
}
}
void IoThreadClientThrottle::WillRedirectRequest(const GURL& new_url,
bool* defer) {
WillStartRequest(defer);
}
void IoThreadClientThrottle::OnIoThreadClientReady(int new_render_process_id,
int new_render_frame_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!MaybeBlockRequest()) {
controller()->Resume();
}
}
bool IoThreadClientThrottle::MaybeBlockRequest() {
if (ShouldBlockRequest()) {
controller()->CancelWithError(net::ERR_ACCESS_DENIED);
return true;
}
return false;
}
bool IoThreadClientThrottle::ShouldBlockRequest() {
scoped_ptr<AwContentsIoThreadClient> io_client =
AwContentsIoThreadClient::FromID(render_process_id_, render_frame_id_);
if (!io_client)
return false;
if (request_->url().SchemeIs(android_webview::kContentScheme) &&
io_client->ShouldBlockContentUrls()) {
return true;
}
if (request_->url().SchemeIsFile() &&
io_client->ShouldBlockFileUrls()) {
const GURL& url = request_->url();
if (!url.has_path() ||
(url.path().find(android_webview::kAndroidResourcePath) != 0 &&
url.path().find(android_webview::kAndroidAssetPath) != 0)) {
return true;
}
}
if (io_client->ShouldBlockNetworkLoads()) {
if (request_->url().SchemeIs(content::kFtpScheme)) {
return true;
}
SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE);
} else {
AwContentsIoThreadClient::CacheMode cache_mode = io_client->GetCacheMode();
switch(cache_mode) {
case AwContentsIoThreadClient::LOAD_CACHE_ELSE_NETWORK:
SetCacheControlFlag(request_, net::LOAD_PREFERRING_CACHE);
break;
case AwContentsIoThreadClient::LOAD_NO_CACHE:
SetCacheControlFlag(request_, net::LOAD_BYPASS_CACHE);
break;
case AwContentsIoThreadClient::LOAD_CACHE_ONLY:
SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE);
break;
default:
break;
}
}
return false;
}
void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() {
content::ResourceDispatcherHost::Get()->SetDelegate(
&g_webview_resource_dispatcher_host_delegate.Get());
}
AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate()
: content::ResourceDispatcherHostDelegate() {
}
AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() {
}
void AwResourceDispatcherHostDelegate::RequestBeginning(
net::URLRequest* request,
content::ResourceContext* resource_context,
appcache::AppCacheService* appcache_service,
ResourceType::Type resource_type,
int child_id,
int route_id,
ScopedVector<content::ResourceThrottle>* throttles) {
AddExtraHeadersIfNeeded(request, resource_context);
const content::ResourceRequestInfo* request_info =
content::ResourceRequestInfo::ForRequest(request);
throttles->push_back(new IoThreadClientThrottle(
child_id, request_info->GetRenderFrameID(), request));
if (resource_type == ResourceType::MAIN_FRAME)
throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor(
request));
}
void AwResourceDispatcherHostDelegate::OnRequestRedirected(
const GURL& redirect_url,
net::URLRequest* request,
content::ResourceContext* resource_context,
content::ResourceResponse* response) {
AddExtraHeadersIfNeeded(request, resource_context);
}
void AwResourceDispatcherHostDelegate::DownloadStarting(
net::URLRequest* request,
content::ResourceContext* resource_context,
int child_id,
int route_id,
int request_id,
bool is_content_initiated,
bool must_download,
ScopedVector<content::ResourceThrottle>* throttles) {
GURL url(request->url());
std::string user_agent;
std::string content_disposition;
std::string mime_type;
int64 content_length = request->GetExpectedContentSize();
request->extra_request_headers().GetHeader(
net::HttpRequestHeaders::kUserAgent, &user_agent);
net::HttpResponseHeaders* response_headers = request->response_headers();
if (response_headers) {
response_headers->GetNormalizedHeader("content-disposition",
&content_disposition);
response_headers->GetMimeType(&mime_type);
}
request->Cancel();
const content::ResourceRequestInfo* request_info =
content::ResourceRequestInfo::ForRequest(request);
scoped_ptr<AwContentsIoThreadClient> io_client =
AwContentsIoThreadClient::FromID(
child_id, request_info->GetRenderFrameID());
if ("GET" == request->method() && io_client) {
io_client->NewDownload(url,
user_agent,
content_disposition,
mime_type,
content_length);
}
}
content::ResourceDispatcherHostLoginDelegate*
AwResourceDispatcherHostDelegate::CreateLoginDelegate(
net::AuthChallengeInfo* auth_info,
net::URLRequest* request) {
return new AwLoginDelegate(auth_info, request);
}
bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(
const GURL& url,
int child_id,
int route_id,
bool initiated_by_user_gesture) {
NOTREACHED();
return false;
}
void AwResourceDispatcherHostDelegate::OnResponseStarted(
net::URLRequest* request,
content::ResourceContext* resource_context,
content::ResourceResponse* response,
IPC::Sender* sender) {
const content::ResourceRequestInfo* request_info =
content::ResourceRequestInfo::ForRequest(request);
if (!request_info) {
DLOG(FATAL) << "Started request without associated info: " <<
request->url();
return;
}
if (request_info->GetResourceType() == ResourceType::MAIN_FRAME) {
auto_login_parser::HeaderData header_data;
if (auto_login_parser::ParserHeaderInResponse(
request, auto_login_parser::ALLOW_ANY_REALM, &header_data)) {
scoped_ptr<AwContentsIoThreadClient> io_client =
AwContentsIoThreadClient::FromID(request_info->GetChildID(),
request_info->GetRenderFrameID());
if (io_client) {
io_client->NewLoginRequest(
header_data.realm, header_data.account, header_data.args);
}
}
}
}
void AwResourceDispatcherHostDelegate::RemovePendingThrottleOnIoThread(
IoThreadClientThrottle* throttle) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
PendingThrottleMap::iterator it = pending_throttles_.find(
FrameRouteIDPair(throttle->render_process_id(),
throttle->render_frame_id()));
if (it != pending_throttles_.end()) {
pending_throttles_.erase(it);
}
}
void AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
int new_render_process_id,
int new_render_frame_id) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(
&AwResourceDispatcherHostDelegate::OnIoThreadClientReadyInternal,
base::Unretained(
g_webview_resource_dispatcher_host_delegate.Pointer()),
new_render_process_id, new_render_frame_id));
}
void AwResourceDispatcherHostDelegate::AddPendingThrottle(
int render_process_id,
int render_frame_id,
IoThreadClientThrottle* pending_throttle) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(
&AwResourceDispatcherHostDelegate::AddPendingThrottleOnIoThread,
base::Unretained(
g_webview_resource_dispatcher_host_delegate.Pointer()),
render_process_id, render_frame_id, pending_throttle));
}
void AwResourceDispatcherHostDelegate::AddPendingThrottleOnIoThread(
int render_process_id,
int render_frame_id_id,
IoThreadClientThrottle* pending_throttle) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
pending_throttles_.insert(
std::pair<FrameRouteIDPair, IoThreadClientThrottle*>(
FrameRouteIDPair(render_process_id, render_frame_id_id),
pending_throttle));
}
void AwResourceDispatcherHostDelegate::OnIoThreadClientReadyInternal(
int new_render_process_id,
int new_render_frame_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
PendingThrottleMap::iterator it = pending_throttles_.find(
FrameRouteIDPair(new_render_process_id, new_render_frame_id));
if (it != pending_throttles_.end()) {
IoThreadClientThrottle* throttle = it->second;
throttle->OnIoThreadClientReady(new_render_process_id, new_render_frame_id);
pending_throttles_.erase(it);
}
}
void AwResourceDispatcherHostDelegate::AddExtraHeadersIfNeeded(
net::URLRequest* request,
content::ResourceContext* resource_context) {
const content::ResourceRequestInfo* request_info =
content::ResourceRequestInfo::ForRequest(request);
if (!request_info) return;
if (request_info->GetResourceType() != ResourceType::MAIN_FRAME) return;
const content::PageTransition transition = request_info->GetPageTransition();
const bool is_load_url =
transition & content::PAGE_TRANSITION_FROM_API;
const bool is_go_back_forward =
transition & content::PAGE_TRANSITION_FORWARD_BACK;
const bool is_reload = content::PageTransitionCoreTypeIs(
transition, content::PAGE_TRANSITION_RELOAD);
if (is_load_url || is_go_back_forward || is_reload) {
AwResourceContext* awrc = static_cast<AwResourceContext*>(resource_context);
std::string extra_headers = awrc->GetExtraHeaders(request->url());
if (!extra_headers.empty()) {
net::HttpRequestHeaders headers;
headers.AddHeadersFromString(extra_headers);
for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext(); ) {
request->SetExtraRequestHeaderByName(it.name(), it.value(), false);
}
}
}
}
}