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/. */
7 #ifndef mozilla_dom_serviceworkerjob_h
8 #define mozilla_dom_serviceworkerjob_h
22 class ServiceWorkerJob
{
24 // Implement this interface to receive notification when a job completes or
28 // Called once when the job completes. If the job is started, then this
29 // will be called. If a job is never executed due to browser shutdown,
30 // then this method will never be called. This method is always called
31 // on the main thread asynchronously after Start() completes.
32 virtual void JobFinished(ServiceWorkerJob
* aJob
, ErrorResult
& aStatus
) = 0;
34 // If the job has not started and will never start, then this will be
35 // called; either JobFinished or JobDiscarded will be called, but not both.
36 virtual void JobDiscarded(ErrorResult
& aStatus
) = 0;
38 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
41 enum class Type
{ Register
, Update
, Unregister
};
43 enum class State
{ Initial
, Started
, Finished
};
47 State
GetState() const;
49 // Determine if the job has been canceled. This does not change the
50 // current State, but indicates that the job should progress to Finished
51 // as soon as possible.
52 bool Canceled() const;
54 // Determine if the result callbacks have already been called. This is
55 // equivalent to the spec checked to see if the job promise has settled.
56 bool ResultCallbacksInvoked() const;
58 bool IsEquivalentTo(ServiceWorkerJob
* aJob
) const;
60 // Add a callback that will be invoked when the job's result is available.
61 // Some job types will invoke this before the job is actually finished.
62 // If an early callback does not occur, then it will be called automatically
63 // when Finish() is called. These callbacks will be invoked while the job
65 void AppendResultCallback(Callback
* aCallback
);
67 // This takes ownership of any result callbacks associated with the given job
68 // and then appends them to this job's callback list.
69 void StealResultCallbacksFrom(ServiceWorkerJob
* aJob
);
71 // Start the job. All work will be performed asynchronously on
72 // the main thread. The Finish() method must be called exactly
73 // once after this point. A final callback must be provided. It
74 // will be invoked after all other callbacks have been processed.
75 void Start(Callback
* aFinalCallback
);
77 // Set an internal flag indicating that a started job should finish as
82 ServiceWorkerJob(Type aType
, nsIPrincipal
* aPrincipal
,
83 const nsACString
& aScope
, nsCString aScriptSpec
);
85 virtual ~ServiceWorkerJob();
87 // Invoke the result callbacks immediately. The job must be in the
88 // Started state or be canceled and in the Initial state. The callbacks are
89 // cleared after being invoked, so subsequent method calls have no effect.
90 void InvokeResultCallbacks(ErrorResult
& aRv
);
92 // Convenience method that converts to ErrorResult and calls real method.
93 void InvokeResultCallbacks(nsresult aRv
);
95 // Indicate that the job has completed. The must be called exactly
96 // once after Start() has initiated job execution. It may not be
97 // called until Start() has returned.
98 void Finish(ErrorResult
& aRv
);
100 // Convenience method that converts to ErrorResult and calls real method.
101 void Finish(nsresult aRv
);
103 // Specific job types should define AsyncExecute to begin their work.
104 // All errors and successes must result in Finish() being called.
105 virtual void AsyncExecute() = 0;
108 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
109 const nsCString mScope
;
110 const nsCString mScriptSpec
;
113 RefPtr
<Callback
> mFinalCallback
;
114 nsTArray
<RefPtr
<Callback
>> mResultCallbackList
;
117 bool mResultCallbacksInvoked
;
120 NS_INLINE_DECL_REFCOUNTING(ServiceWorkerJob
)
124 } // namespace mozilla
126 #endif // mozilla_dom_serviceworkerjob_h