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_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_
6 #define CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_
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"
20 // This file is to have common definitions that are to be shared by
21 // browser and child process.
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;
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;
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
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
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
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;
70 typedef std::map<std::string, std::string, ServiceWorkerCaseInsensitiveCompare>
71 ServiceWorkerHeaderMap;
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,
81 ~ServiceWorkerFetchRequest();
83 FetchRequestMode mode;
84 RequestContextType request_context_type;
85 RequestContextFrameType frame_type;
88 ServiceWorkerHeaderMap headers;
89 std::string blob_uuid;
92 FetchCredentialsMode credentials_mode;
96 // Represents a response to a fetch.
97 struct CONTENT_EXPORT ServiceWorkerResponse {
98 ServiceWorkerResponse();
99 ServiceWorkerResponse(const GURL& url,
101 const std::string& status_text,
102 blink::WebServiceWorkerResponseType response_type,
103 const ServiceWorkerHeaderMap& headers,
104 const std::string& blob_uuid,
106 ~ServiceWorkerResponse();
110 std::string status_text;
111 blink::WebServiceWorkerResponseType response_type;
112 ServiceWorkerHeaderMap headers;
113 std::string blob_uuid;
117 // Controls how requests are matched in the Cache API.
118 struct CONTENT_EXPORT ServiceWorkerCacheQueryParams {
119 ServiceWorkerCacheQueryParams();
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
136 // A single batch operation for the Cache API.
137 struct CONTENT_EXPORT ServiceWorkerBatchOperation {
138 ServiceWorkerBatchOperation();
140 ServiceWorkerCacheOperationType operation_type;
141 ServiceWorkerFetchRequest request;
142 ServiceWorkerResponse response;
143 ServiceWorkerCacheQueryParams match_params;
146 // Represents initialization info for a WebServiceWorker object.
147 struct CONTENT_EXPORT ServiceWorkerObjectInfo {
148 ServiceWorkerObjectInfo();
152 blink::WebServiceWorkerState state;
156 struct ServiceWorkerRegistrationObjectInfo {
157 ServiceWorkerRegistrationObjectInfo();
160 int64 registration_id;
163 struct ServiceWorkerVersionAttributes {
164 ServiceWorkerObjectInfo installing;
165 ServiceWorkerObjectInfo waiting;
166 ServiceWorkerObjectInfo active;
169 class ChangedVersionAttributesMask {
172 INSTALLING_VERSION = 1 << 0,
173 WAITING_VERSION = 1 << 1,
174 ACTIVE_VERSION = 1 << 2,
175 CONTROLLING_VERSION = 1 << 3,
178 ChangedVersionAttributesMask() : changed_(0) {}
179 explicit ChangedVersionAttributesMask(int changed) : changed_(changed) {}
181 int changed() const { return changed_; }
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); }
193 } // namespace content
195 #endif // CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_