PluginWidgetProxy.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "PluginWidgetProxy.h"
  5. #include "mozilla/dom/TabChild.h"
  6. #include "mozilla/plugins/PluginWidgetChild.h"
  7. #include "mozilla/plugins/PluginInstanceParent.h"
  8. #include "nsDebug.h"
  9. #define PWLOG(...)
  10. // #define PWLOG(...) printf_stderr(__VA_ARGS__)
  11. /* static */
  12. already_AddRefed<nsIWidget>
  13. nsIWidget::CreatePluginProxyWidget(TabChild* aTabChild,
  14. mozilla::plugins::PluginWidgetChild* aActor)
  15. {
  16. nsCOMPtr<nsIWidget> widget =
  17. new mozilla::widget::PluginWidgetProxy(aTabChild, aActor);
  18. return widget.forget();
  19. }
  20. namespace mozilla {
  21. namespace widget {
  22. using mozilla::plugins::PluginInstanceParent;
  23. NS_IMPL_ISUPPORTS_INHERITED(PluginWidgetProxy, PuppetWidget, nsIWidget)
  24. #define ENSURE_CHANNEL do { \
  25. if (!mActor) { \
  26. NS_WARNING("called on an invalid channel."); \
  27. return NS_ERROR_FAILURE; \
  28. } \
  29. } while (0)
  30. PluginWidgetProxy::PluginWidgetProxy(dom::TabChild* aTabChild,
  31. mozilla::plugins::PluginWidgetChild* aActor) :
  32. PuppetWidget(aTabChild),
  33. mActor(aActor),
  34. mCachedPluginPort(0)
  35. {
  36. // See ChannelDestroyed() in the header
  37. mActor->SetWidget(this);
  38. }
  39. PluginWidgetProxy::~PluginWidgetProxy()
  40. {
  41. PWLOG("PluginWidgetProxy::~PluginWidgetProxy()\n");
  42. }
  43. nsresult
  44. PluginWidgetProxy::Create(nsIWidget* aParent,
  45. nsNativeWidget aNativeParent,
  46. const LayoutDeviceIntRect& aRect,
  47. nsWidgetInitData* aInitData)
  48. {
  49. ENSURE_CHANNEL;
  50. PWLOG("PluginWidgetProxy::Create()\n");
  51. nsresult rv = NS_ERROR_UNEXPECTED;
  52. uint64_t scrollCaptureId;
  53. uintptr_t pluginInstanceId;
  54. mActor->SendCreate(&rv, &scrollCaptureId, &pluginInstanceId);
  55. if (NS_FAILED(rv)) {
  56. NS_WARNING("failed to create chrome widget, plugins won't paint.");
  57. return rv;
  58. }
  59. BaseCreate(aParent, aInitData);
  60. mParent = aParent;
  61. mBounds = aRect;
  62. mEnabled = true;
  63. mVisible = true;
  64. #if defined(XP_WIN)
  65. PluginInstanceParent* instance =
  66. PluginInstanceParent::LookupPluginInstanceByID(pluginInstanceId);
  67. if (instance) {
  68. Unused << NS_WARN_IF(NS_FAILED(instance->SetScrollCaptureId(scrollCaptureId)));
  69. }
  70. #endif
  71. return NS_OK;
  72. }
  73. NS_IMETHODIMP
  74. PluginWidgetProxy::SetParent(nsIWidget* aNewParent)
  75. {
  76. nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
  77. nsIWidget* parent = GetParent();
  78. if (parent) {
  79. parent->RemoveChild(this);
  80. }
  81. if (aNewParent) {
  82. aNewParent->AddChild(this);
  83. }
  84. mParent = aNewParent;
  85. return NS_OK;
  86. }
  87. nsIWidget*
  88. PluginWidgetProxy::GetParent(void)
  89. {
  90. return mParent.get();
  91. }
  92. void
  93. PluginWidgetProxy::Destroy()
  94. {
  95. PWLOG("PluginWidgetProxy::Destroy()\n");
  96. if (mActor) {
  97. // Communicate that the layout widget has been torn down before the sub
  98. // protocol.
  99. mActor->ProxyShutdown();
  100. mActor = nullptr;
  101. }
  102. PuppetWidget::Destroy();
  103. }
  104. void
  105. PluginWidgetProxy::GetWindowClipRegion(nsTArray<LayoutDeviceIntRect>* aRects)
  106. {
  107. if (mClipRects && mClipRectCount) {
  108. aRects->AppendElements(mClipRects.get(), mClipRectCount);
  109. }
  110. }
  111. void*
  112. PluginWidgetProxy::GetNativeData(uint32_t aDataType)
  113. {
  114. if (!mActor) {
  115. return nullptr;
  116. }
  117. auto tab = static_cast<mozilla::dom::TabChild*>(mActor->Manager());
  118. if (tab && tab->IsDestroyed()) {
  119. return nullptr;
  120. }
  121. switch (aDataType) {
  122. case NS_NATIVE_PLUGIN_PORT:
  123. case NS_NATIVE_WINDOW:
  124. case NS_NATIVE_SHAREABLE_WINDOW:
  125. break;
  126. default:
  127. NS_WARNING("PluginWidgetProxy::GetNativeData received request for unsupported data type.");
  128. return nullptr;
  129. }
  130. // The parent side window handle or xid never changes so we can
  131. // cache this for our lifetime.
  132. if (mCachedPluginPort) {
  133. return (void*)mCachedPluginPort;
  134. }
  135. mActor->SendGetNativePluginPort(&mCachedPluginPort);
  136. PWLOG("PluginWidgetProxy::GetNativeData %p\n", (void*)mCachedPluginPort);
  137. return (void*)mCachedPluginPort;
  138. }
  139. #if defined(XP_WIN)
  140. void
  141. PluginWidgetProxy::SetNativeData(uint32_t aDataType, uintptr_t aVal)
  142. {
  143. if (!mActor) {
  144. return;
  145. }
  146. auto tab = static_cast<mozilla::dom::TabChild*>(mActor->Manager());
  147. if (tab && tab->IsDestroyed()) {
  148. return;
  149. }
  150. switch (aDataType) {
  151. case NS_NATIVE_CHILD_WINDOW:
  152. mActor->SendSetNativeChildWindow(aVal);
  153. break;
  154. default:
  155. NS_ERROR("SetNativeData called with unsupported data type.");
  156. }
  157. }
  158. #endif
  159. NS_IMETHODIMP
  160. PluginWidgetProxy::SetFocus(bool aRaise)
  161. {
  162. ENSURE_CHANNEL;
  163. PWLOG("PluginWidgetProxy::SetFocus(%d)\n", aRaise);
  164. mActor->SendSetFocus(aRaise);
  165. return NS_OK;
  166. }
  167. } // namespace widget
  168. } // namespace mozilla