WebContextMenuItemData.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #if ENABLE(CONTEXT_MENUS)
  27. #include "WebContextMenuItemData.h"
  28. #include "APIObject.h"
  29. #include "ArgumentCoders.h"
  30. #include "Arguments.h"
  31. #include <wtf/text/CString.h>
  32. #include <WebCore/ContextMenu.h>
  33. using namespace WebCore;
  34. namespace WebKit {
  35. WebContextMenuItemData::WebContextMenuItemData()
  36. : m_type(WebCore::ActionType)
  37. , m_action(WebCore::ContextMenuItemTagNoAction)
  38. , m_enabled(true)
  39. , m_checked(false)
  40. {
  41. }
  42. WebContextMenuItemData::WebContextMenuItemData(WebCore::ContextMenuItemType type, WebCore::ContextMenuAction action, const String& title, bool enabled, bool checked)
  43. : m_type(type)
  44. , m_action(action)
  45. , m_title(title)
  46. , m_enabled(enabled)
  47. , m_checked(checked)
  48. {
  49. ASSERT(type == WebCore::ActionType || type == WebCore::CheckableActionType || type == WebCore::SeparatorType);
  50. }
  51. WebContextMenuItemData::WebContextMenuItemData(WebCore::ContextMenuAction action, const String& title, bool enabled, const Vector<WebContextMenuItemData>& submenu)
  52. : m_type(WebCore::SubmenuType)
  53. , m_action(action)
  54. , m_title(title)
  55. , m_enabled(enabled)
  56. , m_checked(false)
  57. , m_submenu(submenu)
  58. {
  59. }
  60. WebContextMenuItemData::WebContextMenuItemData(const WebCore::ContextMenuItem& item, WebCore::ContextMenu* menu)
  61. : m_type(item.type())
  62. , m_action(item.action())
  63. , m_title(item.title())
  64. {
  65. if (m_type == WebCore::SubmenuType) {
  66. #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
  67. const Vector<WebCore::ContextMenuItem>& coreSubmenu = item.subMenuItems();
  68. #else
  69. Vector<WebCore::ContextMenuItem> coreSubmenu = WebCore::contextMenuItemVector(item.platformSubMenu());
  70. #endif
  71. m_submenu = kitItems(coreSubmenu, menu);
  72. }
  73. m_enabled = item.enabled();
  74. m_checked = item.checked();
  75. }
  76. ContextMenuItem WebContextMenuItemData::core() const
  77. {
  78. if (m_type != SubmenuType)
  79. return ContextMenuItem(m_type, m_action, m_title, m_enabled, m_checked);
  80. Vector<ContextMenuItem> subMenuItems = coreItems(m_submenu);
  81. return ContextMenuItem(m_action, m_title, m_enabled, m_checked, subMenuItems);
  82. }
  83. APIObject* WebContextMenuItemData::userData() const
  84. {
  85. return m_userData.get();
  86. }
  87. void WebContextMenuItemData::setUserData(APIObject* userData)
  88. {
  89. m_userData = userData;
  90. }
  91. void WebContextMenuItemData::encode(CoreIPC::ArgumentEncoder& encoder) const
  92. {
  93. encoder.encodeEnum(m_type);
  94. encoder.encodeEnum(m_action);
  95. encoder << m_title;
  96. encoder << m_checked;
  97. encoder << m_enabled;
  98. encoder << m_submenu;
  99. }
  100. bool WebContextMenuItemData::decode(CoreIPC::ArgumentDecoder& decoder, WebContextMenuItemData& item)
  101. {
  102. WebCore::ContextMenuItemType type;
  103. if (!decoder.decodeEnum(type))
  104. return false;
  105. WebCore::ContextMenuAction action;
  106. if (!decoder.decodeEnum(action))
  107. return false;
  108. String title;
  109. if (!decoder.decode(title))
  110. return false;
  111. bool checked;
  112. if (!decoder.decode(checked))
  113. return false;
  114. bool enabled;
  115. if (!decoder.decode(enabled))
  116. return false;
  117. Vector<WebContextMenuItemData> submenu;
  118. if (!decoder.decode(submenu))
  119. return false;
  120. switch (type) {
  121. case WebCore::ActionType:
  122. case WebCore::SeparatorType:
  123. case WebCore::CheckableActionType:
  124. item = WebContextMenuItemData(type, action, title, enabled, checked);
  125. break;
  126. case WebCore::SubmenuType:
  127. item = WebContextMenuItemData(action, title, enabled, submenu);
  128. break;
  129. default:
  130. ASSERT_NOT_REACHED();
  131. return false;
  132. }
  133. return true;
  134. }
  135. Vector<WebContextMenuItemData> kitItems(const Vector<WebCore::ContextMenuItem>& coreItemVector, WebCore::ContextMenu* menu)
  136. {
  137. Vector<WebContextMenuItemData> result;
  138. result.reserveCapacity(coreItemVector.size());
  139. for (unsigned i = 0; i < coreItemVector.size(); ++i)
  140. result.append(WebContextMenuItemData(coreItemVector[i], menu));
  141. return result;
  142. }
  143. Vector<ContextMenuItem> coreItems(const Vector<WebContextMenuItemData>& kitItemVector)
  144. {
  145. Vector<ContextMenuItem> result;
  146. result.reserveCapacity(kitItemVector.size());
  147. for (unsigned i = 0; i < kitItemVector.size(); ++i)
  148. result.append(kitItemVector[i].core());
  149. return result;
  150. }
  151. } // namespace WebKit
  152. #endif // ENABLE(CONTEXT_MENUS)