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