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#import "config.h"
27#import "PluginControllerProxy.h"
28
29#if ENABLE(NETSCAPE_PLUGIN_API)
30
31#import "LayerHostingContext.h"
32#import "PluginCreationParameters.h"
33#import "PluginProcess.h"
34#import "PluginProcessProxyMessages.h"
35#import "PluginProxyMessages.h"
36#import "WebProcessConnection.h"
37#import <QuartzCore/QuartzCore.h>
38
39using namespace WebCore;
40
41namespace WebKit {
42
43void PluginControllerProxy::pluginFocusOrWindowFocusChanged(bool pluginHasFocusAndWindowHasFocus)
44{
45    m_connection->connection()->send(Messages::PluginProxy::PluginFocusOrWindowFocusChanged(pluginHasFocusAndWindowHasFocus), m_pluginInstanceID);
46}
47
48void PluginControllerProxy::setComplexTextInputState(PluginComplexTextInputState pluginComplexTextInputState)
49{
50    m_connection->connection()->send(Messages::PluginProxy::SetComplexTextInputState(pluginComplexTextInputState), m_pluginInstanceID, IPC::DispatchMessageEvenWhenWaitingForSyncReply);
51}
52
53mach_port_t PluginControllerProxy::compositingRenderServerPort()
54{
55    return PluginProcess::shared().compositingRenderServerPort();
56}
57
58void PluginControllerProxy::openPluginPreferencePane()
59{
60    PluginProcess::shared().parentProcessConnection()->send(Messages::PluginProcessProxy::OpenPluginPreferencePane(), 0);
61}
62
63void PluginControllerProxy::platformInitialize(const PluginCreationParameters& creationParameters)
64{
65    ASSERT(!m_layerHostingContext);
66    updateLayerHostingContext(creationParameters.parameters.layerHostingMode);
67}
68
69void PluginControllerProxy::platformDestroy()
70{
71    if (!m_layerHostingContext)
72        return;
73
74    m_layerHostingContext->invalidate();
75    m_layerHostingContext = nullptr;
76}
77
78uint32_t PluginControllerProxy::remoteLayerClientID() const
79{
80    if (!m_layerHostingContext)
81        return 0;
82
83    return m_layerHostingContext->contextID();
84}
85
86void PluginControllerProxy::platformGeometryDidChange()
87{
88    CALayer *pluginLayer = m_plugin->pluginLayer();
89
90    // We don't want to animate to the new size so we disable actions for this transaction.
91    [CATransaction begin];
92    [CATransaction setValue:[NSNumber numberWithBool:YES] forKey:kCATransactionDisableActions];
93    [pluginLayer setFrame:CGRectMake(0, 0, m_pluginSize.width(), m_pluginSize.height())];
94    [CATransaction commit];
95}
96
97void PluginControllerProxy::windowFocusChanged(bool hasFocus)
98{
99    m_plugin->windowFocusChanged(hasFocus);
100}
101
102void PluginControllerProxy::windowAndViewFramesChanged(const IntRect& windowFrameInScreenCoordinates, const IntRect& viewFrameInWindowCoordinates)
103{
104    m_plugin->windowAndViewFramesChanged(windowFrameInScreenCoordinates, viewFrameInWindowCoordinates);
105}
106
107void PluginControllerProxy::windowVisibilityChanged(bool isVisible)
108{
109    m_plugin->windowVisibilityChanged(isVisible);
110    if (isVisible)
111        m_connection->pluginDidBecomeVisible(m_pluginInstanceID);
112    else
113        m_connection->pluginDidBecomeHidden(m_pluginInstanceID);
114}
115
116void PluginControllerProxy::sendComplexTextInput(const String& textInput)
117{
118    m_plugin->sendComplexTextInput(textInput);
119}
120
121void PluginControllerProxy::setLayerHostingMode(uint32_t opaqueLayerHostingMode)
122{
123    LayerHostingMode layerHostingMode = static_cast<LayerHostingMode>(opaqueLayerHostingMode);
124
125    m_plugin->setLayerHostingMode(layerHostingMode);
126    updateLayerHostingContext(layerHostingMode);
127
128    if (m_layerHostingContext)
129        m_connection->connection()->send(Messages::PluginProxy::SetLayerHostingContextID(m_layerHostingContext->contextID()), m_pluginInstanceID);
130}
131
132void PluginControllerProxy::updateLayerHostingContext(LayerHostingMode layerHostingMode)
133{
134    CALayer *platformLayer = m_plugin->pluginLayer();
135    if (!platformLayer)
136        return;
137
138    if (m_layerHostingContext) {
139        if (m_layerHostingContext->layerHostingMode() == layerHostingMode)
140            return;
141
142        m_layerHostingContext->invalidate();
143    }
144
145    switch (layerHostingMode) {
146        case LayerHostingMode::InProcess:
147            m_layerHostingContext = LayerHostingContext::createForPort(PluginProcess::shared().compositingRenderServerPort());
148            break;
149#if HAVE(OUT_OF_PROCESS_LAYER_HOSTING)
150        case LayerHostingMode::OutOfProcess:
151            m_layerHostingContext = LayerHostingContext::createForExternalHostingProcess();
152            break;
153#endif
154    }
155
156    m_layerHostingContext->setRootLayer(platformLayer);
157}
158
159} // namespace WebKit
160
161#endif // ENABLE(NETSCAPE_PLUGIN_API)
162