1/*
2 * Copyright (C) 2010, 2012 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#ifndef PluginView_h
27#define PluginView_h
28
29#include "LayerTreeContext.h"
30#include "NPRuntimeObjectMap.h"
31#include "Plugin.h"
32#include "PluginController.h"
33#include "WebFrame.h"
34#include <WebCore/FindOptions.h>
35#include <WebCore/Image.h>
36#include <WebCore/MediaCanStartListener.h>
37#include <WebCore/PluginViewBase.h>
38#include <WebCore/ResourceError.h>
39#include <WebCore/ResourceResponse.h>
40#include <WebCore/Timer.h>
41#include <WebCore/ViewState.h>
42#include <memory>
43#include <wtf/Deque.h>
44#include <wtf/RunLoop.h>
45
46// FIXME: Eventually this should move to WebCore.
47
48namespace WebCore {
49class Frame;
50class HTMLPlugInElement;
51class MouseEvent;
52class RenderBoxModelObject;
53}
54
55namespace WebKit {
56
57class WebEvent;
58
59class PluginView : public WebCore::PluginViewBase, public PluginController, private WebCore::MediaCanStartListener, private WebFrame::LoadListener {
60public:
61    static PassRefPtr<PluginView> create(PassRefPtr<WebCore::HTMLPlugInElement>, PassRefPtr<Plugin>, const Plugin::Parameters&);
62
63    void recreateAndInitialize(PassRefPtr<Plugin>);
64
65    WebCore::Frame* frame() const;
66
67    bool isBeingDestroyed() const { return m_isBeingDestroyed; }
68
69    void manualLoadDidReceiveResponse(const WebCore::ResourceResponse&);
70    void manualLoadDidReceiveData(const char* bytes, int length);
71    void manualLoadDidFinishLoading();
72    void manualLoadDidFail(const WebCore::ResourceError&);
73
74    void viewStateDidChange(WebCore::ViewState::Flags changed);
75    void setLayerHostingMode(LayerHostingMode);
76
77#if PLATFORM(COCOA)
78    void platformViewStateDidChange(WebCore::ViewState::Flags changed);
79    void setDeviceScaleFactor(float);
80    void windowAndViewFramesChanged(const WebCore::FloatRect& windowFrameInScreenCoordinates, const WebCore::FloatRect& viewFrameInWindowCoordinates);
81    bool sendComplexTextInput(uint64_t pluginComplexTextInputIdentifier, const String& textInput);
82    RetainPtr<PDFDocument> pdfDocumentForPrinting() const { return m_plugin->pdfDocumentForPrinting(); }
83    NSObject *accessibilityObject() const;
84#endif
85
86    WebCore::HTMLPlugInElement* pluginElement() const { return m_pluginElement.get(); }
87    const Plugin::Parameters& initialParameters() const { return m_parameters; }
88
89    // FIXME: Remove this; nobody should have to know about the plug-in view's renderer except the plug-in view itself.
90    WebCore::RenderBoxModelObject* renderer() const;
91
92    void setPageScaleFactor(double scaleFactor, WebCore::IntPoint origin);
93    double pageScaleFactor() const;
94    bool handlesPageScaleFactor() const;
95
96    void pageScaleFactorDidChange();
97    void topContentInsetDidChange();
98
99    void webPageDestroyed();
100
101    bool handleEditingCommand(const String& commandName, const String& argument);
102    bool isEditingCommandEnabled(const String& commandName);
103
104    unsigned countFindMatches(const String& target, WebCore::FindOptions, unsigned maxMatchCount);
105    bool findString(const String& target, WebCore::FindOptions, unsigned maxMatchCount);
106
107    String getSelectionString() const;
108
109    bool shouldAllowScripting();
110
111    PassRefPtr<WebCore::SharedBuffer> liveResourceData() const;
112    bool performDictionaryLookupAtLocation(const WebCore::FloatPoint&);
113    WebCore::AudioHardwareActivityType audioHardwareActivity() const;
114
115private:
116    PluginView(PassRefPtr<WebCore::HTMLPlugInElement>, PassRefPtr<Plugin>, const Plugin::Parameters& parameters);
117    virtual ~PluginView();
118
119    void initializePlugin();
120
121    void viewGeometryDidChange();
122    void viewVisibilityDidChange();
123    WebCore::IntRect clipRectInWindowCoordinates() const;
124    void focusPluginElement();
125
126    void pendingURLRequestsTimerFired();
127    class URLRequest;
128    void performURLRequest(URLRequest*);
129
130    // Perform a URL request where the frame target is not null.
131    void performFrameLoadURLRequest(URLRequest*);
132
133    // Perform a URL request where the URL protocol is "javascript:".
134    void performJavaScriptURLRequest(URLRequest*);
135
136    class Stream;
137    void addStream(Stream*);
138    void removeStream(Stream*);
139    void cancelAllStreams();
140
141    void redeliverManualStream();
142
143    void pluginSnapshotTimerFired();
144    void pluginDidReceiveUserInteraction();
145
146    bool shouldCreateTransientPaintingSnapshot() const;
147
148    // WebCore::PluginViewBase
149#if PLATFORM(COCOA)
150    virtual PlatformLayer* platformLayer() const override;
151#endif
152    virtual JSC::JSObject* scriptObject(JSC::JSGlobalObject*) override;
153    virtual void storageBlockingStateChanged() override;
154    virtual void privateBrowsingStateChanged(bool) override;
155    virtual bool getFormValue(String&) override;
156    virtual bool scroll(WebCore::ScrollDirection, WebCore::ScrollGranularity) override;
157    virtual WebCore::Scrollbar* horizontalScrollbar() override;
158    virtual WebCore::Scrollbar* verticalScrollbar() override;
159    virtual bool wantsWheelEvents() override;
160    virtual bool shouldAlwaysAutoStart() const override;
161    virtual void beginSnapshottingRunningPlugin() override;
162    virtual bool shouldAllowNavigationFromDrags() const override;
163    virtual bool shouldNotAddLayer() const override;
164
165    // WebCore::Widget
166    virtual void setFrameRect(const WebCore::IntRect&) override;
167    virtual void paint(WebCore::GraphicsContext*, const WebCore::IntRect&) override;
168    virtual void invalidateRect(const WebCore::IntRect&) override;
169    virtual void setFocus(bool) override;
170    virtual void frameRectsChanged() override;
171    virtual void setParent(WebCore::ScrollView*) override;
172    virtual void handleEvent(WebCore::Event*) override;
173    virtual void notifyWidget(WebCore::WidgetNotification) override;
174    virtual void show() override;
175    virtual void hide() override;
176    virtual void setParentVisible(bool) override;
177    virtual bool transformsAffectFrameRect() override;
178    virtual void clipRectChanged() override;
179
180    // WebCore::MediaCanStartListener
181    virtual void mediaCanStart() override;
182
183    // PluginController
184    virtual bool isPluginVisible() override;
185    virtual void invalidate(const WebCore::IntRect&) override;
186    virtual String userAgent() override;
187    virtual 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) override;
188    virtual void cancelStreamLoad(uint64_t streamID) override;
189    virtual void cancelManualStreamLoad() override;
190#if ENABLE(NETSCAPE_PLUGIN_API)
191    virtual NPObject* windowScriptNPObject() override;
192    virtual NPObject* pluginElementNPObject() override;
193    virtual bool evaluate(NPObject*, const String& scriptString, NPVariant* result, bool allowPopups) override;
194#endif
195    virtual void setStatusbarText(const String&) override;
196    virtual bool isAcceleratedCompositingEnabled() override;
197    virtual void pluginProcessCrashed() override;
198    virtual void willSendEventToPlugin() override;
199#if PLATFORM(COCOA)
200    virtual void pluginFocusOrWindowFocusChanged(bool pluginHasFocusAndWindowHasFocus) override;
201    virtual void setComplexTextInputState(PluginComplexTextInputState) override;
202    virtual mach_port_t compositingRenderServerPort() override;
203    virtual void openPluginPreferencePane() override;
204#endif
205    virtual float contentsScaleFactor() override;
206    virtual String proxiesForURL(const String&) override;
207    virtual String cookiesForURL(const String&) override;
208    virtual void setCookiesForURL(const String& urlString, const String& cookieString) override;
209    virtual bool getAuthenticationInfo(const WebCore::ProtectionSpace&, String& username, String& password) override;
210    virtual bool isPrivateBrowsingEnabled() override;
211    virtual bool asynchronousPluginInitializationEnabled() const override;
212    virtual bool asynchronousPluginInitializationEnabledForAllPlugins() const override;
213    virtual bool artificialPluginInitializationDelayEnabled() const override;
214    virtual void protectPluginFromDestruction() override;
215    virtual void unprotectPluginFromDestruction() override;
216#if PLUGIN_ARCHITECTURE(X11)
217    virtual uint64_t createPluginContainer() override;
218    virtual void windowedPluginGeometryDidChange(const WebCore::IntRect& frameRect, const WebCore::IntRect& clipRect, uint64_t windowID) override;
219    virtual void windowedPluginVisibilityDidChange(bool isVisible, uint64_t windowID) override;
220#endif
221
222    virtual void didInitializePlugin() override;
223    virtual void didFailToInitializePlugin() override;
224    void destroyPluginAndReset();
225
226    // WebFrame::LoadListener
227    virtual void didFinishLoad(WebFrame*) override;
228    virtual void didFailLoad(WebFrame*, bool wasCancelled) override;
229
230    std::unique_ptr<WebEvent> createWebEvent(WebCore::MouseEvent*) const;
231
232    RefPtr<WebCore::HTMLPlugInElement> m_pluginElement;
233    RefPtr<Plugin> m_plugin;
234    WebPage* m_webPage;
235    Plugin::Parameters m_parameters;
236
237    bool m_isInitialized;
238    bool m_isWaitingForSynchronousInitialization;
239    bool m_isWaitingUntilMediaCanStart;
240    bool m_isBeingDestroyed;
241    bool m_pluginProcessHasCrashed;
242    bool m_didPlugInStartOffScreen;
243
244    // Pending URLRequests that the plug-in has made.
245    Deque<RefPtr<URLRequest>> m_pendingURLRequests;
246    RunLoop::Timer<PluginView> m_pendingURLRequestsTimer;
247
248    // Pending frame loads that the plug-in has made.
249    typedef HashMap<RefPtr<WebFrame>, RefPtr<URLRequest>> FrameLoadMap;
250    FrameLoadMap m_pendingFrameLoads;
251
252    // Streams that the plug-in has requested to load.
253    HashMap<uint64_t, RefPtr<Stream>> m_streams;
254
255#if ENABLE(NETSCAPE_PLUGIN_API)
256    // A map of all related NPObjects for this plug-in view.
257    NPRuntimeObjectMap m_npRuntimeObjectMap;
258#endif
259
260    // The manual stream state. This is used so we can deliver a manual stream to a plug-in
261    // when it is initialized.
262    enum ManualStreamState {
263        StreamStateInitial,
264        StreamStateHasReceivedResponse,
265        StreamStateFinished,
266        StreamStateFailed
267    };
268    ManualStreamState m_manualStreamState;
269
270    WebCore::ResourceResponse m_manualStreamResponse;
271    WebCore::ResourceError m_manualStreamError;
272    RefPtr<WebCore::SharedBuffer> m_manualStreamData;
273
274    // This snapshot is used to avoid side effects should the plugin run JS during painting.
275    RefPtr<ShareableBitmap> m_transientPaintingSnapshot;
276    // This timer is used when plugin snapshotting is enabled, to capture a plugin placeholder.
277    WebCore::DeferrableOneShotTimer m_pluginSnapshotTimer;
278    unsigned m_countSnapshotRetries;
279    bool m_didReceiveUserInteraction;
280
281    double m_pageScaleFactor;
282};
283
284} // namespace WebKit
285
286#endif // PluginView_h
287