MediaQueryMatcher.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "config.h"
  20. #include "MediaQueryMatcher.h"
  21. #include "Document.h"
  22. #include "Element.h"
  23. #include "Frame.h"
  24. #include "FrameView.h"
  25. #include "MediaList.h"
  26. #include "MediaQueryEvaluator.h"
  27. #include "MediaQueryList.h"
  28. #include "MediaQueryListListener.h"
  29. #include "StyleResolver.h"
  30. namespace WebCore {
  31. MediaQueryMatcher::Listener::Listener(PassRefPtr<MediaQueryListListener> listener, PassRefPtr<MediaQueryList> query)
  32. : m_listener(listener)
  33. , m_query(query)
  34. {
  35. }
  36. MediaQueryMatcher::Listener::~Listener()
  37. {
  38. }
  39. void MediaQueryMatcher::Listener::evaluate(ScriptState* state, MediaQueryEvaluator* evaluator)
  40. {
  41. bool notify;
  42. m_query->evaluate(evaluator, notify);
  43. if (notify)
  44. m_listener->queryChanged(state, m_query.get());
  45. }
  46. MediaQueryMatcher::MediaQueryMatcher(Document* document)
  47. : m_document(document)
  48. , m_evaluationRound(1)
  49. {
  50. ASSERT(m_document);
  51. }
  52. MediaQueryMatcher::~MediaQueryMatcher()
  53. {
  54. }
  55. void MediaQueryMatcher::documentDestroyed()
  56. {
  57. m_listeners.clear();
  58. m_document = 0;
  59. }
  60. String MediaQueryMatcher::mediaType() const
  61. {
  62. if (!m_document || !m_document->frame() || !m_document->frame()->view())
  63. return String();
  64. return m_document->frame()->view()->mediaType();
  65. }
  66. PassOwnPtr<MediaQueryEvaluator> MediaQueryMatcher::prepareEvaluator() const
  67. {
  68. if (!m_document || !m_document->frame())
  69. return nullptr;
  70. Element* documentElement = m_document->documentElement();
  71. if (!documentElement)
  72. return nullptr;
  73. StyleResolver* styleResolver = m_document->ensureStyleResolver();
  74. RefPtr<RenderStyle> rootStyle = styleResolver->styleForElement(documentElement, 0 /*defaultParent*/, DisallowStyleSharing, MatchOnlyUserAgentRules);
  75. return adoptPtr(new MediaQueryEvaluator(mediaType(), m_document->frame(), rootStyle.get()));
  76. }
  77. bool MediaQueryMatcher::evaluate(const MediaQuerySet* media)
  78. {
  79. if (!media)
  80. return false;
  81. OwnPtr<MediaQueryEvaluator> evaluator(prepareEvaluator());
  82. return evaluator && evaluator->eval(media);
  83. }
  84. PassRefPtr<MediaQueryList> MediaQueryMatcher::matchMedia(const String& query)
  85. {
  86. if (!m_document)
  87. return 0;
  88. RefPtr<MediaQuerySet> media = MediaQuerySet::create(query);
  89. #if ENABLE(RESOLUTION_MEDIA_QUERY)
  90. // Add warning message to inspector whenever dpi/dpcm values are used for "screen" media.
  91. reportMediaQueryWarningIfNeeded(m_document, media.get());
  92. #endif
  93. return MediaQueryList::create(this, media, evaluate(media.get()));
  94. }
  95. void MediaQueryMatcher::addListener(PassRefPtr<MediaQueryListListener> listener, PassRefPtr<MediaQueryList> query)
  96. {
  97. if (!m_document)
  98. return;
  99. for (size_t i = 0; i < m_listeners.size(); ++i) {
  100. if (*m_listeners[i]->listener() == *listener && m_listeners[i]->query() == query)
  101. return;
  102. }
  103. m_listeners.append(adoptPtr(new Listener(listener, query)));
  104. }
  105. void MediaQueryMatcher::removeListener(MediaQueryListListener* listener, MediaQueryList* query)
  106. {
  107. if (!m_document)
  108. return;
  109. for (size_t i = 0; i < m_listeners.size(); ++i) {
  110. if (*m_listeners[i]->listener() == *listener && m_listeners[i]->query() == query) {
  111. m_listeners.remove(i);
  112. return;
  113. }
  114. }
  115. }
  116. void MediaQueryMatcher::styleResolverChanged()
  117. {
  118. ASSERT(m_document);
  119. ScriptState* scriptState = mainWorldScriptState(m_document->frame());
  120. if (!scriptState)
  121. return;
  122. ++m_evaluationRound;
  123. OwnPtr<MediaQueryEvaluator> evaluator = prepareEvaluator();
  124. if (!evaluator)
  125. return;
  126. for (size_t i = 0; i < m_listeners.size(); ++i)
  127. m_listeners[i]->evaluate(scriptState, evaluator.get());
  128. }
  129. }