Source code

Revision control

Other Tools

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_serviceworkerjob_h
8
#define mozilla_dom_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
22
class ServiceWorkerJob {
23
public:
24
// Implement this interface to receive notification when a job completes.
25
class Callback {
26
public:
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;
32
33
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
34
};
35
36
enum class Type { Register, Update, Unregister };
37
38
enum class State { Initial, Started, Finished };
39
40
Type GetType() const;
41
42
State GetState() const;
43
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;
48
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;
52
53
bool IsEquivalentTo(ServiceWorkerJob* aJob) const;
54
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
59
// state is Started.
60
void AppendResultCallback(Callback* aCallback);
61
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);
65
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);
71
72
// Set an internal flag indicating that a started job should finish as
73
// soon as possible.
74
void Cancel();
75
76
protected:
77
ServiceWorkerJob(Type aType, nsIPrincipal* aPrincipal,
78
const nsACString& aScope, const nsACString& aScriptSpec);
79
80
virtual ~ServiceWorkerJob();
81
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);
86
87
// Convenience method that converts to ErrorResult and calls real method.
88
void InvokeResultCallbacks(nsresult aRv);
89
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);
94
95
// Convenience method that converts to ErrorResult and calls real method.
96
void Finish(nsresult aRv);
97
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;
101
102
const Type mType;
103
nsCOMPtr<nsIPrincipal> mPrincipal;
104
const nsCString mScope;
105
const nsCString mScriptSpec;
106
107
private:
108
RefPtr<Callback> mFinalCallback;
109
nsTArray<RefPtr<Callback>> mResultCallbackList;
110
State mState;
111
bool mCanceled;
112
bool mResultCallbacksInvoked;
113
114
public:
115
NS_INLINE_DECL_REFCOUNTING(ServiceWorkerJob)
116
};
117
118
} // namespace dom
119
} // namespace mozilla
120
121
#endif // mozilla_dom_serviceworkerjob_h