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 "InspectorBaseAgent.h"
31#include "InspectorDOMAgent.h"
32#include "InspectorStyleSheet.h"
33#include "InspectorValues.h"
34#include "SecurityContext.h"
35
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
42namespace WebCore {
43
44class CSSRule;
45class CSSRuleList;
46class CSSStyleDeclaration;
47class CSSStyleRule;
48class CSSStyleSheet;
49class Document;
50class DocumentStyleSheetCollection;
51class Element;
52class InspectorCSSOMWrappers;
53class InspectorFrontend;
54class InstrumentingAgents;
55class NameNodeMap;
56class Node;
57class NodeList;
58class SelectorProfile;
59class StyleResolver;
60class StyleRule;
61class UpdateRegionLayoutTask;
62
63#if ENABLE(INSPECTOR)
64
65class InspectorCSSAgent
66    : public InspectorBaseAgent<InspectorCSSAgent>
67    , public InspectorDOMAgent::DOMListener
68    , public InspectorBackendDispatcher::CSSCommandHandler
69    , public InspectorStyleSheet::Listener {
70    WTF_MAKE_NONCOPYABLE(InspectorCSSAgent);
71public:
72    class InlineStyleOverrideScope {
73    public:
74        InlineStyleOverrideScope(SecurityContext* context)
75            : m_contentSecurityPolicy(context->contentSecurityPolicy())
76        {
77            m_contentSecurityPolicy->setOverrideAllowInlineStyle(true);
78        }
79
80        ~InlineStyleOverrideScope()
81        {
82            m_contentSecurityPolicy->setOverrideAllowInlineStyle(false);
83        }
84
85    private:
86        ContentSecurityPolicy* m_contentSecurityPolicy;
87    };
88
89    static CSSStyleRule* asCSSStyleRule(CSSRule*);
90
91    static PassOwnPtr<InspectorCSSAgent> create(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InspectorDOMAgent* domAgent)
92    {
93        return adoptPtr(new InspectorCSSAgent(instrumentingAgents, state, domAgent));
94    }
95    ~InspectorCSSAgent();
96
97    bool forcePseudoState(Element*, CSSSelector::PseudoType);
98    virtual void setFrontend(InspectorFrontend*);
99    virtual void clearFrontend();
100    virtual void discardAgent();
101    virtual void restore();
102    virtual void enable(ErrorString*);
103    virtual void disable(ErrorString*);
104    void reset();
105    void mediaQueryResultChanged();
106    void didCreateNamedFlow(Document*, WebKitNamedFlow*);
107    void willRemoveNamedFlow(Document*, WebKitNamedFlow*);
108    void didUpdateRegionLayout(Document*, WebKitNamedFlow*);
109    void regionLayoutUpdated(WebKitNamedFlow*, int documentNodeId);
110
111    virtual void getComputedStyleForNode(ErrorString*, int nodeId, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSComputedStyleProperty> >&);
112    virtual void getInlineStylesForNode(ErrorString*, int nodeId, RefPtr<TypeBuilder::CSS::CSSStyle>& inlineStyle, RefPtr<TypeBuilder::CSS::CSSStyle>& attributes);
113    virtual void getMatchedStylesForNode(ErrorString*, int nodeId, const bool* includePseudo, const bool* includeInherited, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::RuleMatch> >& matchedCSSRules, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::PseudoIdMatches> >& pseudoIdMatches, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::InheritedStyleEntry> >& inheritedEntries);
114    virtual void getAllStyleSheets(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSStyleSheetHeader> >& styleSheetInfos);
115    virtual void getStyleSheet(ErrorString*, const String& styleSheetId, RefPtr<TypeBuilder::CSS::CSSStyleSheetBody>& result);
116    virtual void getStyleSheetText(ErrorString*, const String& styleSheetId, String* result);
117    virtual void setStyleSheetText(ErrorString*, const String& styleSheetId, const String& text);
118    virtual void setStyleText(ErrorString*, const RefPtr<InspectorObject>& styleId, const String& text, RefPtr<TypeBuilder::CSS::CSSStyle>& result);
119    virtual void setPropertyText(ErrorString*, const RefPtr<InspectorObject>& styleId, int propertyIndex, const String& text, bool overwrite, RefPtr<TypeBuilder::CSS::CSSStyle>& result);
120    virtual void toggleProperty(ErrorString*, const RefPtr<InspectorObject>& styleId, int propertyIndex, bool disable, RefPtr<TypeBuilder::CSS::CSSStyle>& result);
121    virtual void setRuleSelector(ErrorString*, const RefPtr<InspectorObject>& ruleId, const String& selector, RefPtr<TypeBuilder::CSS::CSSRule>& result);
122    virtual void addRule(ErrorString*, int contextNodeId, const String& selector, RefPtr<TypeBuilder::CSS::CSSRule>& result);
123    virtual void getSupportedCSSProperties(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSPropertyInfo> >& result);
124    virtual void forcePseudoState(ErrorString*, int nodeId, const RefPtr<InspectorArray>& forcedPseudoClasses);
125    virtual void getNamedFlowCollection(ErrorString*, int documentNodeId, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::NamedFlow> >& result);
126
127    virtual void startSelectorProfiler(ErrorString*);
128    virtual void stopSelectorProfiler(ErrorString*, RefPtr<TypeBuilder::CSS::SelectorProfile>&);
129
130    PassRefPtr<TypeBuilder::CSS::SelectorProfile> stopSelectorProfilerImpl(ErrorString*, bool needProfile);
131    void willMatchRule(StyleRule*, InspectorCSSOMWrappers&, DocumentStyleSheetCollection*);
132    void didMatchRule(bool);
133    void willProcessRule(StyleRule*, StyleResolver*);
134    void didProcessRule();
135
136private:
137    class StyleSheetAction;
138    class SetStyleSheetTextAction;
139    class SetStyleTextAction;
140    class SetPropertyTextAction;
141    class TogglePropertyAction;
142    class SetRuleSelectorAction;
143    class AddRuleAction;
144
145    InspectorCSSAgent(InstrumentingAgents*, InspectorCompositeState*, InspectorDOMAgent*);
146
147    typedef HashMap<String, RefPtr<InspectorStyleSheet> > IdToInspectorStyleSheet;
148    typedef HashMap<CSSStyleSheet*, RefPtr<InspectorStyleSheet> > CSSStyleSheetToInspectorStyleSheet;
149    typedef HashMap<Node*, RefPtr<InspectorStyleSheetForInlineStyle> > NodeToInspectorStyleSheet; // bogus "stylesheets" with elements' inline styles
150    typedef HashMap<RefPtr<Document>, RefPtr<InspectorStyleSheet> > DocumentToViaInspectorStyleSheet; // "via inspector" stylesheets
151    typedef HashMap<int, unsigned> NodeIdToForcedPseudoState;
152
153    void resetNonPersistentData();
154    InspectorStyleSheetForInlineStyle* asInspectorStyleSheet(Element* element);
155    Element* elementForId(ErrorString*, int nodeId);
156    int documentNodeWithRequestedFlowsId(Document*);
157    void collectStyleSheets(CSSStyleSheet*, TypeBuilder::Array<WebCore::TypeBuilder::CSS::CSSStyleSheetHeader>*);
158
159    InspectorStyleSheet* bindStyleSheet(CSSStyleSheet*);
160    InspectorStyleSheet* viaInspectorStyleSheet(Document*, bool createIfAbsent);
161    InspectorStyleSheet* assertStyleSheetForId(ErrorString*, const String&);
162    TypeBuilder::CSS::StyleSheetOrigin::Enum detectOrigin(CSSStyleSheet* pageStyleSheet, Document* ownerDocument);
163
164    PassRefPtr<TypeBuilder::CSS::CSSRule> buildObjectForRule(StyleRule*, StyleResolver*);
165    PassRefPtr<TypeBuilder::CSS::CSSRule> buildObjectForRule(CSSStyleRule*);
166    PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSRule> > buildArrayForRuleList(CSSRuleList*);
167    PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::RuleMatch> > buildArrayForMatchedRuleList(const Vector<RefPtr<StyleRuleBase> >&, StyleResolver*, Element*);
168    PassRefPtr<TypeBuilder::CSS::CSSStyle> buildObjectForAttributesStyle(Element*);
169    PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::Region> > buildArrayForRegions(ErrorString*, PassRefPtr<NodeList>, int documentNodeId);
170    PassRefPtr<TypeBuilder::CSS::NamedFlow> buildObjectForNamedFlow(ErrorString*, WebKitNamedFlow*, int documentNodeId);
171
172    // InspectorDOMAgent::DOMListener implementation
173    virtual void didRemoveDocument(Document*);
174    virtual void didRemoveDOMNode(Node*);
175    virtual void didModifyDOMAttr(Element*);
176
177    // InspectorCSSAgent::Listener implementation
178    virtual void styleSheetChanged(InspectorStyleSheet*);
179
180    void resetPseudoStates();
181
182    InspectorFrontend::CSS* m_frontend;
183    InspectorDOMAgent* m_domAgent;
184
185    IdToInspectorStyleSheet m_idToInspectorStyleSheet;
186    CSSStyleSheetToInspectorStyleSheet m_cssStyleSheetToInspectorStyleSheet;
187    NodeToInspectorStyleSheet m_nodeToInspectorStyleSheet;
188    DocumentToViaInspectorStyleSheet m_documentToInspectorStyleSheet;
189    NodeIdToForcedPseudoState m_nodeIdToForcedPseudoState;
190    HashSet<int> m_namedFlowCollectionsRequested;
191    OwnPtr<UpdateRegionLayoutTask> m_updateRegionLayoutTask;
192
193    int m_lastStyleSheetId;
194
195    OwnPtr<SelectorProfile> m_currentSelectorProfile;
196};
197
198#endif
199
200} // namespace WebCore
201
202#endif // !defined(InspectorCSSAgent_h)
203