MediaQueryMatcher.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #ifndef MediaQueryMatcher_h
  20. #define MediaQueryMatcher_h
  21. #include "ScriptState.h"
  22. #include <wtf/Forward.h>
  23. #include <wtf/RefCounted.h>
  24. #include <wtf/Vector.h>
  25. namespace WebCore {
  26. class Document;
  27. class MediaQueryList;
  28. class MediaQueryListListener;
  29. class MediaQueryEvaluator;
  30. class MediaQuerySet;
  31. // MediaQueryMatcher class is responsible for keeping a vector of pairs
  32. // MediaQueryList x MediaQueryListListener. It is responsible for evaluating the queries
  33. // whenever it is needed and to call the listeners if the corresponding query has changed.
  34. // The listeners must be called in the very same order in which they have been added.
  35. class MediaQueryMatcher : public RefCounted<MediaQueryMatcher> {
  36. public:
  37. static PassRefPtr<MediaQueryMatcher> create(Document* document) { return adoptRef(new MediaQueryMatcher(document)); }
  38. ~MediaQueryMatcher();
  39. void documentDestroyed();
  40. void addListener(PassRefPtr<MediaQueryListListener>, PassRefPtr<MediaQueryList>);
  41. void removeListener(MediaQueryListListener*, MediaQueryList*);
  42. PassRefPtr<MediaQueryList> matchMedia(const String&);
  43. unsigned evaluationRound() const { return m_evaluationRound; }
  44. void styleResolverChanged();
  45. bool evaluate(const MediaQuerySet*);
  46. private:
  47. class Listener {
  48. public:
  49. Listener(PassRefPtr<MediaQueryListListener>, PassRefPtr<MediaQueryList>);
  50. ~Listener();
  51. void evaluate(ScriptState*, MediaQueryEvaluator*);
  52. MediaQueryListListener* listener() { return m_listener.get(); }
  53. MediaQueryList* query() { return m_query.get(); }
  54. private:
  55. RefPtr<MediaQueryListListener> m_listener;
  56. RefPtr<MediaQueryList> m_query;
  57. };
  58. MediaQueryMatcher(Document*);
  59. PassOwnPtr<MediaQueryEvaluator> prepareEvaluator() const;
  60. String mediaType() const;
  61. Document* m_document;
  62. Vector<OwnPtr<Listener> > m_listeners;
  63. // This value is incremented at style selector changes.
  64. // It is used to avoid evaluating queries more then once and to make sure
  65. // that a media query result change is notified exactly once.
  66. unsigned m_evaluationRound;
  67. };
  68. }
  69. #endif // MediaQueryMatcher_h