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/UniquePtr.h"
12 
13 struct ServoComputedValues;
14 struct RawServoStyleSheet;
15 struct RawServoStyleSet;
16 struct RawServoDeclarationBlock;
17 
18 namespace mozilla {
19 namespace dom {
20 class Element;
21 class StyleChildrenIterator;
22 } // namespace dom
23 } // namespace mozilla
24 
25 class nsCSSValue;
26 class nsIDocument;
27 class nsINode;
28 
29 using mozilla::dom::StyleChildrenIterator;
30 
31 typedef nsINode RawGeckoNode;
32 typedef mozilla::dom::Element RawGeckoElement;
33 typedef nsIDocument RawGeckoDocument;
34 
35 // We have these helper types so that we can directly generate
36 // things like &T or Borrowed<T> on the Rust side in the function, providing
37 // additional safety benefits.
38 //
39 // FFI has a problem with templated types, so we just use raw pointers here.
40 //
41 // The "Borrowed" types generate &T or Borrowed<T> in the nullable case.
42 //
43 // The "Owned" types generate Owned<T> or OwnedOrNull<T>. Some of these
44 // are Servo-managed and can be converted to Box<ServoType> on the
45 // Servo side.
46 //
47 // The "Arc" types are Servo-managed Arc<ServoType>s, which are passed
48 // over FFI as Strong<T> (which is nullable).
49 // Note that T != ServoType, rather T is ArcInner<ServoType>
50 #define DECL_BORROWED_REF_TYPE_FOR(type_) typedef type_ const* type_##Borrowed;
51 #define DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_) typedef type_ const* type_##BorrowedOrNull;
52 #define DECL_BORROWED_MUT_REF_TYPE_FOR(type_) typedef type_* type_##BorrowedMut;
53 #define DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR(type_) typedef type_* type_##BorrowedMutOrNull;
54 
55 #define DECL_ARC_REF_TYPE_FOR(type_)         \
56   DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_) \
57   DECL_BORROWED_REF_TYPE_FOR(type_)          \
58   struct MOZ_MUST_USE_TYPE type_##Strong     \
59   {                                          \
60     type_* mPtr;                             \
61     already_AddRefed<type_> Consume();       \
62   };
63 
64 #define DECL_OWNED_REF_TYPE_FOR(type_)    \
65   typedef type_* type_##Owned;            \
66   DECL_BORROWED_REF_TYPE_FOR(type_)       \
67   DECL_BORROWED_MUT_REF_TYPE_FOR(type_)
68 
69 #define DECL_NULLABLE_OWNED_REF_TYPE_FOR(type_)    \
70   typedef type_* type_##OwnedOrNull;               \
71   DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_)       \
72   DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR(type_)
73 
74 DECL_ARC_REF_TYPE_FOR(ServoComputedValues)
75 DECL_ARC_REF_TYPE_FOR(RawServoStyleSheet)
76 DECL_ARC_REF_TYPE_FOR(RawServoDeclarationBlock)
77 // This is a reference to a reference of RawServoDeclarationBlock, which
78 // corresponds to Option<&Arc<RawServoDeclarationBlock>> in Servo side.
79 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawServoDeclarationBlockStrong)
80 
81 DECL_OWNED_REF_TYPE_FOR(RawServoStyleSet)
82 DECL_NULLABLE_OWNED_REF_TYPE_FOR(StyleChildrenIterator)
83 DECL_OWNED_REF_TYPE_FOR(StyleChildrenIterator)
84 
85 // We don't use BorrowedMut because the nodes may alias
86 // Servo itself doesn't directly read or mutate these;
87 // it only asks Gecko to do so. In case we wish to in
88 // the future, we should ensure that things being mutated
89 // are protected from noalias violations by a cell type
90 DECL_BORROWED_REF_TYPE_FOR(RawGeckoNode)
91 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawGeckoNode)
92 DECL_BORROWED_REF_TYPE_FOR(RawGeckoElement)
93 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawGeckoElement)
94 DECL_BORROWED_REF_TYPE_FOR(RawGeckoDocument)
95 DECL_NULLABLE_BORROWED_REF_TYPE_FOR(RawGeckoDocument)
96 DECL_BORROWED_MUT_REF_TYPE_FOR(StyleChildrenIterator)
97 DECL_BORROWED_REF_TYPE_FOR(nsCSSValue)
98 DECL_BORROWED_MUT_REF_TYPE_FOR(nsCSSValue)
99 
100 #undef DECL_ARC_REF_TYPE_FOR
101 #undef DECL_OWNED_REF_TYPE_FOR
102 #undef DECL_NULLABLE_OWNED_REF_TYPE_FOR
103 #undef DECL_BORROWED_REF_TYPE_FOR
104 #undef DECL_NULLABLE_BORROWED_REF_TYPE_FOR
105 #undef DECL_BORROWED_MUT_REF_TYPE_FOR
106 #undef DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR
107 
108 #define DEFINE_REFPTR_TRAITS(name_, type_)           \
109   extern "C" {                                       \
110   void Servo_##name_##_AddRef(type_##Borrowed ptr);  \
111   void Servo_##name_##_Release(type_##Borrowed ptr); \
112   }                                                  \
113   namespace mozilla {                                \
114   template<> struct RefPtrTraits<type_> {            \
115     static void AddRef(type_* aPtr) {                \
116       Servo_##name_##_AddRef(aPtr);                  \
117     }                                                \
118     static void Release(type_* aPtr) {               \
119       Servo_##name_##_Release(aPtr);                 \
120     }                                                \
121   };                                                 \
122   }
123 
124 DEFINE_REFPTR_TRAITS(StyleSheet, RawServoStyleSheet)
125 DEFINE_REFPTR_TRAITS(ComputedValues, ServoComputedValues)
126 DEFINE_REFPTR_TRAITS(DeclarationBlock, RawServoDeclarationBlock)
127 
128 #undef DEFINE_REFPTR_TRAITS
129 
130 extern "C" void Servo_StyleSet_Drop(RawServoStyleSetOwned ptr);
131 
132 namespace mozilla {
133 template<>
134 class DefaultDelete<RawServoStyleSet>
135 {
136 public:
operator()137   void operator()(RawServoStyleSet* aPtr) const
138   {
139     Servo_StyleSet_Drop(aPtr);
140   }
141 };
142 }
143 
144 #endif // mozilla_ServoBindingTypes_h
145