Commit f4a65172 authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

Introduce InspectorAgentState::Clear()

This lets an agent clear all its fields, by calling
agent_state_.Clear();
And now all the methods are called just Clear, a bit
like STL calls different erase methods (range, iterator, value, ...)
just erase. What's being cleared will be distinguished
by the context only, e.g. method parameters and the receiver.

There's pros / cons to this but it's also kinda neat in a way.
I do need to do something about ClearAll either way so that I
can have a virtual method, so this choice felt both convenient
and principled. Hoping to get away with it. :-)

Change-Id: I439b77052e110968f196a21ae63020646a9ba23b
Reviewed-on: https://chromium-review.googlesource.com/1159822Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580064}
parent 9fb60492
...@@ -105,4 +105,10 @@ void InspectorAgentState::InitFrom(InspectorSessionState* session_state) { ...@@ -105,4 +105,10 @@ void InspectorAgentState::InitFrom(InspectorSessionState* session_state) {
for (Field* f : fields_) for (Field* f : fields_)
f->InitFrom(session_state); f->InitFrom(session_state);
} }
void InspectorAgentState::Clear() {
for (Field* f : fields_)
f->Clear();
}
} // namespace blink } // namespace blink
...@@ -72,6 +72,9 @@ class CORE_EXPORT InspectorAgentState { ...@@ -72,6 +72,9 @@ class CORE_EXPORT InspectorAgentState {
Decode(); Decode();
} }
// Clears the field to its default or to the empty map if it's a map field.
virtual void Clear() = 0;
protected: protected:
virtual void Decode() = 0; virtual void Decode() = 0;
...@@ -122,7 +125,7 @@ class CORE_EXPORT InspectorAgentState { ...@@ -122,7 +125,7 @@ class CORE_EXPORT InspectorAgentState {
} }
// Clears the field to its default. // Clears the field to its default.
void Clear() { void Clear() override {
if (default_value_ == value_) if (default_value_ == value_)
return; return;
value_ = default_value_; value_ = default_value_;
...@@ -209,8 +212,12 @@ class CORE_EXPORT InspectorAgentState { ...@@ -209,8 +212,12 @@ class CORE_EXPORT InspectorAgentState {
session_state_->EnqueueUpdate(prefix_key_ + key, WTF::String()); session_state_->EnqueueUpdate(prefix_key_ + key, WTF::String());
} }
// TODO(johannes): Remove this method after callers are migrated to
// ::Clear().
void ClearAll() { Clear(); }
// Clears the entire field. // Clears the entire field.
void ClearAll() { void Clear() override {
// TODO(johannes): Handle this in a single update. // TODO(johannes): Handle this in a single update.
for (const WTF::String& key : map_.Keys()) for (const WTF::String& key : map_.Keys())
session_state_->EnqueueUpdate(prefix_key_ + key, WTF::String()); session_state_->EnqueueUpdate(prefix_key_ + key, WTF::String());
...@@ -262,6 +269,9 @@ class CORE_EXPORT InspectorAgentState { ...@@ -262,6 +269,9 @@ class CORE_EXPORT InspectorAgentState {
// the constructors / initializers of agents takes care of it. // the constructors / initializers of agents takes care of it.
void InitFrom(InspectorSessionState* session_state); void InitFrom(InspectorSessionState* session_state);
// Clears all fields registered with this InspectorAgentState instance.
void Clear();
private: private:
const WTF::String domain_name_; const WTF::String domain_name_;
std::vector<Field*> fields_; std::vector<Field*> fields_;
......
...@@ -227,5 +227,33 @@ TEST(InspectorSessionStateTest, MultipleAgents) { ...@@ -227,5 +227,33 @@ TEST(InspectorSessionStateTest, MultipleAgents) {
keys.push_back(k); keys.push_back(k);
EXPECT_THAT(keys, UnorderedElementsAre("map_agents.1/Pi", "simple_agent.4/")); EXPECT_THAT(keys, UnorderedElementsAre("map_agents.1/Pi", "simple_agent.4/"));
{ // Renderer session, maps_agent clears its fields, and show that it will
// clear the agent's fields, but no other fields.
InspectorSessionState session_state(dev_tools_session.CloneCookie());
AgentWithSimpleFields simple_agent;
simple_agent.agent_state_.InitFrom(&session_state);
AgentWithMapFields maps_agent;
maps_agent.agent_state_.InitFrom(&session_state);
maps_agent.strings_.Set("foo", "bar");
maps_agent.agent_state_.Clear();
EXPECT_TRUE(maps_agent.doubles_.IsEmpty());
EXPECT_TRUE(maps_agent.strings_.IsEmpty());
EXPECT_FALSE(simple_agent.message_.Get().IsEmpty()); // other agent.
dev_tools_session.ApplyUpdates(session_state.TakeUpdates());
}
{ // Renderer session, this time the simple agent clears its fields and
// as a result the session has no more entries (both agents are cleared).
InspectorSessionState session_state(dev_tools_session.CloneCookie());
AgentWithSimpleFields simple_agent;
simple_agent.agent_state_.InitFrom(&session_state);
simple_agent.agent_state_.Clear();
dev_tools_session.ApplyUpdates(session_state.TakeUpdates());
}
EXPECT_TRUE(dev_tools_session.CloneCookie()->entries.IsEmpty());
} }
} // namespace blink } // namespace blink
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