InspectorCSSAgent.h 9.9 KB

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