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_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_
6 #define CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "content/public/browser/service_worker_usage_info.h"
14 #include "net/base/completion_callback.h"
23 // Represents the per-StoragePartition ServiceWorker data.
24 class ServiceWorkerContext
{
26 // https://rawgithub.com/slightlyoff/ServiceWorker/master/spec/service_worker/index.html#url-scope:
27 // roughly, must be of the form "<origin>/<path>/*".
30 typedef base::Callback
<void(bool success
)> ResultCallback
;
32 typedef base::Callback
<void(const std::vector
<ServiceWorkerUsageInfo
>&
33 usage_info
)> GetUsageInfoCallback
;
35 typedef base::Callback
<void(bool has_service_worker
)>
36 CheckHasServiceWorkerCallback
;
38 // Registers the header name which should not be passed to the ServiceWorker.
39 // Must be called from the IO thread.
40 CONTENT_EXPORT
static void AddExcludedHeadersForFetchEvent(
41 const std::set
<std::string
>& header_names
);
43 // Returns true if the header name should not be passed to the ServiceWorker.
44 // Must be called from the IO thread.
45 static bool IsExcludedHeaderNameForFetchEvent(const std::string
& header_name
);
47 // Retrieves the ServiceWorkerContext, if any, associated with |request|.
48 CONTENT_EXPORT
static ServiceWorkerContext
* GetServiceWorkerContext(
49 net::URLRequest
* request
);
51 // Equivalent to calling navigator.serviceWorker.register(script_url, {scope:
52 // pattern}) from a renderer, except that |pattern| is an absolute URL instead
53 // of relative to some current origin. |callback| is passed true when the JS
54 // promise is fulfilled or false when the JS promise is rejected.
56 // The registration can fail if:
57 // * |script_url| is on a different origin from |pattern|
58 // * Fetching |script_url| fails.
59 // * |script_url| fails to parse or its top-level execution fails.
60 // TODO: The error message for this needs to be available to developers.
61 // * Something unexpected goes wrong, like a renderer crash or a full disk.
62 virtual void RegisterServiceWorker(const Scope
& pattern
,
63 const GURL
& script_url
,
64 const ResultCallback
& callback
) = 0;
66 // Equivalent to calling navigator.serviceWorker.unregister(pattern) from a
67 // renderer, except that |pattern| is an absolute URL instead of relative to
68 // some current origin. |callback| is passed true when the JS promise is
69 // fulfilled or false when the JS promise is rejected.
71 // Unregistration can fail if:
72 // * No Service Worker was registered for |pattern|.
73 // * Something unexpected goes wrong, like a renderer crash.
74 virtual void UnregisterServiceWorker(const Scope
& pattern
,
75 const ResultCallback
& callback
) = 0;
77 // TODO(jyasskin): Provide a way to SendMessage to a Scope.
79 // Determines if a request for |url| can be satisfied while offline.
80 // This method always completes asynchronously.
81 virtual void CanHandleMainResourceOffline(const GURL
& url
,
82 const GURL
& first_party
,
83 const net::CompletionCallback
&
86 // Methods used in response to browsing data and quota manager requests.
87 virtual void GetAllOriginsInfo(const GetUsageInfoCallback
& callback
) = 0;
88 virtual void DeleteForOrigin(const GURL
& origin_url
) = 0;
90 // Returns true if an active Service Worker registration exists that matches
91 // |url|, and if |other_url| falls inside the scope of the same registration.
92 // Note this still returns true even if there is a Service Worker registration
93 // which has a longer match for |other_url|.
94 // This function can be called from any thread, but the callback will always
95 // be called on the UI thread.
96 virtual void CheckHasServiceWorker(
98 const GURL
& other_url
,
99 const CheckHasServiceWorkerCallback
& callback
) = 0;
102 ServiceWorkerContext() {}
103 virtual ~ServiceWorkerContext() {}
106 } // namespace content
108 #endif // CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_