Commit 60b4506b authored by Patrick Monette's avatar Patrick Monette Committed by Commit Bot

Improve ObservedProperty to better handle movable types

Also changes NotifiesOnlyOnChangesWithPreviousValue to pass
the previous value by const-ref.

Bug: 999594
Change-Id: I63ce2cdb1d085ac68a7abb2a7b8211be0a353c56
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1787727
Commit-Queue: Patrick Monette <pmonette@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697810}
parent 51662adc
...@@ -129,7 +129,7 @@ void FreezeOriginTrialPolicyAggregator::OnIsCurrentChanged( ...@@ -129,7 +129,7 @@ void FreezeOriginTrialPolicyAggregator::OnIsCurrentChanged(
void FreezeOriginTrialPolicyAggregator::OnOriginTrialFreezePolicyChanged( void FreezeOriginTrialPolicyAggregator::OnOriginTrialFreezePolicyChanged(
const FrameNode* frame_node, const FrameNode* frame_node,
InterventionPolicy previous_value) { const InterventionPolicy& previous_value) {
if (frame_node->IsCurrent()) { if (frame_node->IsCurrent()) {
auto* page_node = PageNodeImpl::FromNode(frame_node->GetPageNode()); auto* page_node = PageNodeImpl::FromNode(frame_node->GetPageNode());
Data* data = Data::Get(page_node); Data* data = Data::Get(page_node);
......
...@@ -27,7 +27,7 @@ class FreezeOriginTrialPolicyAggregator : public FrameNode::ObserverDefaultImpl, ...@@ -27,7 +27,7 @@ class FreezeOriginTrialPolicyAggregator : public FrameNode::ObserverDefaultImpl,
void OnIsCurrentChanged(const FrameNode* frame_node) override; void OnIsCurrentChanged(const FrameNode* frame_node) override;
void OnOriginTrialFreezePolicyChanged( void OnOriginTrialFreezePolicyChanged(
const FrameNode* frame_node, const FrameNode* frame_node,
InterventionPolicy previous_value) override; const InterventionPolicy& previous_value) override;
// GraphOwned implementation: // GraphOwned implementation:
void OnPassedToGraph(Graph* graph) override; void OnPassedToGraph(Graph* graph) override;
......
...@@ -108,7 +108,7 @@ class LenientMockObserver : public FrameNodeImpl::Observer { ...@@ -108,7 +108,7 @@ class LenientMockObserver : public FrameNodeImpl::Observer {
MOCK_METHOD1(OnFrameLifecycleStateChanged, void(const FrameNode*)); MOCK_METHOD1(OnFrameLifecycleStateChanged, void(const FrameNode*));
MOCK_METHOD2(OnOriginTrialFreezePolicyChanged, MOCK_METHOD2(OnOriginTrialFreezePolicyChanged,
void(const FrameNode*, void(const FrameNode*,
resource_coordinator::mojom::InterventionPolicy)); const resource_coordinator::mojom::InterventionPolicy&));
MOCK_METHOD1(OnURLChanged, void(const FrameNode*)); MOCK_METHOD1(OnURLChanged, void(const FrameNode*));
MOCK_METHOD1(OnIsAdFrameChanged, void(const FrameNode*)); MOCK_METHOD1(OnIsAdFrameChanged, void(const FrameNode*));
MOCK_METHOD1(OnNonPersistentNotificationCreated, void(const FrameNode*)); MOCK_METHOD1(OnNonPersistentNotificationCreated, void(const FrameNode*));
......
...@@ -29,15 +29,18 @@ class ObservedPropertyImpl { ...@@ -29,15 +29,18 @@ class ObservedPropertyImpl {
void (ObserverType::*NotifyFunctionPtr)(const NodeType*)> void (ObserverType::*NotifyFunctionPtr)(const NodeType*)>
class NotifiesAlways { class NotifiesAlways {
public: public:
NotifiesAlways() {} NotifiesAlways() = default;
explicit NotifiesAlways(PropertyType initial_value)
: value_(initial_value) {}
~NotifiesAlways() {} template <typename U = PropertyType>
explicit NotifiesAlways(U&& initial_value)
: value_(std::forward<U>(initial_value)) {}
~NotifiesAlways() = default;
// Sets the property and sends a notification. // Sets the property and sends a notification.
void SetAndNotify(NodeImplType* node, PropertyType value) { template <typename U = PropertyType>
value_ = std::forward<PropertyType>(value); void SetAndNotify(NodeImplType* node, U&& value) {
value_ = std::forward<U>(value);
for (auto* observer : node->GetObservers()) for (auto* observer : node->GetObservers())
((observer)->*(NotifyFunctionPtr))(node); ((observer)->*(NotifyFunctionPtr))(node);
} }
...@@ -56,18 +59,21 @@ class ObservedPropertyImpl { ...@@ -56,18 +59,21 @@ class ObservedPropertyImpl {
void (ObserverType::*NotifyFunctionPtr)(const NodeType*)> void (ObserverType::*NotifyFunctionPtr)(const NodeType*)>
class NotifiesOnlyOnChanges { class NotifiesOnlyOnChanges {
public: public:
NotifiesOnlyOnChanges() {} NotifiesOnlyOnChanges() = default;
explicit NotifiesOnlyOnChanges(PropertyType initial_value)
: value_(initial_value) {} template <typename U = PropertyType>
explicit NotifiesOnlyOnChanges(U&& initial_value)
: value_(std::forward<U>(initial_value)) {}
~NotifiesOnlyOnChanges() {} ~NotifiesOnlyOnChanges() = default;
// Sets the property and sends a notification if needed. Returns true if a // Sets the property and sends a notification if needed. Returns true if a
// notification was sent, false otherwise. // notification was sent, false otherwise.
bool SetAndMaybeNotify(NodeImplType* node, PropertyType value) { template <typename U = PropertyType>
bool SetAndMaybeNotify(NodeImplType* node, U&& value) {
if (value_ == value) if (value_ == value)
return false; return false;
value_ = std::forward<PropertyType>(value); value_ = std::forward<U>(value);
for (auto* observer : node->GetObservers()) for (auto* observer : node->GetObservers())
((observer)->*(NotifyFunctionPtr))(node); ((observer)->*(NotifyFunctionPtr))(node);
return true; return true;
...@@ -83,23 +89,26 @@ class ObservedPropertyImpl { ...@@ -83,23 +89,26 @@ class ObservedPropertyImpl {
// notifying observers. // notifying observers.
template <typename PropertyType, template <typename PropertyType,
void (ObserverType::*NotifyFunctionPtr)( void (ObserverType::*NotifyFunctionPtr)(
const NodeType*, const NodeType* node,
PropertyType previous_value)> const PropertyType& previous_value)>
class NotifiesOnlyOnChangesWithPreviousValue { class NotifiesOnlyOnChangesWithPreviousValue {
public: public:
NotifiesOnlyOnChangesWithPreviousValue() {} NotifiesOnlyOnChangesWithPreviousValue() = default;
explicit NotifiesOnlyOnChangesWithPreviousValue(PropertyType initial_value)
: value_(initial_value) {} template <typename U = PropertyType>
explicit NotifiesOnlyOnChangesWithPreviousValue(U&& initial_value)
: value_(std::forward<U>(initial_value)) {}
~NotifiesOnlyOnChangesWithPreviousValue() {} ~NotifiesOnlyOnChangesWithPreviousValue() = default;
// Sets the property and sends a notification if needed. Returns true if a // Sets the property and sends a notification if needed. Returns true if a
// notification was sent, false otherwise. // notification was sent, false otherwise.
bool SetAndMaybeNotify(NodeImplType* node, PropertyType value) { template <typename U = PropertyType>
bool SetAndMaybeNotify(NodeImplType* node, U&& value) {
if (value_ == value) if (value_ == value)
return false; return false;
PropertyType previous_value = value_; PropertyType previous_value = std::move(value_);
value_ = std::forward<PropertyType>(value); value_ = std::forward<U>(value);
for (auto* observer : node->GetObservers()) for (auto* observer : node->GetObservers())
((observer)->*(NotifyFunctionPtr))(node, previous_value); ((observer)->*(NotifyFunctionPtr))(node, previous_value);
return true; return true;
......
...@@ -22,24 +22,17 @@ class DummyObserver { ...@@ -22,24 +22,17 @@ class DummyObserver {
MOCK_METHOD1(NotifyAlwaysConst, void(const DummyNode*)); MOCK_METHOD1(NotifyAlwaysConst, void(const DummyNode*));
MOCK_METHOD1(NotifyOnlyOnChangesConst, void(const DummyNode*)); MOCK_METHOD1(NotifyOnlyOnChangesConst, void(const DummyNode*));
MOCK_METHOD2(NotifyOnlyOnChangesWithPreviousValueConst, MOCK_METHOD2(NotifyOnlyOnChangesWithPreviousValueConst,
void(const DummyNode*, bool)); void(const DummyNode*, const bool&));
}; };
class DummyNode { class DummyNode {
public: public:
DummyNode() {} DummyNode() = default;
~DummyNode() {} ~DummyNode() = default;
void AddObserver(DummyObserver* observer) { void AddObserver(DummyObserver* observer) { observers_.push_back(observer); }
observers_.AddObserver(observer);
new_observers_.push_back(observer);
}
base::ObserverList<DummyObserver>::Unchecked& observers() {
return observers_;
}
const std::vector<DummyObserver*>& GetObservers() { return new_observers_; } const std::vector<DummyObserver*>& GetObservers() { return observers_; }
bool observed_always() const { return observed_always_.value(); } bool observed_always() const { return observed_always_.value(); }
bool observed_only_on_changes() const { bool observed_only_on_changes() const {
...@@ -74,8 +67,7 @@ class DummyNode { ...@@ -74,8 +67,7 @@ class DummyNode {
&DummyObserver::NotifyOnlyOnChangesWithPreviousValueConst> &DummyObserver::NotifyOnlyOnChangesWithPreviousValueConst>
observed_only_on_changes_with_previous_value_{false}; observed_only_on_changes_with_previous_value_{false};
base::ObserverList<DummyObserver>::Unchecked observers_; std::vector<DummyObserver*> observers_;
std::vector<DummyObserver*> new_observers_;
}; };
class GraphPropertiesTest : public ::testing::Test { class GraphPropertiesTest : public ::testing::Test {
...@@ -135,7 +127,7 @@ TEST_F(GraphPropertiesTest, ObservedOnlyOnChangesProperty) { ...@@ -135,7 +127,7 @@ TEST_F(GraphPropertiesTest, ObservedOnlyOnChangesProperty) {
TEST_F(GraphPropertiesTest, ObservedOnlyOnChangesWithPreviousValueProperty) { TEST_F(GraphPropertiesTest, ObservedOnlyOnChangesWithPreviousValueProperty) {
EXPECT_FALSE(node_.observed_only_on_changes_with_previous_value()); EXPECT_FALSE(node_.observed_only_on_changes_with_previous_value());
EXPECT_FALSE(node_.SetObservedOnlyOnChanges(false)); EXPECT_FALSE(node_.SetObservedOnlyOnChangesWithPreviousValue(false));
EXPECT_EQ(false, node_.observed_only_on_changes_with_previous_value()); EXPECT_EQ(false, node_.observed_only_on_changes_with_previous_value());
EXPECT_CALL(observer_, EXPECT_CALL(observer_,
......
...@@ -176,7 +176,7 @@ class FrameNodeObserver { ...@@ -176,7 +176,7 @@ class FrameNodeObserver {
// Invoked when the OriginTrialFreezePolicy changes. // Invoked when the OriginTrialFreezePolicy changes.
virtual void OnOriginTrialFreezePolicyChanged( virtual void OnOriginTrialFreezePolicyChanged(
const FrameNode* frame_node, const FrameNode* frame_node,
InterventionPolicy previous_value) = 0; const InterventionPolicy& previous_value) = 0;
// Invoked when the URL property changes. // Invoked when the URL property changes.
virtual void OnURLChanged(const FrameNode* frame_node) = 0; virtual void OnURLChanged(const FrameNode* frame_node) = 0;
...@@ -213,7 +213,7 @@ class FrameNode::ObserverDefaultImpl : public FrameNodeObserver { ...@@ -213,7 +213,7 @@ class FrameNode::ObserverDefaultImpl : public FrameNodeObserver {
void OnFrameLifecycleStateChanged(const FrameNode* frame_node) override {} void OnFrameLifecycleStateChanged(const FrameNode* frame_node) override {}
void OnOriginTrialFreezePolicyChanged( void OnOriginTrialFreezePolicyChanged(
const FrameNode* frame_node, const FrameNode* frame_node,
InterventionPolicy previous_value) override {} const InterventionPolicy& previous_value) override {}
void OnURLChanged(const FrameNode* frame_node) override {} void OnURLChanged(const FrameNode* frame_node) override {}
void OnIsAdFrameChanged(const FrameNode* frame_node) override {} void OnIsAdFrameChanged(const FrameNode* frame_node) override {}
void OnNonPersistentNotificationCreated( void OnNonPersistentNotificationCreated(
......
...@@ -62,7 +62,7 @@ class DiscardsGraphDumpImpl : public discards::mojom::GraphDump, ...@@ -62,7 +62,7 @@ class DiscardsGraphDumpImpl : public discards::mojom::GraphDump,
// Ignored. // Ignored.
void OnOriginTrialFreezePolicyChanged( void OnOriginTrialFreezePolicyChanged(
const performance_manager::FrameNode* frame_node, const performance_manager::FrameNode* frame_node,
InterventionPolicy previous_value) override {} const InterventionPolicy& previous_value) override {}
void OnURLChanged(const performance_manager::FrameNode* frame_node) override; void OnURLChanged(const performance_manager::FrameNode* frame_node) override;
// Ignored. // Ignored.
void OnIsAdFrameChanged( void OnIsAdFrameChanged(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment