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 : #include "FetchSignal.h"
8 : #include "mozilla/dom/Event.h"
9 : #include "mozilla/dom/FetchSignalBinding.h"
10 :
11 : namespace mozilla {
12 : namespace dom {
13 :
14 : NS_IMPL_CYCLE_COLLECTION_CLASS(FetchSignal)
15 :
16 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FetchSignal,
17 : DOMEventTargetHelper)
18 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mController)
19 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
20 :
21 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FetchSignal,
22 : DOMEventTargetHelper)
23 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK(mController)
24 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
25 :
26 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FetchSignal)
27 0 : NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
28 :
29 0 : NS_IMPL_ADDREF_INHERITED(FetchSignal, DOMEventTargetHelper)
30 0 : NS_IMPL_RELEASE_INHERITED(FetchSignal, DOMEventTargetHelper)
31 :
32 0 : FetchSignal::FetchSignal(FetchController* aController,
33 0 : bool aAborted)
34 : : DOMEventTargetHelper(aController->GetParentObject())
35 : , mController(aController)
36 0 : , mAborted(aAborted)
37 0 : {}
38 :
39 0 : FetchSignal::FetchSignal(bool aAborted)
40 0 : : mAborted(aAborted)
41 0 : {}
42 :
43 : JSObject*
44 0 : FetchSignal::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
45 : {
46 0 : return FetchSignalBinding::Wrap(aCx, this, aGivenProto);
47 : }
48 :
49 : bool
50 0 : FetchSignal::Aborted() const
51 : {
52 0 : return mAborted;
53 : }
54 :
55 : void
56 0 : FetchSignal::Abort()
57 : {
58 0 : MOZ_ASSERT(!mAborted);
59 0 : mAborted = true;
60 :
61 : // Let's inform the followers.
62 0 : for (uint32_t i = 0; i < mFollowers.Length(); ++i) {
63 0 : mFollowers[i]->Aborted();
64 : }
65 :
66 0 : EventInit init;
67 0 : init.mBubbles = false;
68 0 : init.mCancelable = false;
69 :
70 : // TODO which kind of event should we dispatch here?
71 :
72 : RefPtr<Event> event =
73 0 : Event::Constructor(this, NS_LITERAL_STRING("abort"), init);
74 0 : event->SetTrusted(true);
75 :
76 : bool dummy;
77 0 : DispatchEvent(event, &dummy);
78 0 : }
79 :
80 : void
81 0 : FetchSignal::AddFollower(FetchSignal::Follower* aFollower)
82 : {
83 0 : MOZ_DIAGNOSTIC_ASSERT(aFollower);
84 0 : if (!mFollowers.Contains(aFollower)) {
85 0 : mFollowers.AppendElement(aFollower);
86 : }
87 0 : }
88 :
89 : void
90 0 : FetchSignal::RemoveFollower(FetchSignal::Follower* aFollower)
91 : {
92 0 : MOZ_DIAGNOSTIC_ASSERT(aFollower);
93 0 : mFollowers.RemoveElement(aFollower);
94 0 : }
95 :
96 : bool
97 0 : FetchSignal::CanAcceptFollower(FetchSignal::Follower* aFollower) const
98 : {
99 0 : MOZ_DIAGNOSTIC_ASSERT(aFollower);
100 :
101 0 : if (!mController) {
102 0 : return true;
103 : }
104 :
105 0 : if (aFollower == mController) {
106 0 : return false;
107 : }
108 :
109 0 : FetchSignal* following = mController->Following();
110 0 : if (!following) {
111 0 : return true;
112 : }
113 :
114 0 : return following->CanAcceptFollower(aFollower);
115 : }
116 :
117 : // FetchSignal::Follower
118 : // ----------------------------------------------------------------------------
119 :
120 2 : FetchSignal::Follower::~Follower()
121 : {
122 1 : Unfollow();
123 1 : }
124 :
125 : void
126 0 : FetchSignal::Follower::Follow(FetchSignal* aSignal)
127 : {
128 0 : MOZ_DIAGNOSTIC_ASSERT(aSignal);
129 :
130 0 : if (!aSignal->CanAcceptFollower(this)) {
131 0 : return;
132 : }
133 :
134 0 : Unfollow();
135 :
136 0 : mFollowingSignal = aSignal;
137 0 : aSignal->AddFollower(this);
138 : }
139 :
140 : void
141 1 : FetchSignal::Follower::Unfollow()
142 : {
143 1 : if (mFollowingSignal) {
144 0 : mFollowingSignal->RemoveFollower(this);
145 0 : mFollowingSignal = nullptr;
146 : }
147 1 : }
148 :
149 : } // dom namespace
150 : } // mozilla namespace
|