0

Plumb a WebWorkerFetchContext for nested workers

Bug: 829119
Change-Id: Ie393ac9e18bdfcded983d98a16c00254b0954547
Reviewed-on: https://chromium-review.googlesource.com/1000519
Commit-Queue: Nate Chapin <japhet@chromium.org>
Reviewed-by: Bo <boliu@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551391}
This commit is contained in:
Nate Chapin
2018-04-17 18:05:13 +00:00
committed by Commit Bot
parent d3ae8737f7
commit 5390cdffed
19 changed files with 148 additions and 11 deletions

@ -30,6 +30,22 @@ AwURLLoaderThrottleProvider::AwURLLoaderThrottleProvider(
}
}
AwURLLoaderThrottleProvider::AwURLLoaderThrottleProvider(
const AwURLLoaderThrottleProvider& other)
: type_(other.type_) {
DETACH_FROM_THREAD(thread_checker_);
if (other.safe_browsing_)
other.safe_browsing_->Clone(mojo::MakeRequest(&safe_browsing_info_));
}
std::unique_ptr<content::URLLoaderThrottleProvider>
AwURLLoaderThrottleProvider::Clone() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (safe_browsing_info_)
safe_browsing_.Bind(std::move(safe_browsing_info_));
return base::WrapUnique(new AwURLLoaderThrottleProvider(*this));
}
AwURLLoaderThrottleProvider::~AwURLLoaderThrottleProvider() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}

@ -21,12 +21,17 @@ class AwURLLoaderThrottleProvider : public content::URLLoaderThrottleProvider {
~AwURLLoaderThrottleProvider() override;
// content::URLLoaderThrottleProvider implementation.
std::unique_ptr<content::URLLoaderThrottleProvider> Clone() override;
std::vector<std::unique_ptr<content::URLLoaderThrottle>> CreateThrottles(
int render_frame_id,
const blink::WebURLRequest& request,
content::ResourceType resource_type) override;
private:
// This copy constructor works in conjunction with Clone(), not intended for
// general use.
AwURLLoaderThrottleProvider(const AwURLLoaderThrottleProvider& other);
content::URLLoaderThrottleProviderType type_;
safe_browsing::mojom::SafeBrowsingPtrInfo safe_browsing_info_;
@ -34,7 +39,7 @@ class AwURLLoaderThrottleProvider : public content::URLLoaderThrottleProvider {
THREAD_CHECKER(thread_checker_);
DISALLOW_COPY_AND_ASSIGN(AwURLLoaderThrottleProvider);
DISALLOW_ASSIGN(AwURLLoaderThrottleProvider);
};
} // namespace android_webview

@ -31,6 +31,21 @@ AwWebSocketHandshakeThrottleProvider::~AwWebSocketHandshakeThrottleProvider() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
AwWebSocketHandshakeThrottleProvider::AwWebSocketHandshakeThrottleProvider(
const AwWebSocketHandshakeThrottleProvider& other) {
DETACH_FROM_THREAD(thread_checker_);
if (other.safe_browsing_)
other.safe_browsing_->Clone(mojo::MakeRequest(&safe_browsing_info_));
}
std::unique_ptr<content::WebSocketHandshakeThrottleProvider>
AwWebSocketHandshakeThrottleProvider::Clone() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (safe_browsing_info_)
safe_browsing_.Bind(std::move(safe_browsing_info_));
return base::WrapUnique(new AwWebSocketHandshakeThrottleProvider(*this));
}
std::unique_ptr<blink::WebSocketHandshakeThrottle>
AwWebSocketHandshakeThrottleProvider::CreateThrottle(int render_frame_id) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

@ -23,16 +23,22 @@ class AwWebSocketHandshakeThrottleProvider final
~AwWebSocketHandshakeThrottleProvider() override;
// Implements content::WebSocketHandshakeThrottleProvider.
std::unique_ptr<content::WebSocketHandshakeThrottleProvider> Clone() override;
std::unique_ptr<blink::WebSocketHandshakeThrottle> CreateThrottle(
int render_frame_id) override;
private:
// This copy constructor works in conjunction with Clone(), not intended for
// general use.
AwWebSocketHandshakeThrottleProvider(
const AwWebSocketHandshakeThrottleProvider& other);
safe_browsing::mojom::SafeBrowsingPtrInfo safe_browsing_info_;
safe_browsing::mojom::SafeBrowsingPtr safe_browsing_;
THREAD_CHECKER(thread_checker_);
DISALLOW_COPY_AND_ASSIGN(AwWebSocketHandshakeThrottleProvider);
DISALLOW_ASSIGN(AwWebSocketHandshakeThrottleProvider);
};
} // namespace android_webview

@ -68,6 +68,24 @@ URLLoaderThrottleProviderImpl::~URLLoaderThrottleProviderImpl() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
URLLoaderThrottleProviderImpl::URLLoaderThrottleProviderImpl(
const URLLoaderThrottleProviderImpl& other)
: type_(other.type_),
chrome_content_renderer_client_(other.chrome_content_renderer_client_) {
DETACH_FROM_THREAD(thread_checker_);
if (other.safe_browsing_)
other.safe_browsing_->Clone(mojo::MakeRequest(&safe_browsing_info_));
// An ad_delay_factory_ is created, rather than cloning the existing one.
}
std::unique_ptr<content::URLLoaderThrottleProvider>
URLLoaderThrottleProviderImpl::Clone() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (safe_browsing_info_)
safe_browsing_.Bind(std::move(safe_browsing_info_));
return base::WrapUnique(new URLLoaderThrottleProviderImpl(*this));
}
std::vector<std::unique_ptr<content::URLLoaderThrottle>>
URLLoaderThrottleProviderImpl::CreateThrottles(
int render_frame_id,

@ -26,12 +26,17 @@ class URLLoaderThrottleProviderImpl
~URLLoaderThrottleProviderImpl() override;
// content::URLLoaderThrottleProvider implementation.
std::unique_ptr<content::URLLoaderThrottleProvider> Clone() override;
std::vector<std::unique_ptr<content::URLLoaderThrottle>> CreateThrottles(
int render_frame_id,
const blink::WebURLRequest& request,
content::ResourceType resource_type) override;
private:
// This copy constructor works in conjunction with Clone(), not intended for
// general use.
URLLoaderThrottleProviderImpl(const URLLoaderThrottleProviderImpl& other);
std::unique_ptr<subresource_filter::AdDelayThrottle::Factory>
ad_delay_factory_;
@ -43,7 +48,7 @@ class URLLoaderThrottleProviderImpl
THREAD_CHECKER(thread_checker_);
DISALLOW_COPY_AND_ASSIGN(URLLoaderThrottleProviderImpl);
DISALLOW_ASSIGN(URLLoaderThrottleProviderImpl);
};
#endif // CHROME_RENDERER_URL_LOADER_THROTTLE_PROVIDER_IMPL_H_

@ -25,6 +25,21 @@ WebSocketHandshakeThrottleProviderImpl::
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
WebSocketHandshakeThrottleProviderImpl::WebSocketHandshakeThrottleProviderImpl(
const WebSocketHandshakeThrottleProviderImpl& other) {
DETACH_FROM_THREAD(thread_checker_);
DCHECK(other.safe_browsing_);
other.safe_browsing_->Clone(mojo::MakeRequest(&safe_browsing_info_));
}
std::unique_ptr<content::WebSocketHandshakeThrottleProvider>
WebSocketHandshakeThrottleProviderImpl::Clone() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (safe_browsing_info_)
safe_browsing_.Bind(std::move(safe_browsing_info_));
return base::WrapUnique(new WebSocketHandshakeThrottleProviderImpl(*this));
}
std::unique_ptr<blink::WebSocketHandshakeThrottle>
WebSocketHandshakeThrottleProviderImpl::CreateThrottle(int render_frame_id) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

@ -21,16 +21,22 @@ class WebSocketHandshakeThrottleProviderImpl final
~WebSocketHandshakeThrottleProviderImpl() override;
// Implements content::WebSocketHandshakeThrottleProvider.
std::unique_ptr<content::WebSocketHandshakeThrottleProvider> Clone() override;
std::unique_ptr<blink::WebSocketHandshakeThrottle> CreateThrottle(
int render_frame_id) override;
private:
// This copy constructor works in conjunction with Clone(), not intended for
// general use.
WebSocketHandshakeThrottleProviderImpl(
const WebSocketHandshakeThrottleProviderImpl& other);
safe_browsing::mojom::SafeBrowsingPtrInfo safe_browsing_info_;
safe_browsing::mojom::SafeBrowsingPtr safe_browsing_;
THREAD_CHECKER(thread_checker_);
DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeThrottleProviderImpl);
DISALLOW_ASSIGN(WebSocketHandshakeThrottleProviderImpl);
};
#endif // CHROME_RENDERER_WEBSOCKET_HANDSHAKE_THROTTLE_PROVIDER_IMPL_H_

@ -28,6 +28,9 @@ class CONTENT_EXPORT URLLoaderThrottleProvider {
public:
virtual ~URLLoaderThrottleProvider() {}
// Used to copy a URLLoaderThrottleProvider between worker threads.
virtual std::unique_ptr<URLLoaderThrottleProvider> Clone() = 0;
// For requests from frames and dedicated workers, |render_frame_id| should be
// set to the corresponding frame. For requests from shared or
// service workers, |render_frame_id| should be set to MSG_ROUTING_NONE.

@ -23,6 +23,9 @@ class CONTENT_EXPORT WebSocketHandshakeThrottleProvider {
public:
virtual ~WebSocketHandshakeThrottleProvider() {}
// Used to copy a WebSocketHandshakeThrottleProvider between worker threads.
virtual std::unique_ptr<WebSocketHandshakeThrottleProvider> Clone() = 0;
// For requests from frames and dedicated workers, |render_frame_id| should be
// set to the corresponding frame. For requests from shared or service
// workers, |render_frame_id| should be set to MSG_ROUTING_NONE.

@ -3594,7 +3594,8 @@ RenderFrameImpl::CreateWorkerFetchContext() {
URLLoaderThrottleProviderType::kWorker),
GetContentClient()
->renderer()
->CreateWebSocketHandshakeThrottleProvider());
->CreateWebSocketHandshakeThrottleProvider(),
ChildThreadImpl::current()->thread_safe_sender());
worker_fetch_context->set_parent_frame_id(routing_id_);
worker_fetch_context->set_site_for_cookies(

@ -105,7 +105,8 @@ WorkerFetchContextImpl::WorkerFetchContextImpl(
direct_network_factory_info,
std::unique_ptr<URLLoaderThrottleProvider> throttle_provider,
std::unique_ptr<WebSocketHandshakeThrottleProvider>
websocket_handshake_throttle_provider)
websocket_handshake_throttle_provider,
ThreadSafeSender* thread_safe_sender)
: binding_(this),
service_worker_client_request_(std::move(service_worker_client_request)),
service_worker_container_host_info_(
@ -113,7 +114,7 @@ WorkerFetchContextImpl::WorkerFetchContextImpl(
url_loader_factory_info_(std::move(url_loader_factory_info)),
direct_network_loader_factory_info_(
std::move(direct_network_factory_info)),
thread_safe_sender_(ChildThreadImpl::current()->thread_safe_sender()),
thread_safe_sender_(thread_safe_sender),
throttle_provider_(std::move(throttle_provider)),
websocket_handshake_throttle_provider_(
std::move(websocket_handshake_throttle_provider)) {
@ -132,6 +133,27 @@ void WorkerFetchContextImpl::SetTerminateSyncLoadEvent(
terminate_sync_load_event_ = terminate_sync_load_event;
}
std::unique_ptr<blink::WebWorkerFetchContext>
WorkerFetchContextImpl::CloneForNestedWorker() {
// TODO(japhet?): This doens't plumb service worker state to nested workers,
// because dedicated workers in service worker-controlled documents are
// currently not spec compliant and we don't want to propagate the wrong
// behavior. See https://crbug.com/731604
auto new_context = std::make_unique<WorkerFetchContextImpl>(
mojom::ServiceWorkerWorkerClientRequest(),
mojom::ServiceWorkerContainerHostPtrInfo(),
shared_url_loader_factory_->Clone(),
direct_network_loader_factory_->Clone(),
throttle_provider_ ? throttle_provider_->Clone() : nullptr,
websocket_handshake_throttle_provider_
? websocket_handshake_throttle_provider_->Clone()
: nullptr,
thread_safe_sender_.get());
new_context->is_on_sub_frame_ = is_on_sub_frame_;
new_context->appcache_host_id_ = appcache_host_id_;
return new_context;
}
void WorkerFetchContextImpl::InitializeOnWorkerThread() {
DCHECK(!resource_dispatcher_);
DCHECK(!binding_.is_bound());

@ -53,10 +53,12 @@ class WorkerFetchContextImpl : public blink::WebWorkerFetchContext,
direct_network_factory_info,
std::unique_ptr<URLLoaderThrottleProvider> throttle_provider,
std::unique_ptr<WebSocketHandshakeThrottleProvider>
websocket_handshake_throttle_provider);
websocket_handshake_throttle_provider,
ThreadSafeSender* thread_safe_sender);
~WorkerFetchContextImpl() override;
// blink::WebWorkerFetchContext implementation:
std::unique_ptr<blink::WebWorkerFetchContext> CloneForNestedWorker() override;
void SetTerminateSyncLoadEvent(base::WaitableEvent*) override;
void InitializeOnWorkerThread() override;
std::unique_ptr<blink::WebURLLoaderFactory> CreateURLLoaderFactory() override;

@ -346,7 +346,8 @@ EmbeddedSharedWorkerStub::CreateWorkerFetchContext(
URLLoaderThrottleProviderType::kWorker),
GetContentClient()
->renderer()
->CreateWebSocketHandshakeThrottleProvider());
->CreateWebSocketHandshakeThrottleProvider(),
ChildThreadImpl::current()->thread_safe_sender());
// TODO(horo): To get the correct first_party_to_cookies for the shared
// worker, we need to check the all documents bounded by the shared worker.

@ -73,6 +73,11 @@ class TestWebSocketHandshakeThrottle
} // namespace
std::unique_ptr<content::WebSocketHandshakeThrottleProvider>
TestWebSocketHandshakeThrottleProvider::Clone() {
return std::make_unique<TestWebSocketHandshakeThrottleProvider>();
}
std::unique_ptr<blink::WebSocketHandshakeThrottle>
TestWebSocketHandshakeThrottleProvider::CreateThrottle(int render_frame_id) {
return std::make_unique<TestWebSocketHandshakeThrottle>();

@ -18,6 +18,7 @@ class TestWebSocketHandshakeThrottleProvider
TestWebSocketHandshakeThrottleProvider() = default;
~TestWebSocketHandshakeThrottleProvider() override = default;
std::unique_ptr<content::WebSocketHandshakeThrottleProvider> Clone() override;
std::unique_ptr<blink::WebSocketHandshakeThrottle> CreateThrottle(
int render_frame_id) override;

@ -32,6 +32,11 @@ class WebWorkerFetchContext {
public:
virtual ~WebWorkerFetchContext() = default;
// Used to copy a worker fetch context between worker threads.
virtual std::unique_ptr<WebWorkerFetchContext> CloneForNestedWorker() {
return nullptr;
}
// Set a raw pointer of a WaitableEvent which will be signaled from the main
// thread when the worker's GlobalScope is terminated, which will terminate
// sync loading requests on the worker thread. It is guaranteed that the

@ -110,6 +110,10 @@ class WorkerFetchContext final : public BaseFetchContext {
ResourceRequest&) override;
scoped_refptr<base::SingleThreadTaskRunner> GetLoadingTaskRunner() override;
WebWorkerFetchContext* GetWebWorkerFetchContext() {
return web_context_.get();
}
void Trace(blink::Visitor*) override;
private:

@ -76,9 +76,13 @@ void ThreadedMessagingProxyBase::InitializeWorkerThread(
GetExecutionContext()->Fetcher()->Context().ApplicationCacheHostID());
web_worker_fetch_context->SetIsOnSubframe(web_frame != web_frame->Top());
}
} else if (execution_context_->IsWorkerGlobalScope()) {
web_worker_fetch_context =
static_cast<WorkerFetchContext&>(
ToWorkerGlobalScope(execution_context_)->Fetcher()->Context())
.GetWebWorkerFetchContext()
->CloneForNestedWorker();
}
// TODO(japhet): Add a way to clone a WebWorkerFetchContext between worker
// threads for nested workers.
if (web_worker_fetch_context) {
web_worker_fetch_context->SetTerminateSyncLoadEvent(