• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_
6 #define CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_
7 
8 #include "base/basictypes.h"
9 #include "base/callback_forward.h"
10 #include "url/gurl.h"
11 
12 namespace content {
13 
14 // Represents the per-StoragePartition ServiceWorker data.  Must be used from
15 // the UI thread.
16 class ServiceWorkerContext {
17  public:
18   // https://rawgithub.com/slightlyoff/ServiceWorker/master/spec/service_worker/index.html#url-scope:
19   // roughly, must be of the form "<origin>/<path>/*".
20   typedef GURL Scope;
21 
22   typedef base::Callback<void(bool success)> ResultCallback;
23 
24   // Equivalent to calling navigator.serviceWorker.register(script_url, {scope:
25   // pattern}) from a renderer, except that |pattern| is an absolute URL instead
26   // of relative to some current origin.  |callback| is passed true when the JS
27   // promise is fulfilled or false when the JS promise is rejected.
28   //
29   // The registration can fail if:
30   //  * |script_url| is on a different origin from |pattern|
31   //  * Fetching |script_url| fails.
32   //  * |script_url| fails to parse or its top-level execution fails.
33   //    TODO: The error message for this needs to be available to developers.
34   //  * Something unexpected goes wrong, like a renderer crash or a full disk.
35   virtual void RegisterServiceWorker(const Scope& pattern,
36                                      const GURL& script_url,
37                                      const ResultCallback& callback) = 0;
38 
39   // Equivalent to calling navigator.serviceWorker.unregister(pattern) from a
40   // renderer, except that |pattern| is an absolute URL instead of relative to
41   // some current origin.  |callback| is passed true when the JS promise is
42   // fulfilled or false when the JS promise is rejected.
43   //
44   // Unregistration can fail if:
45   //  * No Service Worker was registered for |pattern|.
46   //  * Something unexpected goes wrong, like a renderer crash.
47   virtual void UnregisterServiceWorker(const Scope& pattern,
48                                        const ResultCallback& callback) = 0;
49 
50   // TODO(jyasskin): Provide a way to SendMessage to a Scope.
51 
52   // Synchronously releases all of the RenderProcessHosts that have Service
53   // Workers running inside them, and prevents any new Service Worker instances
54   // from starting up.
55   virtual void Terminate() = 0;
56 
57  protected:
ServiceWorkerContext()58   ServiceWorkerContext() {}
~ServiceWorkerContext()59   virtual ~ServiceWorkerContext() {}
60 };
61 
62 }  // namespace content
63 
64 #endif  // CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_
65