3ea9535f9022bee10f12a601799e9b7092386bd4
[WebKit-https.git] / Source / WebCore / inspector / InspectorCSSAgent.h
1 /*
2  * Copyright (C) 2010, Google 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  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #ifndef InspectorCSSAgent_h
26 #define InspectorCSSAgent_h
27
28 #include "CSSSelector.h"
29 #include "ContentSecurityPolicy.h"
30 #include "InspectorDOMAgent.h"
31 #include "InspectorStyleSheet.h"
32 #include "InspectorWebAgentBase.h"
33 #include "InspectorWebBackendDispatchers.h"
34 #include "SecurityContext.h"
35 #include <inspector/InspectorValues.h>
36 #include <wtf/HashMap.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RefPtr.h>
40 #include <wtf/text/WTFString.h>
41
42 namespace Inspector {
43 class InspectorCSSFrontendDispatcher;
44 }
45
46 namespace WebCore {
47
48 class CSSRule;
49 class CSSRuleList;
50 class CSSStyleDeclaration;
51 class CSSStyleRule;
52 class CSSStyleSheet;
53 class Document;
54 class DocumentStyleSheetCollection;
55 class Element;
56 class InspectorCSSOMWrappers;
57 class InstrumentingAgents;
58 class NameNodeMap;
59 class Node;
60 class NodeList;
61 class StyleResolver;
62 class StyleRule;
63 class UpdateRegionLayoutTask;
64 class ChangeRegionOversetTask;
65
66 #if ENABLE(INSPECTOR)
67
68 class InspectorCSSAgent
69     : public InspectorAgentBase
70     , public InspectorDOMAgent::DOMListener
71     , public Inspector::InspectorCSSBackendDispatcherHandler
72     , public InspectorStyleSheet::Listener {
73     WTF_MAKE_NONCOPYABLE(InspectorCSSAgent);
74 public:
75     class InlineStyleOverrideScope {
76     public:
77         InlineStyleOverrideScope(SecurityContext* context)
78             : m_contentSecurityPolicy(context->contentSecurityPolicy())
79         {
80             m_contentSecurityPolicy->setOverrideAllowInlineStyle(true);
81         }
82
83         ~InlineStyleOverrideScope()
84         {
85             m_contentSecurityPolicy->setOverrideAllowInlineStyle(false);
86         }
87
88     private:
89         ContentSecurityPolicy* m_contentSecurityPolicy;
90     };
91
92     InspectorCSSAgent(InstrumentingAgents*, InspectorDOMAgent*);
93     ~InspectorCSSAgent();
94
95     static CSSStyleRule* asCSSStyleRule(CSSRule*);
96
97     bool forcePseudoState(Element*, CSSSelector::PseudoType);
98     virtual void didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel*, Inspector::InspectorBackendDispatcher*) override;
99     virtual void willDestroyFrontendAndBackend() override;
100     virtual void discardAgent();
101     virtual void enable(ErrorString*);
102     virtual void disable(ErrorString*);
103     void reset();
104     void mediaQueryResultChanged();
105     void didCreateNamedFlow(Document*, WebKitNamedFlow*);
106     void willRemoveNamedFlow(Document*, WebKitNamedFlow*);
107     void didUpdateRegionLayout(Document*, WebKitNamedFlow*);
108     void regionLayoutUpdated(WebKitNamedFlow*, int documentNodeId);
109     void didChangeRegionOverset(Document*, WebKitNamedFlow*);
110     void regionOversetChanged(WebKitNamedFlow*, int documentNodeId);
111     void didRegisterNamedFlowContentElement(Document*, WebKitNamedFlow*, Node* contentElement, Node* nextContentElement = nullptr);
112     void didUnregisterNamedFlowContentElement(Document*, WebKitNamedFlow*, Node* contentElement);
113
114     virtual void getComputedStyleForNode(ErrorString*, int nodeId, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::CSSComputedStyleProperty>>&);
115     virtual void getInlineStylesForNode(ErrorString*, int nodeId, RefPtr<Inspector::TypeBuilder::CSS::CSSStyle>& inlineStyle, RefPtr<Inspector::TypeBuilder::CSS::CSSStyle>& attributes);
116     virtual void getMatchedStylesForNode(ErrorString*, int nodeId, const bool* includePseudo, const bool* includeInherited, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::RuleMatch>>& matchedCSSRules, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::PseudoIdMatches>>& pseudoIdMatches, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::InheritedStyleEntry>>& inheritedEntries);
117     virtual void getAllStyleSheets(ErrorString*, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::CSSStyleSheetHeader>>& styleSheetInfos);
118     virtual void getStyleSheet(ErrorString*, const String& styleSheetId, RefPtr<Inspector::TypeBuilder::CSS::CSSStyleSheetBody>& result);
119     virtual void getStyleSheetText(ErrorString*, const String& styleSheetId, String* result);
120     virtual void setStyleSheetText(ErrorString*, const String& styleSheetId, const String& text);
121     virtual void setStyleText(ErrorString*, const RefPtr<Inspector::InspectorObject>& styleId, const String& text, RefPtr<Inspector::TypeBuilder::CSS::CSSStyle>& result);
122     virtual void setPropertyText(ErrorString*, const RefPtr<Inspector::InspectorObject>& styleId, int propertyIndex, const String& text, bool overwrite, RefPtr<Inspector::TypeBuilder::CSS::CSSStyle>& result);
123     virtual void toggleProperty(ErrorString*, const RefPtr<Inspector::InspectorObject>& styleId, int propertyIndex, bool disable, RefPtr<Inspector::TypeBuilder::CSS::CSSStyle>& result);
124     virtual void setRuleSelector(ErrorString*, const RefPtr<Inspector::InspectorObject>& ruleId, const String& selector, RefPtr<Inspector::TypeBuilder::CSS::CSSRule>& result);
125     virtual void addRule(ErrorString*, int contextNodeId, const String& selector, RefPtr<Inspector::TypeBuilder::CSS::CSSRule>& result);
126     virtual void getSupportedCSSProperties(ErrorString*, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::CSSPropertyInfo>>& result);
127     virtual void forcePseudoState(ErrorString*, int nodeId, const RefPtr<Inspector::InspectorArray>& forcedPseudoClasses);
128     virtual void getNamedFlowCollection(ErrorString*, int documentNodeId, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::NamedFlow>>& result);
129
130 private:
131     class StyleSheetAction;
132     class SetStyleSheetTextAction;
133     class SetStyleTextAction;
134     class SetPropertyTextAction;
135     class TogglePropertyAction;
136     class SetRuleSelectorAction;
137     class AddRuleAction;
138
139     typedef HashMap<String, RefPtr<InspectorStyleSheet>> IdToInspectorStyleSheet;
140     typedef HashMap<CSSStyleSheet*, RefPtr<InspectorStyleSheet>> CSSStyleSheetToInspectorStyleSheet;
141     typedef HashMap<Node*, RefPtr<InspectorStyleSheetForInlineStyle>> NodeToInspectorStyleSheet; // bogus "stylesheets" with elements' inline styles
142     typedef HashMap<RefPtr<Document>, RefPtr<InspectorStyleSheet>> DocumentToViaInspectorStyleSheet; // "via inspector" stylesheets
143     typedef HashMap<int, unsigned> NodeIdToForcedPseudoState;
144
145     void resetNonPersistentData();
146     InspectorStyleSheetForInlineStyle* asInspectorStyleSheet(Element* element);
147     Element* elementForId(ErrorString*, int nodeId);
148     int documentNodeWithRequestedFlowsId(Document*);
149     void collectStyleSheets(CSSStyleSheet*, Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::CSSStyleSheetHeader>*);
150
151     InspectorStyleSheet* bindStyleSheet(CSSStyleSheet*);
152     InspectorStyleSheet* viaInspectorStyleSheet(Document*, bool createIfAbsent);
153     InspectorStyleSheet* assertStyleSheetForId(ErrorString*, const String&);
154     Inspector::TypeBuilder::CSS::StyleSheetOrigin::Enum detectOrigin(CSSStyleSheet* pageStyleSheet, Document* ownerDocument);
155
156     PassRefPtr<Inspector::TypeBuilder::CSS::CSSRule> buildObjectForRule(StyleRule*, StyleResolver&);
157     PassRefPtr<Inspector::TypeBuilder::CSS::CSSRule> buildObjectForRule(CSSStyleRule*);
158     PassRefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::CSSRule>> buildArrayForRuleList(CSSRuleList*);
159     PassRefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::RuleMatch>> buildArrayForMatchedRuleList(const Vector<RefPtr<StyleRuleBase>>&, StyleResolver&, Element*);
160     PassRefPtr<Inspector::TypeBuilder::CSS::CSSStyle> buildObjectForAttributesStyle(Element*);
161     PassRefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::CSS::Region>> buildArrayForRegions(ErrorString*, PassRefPtr<NodeList>, int documentNodeId);
162     PassRefPtr<Inspector::TypeBuilder::CSS::NamedFlow> buildObjectForNamedFlow(ErrorString*, WebKitNamedFlow*, int documentNodeId);
163
164     // InspectorDOMAgent::DOMListener implementation
165     virtual void didRemoveDocument(Document*);
166     virtual void didRemoveDOMNode(Node*);
167     virtual void didModifyDOMAttr(Element*);
168
169     // InspectorCSSAgent::Listener implementation
170     virtual void styleSheetChanged(InspectorStyleSheet*);
171
172     void resetPseudoStates();
173
174     std::unique_ptr<Inspector::InspectorCSSFrontendDispatcher> m_frontendDispatcher;
175     RefPtr<Inspector::InspectorCSSBackendDispatcher> m_backendDispatcher;
176     InspectorDOMAgent* m_domAgent;
177
178     IdToInspectorStyleSheet m_idToInspectorStyleSheet;
179     CSSStyleSheetToInspectorStyleSheet m_cssStyleSheetToInspectorStyleSheet;
180     NodeToInspectorStyleSheet m_nodeToInspectorStyleSheet;
181     DocumentToViaInspectorStyleSheet m_documentToInspectorStyleSheet;
182     NodeIdToForcedPseudoState m_nodeIdToForcedPseudoState;
183     HashSet<int> m_namedFlowCollectionsRequested;
184     OwnPtr<UpdateRegionLayoutTask> m_updateRegionLayoutTask;
185     OwnPtr<ChangeRegionOversetTask> m_changeRegionOversetTask;
186
187     int m_lastStyleSheetId;
188 };
189
190 #endif
191
192 } // namespace WebCore
193
194 #endif // !defined(InspectorCSSAgent_h)