Object.cpp 7.8 KB

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