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 "InspectorDOMAgent.h"
29 #include "InspectorStyleSheet.h"
30 #include "InspectorValues.h"
31 #include "PlatformString.h"
32 
33 #include <wtf/HashMap.h>
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefPtr.h>
36 
37 namespace WebCore {
38 
39 class CSSRule;
40 class CSSRuleList;
41 class CSSStyleDeclaration;
42 class CSSStyleRule;
43 class CSSStyleSheet;
44 class Document;
45 class Element;
46 class InspectorFrontend;
47 class InstrumentingAgents;
48 class NameNodeMap;
49 class Node;
50 class StyleBase;
51 
52 #if ENABLE(INSPECTOR)
53 
54 class InspectorCSSAgent : public InspectorDOMAgent::DOMListener {
55     WTF_MAKE_NONCOPYABLE(InspectorCSSAgent);
56 public:
57     static CSSStyleSheet* parentStyleSheet(StyleBase*);
58     static CSSStyleRule* asCSSStyleRule(StyleBase*);
59 
60     InspectorCSSAgent(InstrumentingAgents*, InspectorDOMAgent*);
61     ~InspectorCSSAgent();
62 
63     void reset();
64     void getStylesForNode(ErrorString*, int nodeId, RefPtr<InspectorObject>* result);
65     void getInlineStyleForNode(ErrorString*, int nodeId, RefPtr<InspectorObject>* style);
66     void getComputedStyleForNode(ErrorString*, int nodeId, RefPtr<InspectorObject>* style);
67     void getAllStyleSheets(ErrorString*, RefPtr<InspectorArray>* styleSheetInfos);
68     void getStyleSheet(ErrorString*, const String& styleSheetId, RefPtr<InspectorObject>* result);
69     void getStyleSheetText(ErrorString*, const String& styleSheetId, String* result);
70     void setStyleSheetText(ErrorString*, const String& styleSheetId, const String& text);
71     void setPropertyText(ErrorString*, const RefPtr<InspectorObject>& styleId, int propertyIndex, const String& text, bool overwrite, RefPtr<InspectorObject>* result);
72     void toggleProperty(ErrorString*, const RefPtr<InspectorObject>& styleId, int propertyIndex, bool disable, RefPtr<InspectorObject>* result);
73     void setRuleSelector(ErrorString*, const RefPtr<InspectorObject>& ruleId, const String& selector, RefPtr<InspectorObject>* result);
74     void addRule(ErrorString*, const int contextNodeId, const String& selector, RefPtr<InspectorObject>* result);
75     void getSupportedCSSProperties(ErrorString*, RefPtr<InspectorArray>* result);
76 
77 private:
78     typedef HashMap<String, RefPtr<InspectorStyleSheet> > IdToInspectorStyleSheet;
79     typedef HashMap<CSSStyleSheet*, RefPtr<InspectorStyleSheet> > CSSStyleSheetToInspectorStyleSheet;
80     typedef HashMap<Node*, RefPtr<InspectorStyleSheetForInlineStyle> > NodeToInspectorStyleSheet; // bogus "stylesheets" with elements' inline styles
81     typedef HashMap<RefPtr<Document>, RefPtr<InspectorStyleSheet> > DocumentToViaInspectorStyleSheet; // "via inspector" stylesheets
82 
83     static Element* inlineStyleElement(CSSStyleDeclaration*);
84 
85     InspectorStyleSheetForInlineStyle* asInspectorStyleSheet(Element* element);
86     Element* elementForId(ErrorString*, int nodeId);
87 
88     InspectorStyleSheet* bindStyleSheet(CSSStyleSheet*);
89     InspectorStyleSheet* viaInspectorStyleSheet(Document*, bool createIfAbsent);
90     InspectorStyleSheet* assertStyleSheetForId(ErrorString*, const String&);
91     String detectOrigin(CSSStyleSheet* pageStyleSheet, Document* ownerDocument);
92 
93     PassRefPtr<InspectorArray> buildArrayForRuleList(CSSRuleList* ruleList);
94     PassRefPtr<InspectorArray> buildArrayForAttributeStyles(Element*);
95 
96     // InspectorDOMAgent::DOMListener interface
97     virtual void didRemoveDocument(Document*);
98     virtual void didRemoveDOMNode(Node*);
99     virtual void didModifyDOMAttr(Element*);
100 
101     InstrumentingAgents* m_instrumentingAgents;
102     InspectorDOMAgent* m_domAgent;
103 
104     IdToInspectorStyleSheet m_idToInspectorStyleSheet;
105     CSSStyleSheetToInspectorStyleSheet m_cssStyleSheetToInspectorStyleSheet;
106     NodeToInspectorStyleSheet m_nodeToInspectorStyleSheet;
107     DocumentToViaInspectorStyleSheet m_documentToInspectorStyleSheet;
108 
109     int m_lastStyleSheetId;
110     int m_lastRuleId;
111     int m_lastStyleId;
112 };
113 
114 #endif
115 
116 } // namespace WebCore
117 
118 #endif // !defined(InspectorCSSAgent_h)
119