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
,
89 const ResultCallback
& callback
) = 0;
91 // Returns true if an active Service Worker registration exists that matches
92 // |url|, and if |other_url| falls inside the scope of the same registration.
93 // Note this still returns true even if there is a Service Worker registration
94 // which has a longer match for |other_url|.
95 // This function can be called from any thread, but the callback will always
96 // be called on the UI thread.
97 virtual void CheckHasServiceWorker(
99 const GURL
& other_url
,
100 const CheckHasServiceWorkerCallback
& callback
) = 0;
102 // Stops all running workers on the given |origin|.
103 virtual void StopAllServiceWorkersForOrigin(const GURL
& origin
) = 0;
105 // Stops all running service workers and unregisters all service worker
106 // registrations. This method is used in LayoutTests to make sure that the
107 // existing service worker will not affect the succeeding tests.
108 virtual void ClearAllServiceWorkersForTest(const base::Closure
& callback
) = 0;
111 ServiceWorkerContext() {}
112 virtual ~ServiceWorkerContext() {}
115 } // namespace content
117 #endif // CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_