Commit 7e40ec77 authored by horo's avatar horo Committed by Commit bot

[ServiceWorker] Plumbing the request mode from the renderer to the ServiceWorker. [2/2 chromium]

[1/2] blink: https://codereview.chromium.org/587213003/
[2/2] chromium: https://codereview.chromium.org/588153002/

This value is passed from the renederer to the ServiceWorker in the following steps.

In the renederer process:
  blink::ResourceRequest::setFetchRequestMode()
 blink::ResourceRequest::m_fetchRequestMode
  content::WebURLLoaderImpl::Context::Start()
   blink::WebURLRequest::fetchRequestMode()
    GetFetchRequestMode()
 conetnt::RequestInfo::service_worker_request_mode

In the browser process:
 conetnt::RequestInfo::service_worker_request_mode
  content::ResourceDispatcherHostImpl::BeginRequest()
   content::ServiceWorkerRequestHandler::InitializeHandler()
    content::ServiceWorkerProviderHost::CreateRequestHandler()
     content::ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler()
 content::ServiceWorkerControlleeRequestHandler::request_mode_
  content::ServiceWorkerControlleeRequestHandler::MaybeCreateJob()
   content::ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob()
 content::ServiceWorkerURLRequestJob::request_mode_
  content::ServiceWorkerURLRequestJob::CreateFetchRequest()
 content::ServiceWorkerFetchRequest::mode

In the ServiceWorker process:
 content::ServiceWorkerFetchRequest::mode
  content::ServiceWorkerScriptContext::OnFetchEvent()
   GetBlinkFetchRequestMode()
   blink::WebServiceWorkerRequest::setMode()
 blink::WebServiceWorkerRequest::WebServiceWorkerRequestPrivate::m_mode
  blink::ServiceWorkerGlobalScopeProxy::dispatchFetchEvent()
   blilnk::RespondWithObserver::create()
 blilnk::RespondWithObserver::m_requestMode

 blink::WebServiceWorkerRequest::WebServiceWorkerRequestPrivate::m_mode
  blink::Request::create()
  blink::FetchRequestData::create()
 blink::FetchRequestData::m_mode

BUG=416371,408507

Review URL: https://codereview.chromium.org/588153002

Cr-Commit-Position: refs/heads/master@{#297391}
parent 88bf4dda
......@@ -1206,6 +1206,7 @@ void ResourceDispatcherHostImpl::BeginRequest(
child_id,
request_data.service_worker_provider_id,
request_data.skip_service_worker || is_sync_load,
request_data.fetch_request_mode,
request_data.resource_type,
request_data.request_body);
......
......@@ -23,6 +23,7 @@ ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler(
base::WeakPtr<ServiceWorkerContextCore> context,
base::WeakPtr<ServiceWorkerProviderHost> provider_host,
base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
FetchRequestMode request_mode,
ResourceType resource_type,
scoped_refptr<ResourceRequestBody> body)
: ServiceWorkerRequestHandler(context,
......@@ -31,6 +32,7 @@ ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler(
resource_type),
is_main_resource_load_(
ServiceWorkerUtils::IsMainResourceType(resource_type)),
request_mode_(request_mode),
body_(body),
weak_factory_(this) {
}
......@@ -76,8 +78,12 @@ net::URLRequestJob* ServiceWorkerControlleeRequestHandler::MaybeCreateJob(
// It's for original request (A) or redirect case (B-a or B-b).
DCHECK(!job_.get() || job_->ShouldForwardToServiceWorker());
job_ = new ServiceWorkerURLRequestJob(
request, network_delegate, provider_host_, blob_storage_context_, body_);
job_ = new ServiceWorkerURLRequestJob(request,
network_delegate,
provider_host_,
blob_storage_context_,
request_mode_,
body_);
if (is_main_resource_load_)
PrepareForMainResource(request->url());
else
......
......@@ -7,6 +7,7 @@
#include "base/gtest_prod_util.h"
#include "content/browser/service_worker/service_worker_request_handler.h"
#include "content/common/service_worker/service_worker_types.h"
namespace net {
class NetworkDelegate;
......@@ -29,6 +30,7 @@ class CONTENT_EXPORT ServiceWorkerControlleeRequestHandler
base::WeakPtr<ServiceWorkerContextCore> context,
base::WeakPtr<ServiceWorkerProviderHost> provider_host,
base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
FetchRequestMode request_mode,
ResourceType resource_type,
scoped_refptr<ResourceRequestBody> body);
virtual ~ServiceWorkerControlleeRequestHandler();
......@@ -64,6 +66,7 @@ class CONTENT_EXPORT ServiceWorkerControlleeRequestHandler
bool is_main_resource_load_;
scoped_refptr<ServiceWorkerURLRequestJob> job_;
FetchRequestMode request_mode_;
scoped_refptr<ResourceRequestBody> body_;
base::WeakPtrFactory<ServiceWorkerControlleeRequestHandler> weak_factory_;
......
......@@ -100,6 +100,7 @@ TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) {
context()->AsWeakPtr(),
provider_host_,
base::WeakPtr<storage::BlobStorageContext>(),
FETCH_REQUEST_MODE_NO_CORS,
RESOURCE_TYPE_MAIN_FRAME,
scoped_refptr<ResourceRequestBody>()));
scoped_refptr<net::URLRequestJob> job =
......@@ -150,6 +151,7 @@ TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) {
context()->AsWeakPtr(),
provider_host_,
base::WeakPtr<storage::BlobStorageContext>(),
FETCH_REQUEST_MODE_NO_CORS,
RESOURCE_TYPE_MAIN_FRAME,
scoped_refptr<ResourceRequestBody>()));
scoped_refptr<net::URLRequestJob> job =
......
......@@ -16,6 +16,7 @@
#include "content/browser/service_worker/service_worker_version.h"
#include "content/common/resource_request_body.h"
#include "content/common/service_worker/service_worker_messages.h"
#include "content/common/service_worker/service_worker_types.h"
namespace content {
......@@ -145,6 +146,7 @@ void ServiceWorkerProviderHost::DisassociateRegistration() {
scoped_ptr<ServiceWorkerRequestHandler>
ServiceWorkerProviderHost::CreateRequestHandler(
FetchRequestMode request_mode,
ResourceType resource_type,
base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
scoped_refptr<ResourceRequestBody> body) {
......@@ -156,8 +158,12 @@ ServiceWorkerProviderHost::CreateRequestHandler(
if (ServiceWorkerUtils::IsMainResourceType(resource_type) ||
controlling_version()) {
return scoped_ptr<ServiceWorkerRequestHandler>(
new ServiceWorkerControlleeRequestHandler(
context_, AsWeakPtr(), blob_storage_context, resource_type, body));
new ServiceWorkerControlleeRequestHandler(context_,
AsWeakPtr(),
blob_storage_context,
request_mode,
resource_type,
body));
}
return scoped_ptr<ServiceWorkerRequestHandler>();
}
......
......@@ -94,6 +94,7 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
// Returns a handler for a request, the handler may return NULL if
// the request doesn't require special handling.
scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler(
FetchRequestMode request_mode,
ResourceType resource_type,
base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
scoped_refptr<ResourceRequestBody> body);
......
......@@ -60,6 +60,7 @@ void ServiceWorkerRequestHandler::InitializeHandler(
int process_id,
int provider_id,
bool skip_service_worker,
FetchRequestMode request_mode,
ResourceType resource_type,
scoped_refptr<ResourceRequestBody> body) {
if (!request->url().SchemeIsHTTPOrHTTPS() ||
......@@ -84,8 +85,10 @@ void ServiceWorkerRequestHandler::InitializeHandler(
}
scoped_ptr<ServiceWorkerRequestHandler> handler(
provider_host->CreateRequestHandler(
resource_type, blob_storage_context->AsWeakPtr(), body));
provider_host->CreateRequestHandler(request_mode,
resource_type,
blob_storage_context->AsWeakPtr(),
body));
if (!handler)
return;
......
......@@ -11,6 +11,7 @@
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/common/service_worker/service_worker_status_code.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/common/resource_type.h"
#include "net/url_request/url_request_job_factory.h"
......@@ -49,6 +50,7 @@ class CONTENT_EXPORT ServiceWorkerRequestHandler
int process_id,
int provider_id,
bool skip_service_worker,
FetchRequestMode request_mode,
ResourceType resource_type,
scoped_refptr<ResourceRequestBody> body);
......
......@@ -83,12 +83,14 @@ class ServiceWorkerRequestHandlerTest : public testing::Test {
scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL);
request->set_method(method);
FetchRequestMode request_mode = FETCH_REQUEST_MODE_NO_CORS;
ServiceWorkerRequestHandler::InitializeHandler(request.get(),
context_wrapper(),
&blob_storage_context_,
kMockRenderProcessId,
kMockProviderId,
skip_service_worker,
request_mode,
resource_type,
NULL);
return ServiceWorkerRequestHandler::GetHandler(request.get()) != NULL;
......
......@@ -35,12 +35,14 @@ ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob(
net::NetworkDelegate* network_delegate,
base::WeakPtr<ServiceWorkerProviderHost> provider_host,
base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
FetchRequestMode request_mode,
scoped_refptr<ResourceRequestBody> body)
: net::URLRequestJob(request, network_delegate),
provider_host_(provider_host),
response_type_(NOT_DETERMINED),
is_started_(false),
blob_storage_context_(blob_storage_context),
request_mode_(request_mode),
body_(body),
weak_factory_(this) {
}
......@@ -264,7 +266,7 @@ ServiceWorkerURLRequestJob::CreateFetchRequest() {
CreateRequestBodyBlob(&blob_uuid, &blob_size);
scoped_ptr<ServiceWorkerFetchRequest> request(
new ServiceWorkerFetchRequest());
request->mode = request_mode_;
request->url = request_->url();
request->method = request_->method();
const net::HttpRequestHeaders& headers = request_->extra_request_headers();
......
......@@ -38,6 +38,7 @@ class CONTENT_EXPORT ServiceWorkerURLRequestJob
net::NetworkDelegate* network_delegate,
base::WeakPtr<ServiceWorkerProviderHost> provider_host,
base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
FetchRequestMode request_mode,
scoped_refptr<ResourceRequestBody> body);
// Sets the response type.
......@@ -158,6 +159,7 @@ class CONTENT_EXPORT ServiceWorkerURLRequestJob
scoped_ptr<ServiceWorkerFetchDispatcher> fetch_dispatcher_;
base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
scoped_ptr<net::URLRequest> blob_request_;
FetchRequestMode request_mode_;
// ResourceRequestBody has a collection of BlobDataHandles attached to it
// using the userdata mechanism. So we have to keep it not to free the blobs.
scoped_refptr<ResourceRequestBody> body_;
......
......@@ -62,6 +62,7 @@ class MockHttpProtocolHandler
network_delegate,
provider_host_,
blob_storage_context_,
FETCH_REQUEST_MODE_NO_CORS,
scoped_refptr<ResourceRequestBody>());
job->ForwardToServiceWorker();
return job;
......
......@@ -18,6 +18,7 @@ RequestInfo::RequestInfo()
download_to_file(false),
has_user_gesture(false),
skip_service_worker(false),
fetch_request_mode(FETCH_REQUEST_MODE_NO_CORS),
enable_load_timing(false),
extra_data(NULL) {
}
......
......@@ -10,6 +10,7 @@
#include <string>
#include "content/common/content_export.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/common/resource_type.h"
#include "net/base/request_priority.h"
#include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
......@@ -78,6 +79,9 @@ struct CONTENT_EXPORT RequestInfo {
// True if the request should not be handled by the ServiceWorker.
bool skip_service_worker;
// The request mode passed to the ServiceWorker.
FetchRequestMode fetch_request_mode;
// TODO(mmenke): Investigate if enable_load_timing is safe to remove.
// True if load timing data should be collected for the request.
bool enable_load_timing;
......
......@@ -127,6 +127,7 @@ IPCResourceLoaderBridge::IPCResourceLoaderBridge(
request_.download_to_file = request_info.download_to_file;
request_.has_user_gesture = request_info.has_user_gesture;
request_.skip_service_worker = request_info.skip_service_worker;
request_.fetch_request_mode = request_info.fetch_request_mode;
request_.enable_load_timing = request_info.enable_load_timing;
const RequestExtraData kEmptyData;
......
......@@ -22,6 +22,7 @@
#include "content/child/web_url_request_util.h"
#include "content/child/weburlresponse_extradata_impl.h"
#include "content/common/resource_request_body.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/child/request_peer.h"
#include "net/base/data_url.h"
#include "net/base/filename_util.h"
......@@ -199,6 +200,25 @@ int GetInfoFromDataURL(const GURL& url,
return net::OK;
}
#define COMPILE_ASSERT_MATCHING_ENUMS(content_name, blink_name) \
COMPILE_ASSERT( \
static_cast<int>(content_name) == static_cast<int>(blink_name), \
mismatching_enums)
COMPILE_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_SAME_ORIGIN,
WebURLRequest::FetchRequestModeSameOrigin);
COMPILE_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_NO_CORS,
WebURLRequest::FetchRequestModeNoCORS);
COMPILE_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_CORS,
WebURLRequest::FetchRequestModeCORS);
COMPILE_ASSERT_MATCHING_ENUMS(
FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT,
WebURLRequest::FetchRequestModeCORSWithForcedPreflight);
FetchRequestMode GetFetchRequestMode(const WebURLRequest& request) {
return static_cast<FetchRequestMode>(request.fetchRequestMode());
}
} // namespace
// WebURLLoaderImpl::Context --------------------------------------------------
......@@ -397,6 +417,7 @@ void WebURLLoaderImpl::Context::Start(const WebURLRequest& request,
request_info.download_to_file = request.downloadToFile();
request_info.has_user_gesture = request.hasUserGesture();
request_info.skip_service_worker = request.skipServiceWorker();
request_info.fetch_request_mode = GetFetchRequestMode(request);
request_info.extra_data = request.extraData();
referrer_policy_ = request.referrerPolicy();
request_info.referrer_policy = request.referrerPolicy();
......
......@@ -11,6 +11,7 @@
#include "base/process/process.h"
#include "content/common/content_param_traits_macros.h"
#include "content/common/resource_request_body.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/common/common_param_traits.h"
#include "content/public/common/resource_response.h"
#include "ipc/ipc_message_macros.h"
......@@ -84,6 +85,9 @@ IPC_ENUM_TRAITS_MAX_VALUE( \
net::HttpResponseInfo::ConnectionInfo, \
net::HttpResponseInfo::NUM_OF_CONNECTION_INFOS - 1)
IPC_ENUM_TRAITS_MAX_VALUE(content::FetchRequestMode,
content::FETCH_REQUEST_MODE_LAST)
IPC_STRUCT_TRAITS_BEGIN(content::ResourceResponseHead)
IPC_STRUCT_TRAITS_PARENT(content::ResourceResponseInfo)
IPC_STRUCT_TRAITS_MEMBER(request_start)
......@@ -191,6 +195,9 @@ IPC_STRUCT_BEGIN(ResourceHostMsg_Request)
// True if the request should not be handled by the ServiceWorker.
IPC_STRUCT_MEMBER(bool, skip_service_worker)
// The request mode passed to the ServiceWorker.
IPC_STRUCT_MEMBER(content::FetchRequestMode, fetch_request_mode)
// Optional resource request body (may be null).
IPC_STRUCT_MEMBER(scoped_refptr<content::ResourceRequestBody>,
request_body)
......
......@@ -32,6 +32,7 @@ IPC_ENUM_TRAITS_MAX_VALUE(blink::WebServiceWorkerState,
blink::WebServiceWorkerStateLast)
IPC_STRUCT_TRAITS_BEGIN(content::ServiceWorkerFetchRequest)
IPC_STRUCT_TRAITS_MEMBER(mode)
IPC_STRUCT_TRAITS_MEMBER(url)
IPC_STRUCT_TRAITS_MEMBER(method)
IPC_STRUCT_TRAITS_MEMBER(headers)
......
......@@ -6,8 +6,9 @@
namespace content {
ServiceWorkerFetchRequest::ServiceWorkerFetchRequest() : blob_size(0),
is_reload(false) {}
ServiceWorkerFetchRequest::ServiceWorkerFetchRequest()
: mode(FETCH_REQUEST_MODE_NO_CORS), blob_size(0), is_reload(false) {
}
ServiceWorkerFetchRequest::ServiceWorkerFetchRequest(
const GURL& url,
......@@ -15,12 +16,14 @@ ServiceWorkerFetchRequest::ServiceWorkerFetchRequest(
const ServiceWorkerHeaderMap& headers,
const GURL& referrer,
bool is_reload)
: url(url),
: mode(FETCH_REQUEST_MODE_NO_CORS),
url(url),
method(method),
headers(headers),
blob_size(0),
referrer(referrer),
is_reload(is_reload) {}
is_reload(is_reload) {
}
ServiceWorkerFetchRequest::~ServiceWorkerFetchRequest() {}
......
......@@ -34,6 +34,14 @@ static const int64 kInvalidServiceWorkerResourceId = -1;
static const int64 kInvalidServiceWorkerResponseId = -1;
static const int kInvalidEmbeddedWorkerThreadId = -1;
enum FetchRequestMode {
FETCH_REQUEST_MODE_SAME_ORIGIN,
FETCH_REQUEST_MODE_NO_CORS,
FETCH_REQUEST_MODE_CORS,
FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT,
FETCH_REQUEST_MODE_LAST = FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT
};
// Indicates how the service worker handled a fetch event.
enum ServiceWorkerFetchEventResult {
// Browser should fallback to native fetch.
......@@ -62,6 +70,7 @@ struct CONTENT_EXPORT ServiceWorkerFetchRequest {
bool is_reload);
~ServiceWorkerFetchRequest();
FetchRequestMode mode;
GURL url;
std::string method;
ServiceWorkerHeaderMap headers;
......
......@@ -34,6 +34,11 @@ void SendPostMessageToDocumentOnMainThread(
WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release())));
}
blink::WebURLRequest::FetchRequestMode GetBlinkFetchRequestMode(
FetchRequestMode mode) {
return static_cast<blink::WebURLRequest::FetchRequestMode>(mode);
}
} // namespace
ServiceWorkerScriptContext::ServiceWorkerScriptContext(
......@@ -177,6 +182,7 @@ void ServiceWorkerScriptContext::OnFetchEvent(
}
webRequest.setReferrer(blink::WebString::fromUTF8(request.referrer.spec()),
blink::WebReferrerPolicyDefault);
webRequest.setMode(GetBlinkFetchRequestMode(request.mode));
webRequest.setIsReload(request.is_reload);
fetch_start_timings_[request_id] = base::TimeTicks::Now();
proxy_->dispatchFetchEvent(request_id, webRequest);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment