Object.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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::m_all_objects_list_node>& Object::all_objects()
  15. {
  16. static IntrusiveList<&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. ErrorOr<void> Object::try_add_child(Object& object)
  66. {
  67. // FIXME: Should we support reparenting objects?
  68. VERIFY(!object.parent() || object.parent() == this);
  69. TRY(m_children.try_append(object));
  70. object.m_parent = this;
  71. Core::ChildEvent child_event(Core::Event::ChildAdded, object);
  72. event(child_event);
  73. return {};
  74. }
  75. void Object::add_child(Object& object)
  76. {
  77. MUST(try_add_child(object));
  78. }
  79. void Object::insert_child_before(Object& new_child, Object& before_child)
  80. {
  81. // FIXME: Should we support reparenting objects?
  82. VERIFY(!new_child.parent() || new_child.parent() == this);
  83. new_child.m_parent = this;
  84. m_children.insert_before_matching(new_child, [&](auto& existing_child) { return existing_child.ptr() == &before_child; });
  85. Core::ChildEvent child_event(Core::Event::ChildAdded, new_child, &before_child);
  86. event(child_event);
  87. }
  88. void Object::remove_child(Object& object)
  89. {
  90. for (size_t i = 0; i < m_children.size(); ++i) {
  91. if (m_children.ptr_at(i).ptr() == &object) {
  92. // NOTE: We protect the child so it survives the handling of ChildRemoved.
  93. NonnullRefPtr<Object> protector = object;
  94. object.m_parent = nullptr;
  95. m_children.remove(i);
  96. Core::ChildEvent child_event(Core::Event::ChildRemoved, object);
  97. event(child_event);
  98. return;
  99. }
  100. }
  101. VERIFY_NOT_REACHED();
  102. }
  103. void Object::remove_all_children()
  104. {
  105. while (!m_children.is_empty())
  106. m_children.first().remove_from_parent();
  107. }
  108. void Object::timer_event(Core::TimerEvent&)
  109. {
  110. }
  111. void Object::child_event(Core::ChildEvent&)
  112. {
  113. }
  114. void Object::custom_event(CustomEvent&)
  115. {
  116. }
  117. void Object::start_timer(int ms, TimerShouldFireWhenNotVisible fire_when_not_visible)
  118. {
  119. if (m_timer_id) {
  120. dbgln("{} {:p} already has a timer!", class_name(), this);
  121. VERIFY_NOT_REACHED();
  122. }
  123. m_timer_id = Core::EventLoop::register_timer(*this, ms, true, fire_when_not_visible);
  124. }
  125. void Object::stop_timer()
  126. {
  127. if (!m_timer_id)
  128. return;
  129. bool success = Core::EventLoop::unregister_timer(m_timer_id);
  130. VERIFY(success);
  131. m_timer_id = 0;
  132. }
  133. void Object::dump_tree(int indent)
  134. {
  135. for (int i = 0; i < indent; ++i) {
  136. out(" ");
  137. }
  138. out("{}{{{:p}}}", class_name(), this);
  139. if (!name().is_null())
  140. out(" {}", name());
  141. outln();
  142. for_each_child([&](auto& child) {
  143. child.dump_tree(indent + 2);
  144. return IterationDecision::Continue;
  145. });
  146. }
  147. void Object::deferred_invoke(Function<void()> invokee)
  148. {
  149. Core::deferred_invoke([invokee = move(invokee), strong_this = NonnullRefPtr(*this)] { invokee(); });
  150. }
  151. void Object::save_to(JsonObject& json)
  152. {
  153. for (auto& it : m_properties) {
  154. auto& property = it.value;
  155. json.set(property->name(), property->get());
  156. }
  157. }
  158. JsonValue Object::property(String const& name) const
  159. {
  160. auto it = m_properties.find(name);
  161. if (it == m_properties.end())
  162. return JsonValue();
  163. return it->value->get();
  164. }
  165. bool Object::set_property(String const& name, JsonValue const& value)
  166. {
  167. auto it = m_properties.find(name);
  168. if (it == m_properties.end())
  169. return false;
  170. return it->value->set(value);
  171. }
  172. bool Object::is_ancestor_of(const Object& other) const
  173. {
  174. if (&other == this)
  175. return false;
  176. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  177. if (ancestor == this)
  178. return true;
  179. }
  180. return false;
  181. }
  182. void Object::dispatch_event(Core::Event& e, Object* stay_within)
  183. {
  184. VERIFY(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  185. auto* target = this;
  186. do {
  187. // If there's an event filter on this target, ask if it wants to swallow this event.
  188. if (target->m_event_filter && !target->m_event_filter(e))
  189. return;
  190. target->event(e);
  191. target = target->parent();
  192. if (target == stay_within) {
  193. // Prevent the event from bubbling any further.
  194. return;
  195. }
  196. } while (target && !e.is_accepted());
  197. }
  198. bool Object::is_visible_for_timer_purposes() const
  199. {
  200. if (parent())
  201. return parent()->is_visible_for_timer_purposes();
  202. return true;
  203. }
  204. void Object::increment_inspector_count(Badge<InspectorServerConnection>)
  205. {
  206. ++m_inspector_count;
  207. if (m_inspector_count == 1)
  208. did_begin_inspection();
  209. }
  210. void Object::decrement_inspector_count(Badge<InspectorServerConnection>)
  211. {
  212. --m_inspector_count;
  213. if (!m_inspector_count)
  214. did_end_inspection();
  215. }
  216. void Object::register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter)
  217. {
  218. m_properties.set(name, make<Property>(name, move(getter), move(setter)));
  219. }
  220. void Object::set_event_filter(Function<bool(Core::Event&)> filter)
  221. {
  222. m_event_filter = move(filter);
  223. }
  224. static HashMap<StringView, ObjectClassRegistration*>& object_classes()
  225. {
  226. static HashMap<StringView, ObjectClassRegistration*> s_map;
  227. return s_map;
  228. }
  229. ObjectClassRegistration::ObjectClassRegistration(StringView class_name, Function<RefPtr<Object>()> factory, ObjectClassRegistration* parent_class)
  230. : m_class_name(class_name)
  231. , m_factory(move(factory))
  232. , m_parent_class(parent_class)
  233. {
  234. object_classes().set(class_name, this);
  235. }
  236. ObjectClassRegistration::~ObjectClassRegistration()
  237. {
  238. }
  239. bool ObjectClassRegistration::is_derived_from(const ObjectClassRegistration& base_class) const
  240. {
  241. if (&base_class == this)
  242. return true;
  243. if (!m_parent_class)
  244. return false;
  245. return m_parent_class->is_derived_from(base_class);
  246. }
  247. void ObjectClassRegistration::for_each(Function<void(const ObjectClassRegistration&)> callback)
  248. {
  249. for (auto& it : object_classes()) {
  250. callback(*it.value);
  251. }
  252. }
  253. const ObjectClassRegistration* ObjectClassRegistration::find(StringView class_name)
  254. {
  255. return object_classes().get(class_name).value_or(nullptr);
  256. }
  257. }