1/*
2 * Copyright (C) 2010, 2012, 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#include "config.h"
27#include "PluginView.h"
28
29#include "NPRuntimeUtilities.h"
30#include "Plugin.h"
31#include "ShareableBitmap.h"
32#include "WebCoreArgumentCoders.h"
33#include "WebEvent.h"
34#include "WebLoaderStrategy.h"
35#include "WebPage.h"
36#include "WebPageProxyMessages.h"
37#include "WebProcess.h"
38#include <WebCore/BitmapImage.h>
39#include <WebCore/Chrome.h>
40#include <WebCore/CookieJar.h>
41#include <WebCore/Credential.h>
42#include <WebCore/CredentialStorage.h>
43#include <WebCore/DocumentLoader.h>
44#include <WebCore/EventHandler.h>
45#include <WebCore/EventNames.h>
46#include <WebCore/FocusController.h>
47#include <WebCore/Frame.h>
48#include <WebCore/FrameLoadRequest.h>
49#include <WebCore/FrameLoader.h>
50#include <WebCore/FrameLoaderClient.h>
51#include <WebCore/FrameView.h>
52#include <WebCore/GraphicsContext.h>
53#include <WebCore/HTMLPlugInElement.h>
54#include <WebCore/HTMLPlugInImageElement.h>
55#include <WebCore/HTTPHeaderNames.h>
56#include <WebCore/HostWindow.h>
57#include <WebCore/MIMETypeRegistry.h>
58#include <WebCore/MouseEvent.h>
59#include <WebCore/NetscapePlugInStreamLoader.h>
60#include <WebCore/NetworkStorageSession.h>
61#include <WebCore/Page.h>
62#include <WebCore/PlatformMouseEvent.h>
63#include <WebCore/ProtectionSpace.h>
64#include <WebCore/ProxyServer.h>
65#include <WebCore/RenderEmbeddedObject.h>
66#include <WebCore/ScriptController.h>
67#include <WebCore/ScrollView.h>
68#include <WebCore/SecurityOrigin.h>
69#include <WebCore/SecurityPolicy.h>
70#include <WebCore/Settings.h>
71#include <WebCore/TextEncoding.h>
72#include <WebCore/UserGestureIndicator.h>
73#include <wtf/CompletionHandler.h>
74#include <wtf/text/StringBuilder.h>
75
76#if PLATFORM(X11)
77#include <WebCore/PlatformDisplay.h>
78#endif
79
80namespace WebKit {
81using namespace JSC;
82using namespace WebCore;
83
84// This simulated mouse click delay in HTMLPlugInImageElement.cpp should generally be the same or shorter than this delay.
85static const Seconds pluginSnapshotTimerDelay { 1100_ms };
86
87class PluginView::URLRequest : public RefCounted<URLRequest> {
88public:
89 static Ref<PluginView::URLRequest> create(uint64_t requestID, FrameLoadRequest&& request, bool allowPopups)
90 {
91 return adoptRef(*new URLRequest(requestID, WTFMove(request), allowPopups));
92 }
93
94 uint64_t requestID() const { return m_requestID; }
95 const String& target() const { return m_request.frameName(); }
96 const ResourceRequest & request() const { return m_request.resourceRequest(); }
97 bool allowPopups() const { return m_allowPopups; }
98
99private:
100 URLRequest(uint64_t requestID, FrameLoadRequest&& request, bool allowPopups)
101 : m_requestID { requestID }
102 , m_request { WTFMove(request) }
103 , m_allowPopups { allowPopups }
104 {
105 }
106
107 uint64_t m_requestID;
108 FrameLoadRequest m_request;
109 bool m_allowPopups;
110};
111
112class PluginView::Stream : public RefCounted<PluginView::Stream>, NetscapePlugInStreamLoaderClient {
113public:
114 static Ref<Stream> create(PluginView* pluginView, uint64_t streamID, const ResourceRequest& request)
115 {
116 return adoptRef(*new Stream(pluginView, streamID, request));
117 }
118 ~Stream();
119
120 void start();
121 void cancel();
122 void continueLoad();
123
124 uint64_t streamID() const { return m_streamID; }
125
126private:
127 Stream(PluginView* pluginView, uint64_t streamID, const ResourceRequest& request)
128 : m_pluginView(pluginView)
129 , m_streamID(streamID)
130 , m_request(request)
131 , m_streamWasCancelled(false)
132 {
133 }
134
135 // NetscapePluginStreamLoaderClient
136 void willSendRequest(NetscapePlugInStreamLoader*, ResourceRequest&&, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&&) override;
137 void didReceiveResponse(NetscapePlugInStreamLoader*, const ResourceResponse&) override;
138 void didReceiveData(NetscapePlugInStreamLoader*, const char*, int) override;
139 void didFail(NetscapePlugInStreamLoader*, const ResourceError&) override;
140 void didFinishLoading(NetscapePlugInStreamLoader*) override;
141
142 PluginView* m_pluginView;
143 uint64_t m_streamID;
144 ResourceRequest m_request;
145 CompletionHandler<void(ResourceRequest&&)> m_loadCallback;
146
147 // True if the stream was explicitly cancelled by calling cancel().
148 // (As opposed to being cancelled by the user hitting the stop button for example.
149 bool m_streamWasCancelled;
150
151 RefPtr<NetscapePlugInStreamLoader> m_loader;
152};
153
154PluginView::Stream::~Stream()
155{
156 if (m_loadCallback)
157 m_loadCallback({ });
158 ASSERT(!m_pluginView);
159}
160
161void PluginView::Stream::start()
162{
163 ASSERT(m_pluginView->m_plugin);
164 ASSERT(!m_loader);
165
166 Frame* frame = m_pluginView->m_pluginElement->document().frame();
167 ASSERT(frame);
168
169 WebProcess::singleton().webLoaderStrategy().schedulePluginStreamLoad(*frame, *this, ResourceRequest {m_request}, [this, protectedThis = makeRef(*this)](RefPtr<NetscapePlugInStreamLoader>&& loader) {
170 m_loader = WTFMove(loader);
171 });
172}
173
174void PluginView::Stream::cancel()
175{
176 ASSERT(m_loader);
177
178 m_streamWasCancelled = true;
179 m_loader->cancel(m_loader->cancelledError());
180 m_loader = nullptr;
181}
182
183void PluginView::Stream::continueLoad()
184{
185 ASSERT(m_pluginView->m_plugin);
186 ASSERT(m_loadCallback);
187
188 m_loadCallback(ResourceRequest(m_request));
189}
190
191static String buildHTTPHeaders(const ResourceResponse& response, long long& expectedContentLength)
192{
193 if (!response.isHTTP())
194 return String();
195
196 StringBuilder header;
197 header.appendLiteral("HTTP ");
198 header.appendNumber(response.httpStatusCode());
199 header.append(' ');
200 header.append(response.httpStatusText());
201 header.append('\n');
202 for (auto& field : response.httpHeaderFields()) {
203 header.append(field.key);
204 header.appendLiteral(": ");
205 header.append(field.value);
206 header.append('\n');
207 }
208
209 // If the content is encoded (most likely compressed), then don't send its length to the plugin,
210 // which is only interested in the decoded length, not yet known at the moment.
211 // <rdar://problem/4470599> tracks a request for -[NSURLResponse expectedContentLength] to incorporate this logic.
212 String contentEncoding = response.httpHeaderField(HTTPHeaderName::ContentEncoding);
213 if (!contentEncoding.isNull() && contentEncoding != "identity")
214 expectedContentLength = -1;
215
216 return header.toString();
217}
218
219static uint32_t lastModifiedDateMS(const ResourceResponse& response)
220{
221 auto lastModified = response.lastModified();
222 if (!lastModified)
223 return 0;
224
225 return lastModified.value().secondsSinceEpoch().millisecondsAs<uint32_t>();
226}
227
228void PluginView::Stream::willSendRequest(NetscapePlugInStreamLoader*, ResourceRequest&& request, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&& decisionHandler)
229{
230 const URL& requestURL = request.url();
231 const URL& redirectResponseURL = redirectResponse.url();
232
233 m_loadCallback = WTFMove(decisionHandler);
234 m_request = request;
235 m_pluginView->m_plugin->streamWillSendRequest(m_streamID, requestURL, redirectResponseURL, redirectResponse.httpStatusCode());
236}
237
238void PluginView::Stream::didReceiveResponse(NetscapePlugInStreamLoader*, const ResourceResponse& response)
239{
240 // Compute the stream related data from the resource response.
241 const URL& responseURL = response.url();
242 const String& mimeType = response.mimeType();
243 long long expectedContentLength = response.expectedContentLength();
244
245 String headers = buildHTTPHeaders(response, expectedContentLength);
246
247 uint32_t streamLength = 0;
248 if (expectedContentLength > 0)
249 streamLength = expectedContentLength;
250
251 m_pluginView->m_plugin->streamDidReceiveResponse(m_streamID, responseURL, streamLength, lastModifiedDateMS(response), mimeType, headers, response.suggestedFilename());
252}
253
254void PluginView::Stream::didReceiveData(NetscapePlugInStreamLoader*, const char* bytes, int length)
255{
256 m_pluginView->m_plugin->streamDidReceiveData(m_streamID, bytes, length);
257}
258
259void PluginView::Stream::didFail(NetscapePlugInStreamLoader*, const ResourceError& error)
260{
261 // Calling streamDidFail could cause us to be deleted, so we hold on to a reference here.
262 Ref<Stream> protect(*this);
263
264 // We only want to call streamDidFail if the stream was not explicitly cancelled by the plug-in.
265 if (!m_streamWasCancelled)
266 m_pluginView->m_plugin->streamDidFail(m_streamID, error.isCancellation());
267
268 m_pluginView->removeStream(this);
269 m_pluginView = 0;
270}
271
272void PluginView::Stream::didFinishLoading(NetscapePlugInStreamLoader*)
273{
274 // Calling streamDidFinishLoading could cause us to be deleted, so we hold on to a reference here.
275 Ref<Stream> protect(*this);
276
277#if ENABLE(NETSCAPE_PLUGIN_API)
278 // Protect the plug-in while we're calling into it.
279 NPRuntimeObjectMap::PluginProtector pluginProtector(&m_pluginView->m_npRuntimeObjectMap);
280#endif
281 m_pluginView->m_plugin->streamDidFinishLoading(m_streamID);
282
283 m_pluginView->removeStream(this);
284 m_pluginView = 0;
285}
286
287static inline WebPage* webPage(HTMLPlugInElement* pluginElement)
288{
289 Frame* frame = pluginElement->document().frame();
290 ASSERT(frame);
291
292 WebFrame* webFrame = WebFrame::fromCoreFrame(*frame);
293 if (!webFrame)
294 return nullptr;
295
296 return webFrame->page();
297}
298
299Ref<PluginView> PluginView::create(HTMLPlugInElement& pluginElement, Ref<Plugin>&& plugin, const Plugin::Parameters& parameters)
300{
301 return adoptRef(*new PluginView(pluginElement, WTFMove(plugin), parameters));
302}
303
304PluginView::PluginView(HTMLPlugInElement& pluginElement, Ref<Plugin>&& plugin, const Plugin::Parameters& parameters)
305 : PluginViewBase(0)
306 , m_pluginElement(&pluginElement)
307 , m_plugin(WTFMove(plugin))
308 , m_webPage(webPage(m_pluginElement.get()))
309 , m_parameters(parameters)
310 , m_pendingURLRequestsTimer(RunLoop::main(), this, &PluginView::pendingURLRequestsTimerFired)
311 , m_pluginSnapshotTimer(*this, &PluginView::pluginSnapshotTimerFired, pluginSnapshotTimerDelay)
312{
313 m_webPage->addPluginView(this);
314}
315
316PluginView::~PluginView()
317{
318 if (m_webPage)
319 m_webPage->removePluginView(this);
320
321 ASSERT(!m_plugin || !m_plugin->isBeingDestroyed());
322
323 if (m_isWaitingUntilMediaCanStart)
324 m_pluginElement->document().removeMediaCanStartListener(*this);
325
326 m_pluginElement->document().removeAudioProducer(*this);
327
328 destroyPluginAndReset();
329
330 // Null out the plug-in element explicitly so we'll crash earlier if we try to use
331 // the plug-in view after it's been destroyed.
332 m_pluginElement = nullptr;
333}
334
335void PluginView::destroyPluginAndReset()
336{
337 // Cancel all pending frame loads.
338 for (FrameLoadMap::iterator it = m_pendingFrameLoads.begin(), end = m_pendingFrameLoads.end(); it != end; ++it)
339 it->key->setLoadListener(0);
340
341 if (m_plugin) {
342 m_plugin->destroyPlugin();
343
344 m_pendingURLRequests.clear();
345 m_pendingURLRequestsTimer.stop();
346
347#if PLATFORM(COCOA)
348 if (m_webPage)
349 pluginFocusOrWindowFocusChanged(false);
350#endif
351 }
352
353#if ENABLE(NETSCAPE_PLUGIN_API)
354 // Invalidate the object map.
355 m_npRuntimeObjectMap.invalidate();
356#endif
357
358 cancelAllStreams();
359}
360
361void PluginView::recreateAndInitialize(Ref<Plugin>&& plugin)
362{
363 if (m_plugin) {
364 if (m_pluginSnapshotTimer.isActive())
365 m_pluginSnapshotTimer.stop();
366 destroyPluginAndReset();
367 }
368
369 m_plugin = WTFMove(plugin);
370 m_isInitialized = false;
371 m_isWaitingForSynchronousInitialization = false;
372 m_isWaitingUntilMediaCanStart = false;
373 m_manualStreamState = ManualStreamState::Initial;
374 m_transientPaintingSnapshot = nullptr;
375
376 initializePlugin();
377}
378
379void PluginView::setLayerHostingMode(LayerHostingMode layerHostingMode)
380{
381#if HAVE(OUT_OF_PROCESS_LAYER_HOSTING)
382 if (!m_plugin)
383 return;
384
385 if (m_isInitialized)
386 m_plugin->setLayerHostingMode(layerHostingMode);
387 else
388 m_parameters.layerHostingMode = layerHostingMode;
389#else
390 UNUSED_PARAM(layerHostingMode);
391#endif
392}
393
394Frame* PluginView::frame() const
395{
396 return m_pluginElement->document().frame();
397}
398
399void PluginView::manualLoadDidReceiveResponse(const ResourceResponse& response)
400{
401 // The plug-in can be null here if it failed to initialize.
402 if (!m_plugin)
403 return;
404
405 if (!m_isInitialized) {
406 ASSERT(m_manualStreamState == ManualStreamState::Initial);
407 m_manualStreamState = ManualStreamState::HasReceivedResponse;
408 m_manualStreamResponse = response;
409 return;
410 }
411
412 // Compute the stream related data from the resource response.
413 const URL& responseURL = response.url();
414 const String& mimeType = response.mimeType();
415 long long expectedContentLength = response.expectedContentLength();
416
417 String headers = buildHTTPHeaders(response, expectedContentLength);
418
419 uint32_t streamLength = 0;
420 if (expectedContentLength > 0)
421 streamLength = expectedContentLength;
422
423 m_plugin->manualStreamDidReceiveResponse(responseURL, streamLength, lastModifiedDateMS(response), mimeType, headers, response.suggestedFilename());
424}
425
426void PluginView::manualLoadDidReceiveData(const char* bytes, int length)
427{
428 // The plug-in can be null here if it failed to initialize.
429 if (!m_plugin)
430 return;
431
432 if (!m_isInitialized) {
433 ASSERT(m_manualStreamState == ManualStreamState::HasReceivedResponse);
434 if (!m_manualStreamData)
435 m_manualStreamData = SharedBuffer::create();
436
437 m_manualStreamData->append(bytes, length);
438 return;
439 }
440
441 m_plugin->manualStreamDidReceiveData(bytes, length);
442}
443
444void PluginView::manualLoadDidFinishLoading()
445{
446 // The plug-in can be null here if it failed to initialize.
447 if (!m_plugin)
448 return;
449
450 if (!m_isInitialized) {
451 ASSERT(m_manualStreamState == ManualStreamState::HasReceivedResponse);
452 m_manualStreamState = ManualStreamState::Finished;
453 return;
454 }
455
456 m_plugin->manualStreamDidFinishLoading();
457}
458
459void PluginView::manualLoadDidFail(const ResourceError& error)
460{
461 // The plug-in can be null here if it failed to initialize.
462 if (!m_plugin)
463 return;
464
465 if (!m_isInitialized) {
466 m_manualStreamState = ManualStreamState::Finished;
467 m_manualStreamError = error;
468 m_manualStreamData = nullptr;
469 return;
470 }
471
472 m_plugin->manualStreamDidFail(error.isCancellation());
473}
474
475void PluginView::pageScaleFactorDidChange()
476{
477 viewGeometryDidChange();
478}
479
480void PluginView::topContentInsetDidChange()
481{
482 viewGeometryDidChange();
483}
484
485void PluginView::setPageScaleFactor(double scaleFactor, IntPoint)
486{
487 m_pageScaleFactor = scaleFactor;
488 m_webPage->send(Messages::WebPageProxy::PluginScaleFactorDidChange(scaleFactor));
489 m_webPage->send(Messages::WebPageProxy::PluginZoomFactorDidChange(scaleFactor));
490 pageScaleFactorDidChange();
491}
492
493double PluginView::pageScaleFactor() const
494{
495 return m_pageScaleFactor;
496}
497
498bool PluginView::handlesPageScaleFactor() const
499{
500 if (!m_plugin || !m_isInitialized)
501 return false;
502
503 return m_plugin->handlesPageScaleFactor();
504}
505
506bool PluginView::requiresUnifiedScaleFactor() const
507{
508 if (!m_plugin || !m_isInitialized)
509 return false;
510
511 return m_plugin->requiresUnifiedScaleFactor();
512}
513
514void PluginView::webPageDestroyed()
515{
516 m_webPage = 0;
517}
518
519void PluginView::activityStateDidChange(OptionSet<WebCore::ActivityState::Flag> changed)
520{
521 if (!m_plugin || !m_isInitialized)
522 return;
523
524 if (changed & ActivityState::IsVisibleOrOccluded)
525 m_plugin->windowVisibilityChanged(m_webPage->isVisibleOrOccluded());
526 if (changed & ActivityState::WindowIsActive)
527 m_plugin->windowFocusChanged(m_webPage->windowIsFocused());
528}
529
530#if PLATFORM(COCOA)
531void PluginView::setDeviceScaleFactor(float scaleFactor)
532{
533 if (!m_isInitialized || !m_plugin)
534 return;
535
536 m_plugin->contentsScaleFactorChanged(scaleFactor);
537}
538
539void PluginView::windowAndViewFramesChanged(const FloatRect& windowFrameInScreenCoordinates, const FloatRect& viewFrameInWindowCoordinates)
540{
541 if (!m_isInitialized || !m_plugin)
542 return;
543
544 m_plugin->windowAndViewFramesChanged(enclosingIntRect(windowFrameInScreenCoordinates), enclosingIntRect(viewFrameInWindowCoordinates));
545}
546
547bool PluginView::sendComplexTextInput(uint64_t pluginComplexTextInputIdentifier, const String& textInput)
548{
549 if (!m_plugin)
550 return false;
551
552 if (m_plugin->pluginComplexTextInputIdentifier() != pluginComplexTextInputIdentifier)
553 return false;
554
555 m_plugin->sendComplexTextInput(textInput);
556 return true;
557}
558
559id PluginView::accessibilityAssociatedPluginParentForElement(Element* element) const
560{
561 if (!m_plugin)
562 return nil;
563 return m_plugin->accessibilityAssociatedPluginParentForElement(element);
564}
565
566NSObject *PluginView::accessibilityObject() const
567{
568 if (!m_isInitialized || !m_plugin)
569 return 0;
570
571 return m_plugin->accessibilityObject();
572}
573#endif
574
575void PluginView::initializePlugin()
576{
577 if (m_isInitialized)
578 return;
579
580 if (!m_plugin) {
581 // We've already tried and failed to initialize the plug-in.
582 return;
583 }
584
585 if (Frame* frame = m_pluginElement->document().frame()) {
586 if (Page* page = frame->page()) {
587
588 // We shouldn't initialize the plug-in right now, add a listener.
589 if (!page->canStartMedia()) {
590 if (m_isWaitingUntilMediaCanStart)
591 return;
592
593 m_isWaitingUntilMediaCanStart = true;
594 m_pluginElement->document().addMediaCanStartListener(*this);
595 return;
596 }
597 }
598 }
599
600 m_pluginElement->document().addAudioProducer(*this);
601
602#if ENABLE(PRIMARY_SNAPSHOTTED_PLUGIN_HEURISTIC)
603 HTMLPlugInImageElement& plugInImageElement = downcast<HTMLPlugInImageElement>(*m_pluginElement);
604 m_didPlugInStartOffScreen = !m_webPage->plugInIntersectsSearchRect(plugInImageElement);
605#endif
606 m_plugin->initialize(this, m_parameters);
607
608 // Plug-in initialization continued in didFailToInitializePlugin() or didInitializePlugin().
609}
610
611void PluginView::didFailToInitializePlugin()
612{
613 m_plugin = nullptr;
614
615#if ENABLE(NETSCAPE_PLUGIN_API)
616 String frameURLString = frame()->loader().documentLoader()->responseURL().string();
617 String pageURLString = m_webPage->corePage()->mainFrame().loader().documentLoader()->responseURL().string();
618 m_webPage->send(Messages::WebPageProxy::DidFailToInitializePlugin(m_parameters.mimeType, frameURLString, pageURLString));
619#endif // ENABLE(NETSCAPE_PLUGIN_API)
620}
621
622void PluginView::didInitializePlugin()
623{
624 m_isInitialized = true;
625
626#if PLATFORM(COCOA)
627 windowAndViewFramesChanged(m_webPage->windowFrameInScreenCoordinates(), m_webPage->viewFrameInWindowCoordinates());
628#endif
629
630 viewVisibilityDidChange();
631 viewGeometryDidChange();
632
633 if (m_pluginElement->document().focusedElement() == m_pluginElement)
634 m_plugin->setFocus(true);
635
636 redeliverManualStream();
637
638#if PLATFORM(COCOA)
639 if (m_pluginElement->displayState() < HTMLPlugInElement::Restarting) {
640 if (m_plugin->pluginLayer() && frame()) {
641 frame()->view()->enterCompositingMode();
642 m_pluginElement->invalidateStyleAndLayerComposition();
643 }
644 if (frame() && !frame()->settings().maximumPlugInSnapshotAttempts()) {
645 beginSnapshottingRunningPlugin();
646 return;
647 }
648 m_pluginSnapshotTimer.restart();
649 } else {
650 if (m_plugin->pluginLayer() && frame()) {
651 frame()->view()->enterCompositingMode();
652 m_pluginElement->invalidateStyleAndLayerComposition();
653 }
654 if (m_pluginElement->displayState() == HTMLPlugInElement::RestartingWithPendingMouseClick)
655 m_pluginElement->dispatchPendingMouseClick();
656 }
657
658 m_plugin->visibilityDidChange(isVisible());
659 m_plugin->windowVisibilityChanged(m_webPage->isVisibleOrOccluded());
660 m_plugin->windowFocusChanged(m_webPage->windowIsFocused());
661#endif
662
663 if (wantsWheelEvents()) {
664 if (Frame* frame = m_pluginElement->document().frame()) {
665 if (FrameView* frameView = frame->view())
666 frameView->setNeedsLayoutAfterViewConfigurationChange();
667 }
668 }
669
670 if (Frame* frame = m_pluginElement->document().frame()) {
671 auto* webFrame = WebFrame::fromCoreFrame(*frame);
672 if (webFrame->isMainFrame())
673 webFrame->page()->send(Messages::WebPageProxy::MainFramePluginHandlesPageScaleGestureDidChange(handlesPageScaleFactor()));
674 }
675}
676
677#if PLATFORM(COCOA)
678PlatformLayer* PluginView::platformLayer() const
679{
680 // The plug-in can be null here if it failed to initialize.
681 if (!m_isInitialized || !m_plugin || m_pluginProcessHasCrashed)
682 return 0;
683
684 return m_plugin->pluginLayer();
685}
686#endif
687
688JSObject* PluginView::scriptObject(JSGlobalObject* globalObject)
689{
690 // If we're already waiting for synchronous initialization of the plugin,
691 // calls to scriptObject() are from the plug-in itself and need to return 0;
692 if (m_isWaitingForSynchronousInitialization)
693 return 0;
694
695 // We might not have started initialization of the plug-in yet, the plug-in might be in the middle
696 // of being initializing asynchronously, or initialization might have previously failed.
697 if (!m_isInitialized || !m_plugin)
698 return 0;
699
700#if ENABLE(NETSCAPE_PLUGIN_API)
701 NPObject* scriptableNPObject = m_plugin->pluginScriptableNPObject();
702 if (!scriptableNPObject)
703 return 0;
704
705 JSObject* jsObject = m_npRuntimeObjectMap.getOrCreateJSObject(globalObject, scriptableNPObject);
706 releaseNPObject(scriptableNPObject);
707
708 return jsObject;
709#else
710 UNUSED_PARAM(globalObject);
711 return 0;
712#endif
713}
714
715void PluginView::storageBlockingStateChanged()
716{
717 // The plug-in can be null here if it failed to initialize.
718 if (!m_isInitialized || !m_plugin)
719 return;
720
721 bool storageBlockingPolicy = !frame()->document()->securityOrigin().canAccessPluginStorage(frame()->document()->topOrigin());
722
723 m_plugin->storageBlockingStateChanged(storageBlockingPolicy);
724}
725
726void PluginView::privateBrowsingStateChanged(bool privateBrowsingEnabled)
727{
728 // The plug-in can be null here if it failed to initialize.
729 if (!m_isInitialized || !m_plugin)
730 return;
731
732 m_plugin->privateBrowsingStateChanged(privateBrowsingEnabled);
733}
734
735bool PluginView::getFormValue(String& formValue)
736{
737 // The plug-in can be null here if it failed to initialize.
738 if (!m_isInitialized || !m_plugin)
739 return false;
740
741 return m_plugin->getFormValue(formValue);
742}
743
744bool PluginView::scroll(ScrollDirection direction, ScrollGranularity granularity)
745{
746 // The plug-in can be null here if it failed to initialize.
747 if (!m_isInitialized || !m_plugin)
748 return false;
749
750 return m_plugin->handleScroll(direction, granularity);
751}
752
753Scrollbar* PluginView::horizontalScrollbar()
754{
755 // The plug-in can be null here if it failed to initialize.
756 if (!m_isInitialized || !m_plugin)
757 return 0;
758
759 return m_plugin->horizontalScrollbar();
760}
761
762Scrollbar* PluginView::verticalScrollbar()
763{
764 // The plug-in can be null here if it failed to initialize.
765 if (!m_isInitialized || !m_plugin)
766 return 0;
767
768 return m_plugin->verticalScrollbar();
769}
770
771bool PluginView::wantsWheelEvents()
772{
773 // The plug-in can be null here if it failed to initialize.
774 if (!m_isInitialized || !m_plugin)
775 return 0;
776
777 return m_plugin->wantsWheelEvents();
778}
779
780void PluginView::setFrameRect(const WebCore::IntRect& rect)
781{
782 Widget::setFrameRect(rect);
783 viewGeometryDidChange();
784}
785
786void PluginView::paint(GraphicsContext& context, const IntRect& /*dirtyRect*/, Widget::SecurityOriginPaintPolicy)
787{
788 if (!m_plugin || !m_isInitialized || m_pluginElement->displayState() < HTMLPlugInElement::Restarting)
789 return;
790
791 if (context.paintingDisabled()) {
792 if (context.invalidatingControlTints())
793 m_plugin->updateControlTints(context);
794 return;
795 }
796
797 // FIXME: We should try to intersect the dirty rect with the plug-in's clip rect here.
798 IntRect paintRect = IntRect(IntPoint(), frameRect().size());
799
800 if (paintRect.isEmpty())
801 return;
802
803 if (m_transientPaintingSnapshot) {
804 m_transientPaintingSnapshot->paint(context, contentsScaleFactor(), frameRect().location(), m_transientPaintingSnapshot->bounds());
805 return;
806 }
807
808 GraphicsContextStateSaver stateSaver(context);
809
810 // Translate the coordinate system so that the origin is in the top-left corner of the plug-in.
811 context.translate(frameRect().location().x(), frameRect().location().y());
812
813 m_plugin->paint(context, paintRect);
814}
815
816void PluginView::frameRectsChanged()
817{
818 Widget::frameRectsChanged();
819 viewGeometryDidChange();
820}
821
822void PluginView::clipRectChanged()
823{
824 viewGeometryDidChange();
825}
826
827void PluginView::setParent(ScrollView* scrollView)
828{
829 Widget::setParent(scrollView);
830
831 if (scrollView)
832 initializePlugin();
833}
834
835unsigned PluginView::countFindMatches(const String& target, WebCore::FindOptions options, unsigned maxMatchCount)
836{
837 if (!m_isInitialized || !m_plugin)
838 return 0;
839
840 return m_plugin->countFindMatches(target, options, maxMatchCount);
841}
842
843bool PluginView::findString(const String& target, WebCore::FindOptions options, unsigned maxMatchCount)
844{
845 if (!m_isInitialized || !m_plugin)
846 return false;
847
848 return m_plugin->findString(target, options, maxMatchCount);
849}
850
851String PluginView::getSelectionString() const
852{
853 if (!m_isInitialized || !m_plugin)
854 return String();
855
856 return m_plugin->getSelectionString();
857}
858
859std::unique_ptr<WebEvent> PluginView::createWebEvent(MouseEvent& event) const
860{
861 WebEvent::Type type = WebEvent::NoType;
862 unsigned clickCount = 1;
863 if (event.type() == eventNames().mousedownEvent)
864 type = WebEvent::MouseDown;
865 else if (event.type() == eventNames().mouseupEvent)
866 type = WebEvent::MouseUp;
867 else if (event.type() == eventNames().mouseoverEvent) {
868 type = WebEvent::MouseMove;
869 clickCount = 0;
870 } else if (event.type() == eventNames().clickEvent)
871 return nullptr;
872 else
873 ASSERT_NOT_REACHED();
874
875 WebMouseEvent::Button button = WebMouseEvent::NoButton;
876 switch (event.button()) {
877 case WebCore::LeftButton:
878 button = WebMouseEvent::LeftButton;
879 break;
880 case WebCore::MiddleButton:
881 button = WebMouseEvent::MiddleButton;
882 break;
883 case WebCore::RightButton:
884 button = WebMouseEvent::RightButton;
885 break;
886 default:
887 ASSERT_NOT_REACHED();
888 break;
889 }
890
891 OptionSet<WebEvent::Modifier> modifiers;
892 if (event.shiftKey())
893 modifiers.add(WebEvent::Modifier::ShiftKey);
894 if (event.ctrlKey())
895 modifiers.add(WebEvent::Modifier::ControlKey);
896 if (event.altKey())
897 modifiers.add(WebEvent::Modifier::AltKey);
898 if (event.metaKey())
899 modifiers.add(WebEvent::Modifier::MetaKey);
900
901 return std::make_unique<WebMouseEvent>(type, button, event.buttons(), m_plugin->convertToRootView(IntPoint(event.offsetX(), event.offsetY())), event.screenLocation(), 0, 0, 0, clickCount, modifiers, WallTime { }, 0);
902}
903
904void PluginView::handleEvent(Event& event)
905{
906 if (!m_isInitialized || !m_plugin)
907 return;
908
909 const WebEvent* currentEvent = WebPage::currentEvent();
910 std::unique_ptr<WebEvent> simulatedWebEvent;
911 if (is<MouseEvent>(event) && downcast<MouseEvent>(event).isSimulated()) {
912 simulatedWebEvent = createWebEvent(downcast<MouseEvent>(event));
913 currentEvent = simulatedWebEvent.get();
914 }
915 if (!currentEvent)
916 return;
917
918 bool didHandleEvent = false;
919
920 if ((event.type() == eventNames().mousemoveEvent && currentEvent->type() == WebEvent::MouseMove)
921 || (event.type() == eventNames().mousedownEvent && currentEvent->type() == WebEvent::MouseDown)
922 || (event.type() == eventNames().mouseupEvent && currentEvent->type() == WebEvent::MouseUp)) {
923 // FIXME: Clicking in a scroll bar should not change focus.
924 if (currentEvent->type() == WebEvent::MouseDown) {
925 focusPluginElement();
926 frame()->eventHandler().setCapturingMouseEventsElement(m_pluginElement.get());
927 } else if (currentEvent->type() == WebEvent::MouseUp)
928 frame()->eventHandler().setCapturingMouseEventsElement(nullptr);
929
930 didHandleEvent = m_plugin->handleMouseEvent(static_cast<const WebMouseEvent&>(*currentEvent));
931 if (event.type() != eventNames().mousemoveEvent)
932 pluginDidReceiveUserInteraction();
933 } else if (eventNames().isWheelEventType(event.type())
934 && currentEvent->type() == WebEvent::Wheel && m_plugin->wantsWheelEvents()) {
935 didHandleEvent = m_plugin->handleWheelEvent(static_cast<const WebWheelEvent&>(*currentEvent));
936 pluginDidReceiveUserInteraction();
937 } else if (event.type() == eventNames().mouseoverEvent && currentEvent->type() == WebEvent::MouseMove)
938 didHandleEvent = m_plugin->handleMouseEnterEvent(static_cast<const WebMouseEvent&>(*currentEvent));
939 else if (event.type() == eventNames().mouseoutEvent && currentEvent->type() == WebEvent::MouseMove)
940 didHandleEvent = m_plugin->handleMouseLeaveEvent(static_cast<const WebMouseEvent&>(*currentEvent));
941 else if (event.type() == eventNames().contextmenuEvent && currentEvent->type() == WebEvent::MouseDown) {
942 didHandleEvent = m_plugin->handleContextMenuEvent(static_cast<const WebMouseEvent&>(*currentEvent));
943 pluginDidReceiveUserInteraction();
944 } else if ((event.type() == eventNames().keydownEvent && currentEvent->type() == WebEvent::KeyDown)
945 || (event.type() == eventNames().keyupEvent && currentEvent->type() == WebEvent::KeyUp)) {
946 didHandleEvent = m_plugin->handleKeyboardEvent(static_cast<const WebKeyboardEvent&>(*currentEvent));
947 pluginDidReceiveUserInteraction();
948 }
949
950 if (didHandleEvent)
951 event.setDefaultHandled();
952}
953
954bool PluginView::handleEditingCommand(const String& commandName, const String& argument)
955{
956 if (!m_isInitialized || !m_plugin)
957 return false;
958
959 return m_plugin->handleEditingCommand(commandName, argument);
960}
961
962bool PluginView::isEditingCommandEnabled(const String& commandName)
963{
964 if (!m_isInitialized || !m_plugin)
965 return false;
966
967 return m_plugin->isEditingCommandEnabled(commandName);
968}
969
970bool PluginView::shouldAllowScripting()
971{
972 if (!m_isInitialized || !m_plugin)
973 return false;
974
975 return m_plugin->shouldAllowScripting();
976}
977
978bool PluginView::shouldAllowNavigationFromDrags() const
979{
980 if (!m_isInitialized || !m_plugin)
981 return false;
982
983 return m_plugin->shouldAllowNavigationFromDrags();
984}
985
986bool PluginView::shouldNotAddLayer() const
987{
988 return m_pluginElement->displayState() < HTMLPlugInElement::Restarting && !m_plugin->supportsSnapshotting();
989}
990
991void PluginView::willDetachRenderer()
992{
993 if (!m_isInitialized || !m_plugin)
994 return;
995
996 m_plugin->willDetachRenderer();
997}
998
999RefPtr<SharedBuffer> PluginView::liveResourceData() const
1000{
1001 if (!m_isInitialized || !m_plugin) {
1002 if (m_manualStreamData && m_manualStreamState == ManualStreamState::Finished)
1003 return m_manualStreamData;
1004
1005 return nullptr;
1006 }
1007
1008 return m_plugin->liveResourceData();
1009}
1010
1011bool PluginView::performDictionaryLookupAtLocation(const WebCore::FloatPoint& point)
1012{
1013 if (!m_isInitialized || !m_plugin)
1014 return false;
1015
1016 return m_plugin->performDictionaryLookupAtLocation(point);
1017}
1018
1019String PluginView::getSelectionForWordAtPoint(const WebCore::FloatPoint& point) const
1020{
1021 if (!m_isInitialized || !m_plugin)
1022 return String();
1023
1024 return m_plugin->getSelectionForWordAtPoint(point);
1025}
1026
1027bool PluginView::existingSelectionContainsPoint(const WebCore::FloatPoint& point) const
1028{
1029 if (!m_isInitialized || !m_plugin)
1030 return false;
1031
1032 return m_plugin->existingSelectionContainsPoint(point);
1033}
1034
1035void PluginView::notifyWidget(WidgetNotification notification)
1036{
1037 switch (notification) {
1038 case WillPaintFlattened:
1039 if (shouldCreateTransientPaintingSnapshot())
1040 m_transientPaintingSnapshot = m_plugin->snapshot();
1041 break;
1042 case DidPaintFlattened:
1043 m_transientPaintingSnapshot = nullptr;
1044 break;
1045 }
1046}
1047
1048void PluginView::show()
1049{
1050 bool wasVisible = isVisible();
1051
1052 setSelfVisible(true);
1053
1054 if (!wasVisible)
1055 viewVisibilityDidChange();
1056
1057 Widget::show();
1058}
1059
1060void PluginView::hide()
1061{
1062 bool wasVisible = isVisible();
1063
1064 setSelfVisible(false);
1065
1066 if (wasVisible)
1067 viewVisibilityDidChange();
1068
1069 Widget::hide();
1070}
1071
1072void PluginView::setParentVisible(bool isVisible)
1073{
1074 if (isParentVisible() == isVisible)
1075 return;
1076
1077 Widget::setParentVisible(isVisible);
1078 viewVisibilityDidChange();
1079}
1080
1081bool PluginView::transformsAffectFrameRect()
1082{
1083 return false;
1084}
1085
1086void PluginView::viewGeometryDidChange()
1087{
1088 if (!m_isInitialized || !m_plugin || !parent())
1089 return;
1090
1091 ASSERT(frame());
1092 float pageScaleFactor = frame()->page() ? frame()->page()->pageScaleFactor() : 1;
1093
1094 IntPoint scaledFrameRectLocation(frameRect().location().x() * pageScaleFactor, frameRect().location().y() * pageScaleFactor);
1095 IntPoint scaledLocationInRootViewCoordinates(parent()->contentsToRootView(scaledFrameRectLocation));
1096
1097 // FIXME: We still don't get the right coordinates for transformed plugins.
1098 AffineTransform transform;
1099 transform.translate(scaledLocationInRootViewCoordinates.x(), scaledLocationInRootViewCoordinates.y());
1100 transform.scale(pageScaleFactor);
1101
1102 // FIXME: The way we calculate this clip rect isn't correct.
1103 // But it is still important to distinguish between empty and non-empty rects so we can notify the plug-in when it becomes invisible.
1104 // Making the rect actually correct is covered by https://bugs.webkit.org/show_bug.cgi?id=95362
1105 IntRect clipRect = boundsRect();
1106
1107 // FIXME: We can only get a semi-reliable answer from clipRectInWindowCoordinates() when the page is not scaled.
1108 // Fixing that is tracked in <rdar://problem/9026611> - Make the Widget hierarchy play nicely with transforms, for zoomed plug-ins and iframes
1109 if (pageScaleFactor == 1) {
1110 clipRect = clipRectInWindowCoordinates();
1111 if (!clipRect.isEmpty())
1112 clipRect = boundsRect();
1113 }
1114
1115 m_plugin->geometryDidChange(size(), clipRect, transform);
1116}
1117
1118void PluginView::viewVisibilityDidChange()
1119{
1120 if (!m_isInitialized || !m_plugin || !parent())
1121 return;
1122
1123 m_plugin->visibilityDidChange(isVisible());
1124}
1125
1126IntRect PluginView::clipRectInWindowCoordinates() const
1127{
1128 // Get the frame rect in window coordinates.
1129 IntRect frameRectInWindowCoordinates = parent()->contentsToWindow(frameRect());
1130
1131 Frame* frame = this->frame();
1132
1133 // Get the window clip rect for the plugin element (in window coordinates).
1134 IntRect windowClipRect = frame->view()->windowClipRectForFrameOwner(m_pluginElement.get(), true);
1135
1136 // Intersect the two rects to get the view clip rect in window coordinates.
1137 frameRectInWindowCoordinates.intersect(windowClipRect);
1138
1139 return frameRectInWindowCoordinates;
1140}
1141
1142void PluginView::focusPluginElement()
1143{
1144 ASSERT(frame());
1145
1146 if (Page* page = frame()->page())
1147 page->focusController().setFocusedElement(m_pluginElement.get(), *frame());
1148 else
1149 frame()->document()->setFocusedElement(m_pluginElement.get());
1150}
1151
1152void PluginView::pendingURLRequestsTimerFired()
1153{
1154 ASSERT(!m_pendingURLRequests.isEmpty());
1155
1156 RefPtr<URLRequest> urlRequest = m_pendingURLRequests.takeFirst();
1157
1158 // If there are more requests to perform, reschedule the timer.
1159 if (!m_pendingURLRequests.isEmpty())
1160 m_pendingURLRequestsTimer.startOneShot(0_s);
1161
1162 performURLRequest(urlRequest.get());
1163}
1164
1165void PluginView::performURLRequest(URLRequest* request)
1166{
1167 // This protector is needed to make sure the PluginView is not destroyed while it is still needed.
1168 Ref<PluginView> protect(*this);
1169
1170 // First, check if this is a javascript: url.
1171 if (WTF::protocolIsJavaScript(request->request().url())) {
1172 performJavaScriptURLRequest(request);
1173 return;
1174 }
1175
1176 if (!request->target().isNull()) {
1177 performFrameLoadURLRequest(request);
1178 return;
1179 }
1180
1181 // This request is to load a URL and create a stream.
1182 auto stream = PluginView::Stream::create(this, request->requestID(), request->request());
1183 addStream(stream.ptr());
1184 stream->start();
1185}
1186
1187void PluginView::performFrameLoadURLRequest(URLRequest* request)
1188{
1189 ASSERT(!request->target().isNull());
1190
1191 Frame* frame = m_pluginElement->document().frame();
1192 if (!frame)
1193 return;
1194
1195 if (!m_pluginElement->document().securityOrigin().canDisplay(request->request().url())) {
1196 // We can't load the request, send back a reply to the plug-in.
1197 m_plugin->frameDidFail(request->requestID(), false);
1198 return;
1199 }
1200
1201 UserGestureIndicator gestureIndicator(request->allowPopups() ? Optional<ProcessingUserGestureState>(ProcessingUserGesture) : WTF::nullopt);
1202
1203 // First, try to find a target frame.
1204 Frame* targetFrame = frame->loader().findFrameForNavigation(request->target());
1205 if (!targetFrame) {
1206 // We did not find a target frame. Ask our frame to load the page. This may or may not create a popup window.
1207 FrameLoadRequest frameLoadRequest { *frame, request->request(), ShouldOpenExternalURLsPolicy::ShouldNotAllow };
1208 frameLoadRequest.setFrameName(request->target());
1209 frameLoadRequest.setShouldCheckNewWindowPolicy(true);
1210 frame->loader().load(WTFMove(frameLoadRequest));
1211
1212 // FIXME: We don't know whether the window was successfully created here so we just assume that it worked.
1213 // It's better than not telling the plug-in anything.
1214 m_plugin->frameDidFinishLoading(request->requestID());
1215 return;
1216 }
1217
1218 // Now ask the frame to load the request.
1219 targetFrame->loader().load(FrameLoadRequest(*targetFrame, request->request(), ShouldOpenExternalURLsPolicy::ShouldNotAllow));
1220
1221 auto* targetWebFrame = WebFrame::fromCoreFrame(*targetFrame);
1222 ASSERT(targetWebFrame);
1223
1224 if (WebFrame::LoadListener* loadListener = targetWebFrame->loadListener()) {
1225 // Check if another plug-in view or even this view is waiting for the frame to load.
1226 // If it is, tell it that the load was cancelled because it will be anyway.
1227 loadListener->didFailLoad(targetWebFrame, true);
1228 }
1229
1230 m_pendingFrameLoads.set(targetWebFrame, request);
1231 targetWebFrame->setLoadListener(this);
1232}
1233
1234void PluginView::performJavaScriptURLRequest(URLRequest* request)
1235{
1236 ASSERT(WTF::protocolIsJavaScript(request->request().url()));
1237
1238 RefPtr<Frame> frame = m_pluginElement->document().frame();
1239 if (!frame)
1240 return;
1241
1242 String jsString = decodeURLEscapeSequences(request->request().url().string().substring(sizeof("javascript:") - 1));
1243
1244 if (!request->target().isNull()) {
1245 // For security reasons, only allow JS requests to be made on the frame that contains the plug-in.
1246 if (frame->tree().find(request->target(), *frame) != frame) {
1247 // Let the plug-in know that its frame load failed.
1248 m_plugin->frameDidFail(request->requestID(), false);
1249 return;
1250 }
1251 }
1252
1253 // Evaluate the JavaScript code. Note that running JavaScript here could cause the plug-in to be destroyed, so we
1254 // grab references to the plug-in here.
1255 RefPtr<Plugin> plugin = m_plugin;
1256 auto result = frame->script().executeScript(jsString, request->allowPopups());
1257
1258 if (!result)
1259 return;
1260
1261 // Check if evaluating the JavaScript destroyed the plug-in.
1262 if (!plugin->controller())
1263 return;
1264
1265 // Don't notify the plug-in at all about targeted javascript: requests. This matches Mozilla and WebKit1.
1266 if (!request->target().isNull())
1267 return;
1268
1269 ExecState* scriptState = frame->script().globalObject(pluginWorld())->globalExec();
1270 String resultString;
1271 result.getString(scriptState, resultString);
1272
1273 // Send the result back to the plug-in.
1274 plugin->didEvaluateJavaScript(request->requestID(), resultString);
1275}
1276
1277void PluginView::addStream(Stream* stream)
1278{
1279 ASSERT(!m_streams.contains(stream->streamID()));
1280 m_streams.set(stream->streamID(), stream);
1281}
1282
1283void PluginView::removeStream(Stream* stream)
1284{
1285 ASSERT(m_streams.get(stream->streamID()) == stream);
1286
1287 m_streams.remove(stream->streamID());
1288}
1289
1290void PluginView::cancelAllStreams()
1291{
1292 for (auto& stream : copyToVector(m_streams.values()))
1293 stream->cancel();
1294
1295 // Cancelling a stream removes it from the m_streams map, so if we cancel all streams the map should be empty.
1296 ASSERT(m_streams.isEmpty());
1297}
1298
1299void PluginView::redeliverManualStream()
1300{
1301 if (m_manualStreamState == ManualStreamState::Initial) {
1302 // Nothing to do.
1303 return;
1304 }
1305
1306 if (m_manualStreamState == ManualStreamState::Failed) {
1307 manualLoadDidFail(m_manualStreamError);
1308 return;
1309 }
1310
1311 // Deliver the response.
1312 manualLoadDidReceiveResponse(m_manualStreamResponse);
1313
1314 // Deliver the data.
1315 if (m_manualStreamData) {
1316 for (const auto& element : *m_manualStreamData)
1317 manualLoadDidReceiveData(element.segment->data(), element.segment->size());
1318 m_manualStreamData = nullptr;
1319 }
1320
1321 if (m_manualStreamState == ManualStreamState::Finished)
1322 manualLoadDidFinishLoading();
1323}
1324
1325void PluginView::invalidateRect(const IntRect& dirtyRect)
1326{
1327 if (!parent() || !m_plugin || !m_isInitialized)
1328 return;
1329
1330#if PLATFORM(COCOA)
1331 if (m_plugin->pluginLayer())
1332 return;
1333#endif
1334
1335 if (m_pluginElement->displayState() < HTMLPlugInElement::Restarting)
1336 return;
1337
1338 auto* renderer = m_pluginElement->renderer();
1339 if (!is<RenderEmbeddedObject>(renderer))
1340 return;
1341 auto& object = downcast<RenderEmbeddedObject>(*renderer);
1342
1343 IntRect contentRect(dirtyRect);
1344 contentRect.move(object.borderLeft() + object.paddingLeft(), object.borderTop() + object.paddingTop());
1345 renderer->repaintRectangle(contentRect);
1346}
1347
1348void PluginView::setFocus(bool hasFocus)
1349{
1350 Widget::setFocus(hasFocus);
1351
1352 if (!m_isInitialized || !m_plugin)
1353 return;
1354
1355 m_plugin->setFocus(hasFocus);
1356}
1357
1358void PluginView::mediaCanStart(WebCore::Document&)
1359{
1360 ASSERT(m_isWaitingUntilMediaCanStart);
1361 m_isWaitingUntilMediaCanStart = false;
1362
1363 initializePlugin();
1364}
1365
1366void PluginView::pageMutedStateDidChange()
1367{
1368#if ENABLE(NETSCAPE_PLUGIN_API)
1369 // The plug-in can be null here if it failed to initialize.
1370 if (!m_isInitialized || !m_plugin)
1371 return;
1372
1373 m_plugin->mutedStateChanged(isMuted());
1374#endif
1375}
1376
1377void PluginView::invalidate(const IntRect& dirtyRect)
1378{
1379 invalidateRect(dirtyRect);
1380}
1381
1382String PluginView::userAgent()
1383{
1384 Frame* frame = m_pluginElement->document().frame();
1385 if (!frame)
1386 return String();
1387
1388 return frame->loader().client().userAgent(URL());
1389}
1390
1391void PluginView::loadURL(uint64_t requestID, const String& method, const String& urlString, const String& target, const HTTPHeaderMap& headerFields, const Vector<uint8_t>& httpBody, bool allowPopups)
1392{
1393 FrameLoadRequest frameLoadRequest { m_pluginElement->document(), m_pluginElement->document().securityOrigin(), { }, target, LockHistory::No, LockBackForwardList::No, MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, ShouldOpenExternalURLsPolicy::ShouldNotAllow, InitiatedByMainFrame::Unknown };
1394 frameLoadRequest.resourceRequest().setHTTPMethod(method);
1395 frameLoadRequest.resourceRequest().setURL(m_pluginElement->document().completeURL(urlString));
1396 frameLoadRequest.resourceRequest().setHTTPHeaderFields(headerFields);
1397 if (!httpBody.isEmpty()) {
1398 frameLoadRequest.resourceRequest().setHTTPBody(FormData::create(httpBody.data(), httpBody.size()));
1399 if (frameLoadRequest.resourceRequest().httpContentType().isEmpty())
1400 frameLoadRequest.resourceRequest().setHTTPContentType("application/x-www-form-urlencoded");
1401 }
1402
1403 String referrer = SecurityPolicy::generateReferrerHeader(frame()->document()->referrerPolicy(), frameLoadRequest.resourceRequest().url(), frame()->loader().outgoingReferrer());
1404 if (!referrer.isEmpty())
1405 frameLoadRequest.resourceRequest().setHTTPReferrer(referrer);
1406
1407 m_pendingURLRequests.append(URLRequest::create(requestID, WTFMove(frameLoadRequest), allowPopups));
1408 m_pendingURLRequestsTimer.startOneShot(0_s);
1409}
1410
1411void PluginView::cancelStreamLoad(uint64_t streamID)
1412{
1413 // Keep a reference to the stream. Stream::cancel might remove the stream from the map, and thus
1414 // releasing its last reference.
1415 RefPtr<Stream> stream = m_streams.get(streamID);
1416 if (!stream)
1417 return;
1418
1419 // Cancelling the stream here will remove it from the map.
1420 stream->cancel();
1421 ASSERT(!m_streams.contains(streamID));
1422}
1423
1424void PluginView::continueStreamLoad(uint64_t streamID)
1425{
1426 RefPtr<Stream> stream = m_streams.get(streamID);
1427 if (!stream)
1428 return;
1429
1430 stream->continueLoad();
1431}
1432
1433void PluginView::cancelManualStreamLoad()
1434{
1435 if (!frame())
1436 return;
1437
1438 DocumentLoader* documentLoader = frame()->loader().activeDocumentLoader();
1439 ASSERT(documentLoader);
1440
1441 if (documentLoader->isLoadingMainResource())
1442 documentLoader->cancelMainResourceLoad(frame()->loader().cancelledError(m_parameters.url));
1443}
1444
1445#if ENABLE(NETSCAPE_PLUGIN_API)
1446NPObject* PluginView::windowScriptNPObject()
1447{
1448 if (!frame())
1449 return nullptr;
1450
1451 if (!frame()->script().canExecuteScripts(NotAboutToExecuteScript)) {
1452 // FIXME: Investigate if other browsers allow plug-ins to access JavaScript objects even if JavaScript is disabled.
1453 return nullptr;
1454 }
1455
1456 return m_npRuntimeObjectMap.getOrCreateNPObject(pluginWorld().vm(), frame()->windowProxy().jsWindowProxy(pluginWorld())->window());
1457}
1458
1459NPObject* PluginView::pluginElementNPObject()
1460{
1461 if (!frame())
1462 return 0;
1463
1464 if (!frame()->script().canExecuteScripts(NotAboutToExecuteScript)) {
1465 // FIXME: Investigate if other browsers allow plug-ins to access JavaScript objects even if JavaScript is disabled.
1466 return 0;
1467 }
1468
1469 JSObject* object = frame()->script().jsObjectForPluginElement(m_pluginElement.get());
1470 ASSERT(object);
1471
1472 return m_npRuntimeObjectMap.getOrCreateNPObject(pluginWorld().vm(), object);
1473}
1474
1475bool PluginView::evaluate(NPObject* npObject, const String& scriptString, NPVariant* result, bool allowPopups)
1476{
1477 // FIXME: Is this check necessary?
1478 if (!m_pluginElement->document().frame())
1479 return false;
1480
1481 // Calling evaluate will run JavaScript that can potentially remove the plug-in element, so we need to
1482 // protect the plug-in view from destruction.
1483 NPRuntimeObjectMap::PluginProtector pluginProtector(&m_npRuntimeObjectMap);
1484
1485 UserGestureIndicator gestureIndicator(allowPopups ? Optional<ProcessingUserGestureState>(ProcessingUserGesture) : WTF::nullopt);
1486 return m_npRuntimeObjectMap.evaluate(npObject, scriptString, result);
1487}
1488
1489void PluginView::setPluginIsPlayingAudio(bool pluginIsPlayingAudio)
1490{
1491 if (m_pluginIsPlayingAudio == pluginIsPlayingAudio)
1492 return;
1493
1494 m_pluginIsPlayingAudio = pluginIsPlayingAudio;
1495 m_pluginElement->document().updateIsPlayingMedia();
1496}
1497
1498bool PluginView::isMuted() const
1499{
1500 if (!frame() || !frame()->page())
1501 return false;
1502
1503 return frame()->page()->isAudioMuted();
1504}
1505#endif
1506
1507void PluginView::setStatusbarText(const String& statusbarText)
1508{
1509 if (!frame())
1510 return;
1511
1512 Page* page = frame()->page();
1513 if (!page)
1514 return;
1515
1516 page->chrome().setStatusbarText(*frame(), statusbarText);
1517}
1518
1519bool PluginView::isAcceleratedCompositingEnabled()
1520{
1521 if (!frame())
1522 return false;
1523
1524 // We know that some plug-ins can support snapshotting without needing
1525 // accelerated compositing. Since we're trying to snapshot them anyway,
1526 // put them into normal compositing mode. A side benefit is that this might
1527 // allow the entire page to stay in that mode.
1528 if (m_pluginElement->displayState() < HTMLPlugInElement::Restarting && m_parameters.mimeType == "application/x-shockwave-flash")
1529 return false;
1530
1531 return frame()->settings().acceleratedCompositingEnabled();
1532}
1533
1534void PluginView::pluginProcessCrashed()
1535{
1536 m_pluginProcessHasCrashed = true;
1537
1538 auto* renderer = m_pluginElement->renderer();
1539 if (!is<RenderEmbeddedObject>(renderer))
1540 return;
1541
1542 m_pluginElement->invalidateStyleAndLayerComposition();
1543
1544 downcast<RenderEmbeddedObject>(*renderer).setPluginUnavailabilityReason(RenderEmbeddedObject::PluginCrashed);
1545
1546 Widget::invalidate();
1547}
1548
1549#if PLATFORM(COCOA)
1550void PluginView::pluginFocusOrWindowFocusChanged(bool pluginHasFocusAndWindowHasFocus)
1551{
1552 if (m_webPage)
1553 m_webPage->send(Messages::WebPageProxy::PluginFocusOrWindowFocusChanged(m_plugin->pluginComplexTextInputIdentifier(), pluginHasFocusAndWindowHasFocus));
1554}
1555
1556void PluginView::setComplexTextInputState(PluginComplexTextInputState pluginComplexTextInputState)
1557{
1558 if (m_webPage)
1559 m_webPage->send(Messages::WebPageProxy::SetPluginComplexTextInputState(m_plugin->pluginComplexTextInputIdentifier(), pluginComplexTextInputState));
1560}
1561
1562const MachSendRight& PluginView::compositingRenderServerPort()
1563{
1564 return WebProcess::singleton().compositingRenderServerPort();
1565}
1566
1567#endif
1568
1569float PluginView::contentsScaleFactor()
1570{
1571 if (Page* page = frame() ? frame()->page() : 0)
1572 return page->deviceScaleFactor();
1573
1574 return 1;
1575}
1576
1577String PluginView::proxiesForURL(const String& urlString)
1578{
1579 Vector<ProxyServer> proxyServers = proxyServersForURL(URL(URL(), urlString));
1580 return toString(proxyServers);
1581}
1582
1583String PluginView::cookiesForURL(const String& urlString)
1584{
1585 if (auto* page = m_pluginElement->document().page())
1586 return page->cookieJar().cookies(m_pluginElement->document(), URL(URL(), urlString));
1587 ASSERT_NOT_REACHED();
1588 return { };
1589}
1590
1591void PluginView::setCookiesForURL(const String& urlString, const String& cookieString)
1592{
1593 if (auto* page = m_pluginElement->document().page())
1594 page->cookieJar().setCookies(m_pluginElement->document(), URL(URL(), urlString), cookieString);
1595 else
1596 ASSERT_NOT_REACHED();
1597}
1598
1599bool PluginView::getAuthenticationInfo(const ProtectionSpace& protectionSpace, String& username, String& password)
1600{
1601 auto* contentDocument = m_pluginElement->contentDocument();
1602 if (!contentDocument)
1603 return false;
1604
1605 auto credential = CredentialStorage::getFromPersistentStorage(protectionSpace);
1606 if (!credential.hasPassword())
1607 return false;
1608
1609 username = credential.user();
1610 password = credential.password();
1611
1612 return true;
1613}
1614
1615bool PluginView::isPrivateBrowsingEnabled()
1616{
1617 // If we can't get the real setting, we'll assume that private browsing is enabled.
1618 if (!frame())
1619 return true;
1620
1621 if (!frame()->document()->securityOrigin().canAccessPluginStorage(frame()->document()->topOrigin()))
1622 return true;
1623
1624 return frame()->page()->usesEphemeralSession();
1625}
1626
1627bool PluginView::asynchronousPluginInitializationEnabled() const
1628{
1629 return m_webPage->asynchronousPluginInitializationEnabled();
1630}
1631
1632bool PluginView::asynchronousPluginInitializationEnabledForAllPlugins() const
1633{
1634 return m_webPage->asynchronousPluginInitializationEnabledForAllPlugins();
1635}
1636
1637bool PluginView::artificialPluginInitializationDelayEnabled() const
1638{
1639 return m_webPage->artificialPluginInitializationDelayEnabled();
1640}
1641
1642void PluginView::protectPluginFromDestruction()
1643{
1644 if (m_plugin && !m_plugin->isBeingDestroyed())
1645 ref();
1646}
1647
1648void PluginView::unprotectPluginFromDestruction()
1649{
1650 if (!m_plugin || m_plugin->isBeingDestroyed())
1651 return;
1652
1653 // A plug-in may ask us to evaluate JavaScript that removes the plug-in from the
1654 // page, but expect the object to still be alive when the call completes. Flash,
1655 // for example, may crash if the plug-in is destroyed and we return to code for
1656 // the destroyed object higher on the stack. To prevent this, if the plug-in has
1657 // only one remaining reference, call deref() asynchronously.
1658 if (hasOneRef()) {
1659 RunLoop::main().dispatch([lastRef = adoptRef(*this)] {
1660 });
1661 return;
1662 }
1663
1664 deref();
1665}
1666
1667void PluginView::didFinishLoad(WebFrame* webFrame)
1668{
1669 RefPtr<URLRequest> request = m_pendingFrameLoads.take(webFrame);
1670 ASSERT(request);
1671 webFrame->setLoadListener(0);
1672
1673 m_plugin->frameDidFinishLoading(request->requestID());
1674}
1675
1676void PluginView::didFailLoad(WebFrame* webFrame, bool wasCancelled)
1677{
1678 RefPtr<URLRequest> request = m_pendingFrameLoads.take(webFrame);
1679 ASSERT(request);
1680 webFrame->setLoadListener(0);
1681
1682 m_plugin->frameDidFail(request->requestID(), wasCancelled);
1683}
1684
1685#if PLATFORM(X11)
1686uint64_t PluginView::createPluginContainer()
1687{
1688 uint64_t windowID = 0;
1689 if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::X11)
1690 m_webPage->sendSync(Messages::WebPageProxy::CreatePluginContainer(), Messages::WebPageProxy::CreatePluginContainer::Reply(windowID));
1691 return windowID;
1692}
1693
1694void PluginView::windowedPluginGeometryDidChange(const WebCore::IntRect& frameRect, const WebCore::IntRect& clipRect, uint64_t windowID)
1695{
1696 m_webPage->send(Messages::WebPageProxy::WindowedPluginGeometryDidChange(frameRect, clipRect, windowID));
1697}
1698
1699void PluginView::windowedPluginVisibilityDidChange(bool isVisible, uint64_t windowID)
1700{
1701 m_webPage->send(Messages::WebPageProxy::WindowedPluginVisibilityDidChange(isVisible, windowID));
1702}
1703#endif
1704
1705#if PLATFORM(COCOA)
1706static bool isAlmostSolidColor(BitmapImage* bitmap)
1707{
1708 CGImageRef image = bitmap->nativeImage().get();
1709 ASSERT(CGImageGetBitsPerComponent(image) == 8);
1710
1711 CGBitmapInfo imageInfo = CGImageGetBitmapInfo(image);
1712 if (!(imageInfo & kCGBitmapByteOrder32Little) || (imageInfo & kCGBitmapAlphaInfoMask) != kCGImageAlphaPremultipliedFirst) {
1713 // FIXME: Consider being able to handle other pixel formats.
1714 ASSERT_NOT_REACHED();
1715 return false;
1716 }
1717
1718 size_t width = CGImageGetWidth(image);
1719 size_t height = CGImageGetHeight(image);
1720 size_t bytesPerRow = CGImageGetBytesPerRow(image);
1721
1722 RetainPtr<CFDataRef> provider = adoptCF(CGDataProviderCopyData(CGImageGetDataProvider(image)));
1723 const UInt8* data = CFDataGetBytePtr(provider.get());
1724
1725 // Overlay a grid of sampling dots on top of a grayscale version of the image.
1726 // For the interior points, calculate the difference in luminance among the sample point
1727 // and its surrounds points, scaled by transparency.
1728 const unsigned sampleRows = 7;
1729 const unsigned sampleCols = 7;
1730 // FIXME: Refine the proper number of samples, and accommodate different aspect ratios.
1731 if (width < sampleCols || height < sampleRows)
1732 return false;
1733
1734 // Ensure that the last row/column land on the image perimeter.
1735 const float strideWidth = static_cast<float>(width - 1) / (sampleCols - 1);
1736 const float strideHeight = static_cast<float>(height - 1) / (sampleRows - 1);
1737 float samples[sampleRows][sampleCols];
1738
1739 // Find the luminance of the sample points.
1740 float y = 0;
1741 const UInt8* row = data;
1742 for (unsigned i = 0; i < sampleRows; ++i) {
1743 float x = 0;
1744 for (unsigned j = 0; j < sampleCols; ++j) {
1745 const UInt8* p0 = row + (static_cast<int>(x + .5)) * 4;
1746 // R G B A
1747 samples[i][j] = (0.2125 * *p0 + 0.7154 * *(p0+1) + 0.0721 * *(p0+2)) * *(p0+3) / 255;
1748 x += strideWidth;
1749 }
1750 y += strideHeight;
1751 row = data + (static_cast<int>(y + .5)) * bytesPerRow;
1752 }
1753
1754 // Determine the image score.
1755 float accumScore = 0;
1756 for (unsigned i = 1; i < sampleRows - 1; ++i) {
1757 for (unsigned j = 1; j < sampleCols - 1; ++j) {
1758 float diff = samples[i - 1][j] + samples[i + 1][j] + samples[i][j - 1] + samples[i][j + 1] - 4 * samples[i][j];
1759 accumScore += diff * diff;
1760 }
1761 }
1762
1763 // The score for a given sample can be within the range of 0 and 255^2.
1764 return accumScore < 2500 * (sampleRows - 2) * (sampleCols - 2);
1765}
1766#endif
1767
1768void PluginView::pluginSnapshotTimerFired()
1769{
1770#if ENABLE(PRIMARY_SNAPSHOTTED_PLUGIN_HEURISTIC)
1771 HTMLPlugInImageElement& plugInImageElement = downcast<HTMLPlugInImageElement>(*m_pluginElement);
1772 bool isPlugInOnScreen = m_webPage->plugInIntersectsSearchRect(plugInImageElement);
1773 bool plugInCameOnScreen = isPlugInOnScreen && m_didPlugInStartOffScreen;
1774 bool snapshotFound = false;
1775#endif
1776
1777 if (m_plugin && m_plugin->supportsSnapshotting()) {
1778 // Snapshot might be 0 if plugin size is 0x0.
1779 RefPtr<ShareableBitmap> snapshot = m_plugin->snapshot();
1780 RefPtr<Image> snapshotImage;
1781 if (snapshot)
1782 snapshotImage = snapshot->createImage();
1783 m_pluginElement->updateSnapshot(snapshotImage.get());
1784
1785 if (snapshotImage) {
1786#if ENABLE(PRIMARY_SNAPSHOTTED_PLUGIN_HEURISTIC)
1787 bool snapshotIsAlmostSolidColor = isAlmostSolidColor(downcast<BitmapImage>(snapshotImage.get()));
1788 snapshotFound = !snapshotIsAlmostSolidColor;
1789#endif
1790
1791#if PLATFORM(COCOA)
1792 unsigned maximumSnapshotRetries = frame() ? frame()->settings().maximumPlugInSnapshotAttempts() : 0;
1793 if (snapshotIsAlmostSolidColor && m_countSnapshotRetries < maximumSnapshotRetries && !plugInCameOnScreen) {
1794 ++m_countSnapshotRetries;
1795 m_pluginSnapshotTimer.restart();
1796 return;
1797 }
1798#endif
1799 }
1800 }
1801
1802#if ENABLE(PRIMARY_SNAPSHOTTED_PLUGIN_HEURISTIC)
1803 unsigned candidateArea = 0;
1804 unsigned maximumSnapshotRetries = frame() ? frame()->settings().maximumPlugInSnapshotAttempts() : 0;
1805 bool noSnapshotFoundAfterMaxRetries = m_countSnapshotRetries == maximumSnapshotRetries && !isPlugInOnScreen && !snapshotFound;
1806 if (m_webPage->plugInIsPrimarySize(plugInImageElement, candidateArea)
1807 && (noSnapshotFoundAfterMaxRetries || plugInCameOnScreen))
1808 m_pluginElement->setDisplayState(HTMLPlugInElement::Playing);
1809 else
1810#endif
1811 m_pluginElement->setDisplayState(HTMLPlugInElement::DisplayingSnapshot);
1812}
1813
1814void PluginView::beginSnapshottingRunningPlugin()
1815{
1816 m_pluginSnapshotTimer.restart();
1817}
1818
1819bool PluginView::shouldAlwaysAutoStart() const
1820{
1821 if (!m_plugin)
1822 return PluginViewBase::shouldAlwaysAutoStart();
1823
1824 if (MIMETypeRegistry::isJavaAppletMIMEType(m_parameters.mimeType))
1825 return true;
1826
1827 return m_plugin->shouldAlwaysAutoStart();
1828}
1829
1830void PluginView::pluginDidReceiveUserInteraction()
1831{
1832 if (frame() && !frame()->settings().plugInSnapshottingEnabled())
1833 return;
1834
1835 if (m_didReceiveUserInteraction)
1836 return;
1837
1838 m_didReceiveUserInteraction = true;
1839
1840 HTMLPlugInImageElement& plugInImageElement = downcast<HTMLPlugInImageElement>(*m_pluginElement);
1841 String pageOrigin = plugInImageElement.document().page()->mainFrame().document()->baseURL().host().toString();
1842 String pluginOrigin = plugInImageElement.loadedUrl().host().toString();
1843 String mimeType = plugInImageElement.serviceType();
1844
1845 WebProcess::singleton().plugInDidReceiveUserInteraction(pageOrigin, pluginOrigin, mimeType, plugInImageElement.document().page()->sessionID());
1846}
1847
1848bool PluginView::shouldCreateTransientPaintingSnapshot() const
1849{
1850 if (!m_plugin)
1851 return false;
1852
1853 if (!m_isInitialized)
1854 return false;
1855
1856 if (FrameView* frameView = frame()->view()) {
1857 if (frameView->paintBehavior().containsAny({ PaintBehavior::SelectionOnly, PaintBehavior::SelectionAndBackgroundsOnly, PaintBehavior::ForceBlackText})) {
1858 // This paint behavior is used when drawing the find indicator and there's no need to
1859 // snapshot plug-ins, because they can never be painted as part of the find indicator.
1860 return false;
1861 }
1862 }
1863
1864 if (!m_plugin->canCreateTransientPaintingSnapshot())
1865 return false;
1866
1867 return true;
1868}
1869
1870} // namespace WebKit
1871