• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Collabora Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "config.h"
28 #include "PluginView.h"
29 
30 #include "Document.h"
31 #include "DocumentLoader.h"
32 #include "Element.h"
33 #include "FrameLoader.h"
34 #include "FrameTree.h"
35 #include "Frame.h"
36 #include "FrameView.h"
37 #include "GraphicsContext.h"
38 #include "Image.h"
39 #include "HTMLNames.h"
40 #include "HTMLPlugInElement.h"
41 #include "JSDOMWindow.h"
42 #include "KeyboardEvent.h"
43 #include "MIMETypeRegistry.h"
44 #include "MouseEvent.h"
45 #include "NotImplemented.h"
46 #include "Page.h"
47 #include "FocusController.h"
48 #include "PlatformMouseEvent.h"
49 #if PLATFORM(WIN_OS) && !PLATFORM(WX) && ENABLE(NETSCAPE_PLUGIN_API)
50 #include "PluginMessageThrottlerWin.h"
51 #endif
52 #include "PluginPackage.h"
53 #include "JSDOMBinding.h"
54 #include "ScriptController.h"
55 #include "ScriptValue.h"
56 #include "PluginDatabase.h"
57 #include "PluginDebug.h"
58 #include "PluginMainThreadScheduler.h"
59 #include "PluginPackage.h"
60 #include "RenderBox.h"
61 #include "RenderObject.h"
62 #include "c_instance.h"
63 #include "npruntime_impl.h"
64 #include "runtime_root.h"
65 #include "Settings.h"
66 #include "runtime.h"
67 #include <runtime/JSLock.h>
68 #include <runtime/JSValue.h>
69 #include <wtf/ASCIICType.h>
70 
71 using JSC::ExecState;
72 using JSC::JSLock;
73 using JSC::JSObject;
74 using JSC::JSValuePtr;
75 using JSC::UString;
76 
77 using std::min;
78 
79 using namespace WTF;
80 
81 namespace WebCore {
82 
83 using namespace HTMLNames;
84 
85 static int s_callingPlugin;
86 
scriptStringIfJavaScriptURL(const KURL & url)87 static String scriptStringIfJavaScriptURL(const KURL& url)
88 {
89     if (!url.protocolIs("javascript"))
90         return String();
91 
92     // This returns an unescaped string
93     return decodeURLEscapeSequences(url.string().substring(11));
94 }
95 
96 PluginView* PluginView::s_currentPluginView = 0;
97 
popPopupsStateTimerFired(Timer<PluginView> *)98 void PluginView::popPopupsStateTimerFired(Timer<PluginView>*)
99 {
100     popPopupsEnabledState();
101 }
102 
windowClipRect() const103 IntRect PluginView::windowClipRect() const
104 {
105     // Start by clipping to our bounds.
106     IntRect clipRect(m_windowRect);
107 
108     // Take our element and get the clip rect from the enclosing layer and frame view.
109     RenderLayer* layer = m_element->renderer()->enclosingLayer();
110     FrameView* parentView = m_element->document()->view();
111     clipRect.intersect(parentView->windowClipRectForLayer(layer, true));
112 
113     return clipRect;
114 }
115 
setFrameRect(const IntRect & rect)116 void PluginView::setFrameRect(const IntRect& rect)
117 {
118     if (m_element->document()->printing())
119         return;
120 
121 #if defined(ANDROID_PLUGINS)
122     if (rect != frameRect()) {
123         Widget::setFrameRect(rect);
124         setNPWindowRect(rect);  // only call when it changes
125     }
126 #else
127     if (rect != frameRect())
128         Widget::setFrameRect(rect);
129 #endif
130 
131     updatePluginWidget();
132 
133 #if PLATFORM(WIN_OS)
134     // On Windows, always call plugin to change geometry.
135     setNPWindowRect(rect);
136 #elif XP_UNIX
137     // On Unix, only call plugin if it's full-page.
138     if (m_mode == NP_FULL)
139         setNPWindowRect(rect);
140 #endif
141 }
142 
frameRectsChanged()143 void PluginView::frameRectsChanged()
144 {
145     updatePluginWidget();
146 }
147 
handleEvent(Event * event)148 void PluginView::handleEvent(Event* event)
149 {
150     if (!m_plugin || m_isWindowed)
151         return;
152 
153     if (event->isMouseEvent())
154         handleMouseEvent(static_cast<MouseEvent*>(event));
155     else if (event->isKeyboardEvent())
156         handleKeyboardEvent(static_cast<KeyboardEvent*>(event));
157 }
158 
start()159 bool PluginView::start()
160 {
161     if (m_isStarted)
162         return false;
163 
164     PluginMainThreadScheduler::scheduler().registerPlugin(m_instance);
165 
166     ASSERT(m_plugin);
167     ASSERT(m_plugin->pluginFuncs()->newp);
168 
169     NPError npErr;
170     {
171         PluginView::setCurrentPluginView(this);
172         JSC::JSLock::DropAllLocks dropAllLocks(false);
173         setCallingPlugin(true);
174         npErr = m_plugin->pluginFuncs()->newp((NPMIMEType)m_mimeType.data(), m_instance, m_mode, m_paramCount, m_paramNames, m_paramValues, NULL);
175         setCallingPlugin(false);
176         LOG_NPERROR(npErr);
177         PluginView::setCurrentPluginView(0);
178     }
179 
180     if (npErr != NPERR_NO_ERROR)
181         return false;
182 
183     m_isStarted = true;
184 
185     if (!m_url.isEmpty() && !m_loadManually) {
186         FrameLoadRequest frameLoadRequest;
187         frameLoadRequest.resourceRequest().setHTTPMethod("GET");
188         frameLoadRequest.resourceRequest().setURL(m_url);
189         load(frameLoadRequest, false, 0);
190     }
191 
192     return true;
193 }
194 
setCurrentPluginView(PluginView * pluginView)195 void PluginView::setCurrentPluginView(PluginView* pluginView)
196 {
197     s_currentPluginView = pluginView;
198 }
199 
currentPluginView()200 PluginView* PluginView::currentPluginView()
201 {
202     return s_currentPluginView;
203 }
204 
createUTF8String(const String & str)205 static char* createUTF8String(const String& str)
206 {
207     CString cstr = str.utf8();
208     char* result = reinterpret_cast<char*>(fastMalloc(cstr.length() + 1));
209 
210     strncpy(result, cstr.data(), cstr.length() + 1);
211 
212     return result;
213 }
214 
getString(ScriptController * proxy,JSValuePtr result,String & string)215 static bool getString(ScriptController* proxy, JSValuePtr result, String& string)
216 {
217     if (!proxy || !result || result.isUndefined())
218         return false;
219     JSLock lock(false);
220 
221     ExecState* exec = proxy->globalObject()->globalExec();
222     UString ustring = result.toString(exec);
223     exec->clearException();
224 
225     string = ustring;
226     return true;
227 }
228 
performRequest(PluginRequest * request)229 void PluginView::performRequest(PluginRequest* request)
230 {
231     // don't let a plugin start any loads if it is no longer part of a document that is being
232     // displayed unless the loads are in the same frame as the plugin.
233     const String& targetFrameName = request->frameLoadRequest().frameName();
234     if (m_parentFrame->loader()->documentLoader() != m_parentFrame->loader()->activeDocumentLoader() &&
235         (targetFrameName.isNull() || m_parentFrame->tree()->find(targetFrameName) != m_parentFrame))
236         return;
237 
238     KURL requestURL = request->frameLoadRequest().resourceRequest().url();
239     String jsString = scriptStringIfJavaScriptURL(requestURL);
240 
241     if (jsString.isNull()) {
242         // if this is not a targeted request, create a stream for it. otherwise,
243         // just pass it off to the loader
244         if (targetFrameName.isEmpty()) {
245             RefPtr<PluginStream> stream = PluginStream::create(this, m_parentFrame, request->frameLoadRequest().resourceRequest(), request->sendNotification(), request->notifyData(), plugin()->pluginFuncs(), instance(), m_plugin->quirks());
246             m_streams.add(stream);
247             stream->start();
248         } else {
249             m_parentFrame->loader()->load(request->frameLoadRequest().resourceRequest(), targetFrameName, false);
250 
251             // FIXME: <rdar://problem/4807469> This should be sent when the document has finished loading
252             if (request->sendNotification()) {
253                 PluginView::setCurrentPluginView(this);
254                 JSC::JSLock::DropAllLocks dropAllLocks(false);
255                 setCallingPlugin(true);
256                 m_plugin->pluginFuncs()->urlnotify(m_instance, requestURL.string().utf8().data(), NPRES_DONE, request->notifyData());
257                 setCallingPlugin(false);
258                 PluginView::setCurrentPluginView(0);
259             }
260         }
261         return;
262     }
263 
264     // Targeted JavaScript requests are only allowed on the frame that contains the JavaScript plugin
265     // and this has been made sure in ::load.
266     ASSERT(targetFrameName.isEmpty() || m_parentFrame->tree()->find(targetFrameName) == m_parentFrame);
267 
268     // Executing a script can cause the plugin view to be destroyed, so we keep a reference to the parent frame.
269     RefPtr<Frame> parentFrame = m_parentFrame;
270     JSValuePtr result = m_parentFrame->loader()->executeScript(jsString, request->shouldAllowPopups()).jsValue();
271 
272     if (targetFrameName.isNull()) {
273         String resultString;
274 
275         CString cstr;
276         if (getString(parentFrame->script(), result, resultString))
277             cstr = resultString.utf8();
278 
279         RefPtr<PluginStream> stream = PluginStream::create(this, m_parentFrame, request->frameLoadRequest().resourceRequest(), request->sendNotification(), request->notifyData(), plugin()->pluginFuncs(), instance(), m_plugin->quirks());
280         m_streams.add(stream);
281         stream->sendJavaScriptStream(requestURL, cstr);
282     }
283 }
284 
requestTimerFired(Timer<PluginView> * timer)285 void PluginView::requestTimerFired(Timer<PluginView>* timer)
286 {
287     ASSERT(timer == &m_requestTimer);
288     ASSERT(m_requests.size() > 0);
289     ASSERT(!m_isJavaScriptPaused);
290 
291     PluginRequest* request = m_requests[0];
292     m_requests.remove(0);
293 
294     // Schedule a new request before calling performRequest since the call to
295     // performRequest can cause the plugin view to be deleted.
296     if (m_requests.size() > 0)
297         m_requestTimer.startOneShot(0);
298 
299     performRequest(request);
300     delete request;
301 }
302 
scheduleRequest(PluginRequest * request)303 void PluginView::scheduleRequest(PluginRequest* request)
304 {
305     m_requests.append(request);
306 
307     if (!m_isJavaScriptPaused)
308         m_requestTimer.startOneShot(0);
309 }
310 
load(const FrameLoadRequest & frameLoadRequest,bool sendNotification,void * notifyData)311 NPError PluginView::load(const FrameLoadRequest& frameLoadRequest, bool sendNotification, void* notifyData)
312 {
313     ASSERT(frameLoadRequest.resourceRequest().httpMethod() == "GET" || frameLoadRequest.resourceRequest().httpMethod() == "POST");
314 
315     KURL url = frameLoadRequest.resourceRequest().url();
316 
317     if (url.isEmpty())
318         return NPERR_INVALID_URL;
319 
320     // Don't allow requests to be made when the document loader is stopping all loaders.
321     if (m_parentFrame->loader()->documentLoader()->isStopping())
322         return NPERR_GENERIC_ERROR;
323 
324     const String& targetFrameName = frameLoadRequest.frameName();
325     String jsString = scriptStringIfJavaScriptURL(url);
326 
327     if (!jsString.isNull()) {
328         Settings* settings = m_parentFrame->settings();
329 
330         // Return NPERR_GENERIC_ERROR if JS is disabled. This is what Mozilla does.
331         if (!settings || !settings->isJavaScriptEnabled())
332             return NPERR_GENERIC_ERROR;
333 
334         // For security reasons, only allow JS requests to be made on the frame that contains the plug-in.
335         if (!targetFrameName.isNull() && m_parentFrame->tree()->find(targetFrameName) != m_parentFrame)
336             return NPERR_INVALID_PARAM;
337     } else if (!FrameLoader::canLoad(url, String(), m_parentFrame->document())) {
338             return NPERR_GENERIC_ERROR;
339     }
340 
341     PluginRequest* request = new PluginRequest(frameLoadRequest, sendNotification, notifyData, arePopupsAllowed());
342     scheduleRequest(request);
343 
344     return NPERR_NO_ERROR;
345 }
346 
makeURL(const KURL & baseURL,const char * relativeURLString)347 static KURL makeURL(const KURL& baseURL, const char* relativeURLString)
348 {
349     String urlString = relativeURLString;
350 
351     // Strip return characters.
352     urlString.replace('\n', "");
353     urlString.replace('\r', "");
354 
355     return KURL(baseURL, urlString);
356 }
357 
getURLNotify(const char * url,const char * target,void * notifyData)358 NPError PluginView::getURLNotify(const char* url, const char* target, void* notifyData)
359 {
360     FrameLoadRequest frameLoadRequest;
361 
362     frameLoadRequest.setFrameName(target);
363     frameLoadRequest.resourceRequest().setHTTPMethod("GET");
364     frameLoadRequest.resourceRequest().setURL(makeURL(m_baseURL, url));
365 
366     return load(frameLoadRequest, true, notifyData);
367 }
368 
getURL(const char * url,const char * target)369 NPError PluginView::getURL(const char* url, const char* target)
370 {
371     FrameLoadRequest frameLoadRequest;
372 
373     frameLoadRequest.setFrameName(target);
374     frameLoadRequest.resourceRequest().setHTTPMethod("GET");
375     frameLoadRequest.resourceRequest().setURL(makeURL(m_baseURL, url));
376 
377     return load(frameLoadRequest, false, 0);
378 }
379 
postURLNotify(const char * url,const char * target,uint32 len,const char * buf,NPBool file,void * notifyData)380 NPError PluginView::postURLNotify(const char* url, const char* target, uint32 len, const char* buf, NPBool file, void* notifyData)
381 {
382     return handlePost(url, target, len, buf, file, notifyData, true, true);
383 }
384 
postURL(const char * url,const char * target,uint32 len,const char * buf,NPBool file)385 NPError PluginView::postURL(const char* url, const char* target, uint32 len, const char* buf, NPBool file)
386 {
387     // As documented, only allow headers to be specified via NPP_PostURL when using a file.
388     return handlePost(url, target, len, buf, file, 0, false, file);
389 }
390 
newStream(NPMIMEType type,const char * target,NPStream ** stream)391 NPError PluginView::newStream(NPMIMEType type, const char* target, NPStream** stream)
392 {
393     notImplemented();
394     // Unsupported
395     return NPERR_GENERIC_ERROR;
396 }
397 
write(NPStream * stream,int32 len,void * buffer)398 int32 PluginView::write(NPStream* stream, int32 len, void* buffer)
399 {
400     notImplemented();
401     // Unsupported
402     return -1;
403 }
404 
destroyStream(NPStream * stream,NPReason reason)405 NPError PluginView::destroyStream(NPStream* stream, NPReason reason)
406 {
407     PluginStream* browserStream = static_cast<PluginStream*>(stream->ndata);
408 
409     if (!stream || PluginStream::ownerForStream(stream) != m_instance)
410         return NPERR_INVALID_INSTANCE_ERROR;
411 
412     browserStream->cancelAndDestroyStream(reason);
413     return NPERR_NO_ERROR;
414 }
415 
status(const char * message)416 void PluginView::status(const char* message)
417 {
418     if (Page* page = m_parentFrame->page())
419         page->chrome()->setStatusbarText(m_parentFrame, String(message));
420 }
421 
setValue(NPPVariable variable,void * value)422 NPError PluginView::setValue(NPPVariable variable, void* value)
423 {
424     switch (variable) {
425     case NPPVpluginWindowBool:
426         m_isWindowed = value;
427         return NPERR_NO_ERROR;
428     case NPPVpluginTransparentBool:
429         m_isTransparent = value;
430         return NPERR_NO_ERROR;
431 #if defined(XP_MACOSX)
432     case NPPVpluginDrawingModel:
433         return NPERR_NO_ERROR;
434     case NPPVpluginEventModel:
435         return NPERR_NO_ERROR;
436 #endif
437     default:
438 #ifdef PLUGIN_PLATFORM_SETVALUE
439         return platformSetValue(variable, value);
440 #else
441         notImplemented();
442         return NPERR_GENERIC_ERROR;
443 #endif
444     }
445 }
446 
invalidateTimerFired(Timer<PluginView> * timer)447 void PluginView::invalidateTimerFired(Timer<PluginView>* timer)
448 {
449     ASSERT(timer == &m_invalidateTimer);
450 
451     for (unsigned i = 0; i < m_invalidRects.size(); i++)
452         invalidateRect(m_invalidRects[i]);
453     m_invalidRects.clear();
454 }
455 
456 
pushPopupsEnabledState(bool state)457 void PluginView::pushPopupsEnabledState(bool state)
458 {
459     m_popupStateStack.append(state);
460 }
461 
popPopupsEnabledState()462 void PluginView::popPopupsEnabledState()
463 {
464     m_popupStateStack.removeLast();
465 }
466 
arePopupsAllowed() const467 bool PluginView::arePopupsAllowed() const
468 {
469     if (!m_popupStateStack.isEmpty())
470         return m_popupStateStack.last();
471 
472     return false;
473 }
474 
setJavaScriptPaused(bool paused)475 void PluginView::setJavaScriptPaused(bool paused)
476 {
477     if (m_isJavaScriptPaused == paused)
478         return;
479     m_isJavaScriptPaused = paused;
480 
481     if (m_isJavaScriptPaused)
482         m_requestTimer.stop();
483     else if (!m_requests.isEmpty())
484         m_requestTimer.startOneShot(0);
485 }
486 
bindingInstance()487 PassRefPtr<JSC::Bindings::Instance> PluginView::bindingInstance()
488 {
489 #if ENABLE(NETSCAPE_PLUGIN_API)
490     NPObject* object = 0;
491 
492     if (!m_plugin || !m_plugin->pluginFuncs()->getvalue)
493         return 0;
494 
495     NPError npErr;
496     {
497         PluginView::setCurrentPluginView(this);
498         JSC::JSLock::DropAllLocks dropAllLocks(false);
499         setCallingPlugin(true);
500         npErr = m_plugin->pluginFuncs()->getvalue(m_instance, NPPVpluginScriptableNPObject, &object);
501         setCallingPlugin(false);
502         PluginView::setCurrentPluginView(0);
503     }
504 
505     if (npErr != NPERR_NO_ERROR || !object)
506         return 0;
507 
508     RefPtr<JSC::Bindings::RootObject> root = m_parentFrame->script()->createRootObject(this);
509     RefPtr<JSC::Bindings::Instance> instance = JSC::Bindings::CInstance::create(object, root.release());
510 
511     _NPN_ReleaseObject(object);
512 
513     return instance.release();
514 #else
515     return 0;
516 #endif
517 }
518 
disconnectStream(PluginStream * stream)519 void PluginView::disconnectStream(PluginStream* stream)
520 {
521     ASSERT(m_streams.contains(stream));
522 
523     m_streams.remove(stream);
524 }
525 
setParameters(const Vector<String> & paramNames,const Vector<String> & paramValues)526 void PluginView::setParameters(const Vector<String>& paramNames, const Vector<String>& paramValues)
527 {
528     ASSERT(paramNames.size() == paramValues.size());
529 
530     unsigned size = paramNames.size();
531     unsigned paramCount = 0;
532 
533     m_paramNames = reinterpret_cast<char**>(fastMalloc(sizeof(char*) * size));
534     m_paramValues = reinterpret_cast<char**>(fastMalloc(sizeof(char*) * size));
535 
536     for (unsigned i = 0; i < size; i++) {
537         if (m_plugin->quirks().contains(PluginQuirkRemoveWindowlessVideoParam) && equalIgnoringCase(paramNames[i], "windowlessvideo"))
538             continue;
539 
540         m_paramNames[paramCount] = createUTF8String(paramNames[i]);
541         m_paramValues[paramCount] = createUTF8String(paramValues[i]);
542 
543         paramCount++;
544     }
545 
546     m_paramCount = paramCount;
547 }
548 
PluginView(Frame * parentFrame,const IntSize & size,PluginPackage * plugin,Element * element,const KURL & url,const Vector<String> & paramNames,const Vector<String> & paramValues,const String & mimeType,bool loadManually)549 PluginView::PluginView(Frame* parentFrame, const IntSize& size, PluginPackage* plugin, Element* element, const KURL& url, const Vector<String>& paramNames, const Vector<String>& paramValues, const String& mimeType, bool loadManually)
550     : m_parentFrame(parentFrame)
551     , m_plugin(plugin)
552     , m_element(element)
553     , m_isStarted(false)
554     , m_url(url)
555     , m_baseURL(m_parentFrame->loader()->completeURL(m_parentFrame->document()->baseURL().string()))
556     , m_status(PluginStatusLoadedSuccessfully)
557     , m_requestTimer(this, &PluginView::requestTimerFired)
558     , m_invalidateTimer(this, &PluginView::invalidateTimerFired)
559     , m_popPopupsStateTimer(this, &PluginView::popPopupsStateTimerFired)
560     , m_paramNames(0)
561     , m_paramValues(0)
562 #if defined(XP_MACOSX)
563     , m_isWindowed(false)
564 #else
565     , m_isWindowed(true)
566 #endif
567     , m_isTransparent(false)
568     , m_haveInitialized(false)
569 #if PLATFORM(GTK) || defined(Q_WS_X11)
570     , m_needsXEmbed(false)
571 #endif
572 #if PLATFORM(QT)
573     , m_isNPAPIPlugin(false)
574 #endif
575 #if PLATFORM(WIN_OS) && !PLATFORM(WX) && ENABLE(NETSCAPE_PLUGIN_API)
576     , m_pluginWndProc(0)
577     , m_lastMessage(0)
578     , m_isCallingPluginWndProc(false)
579     , m_wmPrintHDC(0)
580 #endif
581 #if (PLATFORM(QT) && PLATFORM(WIN_OS)) || defined(XP_MACOSX)
582     , m_window(0)
583 #endif
584     , m_loadManually(loadManually)
585     , m_manualStream(0)
586     , m_isJavaScriptPaused(false)
587 {
588 #if defined(ANDROID_PLUGINS)
589     platformInit();
590 #endif
591 
592     if (!m_plugin) {
593         m_status = PluginStatusCanNotFindPlugin;
594         return;
595     }
596 
597     m_instance = &m_instanceStruct;
598     m_instance->ndata = this;
599     m_instance->pdata = 0;
600 
601     m_mimeType = mimeType.utf8();
602 
603     setParameters(paramNames, paramValues);
604 
605 #ifdef XP_UNIX
606     m_npWindow.ws_info = 0;
607 #endif
608 
609     m_mode = m_loadManually ? NP_FULL : NP_EMBED;
610 
611     resize(size);
612 }
613 
didReceiveResponse(const ResourceResponse & response)614 void PluginView::didReceiveResponse(const ResourceResponse& response)
615 {
616     if (m_status != PluginStatusLoadedSuccessfully)
617         return;
618 
619     ASSERT(m_loadManually);
620     ASSERT(!m_manualStream);
621 
622     m_manualStream = PluginStream::create(this, m_parentFrame, m_parentFrame->loader()->activeDocumentLoader()->request(), false, 0, plugin()->pluginFuncs(), instance(), m_plugin->quirks());
623     m_manualStream->setLoadManually(true);
624 
625     m_manualStream->didReceiveResponse(0, response);
626 }
627 
didReceiveData(const char * data,int length)628 void PluginView::didReceiveData(const char* data, int length)
629 {
630     if (m_status != PluginStatusLoadedSuccessfully)
631         return;
632 
633     ASSERT(m_loadManually);
634     ASSERT(m_manualStream);
635 
636     m_manualStream->didReceiveData(0, data, length);
637 }
638 
didFinishLoading()639 void PluginView::didFinishLoading()
640 {
641     if (m_status != PluginStatusLoadedSuccessfully)
642         return;
643 
644     ASSERT(m_loadManually);
645     ASSERT(m_manualStream);
646 
647     m_manualStream->didFinishLoading(0);
648 }
649 
didFail(const ResourceError & error)650 void PluginView::didFail(const ResourceError& error)
651 {
652     if (m_status != PluginStatusLoadedSuccessfully)
653         return;
654 
655     ASSERT(m_loadManually);
656     ASSERT(m_manualStream);
657 
658     m_manualStream->didFail(0, error);
659 }
660 
setCallingPlugin(bool b) const661 void PluginView::setCallingPlugin(bool b) const
662 {
663     if (!m_plugin->quirks().contains(PluginQuirkHasModalMessageLoop))
664         return;
665 
666     if (b)
667         ++s_callingPlugin;
668     else
669         --s_callingPlugin;
670 
671     ASSERT(s_callingPlugin >= 0);
672 }
673 
isCallingPlugin()674 bool PluginView::isCallingPlugin()
675 {
676     return s_callingPlugin > 0;
677 }
678 
create(Frame * parentFrame,const IntSize & size,Element * element,const KURL & url,const Vector<String> & paramNames,const Vector<String> & paramValues,const String & mimeType,bool loadManually)679 PluginView* PluginView::create(Frame* parentFrame, const IntSize& size, Element* element, const KURL& url, const Vector<String>& paramNames, const Vector<String>& paramValues, const String& mimeType, bool loadManually)
680 {
681     // if we fail to find a plugin for this MIME type, findPlugin will search for
682     // a plugin by the file extension and update the MIME type, so pass a mutable String
683     String mimeTypeCopy = mimeType;
684     PluginPackage* plugin = PluginDatabase::installedPlugins()->findPlugin(url, mimeTypeCopy);
685 
686     // No plugin was found, try refreshing the database and searching again
687     if (!plugin && PluginDatabase::installedPlugins()->refresh()) {
688         mimeTypeCopy = mimeType;
689         plugin = PluginDatabase::installedPlugins()->findPlugin(url, mimeTypeCopy);
690     }
691 
692     return new PluginView(parentFrame, size, plugin, element, url, paramNames, paramValues, mimeTypeCopy, loadManually);
693 }
694 
freeStringArray(char ** stringArray,int length)695 void PluginView::freeStringArray(char** stringArray, int length)
696 {
697     if (!stringArray)
698         return;
699 
700     for (int i = 0; i < length; i++)
701         fastFree(stringArray[i]);
702 
703     fastFree(stringArray);
704 }
705 
startsWithBlankLine(const Vector<char> & buffer)706 static inline bool startsWithBlankLine(const Vector<char>& buffer)
707 {
708     return buffer.size() > 0 && buffer[0] == '\n';
709 }
710 
locationAfterFirstBlankLine(const Vector<char> & buffer)711 static inline int locationAfterFirstBlankLine(const Vector<char>& buffer)
712 {
713     const char* bytes = buffer.data();
714     unsigned length = buffer.size();
715 
716     for (unsigned i = 0; i < length - 4; i++) {
717         // Support for Acrobat. It sends "\n\n".
718         if (bytes[i] == '\n' && bytes[i + 1] == '\n')
719             return i + 2;
720 
721         // Returns the position after 2 CRLF's or 1 CRLF if it is the first line.
722         if (bytes[i] == '\r' && bytes[i + 1] == '\n') {
723             i += 2;
724             if (i == 2)
725                 return i;
726             else if (bytes[i] == '\n')
727                 // Support for Director. It sends "\r\n\n" (3880387).
728                 return i + 1;
729             else if (bytes[i] == '\r' && bytes[i + 1] == '\n')
730                 // Support for Flash. It sends "\r\n\r\n" (3758113).
731                 return i + 2;
732         }
733     }
734 
735     return -1;
736 }
737 
findEOL(const char * bytes,unsigned length)738 static inline const char* findEOL(const char* bytes, unsigned length)
739 {
740     // According to the HTTP specification EOL is defined as
741     // a CRLF pair. Unfortunately, some servers will use LF
742     // instead. Worse yet, some servers will use a combination
743     // of both (e.g. <header>CRLFLF<body>), so findEOL needs
744     // to be more forgiving. It will now accept CRLF, LF or
745     // CR.
746     //
747     // It returns NULL if EOLF is not found or it will return
748     // a pointer to the first terminating character.
749     for (unsigned i = 0; i < length; i++) {
750         if (bytes[i] == '\n')
751             return bytes + i;
752         if (bytes[i] == '\r') {
753             // Check to see if spanning buffer bounds
754             // (CRLF is across reads). If so, wait for
755             // next read.
756             if (i + 1 == length)
757                 break;
758 
759             return bytes + i;
760         }
761     }
762 
763     return 0;
764 }
765 
capitalizeRFC822HeaderFieldName(const String & name)766 static inline String capitalizeRFC822HeaderFieldName(const String& name)
767 {
768     bool capitalizeCharacter = true;
769     String result;
770 
771     for (unsigned i = 0; i < name.length(); i++) {
772         UChar c;
773 
774         if (capitalizeCharacter && name[i] >= 'a' && name[i] <= 'z')
775             c = toASCIIUpper(name[i]);
776         else if (!capitalizeCharacter && name[i] >= 'A' && name[i] <= 'Z')
777             c = toASCIILower(name[i]);
778         else
779             c = name[i];
780 
781         if (name[i] == '-')
782             capitalizeCharacter = true;
783         else
784             capitalizeCharacter = false;
785 
786         result.append(c);
787     }
788 
789     return result;
790 }
791 
parseRFC822HeaderFields(const Vector<char> & buffer,unsigned length)792 static inline HTTPHeaderMap parseRFC822HeaderFields(const Vector<char>& buffer, unsigned length)
793 {
794     const char* bytes = buffer.data();
795     const char* eol;
796     String lastKey;
797     HTTPHeaderMap headerFields;
798 
799     // Loop ove rlines until we're past the header, or we can't find any more end-of-lines
800     while ((eol = findEOL(bytes, length))) {
801         const char* line = bytes;
802         int lineLength = eol - bytes;
803 
804         // Move bytes to the character after the terminator as returned by findEOL.
805         bytes = eol + 1;
806         if ((*eol == '\r') && (*bytes == '\n'))
807             bytes++; // Safe since findEOL won't return a spanning CRLF.
808 
809         length -= (bytes - line);
810         if (lineLength == 0)
811             // Blank line; we're at the end of the header
812             break;
813         else if (*line == ' ' || *line == '\t') {
814             // Continuation of the previous header
815             if (lastKey.isNull()) {
816                 // malformed header; ignore it and continue
817                 continue;
818             } else {
819                 // Merge the continuation of the previous header
820                 String currentValue = headerFields.get(lastKey);
821                 String newValue(line, lineLength);
822 
823                 headerFields.set(lastKey, currentValue + newValue);
824             }
825         } else {
826             // Brand new header
827             const char* colon;
828             for (colon = line; *colon != ':' && colon != eol; colon++) {
829                 // empty loop
830             }
831             if (colon == eol)
832                 // malformed header; ignore it and continue
833                 continue;
834             else {
835                 lastKey = capitalizeRFC822HeaderFieldName(String(line, colon - line));
836                 String value;
837 
838                 for (colon++; colon != eol; colon++) {
839                     if (*colon != ' ' && *colon != '\t')
840                         break;
841                 }
842                 if (colon == eol)
843                     value = "";
844                 else
845                     value = String(colon, eol - colon);
846 
847                 String oldValue = headerFields.get(lastKey);
848                 if (!oldValue.isNull()) {
849                     String tmp = oldValue;
850                     tmp += ", ";
851                     tmp += value;
852                     value = tmp;
853                 }
854 
855                 headerFields.set(lastKey, value);
856             }
857         }
858     }
859 
860     return headerFields;
861 }
862 
handlePost(const char * url,const char * target,uint32 len,const char * buf,bool file,void * notifyData,bool sendNotification,bool allowHeaders)863 NPError PluginView::handlePost(const char* url, const char* target, uint32 len, const char* buf, bool file, void* notifyData, bool sendNotification, bool allowHeaders)
864 {
865     if (!url || !len || !buf)
866         return NPERR_INVALID_PARAM;
867 
868     FrameLoadRequest frameLoadRequest;
869 
870     HTTPHeaderMap headerFields;
871     Vector<char> buffer;
872 
873     if (file) {
874         NPError readResult = handlePostReadFile(buffer, len, buf);
875         if(readResult != NPERR_NO_ERROR)
876             return readResult;
877     } else {
878         buffer.resize(len);
879         memcpy(buffer.data(), buf, len);
880     }
881 
882     const char* postData = buffer.data();
883     int postDataLength = buffer.size();
884 
885     if (allowHeaders) {
886         if (startsWithBlankLine(buffer)) {
887             postData++;
888             postDataLength--;
889         } else {
890             int location = locationAfterFirstBlankLine(buffer);
891             if (location != -1) {
892                 // If the blank line is somewhere in the middle of the buffer, everything before is the header
893                 headerFields = parseRFC822HeaderFields(buffer, location);
894                 unsigned dataLength = buffer.size() - location;
895 
896                 // Sometimes plugins like to set Content-Length themselves when they post,
897                 // but WebFoundation does not like that. So we will remove the header
898                 // and instead truncate the data to the requested length.
899                 String contentLength = headerFields.get("Content-Length");
900 
901                 if (!contentLength.isNull())
902                     dataLength = min(contentLength.toInt(), (int)dataLength);
903                 headerFields.remove("Content-Length");
904 
905                 postData += location;
906                 postDataLength = dataLength;
907             }
908         }
909     }
910 
911     frameLoadRequest.resourceRequest().setHTTPMethod("POST");
912     frameLoadRequest.resourceRequest().setURL(makeURL(m_baseURL, url));
913     frameLoadRequest.resourceRequest().addHTTPHeaderFields(headerFields);
914     frameLoadRequest.resourceRequest().setHTTPBody(FormData::create(postData, postDataLength));
915     frameLoadRequest.setFrameName(target);
916 
917     return load(frameLoadRequest, sendNotification, notifyData);
918 }
919 
920 #ifdef PLUGIN_SCHEDULE_TIMER
scheduleTimer(NPP instance,uint32 interval,bool repeat,void (* timerFunc)(NPP,uint32 timerID))921 uint32 PluginView::scheduleTimer(NPP instance, uint32 interval, bool repeat,
922                                void (*timerFunc)(NPP, uint32 timerID))
923 {
924     return m_timerList.schedule(instance, interval, repeat, timerFunc);
925 }
926 
unscheduleTimer(NPP instance,uint32 timerID)927 void PluginView::unscheduleTimer(NPP instance, uint32 timerID)
928 {
929     m_timerList.unschedule(instance, timerID);
930 }
931 #endif
932 
invalidateWindowlessPluginRect(const IntRect & rect)933 void PluginView::invalidateWindowlessPluginRect(const IntRect& rect)
934 {
935     if (!isVisible())
936         return;
937 
938     if (!m_element->renderer())
939         return;
940     RenderBox* renderer = toRenderBox(m_element->renderer());
941 
942     IntRect dirtyRect = rect;
943     dirtyRect.move(renderer->borderLeft() + renderer->paddingLeft(), renderer->borderTop() + renderer->paddingTop());
944     renderer->repaintRectangle(dirtyRect);
945 }
946 
paintMissingPluginIcon(GraphicsContext * context,const IntRect & rect)947 void PluginView::paintMissingPluginIcon(GraphicsContext* context, const IntRect& rect)
948 {
949     static RefPtr<Image> nullPluginImage;
950     if (!nullPluginImage) {
951         nullPluginImage = Image::loadPlatformResource("nullPlugin");
952     }
953 
954     IntRect imageRect(frameRect().x(), frameRect().y(), nullPluginImage->width(), nullPluginImage->height());
955 
956     int xOffset = (frameRect().width() - imageRect.width()) / 2;
957     int yOffset = (frameRect().height() - imageRect.height()) / 2;
958 
959     imageRect.move(xOffset, yOffset);
960 
961     if (!rect.intersects(imageRect)) {
962         return;
963     }
964 
965     context->save();
966     context->clip(windowClipRect());
967     context->drawImage(nullPluginImage.get(), imageRect.location());
968     context->restore();
969 }
970 
971 } // namespace WebCore
972