Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/css/CSSSelector.h"
29 #include "core/dom/SecurityContext.h"
30 #include "core/frame/csp/ContentSecurityPolicy.h"
31 #include "core/inspector/InspectorBaseAgent.h"
32 #include "core/inspector/InspectorDOMAgent.h"
33 #include "core/inspector/InspectorStyleSheet.h"
34 #include "platform/JSONValues.h"
35 #include "wtf/HashCountedSet.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/HashSet.h"
38 #include "wtf/PassRefPtr.h"
39 #include "wtf/RefPtr.h"
40 #include "wtf/Vector.h"
41 #include "wtf/text/WTFString.h"
42
43 namespace WebCore {
44
45 struct CSSParserString;
46 class CSSRule;
47 class CSSRuleList;
48 class CSSStyleRule;
49 class CSSStyleSheet;
50 class Document;
51 class Element;
52 class InspectorFrontend;
53 class InspectorResourceAgent;
54 class InstrumentingAgents;
55 class MediaList;
56 class Node;
57 class NodeList;
58 class PlatformFontUsage;
59 class RenderText;
60 class StyleResolver;
61
62 class InspectorCSSAgent FINAL
63     : public InspectorBaseAgent<InspectorCSSAgent>
64     , public InspectorDOMAgent::DOMListener
65     , public InspectorBackendDispatcher::CSSCommandHandler
66     , public InspectorStyleSheetBase::Listener {
67     WTF_MAKE_NONCOPYABLE(InspectorCSSAgent);
68 public:
69     enum MediaListSource {
70         MediaListSourceLinkedSheet,
71         MediaListSourceInlineSheet,
72         MediaListSourceMediaRule,
73         MediaListSourceImportRule
74     };
75
76     enum StyleSheetsUpdateType {
77         InitialFrontendLoad = 0,
78         ExistingFrontendRefresh,
79     };
80
81     class InlineStyleOverrideScope {
82     public:
83         InlineStyleOverrideScope(SecurityContext* context)
84             : m_contentSecurityPolicy(context->contentSecurityPolicy())
85         {
86             m_contentSecurityPolicy->setOverrideAllowInlineStyle(true);
87         }
88
89         ~InlineStyleOverrideScope()
90         {
91             m_contentSecurityPolicy->setOverrideAllowInlineStyle(false);
92         }
93
94     private:
95         ContentSecurityPolicy* m_contentSecurityPolicy;
96     };
97
98     static CSSStyleRule* asCSSStyleRule(CSSRule*);
99
100     static PassOwnPtr<InspectorCSSAgent> create(InspectorDOMAgent* domAgent, InspectorPageAgent* pageAgent, InspectorResourceAgent* resourceAgent)
101     {
102         return adoptPtr(new InspectorCSSAgent(domAgent, pageAgent, resourceAgent));
103     }
104     virtual ~InspectorCSSAgent();
105
106     bool forcePseudoState(Element*, CSSSelector::PseudoType);
107     virtual void setFrontend(InspectorFrontend*) OVERRIDE;
108     virtual void clearFrontend() OVERRIDE;
109     virtual void discardAgent() OVERRIDE;
110     virtual void didCommitLoadForMainFrame() OVERRIDE;
111     virtual void restore() OVERRIDE;
112     virtual void flushPendingFrontendMessages() OVERRIDE;
113     virtual void enable(ErrorString*, PassRefPtr<EnableCallback>) OVERRIDE;
114     virtual void disable(ErrorString*) OVERRIDE;
115     void reset();
116     void mediaQueryResultChanged();
117     void willMutateRules();
118     void didMutateRules(CSSStyleSheet*);
119     void willMutateStyle();
120     void didMutateStyle(CSSStyleDeclaration*, bool);
121
122     void activeStyleSheetsUpdated(Document*);
123     void documentDetached(Document*);
124
125     virtual void getComputedStyleForNode(ErrorString*, int nodeId, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSComputedStyleProperty> >&) OVERRIDE;
126     virtual void getPlatformFontsForNode(ErrorString*, int nodeId, String* cssFamilyName, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::PlatformFontUsage> >&) OVERRIDE;
127     virtual void getInlineStylesForNode(ErrorString*, int nodeId, RefPtr<TypeBuilder::CSS::CSSStyle>& inlineStyle, RefPtr<TypeBuilder::CSS::CSSStyle>& attributes) OVERRIDE;
128     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> >&, RefPtr<TypeBuilder::Array<TypeBuilder::CSS::InheritedStyleEntry> >& inheritedEntries) OVERRIDE;
129     virtual void getStyleSheetText(ErrorString*, const String& styleSheetId, String* result) OVERRIDE;
130     virtual void setStyleSheetText(ErrorString*, const String& styleSheetId, const String& text) OVERRIDE;
131
132     virtual void setPropertyText(ErrorString*, const String& styleSheetId, const RefPtr<JSONObject>& range, const String& text, RefPtr<TypeBuilder::CSS::CSSStyle>& result) OVERRIDE;
133     virtual void setRuleSelector(ErrorString*, const String& styleSheetId, const RefPtr<JSONObject>& range, const String& selector, RefPtr<TypeBuilder::CSS::CSSRule>& result) OVERRIDE;
134     virtual void createStyleSheet(ErrorString*, const String& frameId, TypeBuilder::CSS::StyleSheetId* outStyleSheetId) OVERRIDE;
135     virtual void addRule(ErrorString*, const String& styleSheetId, const String& selector, RefPtr<TypeBuilder::CSS::CSSRule>& result) OVERRIDE;
136     virtual void forcePseudoState(ErrorString*, int nodeId, const RefPtr<JSONArray>& forcedPseudoClasses) OVERRIDE;
137
138     PassRefPtr<TypeBuilder::CSS::CSSMedia> buildMediaObject(const MediaList*, MediaListSource, const String&, CSSStyleSheet*);
139     PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::CSSMedia> > buildMediaListChain(CSSRule*);
140
141 private:
142     class StyleSheetAction;
143     class SetStyleSheetTextAction;
144     class SetPropertyTextAction;
145     class SetRuleSelectorAction;
146     class AddRuleAction;
147     class EnableResourceClient;
148
149     InspectorCSSAgent(InspectorDOMAgent*, InspectorPageAgent*, InspectorResourceAgent*);
150
151     typedef HashMap<String, RefPtr<InspectorStyleSheet> > IdToInspectorStyleSheet;
152     typedef HashMap<String, RefPtr<InspectorStyleSheetForInlineStyle> > IdToInspectorStyleSheetForInlineStyle;
153     typedef HashMap<Node*, RefPtr<InspectorStyleSheetForInlineStyle> > NodeToInspectorStyleSheet; // bogus "stylesheets" with elements' inline styles
154     typedef HashMap<int, unsigned> NodeIdToForcedPseudoState;
155
156     void wasEnabled(PassRefPtr<EnableCallback>);
157     void resetNonPersistentData();
158     InspectorStyleSheetForInlineStyle* asInspectorStyleSheet(Element* element);
159     Element* elementForId(ErrorString*, int nodeId);
160     void collectAllStyleSheets(Vector<InspectorStyleSheet*>&);
161     void collectAllDocumentStyleSheets(Document*, Vector<CSSStyleSheet*>&);
162     void collectStyleSheets(CSSStyleSheet*, Vector<CSSStyleSheet*>&);
163
164     void updateActiveStyleSheets(Document*, StyleSheetsUpdateType);
165     void setActiveStyleSheets(Document*, const Vector<CSSStyleSheet*>&, StyleSheetsUpdateType);
166
167     void collectPlatformFontsForRenderer(RenderText*, HashCountedSet<String>*);
168
169     InspectorStyleSheet* bindStyleSheet(CSSStyleSheet*);
170     String unbindStyleSheet(InspectorStyleSheet*);
171     InspectorStyleSheet* viaInspectorStyleSheet(Document*, bool createIfAbsent);
172     InspectorStyleSheet* assertInspectorStyleSheetForId(ErrorString*, const String&);
173     InspectorStyleSheetBase* assertStyleSheetForId(ErrorString*, const String&);
174     TypeBuilder::CSS::StyleSheetOrigin::Enum detectOrigin(CSSStyleSheet* pageStyleSheet, Document* ownerDocument);
175     bool styleSheetEditInProgress() const { return m_styleSheetsPendingMutation || m_styleDeclarationPendingMutation || m_isSettingStyleSheetText; }
176
177     PassRefPtr<TypeBuilder::CSS::CSSRule> buildObjectForRule(CSSStyleRule*);
178     PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::RuleMatch> > buildArrayForMatchedRuleList(CSSRuleList*, Element*);
179     PassRefPtr<TypeBuilder::CSS::CSSStyle> buildObjectForAttributesStyle(Element*);
180
181     // InspectorDOMAgent::DOMListener implementation
182     virtual void didRemoveDocument(Document*) OVERRIDE;
183     virtual void didRemoveDOMNode(Node*) OVERRIDE;
184     virtual void didModifyDOMAttr(Element*) OVERRIDE;
185
186     // InspectorStyleSheet::Listener implementation
187     virtual void styleSheetChanged(InspectorStyleSheetBase*) OVERRIDE;
188     virtual void willReparseStyleSheet() OVERRIDE;
189     virtual void didReparseStyleSheet() OVERRIDE;
190
191     void resetPseudoStates();
192
193     InspectorFrontend::CSS* m_frontend;
194     InspectorDOMAgent* m_domAgent;
195     InspectorPageAgent* m_pageAgent;
196     InspectorResourceAgent* m_resourceAgent;
197
198     IdToInspectorStyleSheet m_idToInspectorStyleSheet;
199     IdToInspectorStyleSheetForInlineStyle m_idToInspectorStyleSheetForInlineStyle;
200     HashMap<CSSStyleSheet*, RefPtr<InspectorStyleSheet> > m_cssStyleSheetToInspectorStyleSheet;
201     typedef HashMap<Document*, OwnPtr<HashSet<CSSStyleSheet*> > > DocumentStyleSheets;
202     DocumentStyleSheets m_documentToCSSStyleSheets;
203     HashSet<Document*> m_invalidatedDocuments;
204
205     NodeToInspectorStyleSheet m_nodeToInspectorStyleSheet;
206     HashMap<RefPtr<Document>, RefPtr<InspectorStyleSheet> > m_documentToViaInspectorStyleSheet; // "via inspector" stylesheets
207     NodeIdToForcedPseudoState m_nodeIdToForcedPseudoState;
208
209     RefPtrWillBePersistent<CSSStyleSheet> m_inspectorUserAgentStyleSheet;
210
211     int m_lastStyleSheetId;
212     int m_styleSheetsPendingMutation;
213     bool m_styleDeclarationPendingMutation;
214     bool m_creatingViaInspectorStyleSheet;
215     bool m_isSettingStyleSheetText;
216
217     friend class EnableResourceClient;
218     friend class StyleSheetBinder;
219 };
220
221
222 } // namespace WebCore
223
224 #endif // !defined(InspectorCSSAgent_h)