1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef InspectorLayerTreeAgent_h
30#define InspectorLayerTreeAgent_h
31
32#if ENABLE(INSPECTOR)
33
34#include "InspectorWebAgentBase.h"
35#include "InspectorWebBackendDispatchers.h"
36#include "InspectorWebFrontendDispatchers.h"
37#include "InspectorWebTypeBuilders.h"
38#include "RenderLayer.h"
39#include <wtf/PassRefPtr.h>
40#include <wtf/Vector.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45class InstrumentingAgents;
46
47typedef String ErrorString;
48
49class InspectorLayerTreeAgent : public InspectorAgentBase, public Inspector::InspectorLayerTreeBackendDispatcherHandler {
50    WTF_MAKE_FAST_ALLOCATED;
51public:
52    explicit InspectorLayerTreeAgent(InstrumentingAgents*);
53    ~InspectorLayerTreeAgent();
54
55    virtual void didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel*, Inspector::InspectorBackendDispatcher*) override;
56    virtual void willDestroyFrontendAndBackend(Inspector::InspectorDisconnectReason) override;
57    void reset();
58
59    void layerTreeDidChange();
60    void renderLayerDestroyed(const RenderLayer*);
61    void pseudoElementDestroyed(PseudoElement*);
62
63    // Called from the front-end.
64    virtual void enable(ErrorString*) override;
65    virtual void disable(ErrorString*) override;
66    virtual void layersForNode(ErrorString*, int nodeId, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::LayerTree::Layer>>&) override;
67    virtual void reasonsForCompositingLayer(ErrorString*, const String& layerId, RefPtr<Inspector::TypeBuilder::LayerTree::CompositingReasons>&) override;
68
69private:
70    // RenderLayer-related methods.
71    String bind(const RenderLayer*);
72    void unbind(const RenderLayer*);
73
74    void gatherLayersUsingRenderObjectHierarchy(ErrorString*, RenderObject*, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::LayerTree::Layer>>&);
75    void gatherLayersUsingRenderLayerHierarchy(ErrorString*, RenderLayer*, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::LayerTree::Layer>>&);
76
77    PassRefPtr<Inspector::TypeBuilder::LayerTree::Layer> buildObjectForLayer(ErrorString*, RenderLayer*);
78    PassRefPtr<Inspector::TypeBuilder::LayerTree::IntRect> buildObjectForIntRect(const IntRect&);
79
80    int idForNode(ErrorString*, Node*);
81
82    String bindPseudoElement(PseudoElement*);
83    void unbindPseudoElement(PseudoElement*);
84
85    std::unique_ptr<Inspector::InspectorLayerTreeFrontendDispatcher> m_frontendDispatcher;
86    RefPtr<Inspector::InspectorLayerTreeBackendDispatcher> m_backendDispatcher;
87
88    HashMap<const RenderLayer*, String> m_documentLayerToIdMap;
89    HashMap<String, const RenderLayer*> m_idToLayer;
90
91    HashMap<PseudoElement*, String> m_pseudoElementToIdMap;
92    HashMap<String, PseudoElement*> m_idToPseudoElement;
93};
94
95} // namespace WebCore
96
97#endif // ENABLE(INSPECTOR)
98
99#endif // !defined(InspectorLayerTreeAgent_h)
100