WebContextMenu.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  3. * Copyright (C) 2010 Apple Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #include "config.h"
  22. #if ENABLE(CONTEXT_MENUS)
  23. #include "WebContextMenu.h"
  24. #include "InjectedBundleHitTestResult.h"
  25. #include "InjectedBundleUserMessageCoders.h"
  26. #include "WebCoreArgumentCoders.h"
  27. #include "WebHitTestResult.h"
  28. #include "WebPage.h"
  29. #include "WebPageProxyMessages.h"
  30. #include <WebCore/ContextMenu.h>
  31. #include <WebCore/ContextMenuController.h>
  32. #include <WebCore/Frame.h>
  33. #include <WebCore/FrameView.h>
  34. #include <WebCore/Page.h>
  35. using namespace WebCore;
  36. namespace WebKit {
  37. WebContextMenu::WebContextMenu(WebPage* page)
  38. : m_page(page)
  39. {
  40. }
  41. WebContextMenu::~WebContextMenu()
  42. {
  43. }
  44. void WebContextMenu::show()
  45. {
  46. ContextMenuController* controller = m_page->corePage()->contextMenuController();
  47. if (!controller)
  48. return;
  49. ContextMenu* menu = controller->contextMenu();
  50. if (!menu)
  51. return;
  52. Frame* frame = controller->hitTestResult().innerNodeFrame();
  53. if (!frame)
  54. return;
  55. FrameView* view = frame->view();
  56. if (!view)
  57. return;
  58. Vector<WebContextMenuItemData> menuItems;
  59. RefPtr<APIObject> userData;
  60. menuItemsWithUserData(menuItems, userData);
  61. WebHitTestResult::Data webHitTestResultData(controller->hitTestResult());
  62. // Mark the WebPage has having a shown context menu then notify the UIProcess.
  63. m_page->contextMenuShowing();
  64. m_page->send(Messages::WebPageProxy::ShowContextMenu(view->contentsToWindow(controller->hitTestResult().roundedPointInInnerNodeFrame()), webHitTestResultData, menuItems, InjectedBundleUserMessageEncoder(userData.get())));
  65. }
  66. void WebContextMenu::itemSelected(const WebContextMenuItemData& item)
  67. {
  68. ContextMenuItem coreItem(ActionType, static_cast<ContextMenuAction>(item.action()), item.title());
  69. m_page->corePage()->contextMenuController()->contextMenuItemSelected(&coreItem);
  70. }
  71. void WebContextMenu::menuItemsWithUserData(Vector<WebContextMenuItemData> &menuItems, RefPtr<APIObject>& userData) const
  72. {
  73. ContextMenuController* controller = m_page->corePage()->contextMenuController();
  74. if (!controller)
  75. return;
  76. ContextMenu* menu = controller->contextMenu();
  77. if (!menu)
  78. return;
  79. // Give the bundle client a chance to process the menu.
  80. #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
  81. const Vector<ContextMenuItem>& coreItems = menu->items();
  82. #else
  83. Vector<ContextMenuItem> coreItems = contextMenuItemVector(menu->platformDescription());
  84. #endif
  85. Vector<WebContextMenuItemData> proposedMenu = kitItems(coreItems, menu);
  86. Vector<WebContextMenuItemData> newMenu;
  87. RefPtr<InjectedBundleHitTestResult> hitTestResult = InjectedBundleHitTestResult::create(controller->hitTestResult());
  88. if (m_page->injectedBundleContextMenuClient().getCustomMenuFromDefaultItems(m_page, hitTestResult.get(), proposedMenu, newMenu, userData))
  89. proposedMenu = newMenu;
  90. menuItems = proposedMenu;
  91. }
  92. Vector<WebContextMenuItemData> WebContextMenu::items() const
  93. {
  94. Vector<WebContextMenuItemData> menuItems;
  95. RefPtr<APIObject> userData;
  96. menuItemsWithUserData(menuItems, userData);
  97. return menuItems;
  98. }
  99. } // namespace WebKit
  100. #endif // ENABLE(CONTEXT_MENUS)