InjectedBundleHitTestResult.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2010 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 "InjectedBundleHitTestResult.h"
  27. #include "InjectedBundleNodeHandle.h"
  28. #include "WebFrame.h"
  29. #include "WebFrameLoaderClient.h"
  30. #include <WebCore/Document.h>
  31. #include <WebCore/Element.h>
  32. #include <WebCore/Frame.h>
  33. #include <WebCore/FrameLoader.h>
  34. #include <WebCore/FrameView.h>
  35. #include <WebCore/KURL.h>
  36. #include <wtf/text/WTFString.h>
  37. using namespace WebCore;
  38. namespace WebKit {
  39. PassRefPtr<InjectedBundleHitTestResult> InjectedBundleHitTestResult::create(const WebCore::HitTestResult& hitTestResult)
  40. {
  41. return adoptRef(new InjectedBundleHitTestResult(hitTestResult));
  42. }
  43. PassRefPtr<InjectedBundleNodeHandle> InjectedBundleHitTestResult::nodeHandle() const
  44. {
  45. return InjectedBundleNodeHandle::getOrCreate(m_hitTestResult.innerNonSharedNode());
  46. }
  47. WebFrame* InjectedBundleHitTestResult::frame() const
  48. {
  49. Node* node = m_hitTestResult.innerNonSharedNode();
  50. if (!node)
  51. return 0;
  52. Document* document = node->document();
  53. if (!document)
  54. return 0;
  55. Frame* frame = document->frame();
  56. if (!frame)
  57. return 0;
  58. WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(frame->loader()->client());
  59. return webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
  60. }
  61. WebFrame* InjectedBundleHitTestResult::targetFrame() const
  62. {
  63. Frame* frame = m_hitTestResult.targetFrame();
  64. if (!frame)
  65. return 0;
  66. WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(frame->loader()->client());
  67. return webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
  68. }
  69. String InjectedBundleHitTestResult::absoluteImageURL() const
  70. {
  71. return m_hitTestResult.absoluteImageURL().string();
  72. }
  73. String InjectedBundleHitTestResult::absolutePDFURL() const
  74. {
  75. return m_hitTestResult.absolutePDFURL().string();
  76. }
  77. String InjectedBundleHitTestResult::absoluteLinkURL() const
  78. {
  79. return m_hitTestResult.absoluteLinkURL().string();
  80. }
  81. String InjectedBundleHitTestResult::absoluteMediaURL() const
  82. {
  83. return m_hitTestResult.absoluteMediaURL().string();
  84. }
  85. bool InjectedBundleHitTestResult::mediaIsInFullscreen() const
  86. {
  87. return m_hitTestResult.mediaIsInFullscreen();
  88. }
  89. bool InjectedBundleHitTestResult::mediaHasAudio() const
  90. {
  91. return m_hitTestResult.mediaHasAudio();
  92. }
  93. BundleHitTestResultMediaType InjectedBundleHitTestResult::mediaType() const
  94. {
  95. #if !ENABLE(VIDEO)
  96. return BundleHitTestResultMediaTypeNone;
  97. #else
  98. WebCore::Node* node = m_hitTestResult.innerNonSharedNode();
  99. if (!node->isElementNode())
  100. return BundleHitTestResultMediaTypeNone;
  101. if (!toElement(node)->isMediaElement())
  102. return BundleHitTestResultMediaTypeNone;
  103. return m_hitTestResult.mediaIsVideo() ? BundleHitTestResultMediaTypeVideo : BundleHitTestResultMediaTypeAudio;
  104. #endif
  105. }
  106. String InjectedBundleHitTestResult::linkLabel() const
  107. {
  108. return m_hitTestResult.textContent();
  109. }
  110. String InjectedBundleHitTestResult::linkTitle() const
  111. {
  112. return m_hitTestResult.titleDisplayString();
  113. }
  114. WebCore::IntRect InjectedBundleHitTestResult::imageRect() const
  115. {
  116. WebCore::IntRect imageRect = m_hitTestResult.imageRect();
  117. if (imageRect.isEmpty())
  118. return imageRect;
  119. // The image rect in WebCore::HitTestResult is in frame coordinates, but we need it in WKView
  120. // coordinates since WebKit2 clients don't have enough context to do the conversion themselves.
  121. WebFrame* webFrame = frame();
  122. if (!webFrame)
  123. return imageRect;
  124. WebCore::Frame* coreFrame = webFrame->coreFrame();
  125. if (!coreFrame)
  126. return imageRect;
  127. WebCore::FrameView* view = coreFrame->view();
  128. if (!view)
  129. return imageRect;
  130. return view->contentsToRootView(imageRect);
  131. }
  132. bool InjectedBundleHitTestResult::isSelected() const
  133. {
  134. return m_hitTestResult.isSelected();
  135. }
  136. } // WebKit