1/*
2 * Copyright (C) 2010, 2015 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#pragma once
27
28#if ENABLE(NETSCAPE_PLUGIN_API)
29
30#include "Connection.h"
31#include "Plugin.h"
32#include "PluginProcess.h"
33#include <WebCore/AffineTransform.h>
34#include <WebCore/IntRect.h>
35#include <WebCore/SecurityOrigin.h>
36#include <memory>
37
38#if PLATFORM(COCOA)
39#include <wtf/RetainPtr.h>
40OBJC_CLASS CALayer;
41#endif
42
43namespace WebCore {
44 class HTTPHeaderMap;
45 class ProtectionSpace;
46}
47
48namespace WebKit {
49
50class ShareableBitmap;
51class NPVariantData;
52class PluginProcessConnection;
53
54struct PluginCreationParameters;
55
56class PluginProxy : public Plugin {
57public:
58 static Ref<PluginProxy> create(uint64_t pluginProcessToken, bool isRestartedProcess);
59 ~PluginProxy();
60
61 uint64_t pluginInstanceID() const { return m_pluginInstanceID; }
62 void pluginProcessCrashed();
63
64 void didReceivePluginProxyMessage(IPC::Connection&, IPC::Decoder&);
65 void didReceiveSyncPluginProxyMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
66
67 bool isBeingAsynchronouslyInitialized() const override { return m_waitingOnAsynchronousInitialization; }
68
69private:
70 explicit PluginProxy(uint64_t pluginProcessToken, bool isRestartedProcess);
71
72 // Plugin
73 bool initialize(const Parameters&) override;
74 bool initializeSynchronously();
75
76 void destroy() override;
77 void paint(WebCore::GraphicsContext&, const WebCore::IntRect& dirtyRect) override;
78 bool supportsSnapshotting() const override;
79 RefPtr<ShareableBitmap> snapshot() override;
80#if PLATFORM(COCOA)
81 PlatformLayer* pluginLayer() override;
82#endif
83 bool isTransparent() override;
84 bool wantsWheelEvents() override;
85 void geometryDidChange(const WebCore::IntSize& pluginSize, const WebCore::IntRect& clipRect, const WebCore::AffineTransform& pluginToRootViewTransform) override;
86 void visibilityDidChange(bool isVisible) override;
87 void frameDidFinishLoading(uint64_t requestID) override;
88 void frameDidFail(uint64_t requestID, bool wasCancelled) override;
89 void didEvaluateJavaScript(uint64_t requestID, const String& result) override;
90 void streamWillSendRequest(uint64_t streamID, const URL& requestURL, const URL& responseURL, int responseStatus) override;
91 void streamDidReceiveResponse(uint64_t streamID, const URL& responseURL, uint32_t streamLength, uint32_t lastModifiedTime, const String& mimeType, const String& headers, const String& suggestedFileName) override;
92 void streamDidReceiveData(uint64_t streamID, const char* bytes, int length) override;
93 void streamDidFinishLoading(uint64_t streamID) override;
94 void streamDidFail(uint64_t streamID, bool wasCancelled) override;
95 void manualStreamDidReceiveResponse(const URL& responseURL, uint32_t streamLength, uint32_t lastModifiedTime, const WTF::String& mimeType, const WTF::String& headers, const String& suggestedFileName) override;
96 void manualStreamDidReceiveData(const char* bytes, int length) override;
97 void manualStreamDidFinishLoading() override;
98 void manualStreamDidFail(bool wasCancelled) override;
99
100 bool handleMouseEvent(const WebMouseEvent&) override;
101 bool handleWheelEvent(const WebWheelEvent&) override;
102 bool handleMouseEnterEvent(const WebMouseEvent&) override;
103 bool handleMouseLeaveEvent(const WebMouseEvent&) override;
104 bool handleContextMenuEvent(const WebMouseEvent&) override;
105 bool handleKeyboardEvent(const WebKeyboardEvent&) override;
106 void setFocus(bool) override;
107 bool handleEditingCommand(const String& commandName, const String& argument) override;
108 bool isEditingCommandEnabled(const String& commandName) override;
109 bool shouldAllowScripting() override { return true; }
110 bool shouldAllowNavigationFromDrags() override { return false; }
111
112 bool handlesPageScaleFactor() const override;
113 bool requiresUnifiedScaleFactor() const override;
114
115 NPObject* pluginScriptableNPObject() override;
116
117 void windowFocusChanged(bool) override;
118 void windowVisibilityChanged(bool) override;
119
120#if PLATFORM(COCOA)
121 void windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates) override;
122 uint64_t pluginComplexTextInputIdentifier() const override;
123 void sendComplexTextInput(const String& textInput) override;
124 void setLayerHostingMode(LayerHostingMode) override;
125#endif
126
127 void contentsScaleFactorChanged(float) override;
128 void storageBlockingStateChanged(bool) override;
129 void privateBrowsingStateChanged(bool) override;
130 void mutedStateChanged(bool) override;
131 bool getFormValue(String& formValue) override;
132 bool handleScroll(WebCore::ScrollDirection, WebCore::ScrollGranularity) override;
133 WebCore::Scrollbar* horizontalScrollbar() override;
134 WebCore::Scrollbar* verticalScrollbar() override;
135
136 unsigned countFindMatches(const String&, WebCore::FindOptions, unsigned) override { return 0; }
137 bool findString(const String&, WebCore::FindOptions, unsigned) override { return false; }
138
139 WebCore::IntPoint convertToRootView(const WebCore::IntPoint&) const override;
140
141 RefPtr<WebCore::SharedBuffer> liveResourceData() const override;
142 bool performDictionaryLookupAtLocation(const WebCore::FloatPoint&) override { return false; }
143
144 String getSelectionString() const override { return String(); }
145 String getSelectionForWordAtPoint(const WebCore::FloatPoint&) const override { return String(); }
146 bool existingSelectionContainsPoint(const WebCore::FloatPoint&) const override { return false; }
147
148 float contentsScaleFactor();
149 bool needsBackingStore() const;
150 bool updateBackingStore();
151 uint64_t windowNPObjectID();
152 WebCore::IntRect pluginBounds();
153
154 void geometryDidChange();
155
156 // Message handlers.
157 void loadURL(uint64_t requestID, const String& method, const String& urlString, const String& target, const WebCore::HTTPHeaderMap& headerFields, const Vector<uint8_t>& httpBody, bool allowPopups);
158 void update(const WebCore::IntRect& paintedRect);
159 void proxiesForURL(const String& urlString, CompletionHandler<void(String)>&&);
160 void cookiesForURL(const String& urlString, CompletionHandler<void(String)>&&);
161 void setCookiesForURL(const String& urlString, const String& cookieString);
162 void getAuthenticationInfo(const WebCore::ProtectionSpace&, CompletionHandler<void(bool returnValue, String username, String password)>&&);
163 void getPluginElementNPObject(CompletionHandler<void(uint64_t)>&&);
164 void evaluate(const NPVariantData& npObjectAsVariantData, const String& scriptString, bool allowPopups, CompletionHandler<void(bool returnValue, NPVariantData&& resultData)>&&);
165 void setPluginIsPlayingAudio(bool);
166 void continueStreamLoad(uint64_t streamID);
167 void cancelStreamLoad(uint64_t streamID);
168 void cancelManualStreamLoad();
169 void setStatusbarText(const String& statusbarText);
170#if PLATFORM(COCOA)
171 void pluginFocusOrWindowFocusChanged(bool);
172 void setComplexTextInputState(uint64_t);
173 void setLayerHostingContextID(uint32_t);
174#endif
175#if PLATFORM(X11)
176 void createPluginContainer(CompletionHandler<void(uint64_t windowID)>&&);
177 void windowedPluginGeometryDidChange(const WebCore::IntRect& frameRect, const WebCore::IntRect& clipRect, uint64_t windowID);
178 void windowedPluginVisibilityDidChange(bool isVisible, uint64_t windowID);
179#endif
180
181 bool canInitializeAsynchronously() const;
182
183 void didCreatePlugin(bool wantsWheelEvents, uint32_t remoteLayerClientID, CompletionHandler<void()>&&);
184 void didFailToCreatePlugin(CompletionHandler<void()>&&);
185
186 void didCreatePluginInternal(bool wantsWheelEvents, uint32_t remoteLayerClientID);
187 void didFailToCreatePluginInternal();
188
189 uint64_t m_pluginProcessToken;
190
191 RefPtr<PluginProcessConnection> m_connection;
192 uint64_t m_pluginInstanceID;
193
194 WebCore::IntSize m_pluginSize;
195
196 // The clip rect in plug-in coordinates.
197 WebCore::IntRect m_clipRect;
198
199 // A transform that can be used to convert from root view coordinates to plug-in coordinates.
200 WebCore::AffineTransform m_pluginToRootViewTransform;
201
202 // This is the backing store that we paint when we're told to paint.
203 RefPtr<ShareableBitmap> m_backingStore;
204
205 // This is the shared memory backing store that the plug-in paints into. When the plug-in tells us
206 // that it's painted something in it, we'll blit from it to our own backing store.
207 RefPtr<ShareableBitmap> m_pluginBackingStore;
208
209 // Whether all of the plug-in backing store contains valid data.
210 bool m_pluginBackingStoreContainsValidData;
211
212 bool m_isStarted;
213
214 // Whether we're called invalidate in response to an update call, and are now waiting for a paint call.
215 bool m_waitingForPaintInResponseToUpdate;
216
217 // Whether we should send wheel events to this plug-in or not.
218 bool m_wantsWheelEvents;
219
220 // The client ID for the CA layer in the plug-in process. Will be 0 if the plug-in is not a CA plug-in.
221 uint32_t m_remoteLayerClientID;
222
223 std::unique_ptr<PluginCreationParameters> m_pendingPluginCreationParameters;
224 bool m_waitingOnAsynchronousInitialization;
225
226#if PLATFORM(COCOA)
227 RetainPtr<CALayer> m_pluginLayer;
228#endif
229
230 bool m_isRestartedProcess;
231};
232
233} // namespace WebKit
234
235SPECIALIZE_TYPE_TRAITS_PLUGIN(PluginProxy, PluginProxyType)
236
237#endif // ENABLE(NETSCAPE_PLUGIN_API)
238
239