Allow the browser to Send messages to an embedded worker once the script as been...
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_request_handler.h
blobc8cebafedcf59854005f22f3228e714f0dd9ea0f
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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/supports_user_data.h"
11 #include "content/common/content_export.h"
12 #include "content/common/service_worker/service_worker_status_code.h"
13 #include "content/public/common/resource_type.h"
14 #include "net/url_request/url_request_job_factory.h"
16 namespace net {
17 class NetworkDelegate;
18 class URLRequest;
19 class URLRequestInterceptor;
22 namespace storage {
23 class BlobStorageContext;
26 namespace content {
28 class ResourceRequestBody;
29 class ServiceWorkerContextCore;
30 class ServiceWorkerContextWrapper;
31 class ServiceWorkerProviderHost;
33 // Abstract base class for routing network requests to ServiceWorkers.
34 // Created one per URLRequest and attached to each request.
35 class CONTENT_EXPORT ServiceWorkerRequestHandler
36 : public base::SupportsUserData::Data {
37 public:
38 // Attaches a newly created handler if the given |request| needs to
39 // be handled by ServiceWorker.
40 // TODO(kinuko): While utilizing UserData to attach data to URLRequest
41 // has some precedence, it might be better to attach this handler in a more
42 // explicit way within content layer, e.g. have ResourceRequestInfoImpl
43 // own it.
44 static void InitializeHandler(
45 net::URLRequest* request,
46 ServiceWorkerContextWrapper* context_wrapper,
47 storage::BlobStorageContext* blob_storage_context,
48 int process_id,
49 int provider_id,
50 ResourceType resource_type,
51 scoped_refptr<ResourceRequestBody> body);
53 // Returns the handler attached to |request|. This may return NULL
54 // if no handler is attached.
55 static ServiceWorkerRequestHandler* GetHandler(
56 net::URLRequest* request);
58 // Creates a protocol interceptor for ServiceWorker.
59 static scoped_ptr<net::URLRequestInterceptor> CreateInterceptor();
61 virtual ~ServiceWorkerRequestHandler();
63 // Called via custom URLRequestJobFactory.
64 virtual net::URLRequestJob* MaybeCreateJob(
65 net::URLRequest* request,
66 net::NetworkDelegate* network_delegate) = 0;
68 virtual void GetExtraResponseInfo(
69 bool* was_fetched_via_service_worker,
70 GURL* original_url_via_service_worker) const = 0;
72 protected:
73 ServiceWorkerRequestHandler(
74 base::WeakPtr<ServiceWorkerContextCore> context,
75 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
76 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
77 ResourceType resource_type);
79 base::WeakPtr<ServiceWorkerContextCore> context_;
80 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
81 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
82 ResourceType resource_type_;
84 private:
85 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestHandler);
88 } // namespace content
90 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_