Object.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Assertions.h>
  8. #include <AK/Badge.h>
  9. #include <AK/JsonObject.h>
  10. #include <LibCore/Event.h>
  11. #include <LibCore/EventLoop.h>
  12. #include <LibCore/Object.h>
  13. #include <stdio.h>
  14. namespace Core {
  15. IntrusiveList<&Object::m_all_objects_list_node>& Object::all_objects()
  16. {
  17. static IntrusiveList<&Object::m_all_objects_list_node> objects;
  18. return objects;
  19. }
  20. Object::Object(Object* parent)
  21. : m_parent(parent)
  22. {
  23. all_objects().append(*this);
  24. if (m_parent)
  25. m_parent->add_child(*this);
  26. REGISTER_READONLY_STRING_PROPERTY("class_name", class_name);
  27. REGISTER_DEPRECATED_STRING_PROPERTY("name", name, set_name);
  28. register_property(
  29. "address", [this] { return FlatPtr(this); },
  30. [](auto&) { return false; });
  31. register_property(
  32. "parent", [this] { return FlatPtr(this->parent()); },
  33. [](auto&) { return false; });
  34. }
  35. Object::~Object()
  36. {
  37. // NOTE: We move our children out to a stack vector to prevent other
  38. // code from trying to iterate over them.
  39. auto children = move(m_children);
  40. // NOTE: We also unparent the children, so that they won't try to unparent
  41. // themselves in their own destructors.
  42. for (auto& child : children)
  43. child->m_parent = nullptr;
  44. all_objects().remove(*this);
  45. stop_timer();
  46. if (m_parent)
  47. m_parent->remove_child(*this);
  48. }
  49. void Object::event(Core::Event& event)
  50. {
  51. switch (event.type()) {
  52. case Core::Event::Timer:
  53. return timer_event(static_cast<TimerEvent&>(event));
  54. case Core::Event::ChildAdded:
  55. case Core::Event::ChildRemoved:
  56. return child_event(static_cast<ChildEvent&>(event));
  57. case Core::Event::Invalid:
  58. VERIFY_NOT_REACHED();
  59. break;
  60. case Core::Event::Custom:
  61. return custom_event(static_cast<CustomEvent&>(event));
  62. default:
  63. break;
  64. }
  65. }
  66. ErrorOr<void> Object::try_add_child(Object& object)
  67. {
  68. // FIXME: Should we support reparenting objects?
  69. VERIFY(!object.parent() || object.parent() == this);
  70. TRY(m_children.try_append(object));
  71. object.m_parent = this;
  72. Core::ChildEvent child_event(Core::Event::ChildAdded, object);
  73. event(child_event);
  74. return {};
  75. }
  76. void Object::add_child(Object& object)
  77. {
  78. MUST(try_add_child(object));
  79. }
  80. void Object::insert_child_before(Object& new_child, Object& before_child)
  81. {
  82. // FIXME: Should we support reparenting objects?
  83. VERIFY(!new_child.parent() || new_child.parent() == this);
  84. new_child.m_parent = this;
  85. m_children.insert_before_matching(new_child, [&](auto& existing_child) { return existing_child.ptr() == &before_child; });
  86. Core::ChildEvent child_event(Core::Event::ChildAdded, new_child, &before_child);
  87. event(child_event);
  88. }
  89. void Object::remove_child(Object& object)
  90. {
  91. for (size_t i = 0; i < m_children.size(); ++i) {
  92. if (m_children[i] == &object) {
  93. // NOTE: We protect the child so it survives the handling of ChildRemoved.
  94. NonnullRefPtr<Object> protector = object;
  95. object.m_parent = nullptr;
  96. m_children.remove(i);
  97. Core::ChildEvent child_event(Core::Event::ChildRemoved, object);
  98. event(child_event);
  99. return;
  100. }
  101. }
  102. VERIFY_NOT_REACHED();
  103. }
  104. void Object::remove_all_children()
  105. {
  106. while (!m_children.is_empty())
  107. m_children.first()->remove_from_parent();
  108. }
  109. void Object::timer_event(Core::TimerEvent&)
  110. {
  111. }
  112. void Object::child_event(Core::ChildEvent&)
  113. {
  114. }
  115. void Object::custom_event(CustomEvent&)
  116. {
  117. }
  118. void Object::start_timer(int ms, TimerShouldFireWhenNotVisible fire_when_not_visible)
  119. {
  120. if (m_timer_id) {
  121. dbgln("{} {:p} already has a timer!", class_name(), this);
  122. VERIFY_NOT_REACHED();
  123. }
  124. m_timer_id = Core::EventLoop::register_timer(*this, ms, true, fire_when_not_visible);
  125. }
  126. void Object::stop_timer()
  127. {
  128. if (!m_timer_id)
  129. return;
  130. bool success = Core::EventLoop::unregister_timer(m_timer_id);
  131. if (!success) {
  132. dbgln("{} {:p} could not unregister timer {}", class_name(), this, m_timer_id);
  133. }
  134. m_timer_id = 0;
  135. }
  136. void Object::dump_tree(int indent)
  137. {
  138. for (int i = 0; i < indent; ++i) {
  139. out(" ");
  140. }
  141. out("{}{{{:p}}}", class_name(), this);
  142. if (!name().is_null())
  143. out(" {}", name());
  144. outln();
  145. for_each_child([&](auto& child) {
  146. child.dump_tree(indent + 2);
  147. return IterationDecision::Continue;
  148. });
  149. }
  150. void Object::deferred_invoke(Function<void()> invokee)
  151. {
  152. Core::deferred_invoke([invokee = move(invokee), strong_this = NonnullRefPtr(*this)] { invokee(); });
  153. }
  154. JsonValue Object::property(DeprecatedString const& name) const
  155. {
  156. auto it = m_properties.find(name);
  157. if (it == m_properties.end())
  158. return JsonValue();
  159. return it->value->get();
  160. }
  161. bool Object::set_property(DeprecatedString const& name, JsonValue const& value)
  162. {
  163. auto it = m_properties.find(name);
  164. if (it == m_properties.end())
  165. return false;
  166. return it->value->set(value);
  167. }
  168. bool Object::is_ancestor_of(Object const& other) const
  169. {
  170. if (&other == this)
  171. return false;
  172. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  173. if (ancestor == this)
  174. return true;
  175. }
  176. return false;
  177. }
  178. void Object::dispatch_event(Core::Event& e, Object* stay_within)
  179. {
  180. VERIFY(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  181. auto* target = this;
  182. do {
  183. // If there's an event filter on this target, ask if it wants to swallow this event.
  184. if (target->m_event_filter && !target->m_event_filter(e))
  185. return;
  186. target->event(e);
  187. target = target->parent();
  188. if (target == stay_within) {
  189. // Prevent the event from bubbling any further.
  190. return;
  191. }
  192. } while (target && !e.is_accepted());
  193. }
  194. bool Object::is_visible_for_timer_purposes() const
  195. {
  196. if (parent())
  197. return parent()->is_visible_for_timer_purposes();
  198. return true;
  199. }
  200. void Object::increment_inspector_count(Badge<InspectorServerConnection>)
  201. {
  202. ++m_inspector_count;
  203. if (m_inspector_count == 1)
  204. did_begin_inspection();
  205. }
  206. void Object::decrement_inspector_count(Badge<InspectorServerConnection>)
  207. {
  208. --m_inspector_count;
  209. if (!m_inspector_count)
  210. did_end_inspection();
  211. }
  212. void Object::register_property(DeprecatedString const& name, Function<JsonValue()> getter, Function<bool(JsonValue const&)> setter)
  213. {
  214. m_properties.set(name, make<Property>(name, move(getter), move(setter)));
  215. }
  216. void Object::set_event_filter(Function<bool(Core::Event&)> filter)
  217. {
  218. m_event_filter = move(filter);
  219. }
  220. static HashMap<StringView, ObjectClassRegistration*>& object_classes()
  221. {
  222. static HashMap<StringView, ObjectClassRegistration*> s_map;
  223. return s_map;
  224. }
  225. ObjectClassRegistration::ObjectClassRegistration(StringView class_name, Function<ErrorOr<NonnullRefPtr<Object>>()> factory, ObjectClassRegistration* parent_class)
  226. : m_class_name(class_name)
  227. , m_factory(move(factory))
  228. , m_parent_class(parent_class)
  229. {
  230. object_classes().set(class_name, this);
  231. }
  232. bool ObjectClassRegistration::is_derived_from(ObjectClassRegistration const& base_class) const
  233. {
  234. if (&base_class == this)
  235. return true;
  236. if (!m_parent_class)
  237. return false;
  238. return m_parent_class->is_derived_from(base_class);
  239. }
  240. void ObjectClassRegistration::for_each(Function<void(ObjectClassRegistration const&)> callback)
  241. {
  242. for (auto& it : object_classes()) {
  243. callback(*it.value);
  244. }
  245. }
  246. ObjectClassRegistration const* ObjectClassRegistration::find(StringView class_name)
  247. {
  248. return object_classes().get(class_name).value_or(nullptr);
  249. }
  250. }