Home | History | Annotate | Download | only in service_worker
      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 "third_party/WebKit/public/platform/WebServiceWorkerState.h"
     15 #include "url/gurl.h"
     16 
     17 // This file is to have common definitions that are to be shared by
     18 // browser and child process.
     19 
     20 namespace content {
     21 
     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;
     26 
     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;
     36 
     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
     44 };
     45 
     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;
     49   }
     50 };
     51 
     52 typedef std::map<std::string, std::string, ServiceWorkerCaseInsensitiveCompare>
     53     ServiceWorkerHeaderMap;
     54 
     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,
     61                             const GURL& referrer,
     62                             bool is_reload);
     63   ~ServiceWorkerFetchRequest();
     64 
     65   GURL url;
     66   std::string method;
     67   ServiceWorkerHeaderMap headers;
     68   std::string blob_uuid;
     69   uint64 blob_size;
     70   GURL referrer;
     71   bool is_reload;
     72 };
     73 
     74 // Represents a response to a fetch.
     75 struct CONTENT_EXPORT ServiceWorkerResponse {
     76   ServiceWorkerResponse();
     77   ServiceWorkerResponse(const GURL& url,
     78                         int status_code,
     79                         const std::string& status_text,
     80                         const ServiceWorkerHeaderMap& headers,
     81                         const std::string& blob_uuid);
     82   ~ServiceWorkerResponse();
     83 
     84   GURL url;
     85   int status_code;
     86   std::string status_text;
     87   ServiceWorkerHeaderMap headers;
     88   std::string blob_uuid;
     89 };
     90 
     91 // Controls how requests are matched in the Cache API.
     92 struct CONTENT_EXPORT ServiceWorkerCacheQueryParams {
     93   ServiceWorkerCacheQueryParams();
     94 
     95   bool ignore_search;
     96   bool ignore_method;
     97   bool ignore_vary;
     98   bool prefix_match;
     99 };
    100 
    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
    108 };
    109 
    110 // A single batch operation for the Cache API.
    111 struct CONTENT_EXPORT ServiceWorkerBatchOperation {
    112   ServiceWorkerBatchOperation();
    113 
    114   ServiceWorkerCacheOperationType operation_type;
    115   ServiceWorkerFetchRequest request;
    116   ServiceWorkerResponse response;
    117   ServiceWorkerCacheQueryParams match_params;
    118 };
    119 
    120 // Represents initialization info for a WebServiceWorker object.
    121 struct CONTENT_EXPORT ServiceWorkerObjectInfo {
    122   ServiceWorkerObjectInfo();
    123   int handle_id;
    124   GURL scope;
    125   GURL url;
    126   blink::WebServiceWorkerState state;
    127 };
    128 
    129 struct ServiceWorkerRegistrationObjectInfo {
    130   ServiceWorkerRegistrationObjectInfo();
    131   int handle_id;
    132   GURL scope;
    133 };
    134 
    135 struct ServiceWorkerVersionAttributes {
    136   ServiceWorkerObjectInfo installing;
    137   ServiceWorkerObjectInfo waiting;
    138   ServiceWorkerObjectInfo active;
    139 };
    140 
    141 class ChangedVersionAttributesMask {
    142  public:
    143   enum {
    144     INSTALLING_VERSION = 1 << 0,
    145     WAITING_VERSION = 1 << 1,
    146     ACTIVE_VERSION = 1 << 2,
    147     CONTROLLING_VERSION = 1 << 3,
    148   };
    149 
    150   ChangedVersionAttributesMask() : changed_(0) {}
    151   explicit ChangedVersionAttributesMask(int changed) : changed_(changed) {}
    152 
    153   int changed() const { return changed_; }
    154 
    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); }
    160 
    161  private:
    162   int changed_;
    163 };
    164 
    165 }  // namespace content
    166 
    167 #endif  // CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_TYPES_H_
    168