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_ServoBindingTypes_h
8 #define mozilla_ServoBindingTypes_h
9 
10 #include "mozilla/RefPtr.h"
11 #include "mozilla/ServoTypes.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/gfx/Types.h"
14 #include "nsCSSPropertyID.h"
15 #include "nsStyleAutoArray.h"
16 #include "nsTArray.h"
17 
18 struct RawServoAuthorStyles;
19 struct RawServoStyleSet;
20 struct RawServoSelectorList;
21 struct RawServoSourceSizeList;
22 struct RawServoAnimationValueMap;
23 struct RustString;
24 
25 #define SERVO_ARC_TYPE(name_, type_) struct type_;
26 #include "mozilla/ServoArcTypeList.h"
27 #undef SERVO_ARC_TYPE
28 
29 namespace mozilla {
30 class ServoElementSnapshot;
31 class ServoStyleContext;
32 struct StyleAnimation;
33 struct URLExtraData;
34 namespace dom {
35 class Element;
36 class StyleChildrenIterator;
37 }  // namespace dom
38 struct AnimationPropertySegment;
39 struct ComputedTiming;
40 struct Keyframe;
41 struct PropertyValuePair;
42 struct PropertyStyleAnimationValuePair;
43 enum class OriginFlags : uint8_t;
44 using ComputedKeyframeValues = nsTArray<PropertyStyleAnimationValuePair>;
45 }  // namespace mozilla
46 namespace nsStyleTransformMatrix {
47 enum class MatrixTransformOperator : uint8_t;
48 }
49 
50 class nsCSSPropertyIDSet;
51 class nsCSSValue;
52 struct nsFontFaceRuleContainer;
53 class nsIDocument;
54 class nsINode;
55 class nsPresContext;
56 class nsSimpleContentList;
57 struct nsTimingFunction;
58 class nsXBLBinding;
59 
60 using mozilla::ServoElementSnapshot;
61 using mozilla::dom::StyleChildrenIterator;
62 
63 typedef void* RawServoAnimationValueTableBorrowed;
64 
65 typedef nsINode RawGeckoNode;
66 typedef mozilla::dom::Element RawGeckoElement;
67 typedef nsIDocument RawGeckoDocument;
68 typedef nsPresContext RawGeckoPresContext;
69 typedef nsXBLBinding RawGeckoXBLBinding;
70 typedef mozilla::URLExtraData RawGeckoURLExtraData;
71 typedef nsTArray<RefPtr<RawServoAnimationValue>>
72     RawGeckoServoAnimationValueList;
73 typedef nsTArray<mozilla::Keyframe> RawGeckoKeyframeList;
74 typedef nsTArray<mozilla::PropertyValuePair> RawGeckoPropertyValuePairList;
75 typedef nsTArray<mozilla::ComputedKeyframeValues>
76     RawGeckoComputedKeyframeValuesList;
77 typedef nsStyleAutoArray<mozilla::StyleAnimation> RawGeckoStyleAnimationList;
78 typedef nsTArray<nsFontFaceRuleContainer> RawGeckoFontFaceRuleList;
79 typedef mozilla::AnimationPropertySegment RawGeckoAnimationPropertySegment;
80 typedef mozilla::ComputedTiming RawGeckoComputedTiming;
81 typedef nsTArray<const RawServoStyleRule*> RawGeckoServoStyleRuleList;
82 typedef nsTArray<nsCSSPropertyID> RawGeckoCSSPropertyIDList;
83 typedef mozilla::gfx::Float RawGeckoGfxMatrix4x4[16];
84 typedef mozilla::dom::StyleChildrenIterator RawGeckoStyleChildrenIterator;
85 
86 // We have these helper types so that we can directly generate
87 // things like &T or Borrowed<T> on the Rust side in the function, providing
88 // additional safety benefits.
89 //
90 // FFI has a problem with templated types, so we just use raw pointers here.
91 //
92 // The "Borrowed" types generate &T or Borrowed<T> in the nullable case.
93 //
94 // The "Owned" types generate Owned<T> or OwnedOrNull<T>. Some of these
95 // are Servo-managed and can be converted to Box<ServoType> on the
96 // Servo side.
97 //
98 // The "Arc" types are Servo-managed Arc<ServoType>s, which are passed
99 // over FFI as Strong<T> (which is nullable).
100 // Note that T != ServoType, rather T is ArcInner<ServoType>
101 #define DECL_BORROWED_REF_TYPE_FOR(type_) typedef type_ const* type_##Borrowed;
102 #define DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_) \
103   typedef type_ const* type_##BorrowedOrNull;
104 #define DECL_BORROWED_MUT_REF_TYPE_FOR(type_) typedef type_* type_##BorrowedMut;
105 #define DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR(type_) \
106   typedef type_* type_##BorrowedMutOrNull;
107 
108 #define SERVO_ARC_TYPE(name_, type_)         \
109   DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_) \
110   DECL_BORROWED_REF_TYPE_FOR(type_)          \
111   DECL_BORROWED_MUT_REF_TYPE_FOR(type_)      \
112   struct MOZ_MUST_USE_TYPE type_##Strong {   \
113     type_* mPtr;                             \
114     already_AddRefed<type_> Consume();       \
115   };
116 #include "mozilla/ServoArcTypeList.h"
117 #undef SERVO_ARC_TYPE
118 
119 typedef mozilla::ServoStyleContext const* ServoStyleContextBorrowed;
120 typedef mozilla::ServoStyleContext const* ServoStyleContextBorrowedOrNull;
121 typedef ServoComputedData const* ServoComputedDataBorrowed;
122 
123 struct MOZ_MUST_USE_TYPE ServoStyleContextStrong {
124   mozilla::ServoStyleContext* mPtr;
125   already_AddRefed<mozilla::ServoStyleContext> Consume();
126 };
127 
128 #define DECL_OWNED_REF_TYPE_FOR(type_) \
129   typedef type_* type_##Owned;         \
130   DECL_BORROWED_REF_TYPE_FOR(type_)    \
131   DECL_BORROWED_MUT_REF_TYPE_FOR(type_)
132 
133 #define DECL_NULLABLE_OWNED_REF_TYPE_FOR(type_) \
134   typedef type_* type_##OwnedOrNull;            \
135   DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_)    \
136   DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR(type_)
137 
138 // This is a reference to a reference of RawServoDeclarationBlock, which
139 // corresponds to Option<&Arc<RawServoDeclarationBlock>> in Servo side.
140 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawServoDeclarationBlockStrong)
141 DECL_OWNED_REF_TYPE_FOR(RawServoAuthorStyles)
142 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawServoAuthorStyles)
143 DECL_OWNED_REF_TYPE_FOR(RawServoStyleSet)
144 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawServoStyleSet)
145 DECL_NULLABLE_OWNED_REF_TYPE_FOR(StyleChildrenIterator)
146 DECL_OWNED_REF_TYPE_FOR(StyleChildrenIterator)
147 DECL_OWNED_REF_TYPE_FOR(ServoElementSnapshot)
148 DECL_OWNED_REF_TYPE_FOR(RawServoAnimationValueMap)
149 
150 // We don't use BorrowedMut because the nodes may alias
151 // Servo itself doesn't directly read or mutate these;
152 // it only asks Gecko to do so. In case we wish to in
153 // the future, we should ensure that things being mutated
154 // are protected from noalias violations by a cell type
155 DECL_BORROWED_REF_TYPE_FOR(RawGeckoNode)
156 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawGeckoNode)
157 DECL_BORROWED_REF_TYPE_FOR(RawGeckoElement)
158 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawGeckoElement)
159 DECL_BORROWED_REF_TYPE_FOR(RawGeckoDocument)
160 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawGeckoDocument)
161 DECL_BORROWED_REF_TYPE_FOR(RawGeckoXBLBinding)
162 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawGeckoXBLBinding)
163 DECL_BORROWED_MUT_REF_TYPE_FOR(StyleChildrenIterator)
164 DECL_BORROWED_MUT_REF_TYPE_FOR(ServoElementSnapshot)
165 DECL_BORROWED_REF_TYPE_FOR(nsCSSValue)
166 DECL_BORROWED_MUT_REF_TYPE_FOR(nsCSSValue)
167 DECL_OWNED_REF_TYPE_FOR(RawGeckoPresContext)
168 DECL_BORROWED_REF_TYPE_FOR(RawGeckoPresContext)
169 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoServoAnimationValueList)
170 DECL_BORROWED_REF_TYPE_FOR(RawGeckoServoAnimationValueList)
171 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoKeyframeList)
172 DECL_BORROWED_REF_TYPE_FOR(RawGeckoKeyframeList)
173 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoPropertyValuePairList)
174 DECL_BORROWED_REF_TYPE_FOR(RawGeckoPropertyValuePairList)
175 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoComputedKeyframeValuesList)
176 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoStyleAnimationList)
177 DECL_BORROWED_REF_TYPE_FOR(RawGeckoStyleAnimationList)
178 DECL_BORROWED_MUT_REF_TYPE_FOR(nsTimingFunction)
179 DECL_BORROWED_REF_TYPE_FOR(nsTimingFunction)
180 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoFontFaceRuleList)
181 DECL_BORROWED_REF_TYPE_FOR(RawGeckoAnimationPropertySegment)
182 DECL_BORROWED_REF_TYPE_FOR(RawGeckoComputedTiming)
183 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoServoStyleRuleList)
184 DECL_BORROWED_MUT_REF_TYPE_FOR(nsCSSPropertyIDSet)
185 DECL_BORROWED_REF_TYPE_FOR(RawGeckoCSSPropertyIDList)
186 DECL_BORROWED_REF_TYPE_FOR(nsXBLBinding)
187 DECL_BORROWED_MUT_REF_TYPE_FOR(RawGeckoStyleChildrenIterator)
188 DECL_OWNED_REF_TYPE_FOR(RawServoSelectorList)
189 DECL_BORROWED_REF_TYPE_FOR(RawServoSelectorList)
190 DECL_OWNED_REF_TYPE_FOR(RawServoSourceSizeList)
191 DECL_BORROWED_REF_TYPE_FOR(RawServoSourceSizeList)
192 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawServoSourceSizeList)
193 
194 #undef DECL_ARC_REF_TYPE_FOR
195 #undef DECL_OWNED_REF_TYPE_FOR
196 #undef DECL_NULLABLE_OWNED_REF_TYPE_FOR
197 #undef DECL_BORROWED_REF_TYPE_FOR
198 #undef DECL_NULLABLE_BORROWED_REF_TYPE_FOR
199 #undef DECL_BORROWED_MUT_REF_TYPE_FOR
200 #undef DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR
201 
202 #define SERVO_ARC_TYPE(name_, type_)                                    \
203   extern "C" {                                                          \
204   void Servo_##name_##_AddRef(type_##Borrowed ptr);                     \
205   void Servo_##name_##_Release(type_##Borrowed ptr);                    \
206   }                                                                     \
207   namespace mozilla {                                                   \
208   template <>                                                           \
209   struct RefPtrTraits<type_> {                                          \
210     static void AddRef(type_* aPtr) { Servo_##name_##_AddRef(aPtr); }   \
211     static void Release(type_* aPtr) { Servo_##name_##_Release(aPtr); } \
212   };                                                                    \
213   }
214 #include "mozilla/ServoArcTypeList.h"
215 #undef SERVO_ARC_TYPE
216 
217 #define DEFINE_BOXED_TYPE(name_, type_)                                \
218   extern "C" void Servo_##name_##_Drop(type_##Owned ptr);              \
219   namespace mozilla {                                                  \
220   template <>                                                          \
221   class DefaultDelete<type_> {                                         \
222    public:                                                             \
223     void operator()(type_* aPtr) const { Servo_##name_##_Drop(aPtr); } \
224   };                                                                   \
225   }
226 
227 DEFINE_BOXED_TYPE(StyleSet, RawServoStyleSet);
228 DEFINE_BOXED_TYPE(AuthorStyles, RawServoAuthorStyles);
229 DEFINE_BOXED_TYPE(SelectorList, RawServoSelectorList);
230 DEFINE_BOXED_TYPE(SourceSizeList, RawServoSourceSizeList);
231 
232 #undef DEFINE_BOXED_TYPE
233 
234 #endif  // mozilla_ServoBindingTypes_h
235