1/*
2 * Copyright (C) 2010-2018 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 "DrawingAreaProxy.h"
28
29#include "DrawingAreaMessages.h"
30#include "DrawingAreaProxyMessages.h"
31#include "WebPageProxy.h"
32#include "WebProcessProxy.h"
33
34#if PLATFORM(COCOA)
35#include <wtf/MachSendRight.h>
36#endif
37
38namespace WebKit {
39using namespace WebCore;
40
41DrawingAreaProxy::DrawingAreaProxy(DrawingAreaType type, WebPageProxy& webPageProxy, WebProcessProxy& process)
42 : m_type(type)
43 , m_identifier(DrawingAreaIdentifier::generate())
44 , m_webPageProxy(webPageProxy)
45 , m_process(makeRef(process))
46 , m_size(webPageProxy.viewSize())
47#if PLATFORM(MAC)
48 , m_viewExposedRectChangedTimer(RunLoop::main(), this, &DrawingAreaProxy::viewExposedRectChangedTimerFired)
49#endif
50{
51 process.addMessageReceiver(Messages::DrawingAreaProxy::messageReceiverName(), m_identifier, *this);
52}
53
54DrawingAreaProxy::~DrawingAreaProxy()
55{
56 process().removeMessageReceiver(Messages::DrawingAreaProxy::messageReceiverName(), m_identifier);
57}
58
59bool DrawingAreaProxy::setSize(const IntSize& size, const IntSize& scrollDelta)
60{
61 if (m_size == size && scrollDelta.isZero())
62 return false;
63
64 m_size = size;
65 m_scrollOffset += scrollDelta;
66 sizeDidChange();
67 return true;
68}
69
70#if PLATFORM(COCOA)
71MachSendRight DrawingAreaProxy::createFence()
72{
73 return MachSendRight();
74}
75#endif
76
77IPC::Connection* DrawingAreaProxy::messageSenderConnection() const
78{
79 return process().connection();
80}
81
82bool DrawingAreaProxy::sendMessage(std::unique_ptr<IPC::Encoder> encoder, OptionSet<IPC::SendOption> sendOptions)
83{
84 return process().sendMessage(WTFMove(encoder), sendOptions);
85}
86
87#if PLATFORM(MAC)
88void DrawingAreaProxy::setViewExposedRect(Optional<WebCore::FloatRect> viewExposedRect)
89{
90 if (!m_webPageProxy.hasRunningProcess())
91 return;
92
93 m_viewExposedRect = viewExposedRect;
94
95 if (!m_viewExposedRectChangedTimer.isActive())
96 m_viewExposedRectChangedTimer.startOneShot(0_s);
97}
98
99void DrawingAreaProxy::viewExposedRectChangedTimerFired()
100{
101 if (!m_webPageProxy.hasRunningProcess())
102 return;
103
104 if (m_viewExposedRect == m_lastSentViewExposedRect)
105 return;
106
107 send(Messages::DrawingArea::SetViewExposedRect(m_viewExposedRect));
108 m_lastSentViewExposedRect = m_viewExposedRect;
109}
110#endif // PLATFORM(MAC)
111
112} // namespace WebKit
113