Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_dom_workers_serviceworkerjob_h
8 : #define mozilla_dom_workers_serviceworkerjob_h
9 :
10 : #include "nsCOMPtr.h"
11 : #include "nsString.h"
12 : #include "nsTArray.h"
13 :
14 : class nsIPrincipal;
15 :
16 : namespace mozilla {
17 :
18 : class ErrorResult;
19 :
20 : namespace dom {
21 : namespace workers {
22 :
23 : class ServiceWorkerJob
24 : {
25 : public:
26 : // Implement this interface to receive notification when a job completes.
27 0 : class Callback
28 : {
29 : public:
30 : // Called once when the job completes. If the job is started, then this
31 : // will be called. If a job is never executed due to browser shutdown,
32 : // then this method will never be called. This method is always called
33 : // on the main thread asynchronously after Start() completes.
34 : virtual void JobFinished(ServiceWorkerJob* aJob, ErrorResult& aStatus) = 0;
35 :
36 : NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
37 : };
38 :
39 : enum class Type
40 : {
41 : Register,
42 : Update,
43 : Unregister
44 : };
45 :
46 : enum class State
47 : {
48 : Initial,
49 : Started,
50 : Finished
51 : };
52 :
53 : Type
54 : GetType() const;
55 :
56 : State
57 : GetState() const;
58 :
59 : // Determine if the job has been canceled. This does not change the
60 : // current State, but indicates that the job should progress to Finished
61 : // as soon as possible.
62 : bool
63 : Canceled() const;
64 :
65 : // Determine if the result callbacks have already been called. This is
66 : // equivalent to the spec checked to see if the job promise has settled.
67 : bool
68 : ResultCallbacksInvoked() const;
69 :
70 : bool
71 : IsEquivalentTo(ServiceWorkerJob* aJob) const;
72 :
73 : // Add a callback that will be invoked when the job's result is available.
74 : // Some job types will invoke this before the job is actually finished.
75 : // If an early callback does not occur, then it will be called automatically
76 : // when Finish() is called. These callbacks will be invoked while the job
77 : // state is Started.
78 : void
79 : AppendResultCallback(Callback* aCallback);
80 :
81 : // This takes ownership of any result callbacks associated with the given job
82 : // and then appends them to this job's callback list.
83 : void
84 : StealResultCallbacksFrom(ServiceWorkerJob* aJob);
85 :
86 : // Start the job. All work will be performed asynchronously on
87 : // the main thread. The Finish() method must be called exactly
88 : // once after this point. A final callback must be provided. It
89 : // will be invoked after all other callbacks have been processed.
90 : void
91 : Start(Callback* aFinalCallback);
92 :
93 : // Set an internal flag indicating that a started job should finish as
94 : // soon as possible.
95 : void
96 : Cancel();
97 :
98 : protected:
99 : ServiceWorkerJob(Type aType,
100 : nsIPrincipal* aPrincipal,
101 : const nsACString& aScope,
102 : const nsACString& aScriptSpec);
103 :
104 : virtual ~ServiceWorkerJob();
105 :
106 : // Invoke the result callbacks immediately. The job must be in the
107 : // Started state. The callbacks are cleared after being invoked,
108 : // so subsequent method calls have no effect.
109 : void
110 : InvokeResultCallbacks(ErrorResult& aRv);
111 :
112 : // Convenience method that converts to ErrorResult and calls real method.
113 : void
114 : InvokeResultCallbacks(nsresult aRv);
115 :
116 : // Indicate that the job has completed. The must be called exactly
117 : // once after Start() has initiated job execution. It may not be
118 : // called until Start() has returned.
119 : void
120 : Finish(ErrorResult& aRv);
121 :
122 : // Convenience method that converts to ErrorResult and calls real method.
123 : void
124 : Finish(nsresult aRv);
125 :
126 : // Specific job types should define AsyncExecute to begin their work.
127 : // All errors and successes must result in Finish() being called.
128 : virtual void
129 : AsyncExecute() = 0;
130 :
131 : const Type mType;
132 : nsCOMPtr<nsIPrincipal> mPrincipal;
133 : const nsCString mScope;
134 : const nsCString mScriptSpec;
135 :
136 : private:
137 : RefPtr<Callback> mFinalCallback;
138 : nsTArray<RefPtr<Callback>> mResultCallbackList;
139 : State mState;
140 : bool mCanceled;
141 : bool mResultCallbacksInvoked;
142 :
143 : public:
144 0 : NS_INLINE_DECL_REFCOUNTING(ServiceWorkerJob)
145 : };
146 :
147 : } // namespace workers
148 : } // namespace dom
149 : } // namespace mozilla
150 :
151 : #endif // mozilla_dom_workers_serviceworkerjob_h
|