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 "third_party/WebKit/public/platform/WebServiceWorkerState.h"
17 // This file is to have common definitions that are to be shared by
18 // browser and child process.
22 // Indicates invalid request ID (i.e. the sender does not expect it gets
23 // response for the message) for messaging between browser process
24 // and embedded worker.
25 static const int kInvalidServiceWorkerRequestId = -1;
27 // Constants for invalid identifiers.
28 static const int kInvalidServiceWorkerHandleId = -1;
29 static const int kInvalidServiceWorkerRegistrationHandleId = -1;
30 static const int kInvalidServiceWorkerProviderId = -1;
31 static const int64 kInvalidServiceWorkerRegistrationId = -1;
32 static const int64 kInvalidServiceWorkerVersionId = -1;
33 static const int64 kInvalidServiceWorkerResourceId = -1;
34 static const int64 kInvalidServiceWorkerResponseId = -1;
35 static const int kInvalidEmbeddedWorkerThreadId = -1;
37 // Indicates how the service worker handled a fetch event.
38 enum ServiceWorkerFetchEventResult {
39 // Browser should fallback to native fetch.
40 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK,
41 // Service worker provided a ServiceWorkerResponse.
42 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
43 SERVICE_WORKER_FETCH_EVENT_LAST = SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
46 struct ServiceWorkerCaseInsensitiveCompare {
47 bool operator()(const std::string& lhs, const std::string& rhs) const {
48 return base::strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
52 typedef std::map<std::string, std::string, ServiceWorkerCaseInsensitiveCompare>
53 ServiceWorkerHeaderMap;
55 // To dispatch fetch request from browser to child process.
56 struct CONTENT_EXPORT ServiceWorkerFetchRequest {
57 ServiceWorkerFetchRequest();
58 ServiceWorkerFetchRequest(const GURL& url,
59 const std::string& method,
60 const ServiceWorkerHeaderMap& headers,
63 ~ServiceWorkerFetchRequest();
67 ServiceWorkerHeaderMap headers;
68 std::string blob_uuid;
74 // Represents a response to a fetch.
75 struct CONTENT_EXPORT ServiceWorkerResponse {
76 ServiceWorkerResponse();
77 ServiceWorkerResponse(const GURL& url,
79 const std::string& status_text,
80 const ServiceWorkerHeaderMap& headers,
81 const std::string& blob_uuid);
82 ~ServiceWorkerResponse();
86 std::string status_text;
87 ServiceWorkerHeaderMap headers;
88 std::string blob_uuid;
91 // Controls how requests are matched in the Cache API.
92 struct CONTENT_EXPORT ServiceWorkerCacheQueryParams {
93 ServiceWorkerCacheQueryParams();
101 // The type of a single batch operation in the Cache API.
102 enum ServiceWorkerCacheOperationType {
103 SERVICE_WORKER_CACHE_OPERATION_TYPE_UNDEFINED,
104 SERVICE_WORKER_CACHE_OPERATION_TYPE_PUT,
105 SERVICE_WORKER_CACHE_OPERATION_TYPE_DELETE,
106 SERVICE_WORKER_CACHE_OPERATION_TYPE_LAST =
107 SERVICE_WORKER_CACHE_OPERATION_TYPE_DELETE
110 // A single batch operation for the Cache API.
111 struct CONTENT_EXPORT ServiceWorkerBatchOperation {
112 ServiceWorkerBatchOperation();
114 ServiceWorkerCacheOperationType operation_type;
115 ServiceWorkerFetchRequest request;
116 ServiceWorkerResponse response;
117 ServiceWorkerCacheQueryParams match_params;
120 // Represents initialization info for a WebServiceWorker object.
121 struct CONTENT_EXPORT ServiceWorkerObjectInfo {
122 ServiceWorkerObjectInfo();
126 blink::WebServiceWorkerState state;
129 struct ServiceWorkerRegistrationObjectInfo {
130 ServiceWorkerRegistrationObjectInfo();
135 struct ServiceWorkerVersionAttributes {
136 ServiceWorkerObjectInfo installing;
137 ServiceWorkerObjectInfo waiting;
138 ServiceWorkerObjectInfo active;
141 class ChangedVersionAttributesMask {
144 INSTALLING_VERSION = 1 << 0,
145 WAITING_VERSION = 1 << 1,
146 ACTIVE_VERSION = 1 << 2,
147 CONTROLLING_VERSION = 1 << 3,
150 ChangedVersionAttributesMask() : changed_(0) {}
151 explicit ChangedVersionAttributesMask(int changed) : changed_(changed) {}
153 int changed() const { return changed_; }
155 void add(int changed_versions) { changed_ |= changed_versions; }
156 bool installing_changed() const { return !!(changed_ & INSTALLING_VERSION); }
157 bool waiting_changed() const { return !!(changed_ & WAITING_VERSION); }
158 bool active_changed() const { return !!(changed_ & ACTIVE_VERSION); }
159 bool controller_changed() const { return !!(changed_ & CONTROLLING_VERSION); }
165 } // namespace content
167 #endif // CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_