text: // Copyright 2018 The Chromium Authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  #ifndef COMPONENTS_UI_DEVTOOLS_VIZ_VIEWS_DOM_AGENT_VIZ_H_
  #define COMPONENTS_UI_DEVTOOLS_VIZ_VIEWS_DOM_AGENT_VIZ_H_
  
  #include "base/containers/flat_map.h"
  #include "base/containers/flat_set.h"
  #include "components/ui_devtools/DOM.h"
  #include "components/ui_devtools/dom_agent.h"
  #include "components/viz/common/surfaces/frame_sink_id.h"
  
  namespace viz {
  class FrameSinkManagerImpl;
  }
  
  namespace ui_devtools {
  
  class DOMAgentViz : public DOMAgent {
   public:
    explicit DOMAgentViz(viz::FrameSinkManagerImpl* frame_sink_manager);
    ~DOMAgentViz() override;
  
   private:
    std::unique_ptr<protocol::DOM::Node> BuildTreeForFrameSink(
        UIElement* frame_sink_element,
        const viz::FrameSinkId& frame_sink_id);
  
    // DOM::Backend:
    protocol::Response enable() override;
    protocol::Response disable() override;
  
    // DOMAgent:
    std::vector<UIElement*> CreateChildrenForRoot() override;
    std::unique_ptr<protocol::DOM::Node> BuildTreeForUIElement(
        UIElement* ui_element) override;
  
    // Every time the frontend disconnects we don't destroy DOMAgent so once we
    // establish the connection again we need to clear the FrameSinkId sets
    // because they may carry obsolete data. Then we initialize these with alive
    // FrameSinkIds. Clears the sets of FrameSinkIds that correspond to created
    // FrameSinks, registered FrameSinkIds and those that have corresponding
    // FrameSinkElements created.
    void Clear();
    // Initializes the sets of FrameSinkIds that correspond to registered
    // FrameSinkIds and created FrameSinks.
    void InitFrameSinkSets();
  
    // Mark a FrameSink that has |frame_sink_id| and all its subtree as attached.
    void SetAttachedFrameSink(const viz::FrameSinkId& frame_sink_id);
  
    // These sets are used to create and update the DOM tree.
    base::flat_set<viz::FrameSinkId> registered_frame_sink_ids_;
    base::flat_set<viz::FrameSinkId> client_connected_frame_sinks_;
  
    // This is used to track created FrameSinkElements and will be used for
    // updates in a FrameSink tree.
    base::flat_map<viz::FrameSinkId, UIElement*> frame_sink_elements_;
  
    // This is used to denote attached FrameSinks.
    base::flat_set<viz::FrameSinkId> attached_frame_sinks_;
  
    viz::FrameSinkManagerImpl* frame_sink_manager_;
  
    DISALLOW_COPY_AND_ASSIGN(DOMAgentViz);
  };
  
  }  // namespace ui_devtools
  
  #endif  // COMPONENTS_UI_DEVTOOLS_VIZ_VIEWS_DOM_AGENT_VIZ_H_
  