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