EventDispatcher.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2011 Apple 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''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "EventDispatcher.h"
  27. #include "EventDispatcherMessages.h"
  28. #include "WebEvent.h"
  29. #include "WebEventConversion.h"
  30. #include "WebPage.h"
  31. #include "WebPageProxyMessages.h"
  32. #include "WebProcess.h"
  33. #include <WebCore/Page.h>
  34. #include <WebCore/RunLoop.h>
  35. #include <wtf/MainThread.h>
  36. #if ENABLE(THREADED_SCROLLING)
  37. #include <WebCore/ScrollingCoordinator.h>
  38. #include <WebCore/ScrollingThread.h>
  39. #include <WebCore/ScrollingTree.h>
  40. #endif
  41. using namespace WebCore;
  42. namespace WebKit {
  43. PassRefPtr<EventDispatcher> EventDispatcher::create()
  44. {
  45. return adoptRef(new EventDispatcher);
  46. }
  47. EventDispatcher::EventDispatcher()
  48. : m_queue(WorkQueue::create("com.apple.WebKit.EventDispatcher"))
  49. {
  50. }
  51. EventDispatcher::~EventDispatcher()
  52. {
  53. }
  54. #if ENABLE(THREADED_SCROLLING)
  55. void EventDispatcher::addScrollingTreeForPage(WebPage* webPage)
  56. {
  57. MutexLocker locker(m_scrollingTreesMutex);
  58. ASSERT(webPage->corePage()->scrollingCoordinator());
  59. ASSERT(!m_scrollingTrees.contains(webPage->pageID()));
  60. m_scrollingTrees.set(webPage->pageID(), webPage->corePage()->scrollingCoordinator()->scrollingTree());
  61. }
  62. void EventDispatcher::removeScrollingTreeForPage(WebPage* webPage)
  63. {
  64. MutexLocker locker(m_scrollingTreesMutex);
  65. ASSERT(m_scrollingTrees.contains(webPage->pageID()));
  66. m_scrollingTrees.remove(webPage->pageID());
  67. }
  68. #endif
  69. void EventDispatcher::initializeConnection(CoreIPC::Connection* connection)
  70. {
  71. connection->addWorkQueueMessageReceiver(Messages::EventDispatcher::messageReceiverName(), m_queue.get(), this);
  72. }
  73. void EventDispatcher::wheelEvent(uint64_t pageID, const WebWheelEvent& wheelEvent, bool canGoBack, bool canGoForward)
  74. {
  75. #if ENABLE(THREADED_SCROLLING)
  76. MutexLocker locker(m_scrollingTreesMutex);
  77. if (ScrollingTree* scrollingTree = m_scrollingTrees.get(pageID)) {
  78. PlatformWheelEvent platformWheelEvent = platform(wheelEvent);
  79. // FIXME: It's pretty horrible that we're updating the back/forward state here.
  80. // WebCore should always know the current state and know when it changes so the
  81. // scrolling tree can be notified.
  82. // We only need to do this at the beginning of the gesture.
  83. if (platformWheelEvent.phase() == PlatformWheelEventPhaseBegan)
  84. ScrollingThread::dispatch(bind(&ScrollingTree::updateBackForwardState, scrollingTree, canGoBack, canGoForward));
  85. ScrollingTree::EventResult result = scrollingTree->tryToHandleWheelEvent(platformWheelEvent);
  86. if (result == ScrollingTree::DidHandleEvent || result == ScrollingTree::DidNotHandleEvent) {
  87. sendDidReceiveEvent(pageID, wheelEvent, result == ScrollingTree::DidHandleEvent);
  88. return;
  89. }
  90. }
  91. #else
  92. UNUSED_PARAM(canGoBack);
  93. UNUSED_PARAM(canGoForward);
  94. #endif
  95. RunLoop::main()->dispatch(bind(&EventDispatcher::dispatchWheelEvent, this, pageID, wheelEvent));
  96. }
  97. #if ENABLE(GESTURE_EVENTS)
  98. void EventDispatcher::gestureEvent(uint64_t pageID, const WebGestureEvent& gestureEvent)
  99. {
  100. RunLoop::main()->dispatch(bind(&EventDispatcher::dispatchGestureEvent, this, pageID, gestureEvent));
  101. }
  102. #endif
  103. void EventDispatcher::dispatchWheelEvent(uint64_t pageID, const WebWheelEvent& wheelEvent)
  104. {
  105. ASSERT(isMainThread());
  106. WebPage* webPage = WebProcess::shared().webPage(pageID);
  107. if (!webPage)
  108. return;
  109. webPage->wheelEvent(wheelEvent);
  110. }
  111. #if ENABLE(GESTURE_EVENTS)
  112. void EventDispatcher::dispatchGestureEvent(uint64_t pageID, const WebGestureEvent& gestureEvent)
  113. {
  114. ASSERT(isMainThread());
  115. WebPage* webPage = WebProcess::shared().webPage(pageID);
  116. if (!webPage)
  117. return;
  118. webPage->gestureEvent(gestureEvent);
  119. }
  120. #endif
  121. #if ENABLE(THREADED_SCROLLING)
  122. void EventDispatcher::sendDidReceiveEvent(uint64_t pageID, const WebEvent& event, bool didHandleEvent)
  123. {
  124. WebProcess::shared().parentProcessConnection()->send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(event.type()), didHandleEvent), pageID);
  125. }
  126. #endif
  127. } // namespace WebKit