1/*
2 * Copyright (C) 2014 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#include "config.h"
28#include "ContextMenuContextData.h"
29
30#if ENABLE(CONTEXT_MENUS)
31
32#include "WebCoreArgumentCoders.h"
33#include <WebCore/ContextMenuContext.h>
34#include <WebCore/GraphicsContext.h>
35
36namespace WebKit {
37using namespace WebCore;
38
39ContextMenuContextData::ContextMenuContextData()
40 : m_type(Type::ContextMenu)
41#if ENABLE(SERVICE_CONTROLS)
42 , m_selectionIsEditable(false)
43#endif
44{
45}
46
47ContextMenuContextData::ContextMenuContextData(const WebCore::IntPoint& menuLocation, const Vector<WebKit::WebContextMenuItemData>& menuItems, const ContextMenuContext& context)
48#if ENABLE(SERVICE_CONTROLS)
49 : m_type(context.controlledImage() ? Type::ServicesMenu : Type::ContextMenu)
50#else
51 : m_type(Type::ContextMenu)
52#endif
53 , m_menuLocation(menuLocation)
54 , m_menuItems(menuItems)
55 , m_webHitTestResultData(context.hitTestResult(), true)
56 , m_selectedText(context.selectedText())
57#if ENABLE(SERVICE_CONTROLS)
58 , m_selectionIsEditable(false)
59#endif
60{
61#if ENABLE(SERVICE_CONTROLS)
62 Image* image = context.controlledImage();
63 if (!image)
64 return;
65
66 // FIXME: figure out the rounding strategy for ShareableBitmap.
67 m_controlledImage = ShareableBitmap::createShareable(IntSize(image->size()), { });
68 m_controlledImage->createGraphicsContext()->drawImage(*image, IntPoint());
69#endif
70}
71
72void ContextMenuContextData::encode(IPC::Encoder& encoder) const
73{
74 encoder.encodeEnum(m_type);
75 encoder << m_menuLocation;
76 encoder << m_menuItems;
77 encoder << m_webHitTestResultData;
78 encoder << m_selectedText;
79
80#if ENABLE(SERVICE_CONTROLS)
81 ShareableBitmap::Handle handle;
82 if (m_controlledImage)
83 m_controlledImage->createHandle(handle, SharedMemory::Protection::ReadOnly);
84 encoder << handle;
85 encoder << m_controlledSelectionData;
86 encoder << m_selectedTelephoneNumbers;
87 encoder << m_selectionIsEditable;
88#endif
89}
90
91bool ContextMenuContextData::decode(IPC::Decoder& decoder, ContextMenuContextData& result)
92{
93 if (!decoder.decodeEnum(result.m_type))
94 return false;
95
96 if (!decoder.decode(result.m_menuLocation))
97 return false;
98
99 if (!decoder.decode(result.m_menuItems))
100 return false;
101
102 if (!decoder.decode(result.m_webHitTestResultData))
103 return false;
104
105 if (!decoder.decode(result.m_selectedText))
106 return false;
107
108#if ENABLE(SERVICE_CONTROLS)
109 ShareableBitmap::Handle handle;
110 if (!decoder.decode(handle))
111 return false;
112
113 if (!handle.isNull())
114 result.m_controlledImage = ShareableBitmap::create(handle, SharedMemory::Protection::ReadOnly);
115
116 if (!decoder.decode(result.m_controlledSelectionData))
117 return false;
118 if (!decoder.decode(result.m_selectedTelephoneNumbers))
119 return false;
120 if (!decoder.decode(result.m_selectionIsEditable))
121 return false;
122#endif
123
124 return true;
125}
126
127#if ENABLE(SERVICE_CONTROLS)
128bool ContextMenuContextData::controlledDataIsEditable() const
129{
130 if (!m_controlledSelectionData.isEmpty())
131 return m_selectionIsEditable;
132
133 if (m_controlledImage)
134 return m_webHitTestResultData.isContentEditable;
135
136 return false;
137}
138#endif
139
140} // namespace WebKit
141
142#endif // ENABLE(CONTEXT_MENUS)
143