Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / common / service_worker / service_worker_types.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_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_
6 #define CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/strings/string_util.h"
13 #include "content/common/content_export.h"
14 #include "content/public/common/request_context_frame_type.h"
15 #include "content/public/common/request_context_type.h"
16 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h"
17 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h"
18 #include "url/gurl.h"
19
20 // This file is to have common definitions that are to be shared by
21 // browser and child process.
22
23 namespace content {
24
25 // Indicates invalid request ID (i.e. the sender does not expect it gets
26 // response for the message) for messaging between browser process
27 // and embedded worker.
28 static const int kInvalidServiceWorkerRequestId = -1;
29
30 // Constants for invalid identifiers.
31 static const int kInvalidServiceWorkerHandleId = -1;
32 static const int kInvalidServiceWorkerRegistrationHandleId = -1;
33 static const int kInvalidServiceWorkerProviderId = -1;
34 static const int64 kInvalidServiceWorkerRegistrationId = -1;
35 static const int64 kInvalidServiceWorkerVersionId = -1;
36 static const int64 kInvalidServiceWorkerResourceId = -1;
37 static const int64 kInvalidServiceWorkerResponseId = -1;
38 static const int kInvalidEmbeddedWorkerThreadId = -1;
39
40 enum FetchRequestMode {
41   FETCH_REQUEST_MODE_SAME_ORIGIN,
42   FETCH_REQUEST_MODE_NO_CORS,
43   FETCH_REQUEST_MODE_CORS,
44   FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT,
45   FETCH_REQUEST_MODE_LAST = FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT
46 };
47
48 enum FetchCredentialsMode {
49   FETCH_CREDENTIALS_MODE_OMIT,
50   FETCH_CREDENTIALS_MODE_SAME_ORIGIN,
51   FETCH_CREDENTIALS_MODE_INCLUDE,
52   FETCH_CREDENTIALS_MODE_LAST = FETCH_CREDENTIALS_MODE_INCLUDE
53 };
54
55 // Indicates how the service worker handled a fetch event.
56 enum ServiceWorkerFetchEventResult {
57   // Browser should fallback to native fetch.
58   SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK,
59   // Service worker provided a ServiceWorkerResponse.
60   SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
61   SERVICE_WORKER_FETCH_EVENT_LAST = SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
62 };
63
64 struct ServiceWorkerCaseInsensitiveCompare {
65   bool operator()(const std::string& lhs, const std::string& rhs) const {
66     return base::strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
67   }
68 };
69
70 typedef std::map<std::string, std::string, ServiceWorkerCaseInsensitiveCompare>
71     ServiceWorkerHeaderMap;
72
73 // To dispatch fetch request from browser to child process.
74 struct CONTENT_EXPORT ServiceWorkerFetchRequest {
75   ServiceWorkerFetchRequest();
76   ServiceWorkerFetchRequest(const GURL& url,
77                             const std::string& method,
78                             const ServiceWorkerHeaderMap& headers,
79                             const GURL& referrer,
80                             bool is_reload);
81   ~ServiceWorkerFetchRequest();
82
83   FetchRequestMode mode;
84   RequestContextType request_context_type;
85   RequestContextFrameType frame_type;
86   GURL url;
87   std::string method;
88   ServiceWorkerHeaderMap headers;
89   std::string blob_uuid;
90   uint64 blob_size;
91   GURL referrer;
92   FetchCredentialsMode credentials_mode;
93   bool is_reload;
94 };
95
96 // Represents a response to a fetch.
97 struct CONTENT_EXPORT ServiceWorkerResponse {
98   ServiceWorkerResponse();
99   ServiceWorkerResponse(const GURL& url,
100                         int status_code,
101                         const std::string& status_text,
102                         blink::WebServiceWorkerResponseType response_type,
103                         const ServiceWorkerHeaderMap& headers,
104                         const std::string& blob_uuid,
105                         uint64 blob_size);
106   ~ServiceWorkerResponse();
107
108   GURL url;
109   int status_code;
110   std::string status_text;
111   blink::WebServiceWorkerResponseType response_type;
112   ServiceWorkerHeaderMap headers;
113   std::string blob_uuid;
114   uint64 blob_size;
115 };
116
117 // Controls how requests are matched in the Cache API.
118 struct CONTENT_EXPORT ServiceWorkerCacheQueryParams {
119   ServiceWorkerCacheQueryParams();
120
121   bool ignore_search;
122   bool ignore_method;
123   bool ignore_vary;
124   bool prefix_match;
125 };
126
127 // The type of a single batch operation in the Cache API.
128 enum ServiceWorkerCacheOperationType {
129   SERVICE_WORKER_CACHE_OPERATION_TYPE_UNDEFINED,
130   SERVICE_WORKER_CACHE_OPERATION_TYPE_PUT,
131   SERVICE_WORKER_CACHE_OPERATION_TYPE_DELETE,
132   SERVICE_WORKER_CACHE_OPERATION_TYPE_LAST =
133       SERVICE_WORKER_CACHE_OPERATION_TYPE_DELETE
134 };
135
136 // A single batch operation for the Cache API.
137 struct CONTENT_EXPORT ServiceWorkerBatchOperation {
138   ServiceWorkerBatchOperation();
139
140   ServiceWorkerCacheOperationType operation_type;
141   ServiceWorkerFetchRequest request;
142   ServiceWorkerResponse response;
143   ServiceWorkerCacheQueryParams match_params;
144 };
145
146 // Represents initialization info for a WebServiceWorker object.
147 struct CONTENT_EXPORT ServiceWorkerObjectInfo {
148   ServiceWorkerObjectInfo();
149   int handle_id;
150   GURL scope;
151   GURL url;
152   blink::WebServiceWorkerState state;
153   int64 version_id;
154 };
155
156 struct ServiceWorkerRegistrationObjectInfo {
157   ServiceWorkerRegistrationObjectInfo();
158   int handle_id;
159   GURL scope;
160   int64 registration_id;
161 };
162
163 struct ServiceWorkerVersionAttributes {
164   ServiceWorkerObjectInfo installing;
165   ServiceWorkerObjectInfo waiting;
166   ServiceWorkerObjectInfo active;
167 };
168
169 class ChangedVersionAttributesMask {
170  public:
171   enum {
172     INSTALLING_VERSION = 1 << 0,
173     WAITING_VERSION = 1 << 1,
174     ACTIVE_VERSION = 1 << 2,
175     CONTROLLING_VERSION = 1 << 3,
176   };
177
178   ChangedVersionAttributesMask() : changed_(0) {}
179   explicit ChangedVersionAttributesMask(int changed) : changed_(changed) {}
180
181   int changed() const { return changed_; }
182
183   void add(int changed_versions) { changed_ |= changed_versions; }
184   bool installing_changed() const { return !!(changed_ & INSTALLING_VERSION); }
185   bool waiting_changed() const { return !!(changed_ & WAITING_VERSION); }
186   bool active_changed() const { return !!(changed_ & ACTIVE_VERSION); }
187   bool controller_changed() const { return !!(changed_ & CONTROLLING_VERSION); }
188
189  private:
190   int changed_;
191 };
192
193 }  // namespace content
194
195 #endif  // CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_