Index: services/resource_coordinator/coordination_unit/coordination_unit_impl.h |
diff --git a/services/resource_coordinator/coordination_unit/coordination_unit_impl.h b/services/resource_coordinator/coordination_unit/coordination_unit_impl.h |
index d8b3fe24fdb9721d8bb51e73513408e58d5bc720..fe3bc9bfb39d2d57b8f1caba58249530549e264b 100644 |
--- a/services/resource_coordinator/coordination_unit/coordination_unit_impl.h |
+++ b/services/resource_coordinator/coordination_unit/coordination_unit_impl.h |
@@ -11,19 +11,89 @@ |
#include <unordered_map> |
#include <utility> |
+#include <vector> |
+#include "base/callback.h" |
+ |
#include "base/optional.h" |
#include "base/values.h" |
#include "mojo/public/cpp/bindings/binding_set.h" |
#include "mojo/public/cpp/bindings/interface_request.h" |
#include "mojo/public/cpp/bindings/strong_binding.h" |
#include "services/resource_coordinator/public/cpp/coordination_unit_id.h" |
+#include "services/resource_coordinator/public/cpp/coordination_unit_types.h" |
#include "services/resource_coordinator/public/interfaces/coordination_unit.mojom.h" |
#include "services/resource_coordinator/public/interfaces/coordination_unit_provider.mojom.h" |
#include "services/service_manager/public/cpp/service_context_ref.h" |
namespace resource_coordinator { |
+// Collection to manage CoordinationUnitEvent listeners. |
+// It is naive prototype with lots of room for improvement. |
+template <typename Listener, typename Filter> |
+class CoordinationUnitEventListenerList { |
+ public: |
+ CoordinationUnitEventListenerList() = default; |
+ ~CoordinationUnitEventListenerList() = default; |
+ |
+ // callbacks paired with this kWildcardFilter will always be invoked |
+ static const Filter kWildcardFilter = static_cast<Filter>(0); |
+ |
+ void AddListener(Listener listener, Filter filter) { |
+ listeners_.push_back(std::make_pair(filter, listener)); |
+ } |
+ |
+ void AddListener(Listener listener) { |
+ AddListener(listener, kWildcardFilter); |
+ } |
+ |
+ // Get the listeners who are are either registered to be invoked for a |
+ // specific filter or were registered with the kWildcardFilter. |
+ // TODO(matthalp) add iterator support to replace this call and |
+ // avoid excessive copying that currently occurs |
+ std::vector<Listener> GetListeners(Filter filter) { |
+ std::vector<Listener> listeners; |
+ |
+ for (auto& listener_info : listeners_) { |
+ if (listener_info.first == filter || |
+ listener_info.first == kWildcardFilter) { |
+ listeners.push_back(listener_info.second); |
+ } |
+ } |
+ |
+ return listeners; |
+ } |
+ |
+ std::vector<Listener> GetListeners() { return GetListeners(kWildcardFilter); } |
zhenw
2017/06/13 18:38:41
There can be 2 ways to interpret this:
1. Get the
matthalp
2017/06/13 20:57:11
I see the ambiguity. I don't believe this method i
|
+ |
+ private: |
+ std::vector<std::pair<Filter, Listener>> listeners_; |
zhenw
2017/06/13 18:38:41
A map of vectors is probably more efficient.
matthalp
2017/06/13 20:57:11
I can do this.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(CoordinationUnitEventListenerList); |
+}; |
+ |
class CoordinationUnitImpl : public mojom::CoordinationUnit { |
+ typedef base::Callback<void( |
+ const CoordinationUnitImpl* coordination_unit, |
+ const CoordinationUnitImpl* child_coordination_unit)> |
+ AddChildEventListener; |
zhenw
2017/06/13 18:38:41
Those should follow On*() style.
matthalp
2017/06/13 20:57:11
I can do this.
|
+ typedef base::Callback<void( |
+ const CoordinationUnitImpl* coordination_unit, |
+ const CoordinationUnitImpl* parent_coordination_unit)> |
+ AddParentEventListener; |
+ typedef base::Callback<void( |
+ const CoordinationUnitImpl* coordination_unit, |
+ const CoordinationUnitImpl* removed_child_coordination_unit)> |
+ RemoveChildEventListener; |
+ typedef base::Callback<void( |
+ const CoordinationUnitImpl* coordination_unit, |
+ const CoordinationUnitImpl* removed_parent_coordination_unit)> |
+ RemoveParentEventListener; |
+ typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit, |
+ mojom::PropertyType property)> |
+ PropertyChangedEventListener; |
+ typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit)> |
+ WillBeDestroyedEventListener; |
+ |
public: |
CoordinationUnitImpl( |
const CoordinationUnitID& id, |
@@ -55,10 +125,45 @@ class CoordinationUnitImpl : public mojom::CoordinationUnit { |
// Clear property from internal key-value store |
void ClearProperty(mojom::PropertyType property); |
// Retrieve property from internal key-value store |
- base::Value GetProperty(mojom::PropertyType property); |
+ base::Value GetProperty(mojom::PropertyType property) const; |
// Set property from internal key-value store |
void SetProperty(mojom::PropertyType property, base::Value value); |
+ // Notify an instance it will be destroyed before its destructor is |
+ // actually called. |
+ void WillBeDestroyed(); |
+ |
+ CoordinationUnitEventListenerList<AddChildEventListener, |
+ CoordinationUnitType>& |
+ on_add_child_event_listeners() { |
+ return on_add_child_event_listeners_; |
+ } |
+ CoordinationUnitEventListenerList<AddParentEventListener, |
+ CoordinationUnitType>& |
+ on_add_parent_event_listeners() { |
+ return on_add_parent_event_listeners_; |
+ } |
+ CoordinationUnitEventListenerList<RemoveChildEventListener, |
+ CoordinationUnitType>& |
+ on_remove_child_event_listeners() { |
+ return on_remove_child_event_listeners_; |
+ } |
+ CoordinationUnitEventListenerList<RemoveParentEventListener, |
+ CoordinationUnitType>& |
+ on_remove_parent_event_listeners() { |
+ return on_remove_parent_event_listeners_; |
+ } |
+ CoordinationUnitEventListenerList<PropertyChangedEventListener, |
+ mojom::PropertyType>& |
+ on_property_changed_event_listeners() { |
+ return on_property_changed_event_listeners_; |
+ } |
+ CoordinationUnitEventListenerList<WillBeDestroyedEventListener, |
+ CoordinationUnitType>& |
+ on_will_be_destroyed_event_listeners() { |
+ return on_will_be_destroyed_event_listeners_; |
+ } |
+ |
protected: |
const CoordinationUnitID id_; |
std::set<CoordinationUnitImpl*> children_; |
@@ -93,6 +198,26 @@ class CoordinationUnitImpl : public mojom::CoordinationUnit { |
base::Optional<bool> state_flags_[kNumStateFlags]; |
+ CoordinationUnitEventListenerList<AddChildEventListener, CoordinationUnitType> |
+ on_add_child_event_listeners_; |
+ CoordinationUnitEventListenerList<AddParentEventListener, |
+ CoordinationUnitType> |
+ on_add_parent_event_listeners_; |
+ CoordinationUnitEventListenerList<RemoveChildEventListener, |
+ CoordinationUnitType> |
+ on_remove_child_event_listeners_; |
+ CoordinationUnitEventListenerList<RemoveParentEventListener, |
+ CoordinationUnitType> |
+ on_remove_parent_event_listeners_; |
+ CoordinationUnitEventListenerList<PropertyChangedEventListener, |
+ mojom::PropertyType> |
+ on_property_changed_event_listeners_; |
+ // There is nothing to filter WillBeDestroyedEventCallbacks on so the |
+ // CoordinationUnitType is used as a filter placeholder |
+ CoordinationUnitEventListenerList<WillBeDestroyedEventListener, |
+ CoordinationUnitType> |
+ on_will_be_destroyed_event_listeners_; |
+ |
DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); |
}; |