Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_request_handler.cc
blob4fbd76e78ea4ea37b4121547dcaed7dc927efe1b
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_request_handler.h"
7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_context_wrapper.h"
9 #include "content/browser/service_worker/service_worker_provider_host.h"
10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/browser/service_worker/service_worker_url_request_job.h"
12 #include "content/browser/service_worker/service_worker_utils.h"
13 #include "content/common/service_worker/service_worker_types.h"
14 #include "net/url_request/url_request.h"
16 namespace content {
18 namespace {
20 int kUserDataKey; // Key value is not important.
22 class ServiceWorkerRequestInterceptor
23 : public net::URLRequestJobFactory::ProtocolHandler {
24 public:
25 ServiceWorkerRequestInterceptor() {}
26 virtual ~ServiceWorkerRequestInterceptor() {}
27 virtual net::URLRequestJob* MaybeCreateJob(
28 net::URLRequest* request,
29 net::NetworkDelegate* network_delegate) const OVERRIDE {
30 ServiceWorkerRequestHandler* handler =
31 ServiceWorkerRequestHandler::GetHandler(request);
32 if (!handler)
33 return NULL;
34 return handler->MaybeCreateJob(request, network_delegate);
37 private:
38 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
41 } // namespace
43 void ServiceWorkerRequestHandler::InitializeHandler(
44 net::URLRequest* request,
45 ServiceWorkerContextWrapper* context_wrapper,
46 int process_id,
47 int provider_id,
48 ResourceType::Type resource_type) {
49 if (!ServiceWorkerUtils::IsFeatureEnabled())
50 return;
52 if (!context_wrapper || !context_wrapper->context() ||
53 provider_id == kInvalidServiceWorkerProviderId) {
54 return;
57 ServiceWorkerProviderHost* provider_host =
58 context_wrapper->context()->GetProviderHost(process_id, provider_id);
59 if (!provider_host)
60 return;
62 scoped_ptr<ServiceWorkerRequestHandler> handler(
63 provider_host->CreateRequestHandler(resource_type));
64 if (!handler)
65 return;
67 request->SetUserData(&kUserDataKey, handler.release());
70 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
71 net::URLRequest* request) {
72 return reinterpret_cast<ServiceWorkerRequestHandler*>(
73 request->GetUserData(&kUserDataKey));
76 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
77 ServiceWorkerRequestHandler::CreateInterceptor() {
78 return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
79 new ServiceWorkerRequestInterceptor);
82 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
85 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
86 base::WeakPtr<ServiceWorkerContextCore> context,
87 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
88 ResourceType::Type resource_type)
89 : context_(context),
90 provider_host_(provider_host),
91 resource_type_(resource_type) {
94 } // namespace content