Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_request_handler.h
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.
4
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_
7
8 #include "base/basictypes.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/supports_user_data.h"
11 #include "base/time/time.h"
12 #include "content/common/content_export.h"
13 #include "content/common/service_worker/service_worker_status_code.h"
14 #include "content/common/service_worker/service_worker_types.h"
15 #include "content/public/common/request_context_frame_type.h"
16 #include "content/public/common/request_context_type.h"
17 #include "content/public/common/resource_type.h"
18 #include "net/url_request/url_request_job_factory.h"
19 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h"
20
21 namespace net {
22 class NetworkDelegate;
23 class URLRequest;
24 class URLRequestInterceptor;
25 }
26
27 namespace storage {
28 class BlobStorageContext;
29 }
30
31 namespace content {
32
33 class ResourceContext;
34 class ResourceRequestBody;
35 class ServiceWorkerContextCore;
36 class ServiceWorkerContextWrapper;
37 class ServiceWorkerProviderHost;
38
39 // Abstract base class for routing network requests to ServiceWorkers.
40 // Created one per URLRequest and attached to each request.
41 class CONTENT_EXPORT ServiceWorkerRequestHandler
42     : public base::SupportsUserData::Data {
43  public:
44   // Attaches a newly created handler if the given |request| needs to
45   // be handled by ServiceWorker.
46   // TODO(kinuko): While utilizing UserData to attach data to URLRequest
47   // has some precedence, it might be better to attach this handler in a more
48   // explicit way within content layer, e.g. have ResourceRequestInfoImpl
49   // own it.
50   static void InitializeHandler(
51       net::URLRequest* request,
52       ServiceWorkerContextWrapper* context_wrapper,
53       storage::BlobStorageContext* blob_storage_context,
54       int process_id,
55       int provider_id,
56       bool skip_service_worker,
57       FetchRequestMode request_mode,
58       FetchCredentialsMode credentials_mode,
59       ResourceType resource_type,
60       RequestContextType request_context_type,
61       RequestContextFrameType frame_type,
62       scoped_refptr<ResourceRequestBody> body);
63
64   // Returns the handler attached to |request|. This may return NULL
65   // if no handler is attached.
66   static ServiceWorkerRequestHandler* GetHandler(
67       net::URLRequest* request);
68
69   // Creates a protocol interceptor for ServiceWorker.
70   static scoped_ptr<net::URLRequestInterceptor> CreateInterceptor(
71       ResourceContext* resource_context);
72
73   // Returns true if the request falls into the scope of a ServiceWorker.
74   // It's only reliable after the ServiceWorkerRequestHandler MaybeCreateJob
75   // method runs to completion for this request. The AppCache handler uses
76   // this to avoid colliding with ServiceWorkers.
77   static bool IsControlledByServiceWorker(net::URLRequest* request);
78
79   ~ServiceWorkerRequestHandler() override;
80
81   // Called via custom URLRequestJobFactory.
82   virtual net::URLRequestJob* MaybeCreateJob(
83       net::URLRequest* request,
84       net::NetworkDelegate* network_delegate,
85       ResourceContext* context) = 0;
86
87   virtual void GetExtraResponseInfo(
88       bool* was_fetched_via_service_worker,
89       bool* was_fallback_required_by_service_worker,
90       GURL* original_url_via_service_worker,
91       blink::WebServiceWorkerResponseType* response_type_via_service_worker,
92       base::TimeTicks* fetch_start_time,
93       base::TimeTicks* fetch_ready_time,
94       base::TimeTicks* fetch_end_time) const = 0;
95
96  protected:
97   ServiceWorkerRequestHandler(
98       base::WeakPtr<ServiceWorkerContextCore> context,
99       base::WeakPtr<ServiceWorkerProviderHost> provider_host,
100       base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
101       ResourceType resource_type);
102
103   base::WeakPtr<ServiceWorkerContextCore> context_;
104   base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
105   base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
106   ResourceType resource_type_;
107
108  private:
109   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestHandler);
110 };
111
112 }  // namespace content
113
114 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_