Object.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_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.ptr_at(i).ptr() == &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. VERIFY(success);
  132. m_timer_id = 0;
  133. }
  134. void Object::dump_tree(int indent)
  135. {
  136. for (int i = 0; i < indent; ++i) {
  137. out(" ");
  138. }
  139. out("{}{{{:p}}}", class_name(), this);
  140. if (!name().is_null())
  141. out(" {}", name());
  142. outln();
  143. for_each_child([&](auto& child) {
  144. child.dump_tree(indent + 2);
  145. return IterationDecision::Continue;
  146. });
  147. }
  148. void Object::deferred_invoke(Function<void()> invokee)
  149. {
  150. Core::deferred_invoke([invokee = move(invokee), strong_this = NonnullRefPtr(*this)] { invokee(); });
  151. }
  152. void Object::save_to(JsonObject& json)
  153. {
  154. for (auto& it : m_properties) {
  155. auto& property = it.value;
  156. json.set(property->name(), property->get());
  157. }
  158. }
  159. JsonValue Object::property(DeprecatedString const& name) const
  160. {
  161. auto it = m_properties.find(name);
  162. if (it == m_properties.end())
  163. return JsonValue();
  164. return it->value->get();
  165. }
  166. bool Object::set_property(DeprecatedString const& name, JsonValue const& value)
  167. {
  168. auto it = m_properties.find(name);
  169. if (it == m_properties.end())
  170. return false;
  171. return it->value->set(value);
  172. }
  173. bool Object::is_ancestor_of(Object const& other) const
  174. {
  175. if (&other == this)
  176. return false;
  177. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  178. if (ancestor == this)
  179. return true;
  180. }
  181. return false;
  182. }
  183. void Object::dispatch_event(Core::Event& e, Object* stay_within)
  184. {
  185. VERIFY(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  186. auto* target = this;
  187. do {
  188. // If there's an event filter on this target, ask if it wants to swallow this event.
  189. if (target->m_event_filter && !target->m_event_filter(e))
  190. return;
  191. target->event(e);
  192. target = target->parent();
  193. if (target == stay_within) {
  194. // Prevent the event from bubbling any further.
  195. return;
  196. }
  197. } while (target && !e.is_accepted());
  198. }
  199. bool Object::is_visible_for_timer_purposes() const
  200. {
  201. if (parent())
  202. return parent()->is_visible_for_timer_purposes();
  203. return true;
  204. }
  205. void Object::increment_inspector_count(Badge<InspectorServerConnection>)
  206. {
  207. ++m_inspector_count;
  208. if (m_inspector_count == 1)
  209. did_begin_inspection();
  210. }
  211. void Object::decrement_inspector_count(Badge<InspectorServerConnection>)
  212. {
  213. --m_inspector_count;
  214. if (!m_inspector_count)
  215. did_end_inspection();
  216. }
  217. void Object::register_property(DeprecatedString const& name, Function<JsonValue()> getter, Function<bool(JsonValue const&)> setter)
  218. {
  219. m_properties.set(name, make<Property>(name, move(getter), move(setter)));
  220. }
  221. void Object::set_event_filter(Function<bool(Core::Event&)> filter)
  222. {
  223. m_event_filter = move(filter);
  224. }
  225. static HashMap<StringView, ObjectClassRegistration*>& object_classes()
  226. {
  227. static HashMap<StringView, ObjectClassRegistration*> s_map;
  228. return s_map;
  229. }
  230. ObjectClassRegistration::ObjectClassRegistration(StringView class_name, Function<ErrorOr<NonnullRefPtr<Object>>()> factory, ObjectClassRegistration* parent_class)
  231. : m_class_name(class_name)
  232. , m_factory(move(factory))
  233. , m_parent_class(parent_class)
  234. {
  235. object_classes().set(class_name, this);
  236. }
  237. bool ObjectClassRegistration::is_derived_from(ObjectClassRegistration const& base_class) const
  238. {
  239. if (&base_class == this)
  240. return true;
  241. if (!m_parent_class)
  242. return false;
  243. return m_parent_class->is_derived_from(base_class);
  244. }
  245. void ObjectClassRegistration::for_each(Function<void(ObjectClassRegistration const&)> callback)
  246. {
  247. for (auto& it : object_classes()) {
  248. callback(*it.value);
  249. }
  250. }
  251. ObjectClassRegistration const* ObjectClassRegistration::find(StringView class_name)
  252. {
  253. return object_classes().get(class_name).value_or(nullptr);
  254. }
  255. }