initial import
[vuplus_webkit] / Source / WebCore / plugins / PluginView.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Collabora Ltd. All rights reserved.
4  * Copyright (C) 2010 Girish Ramakrishnan <girish@forwardbias.in>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
26  */
27
28 #include "config.h"
29 #include "PluginView.h"
30
31 #if USE(JSC)
32 #include "BridgeJSC.h"
33 #endif
34 #include "Chrome.h"
35 #include "CookieJar.h"
36 #include "Document.h"
37 #include "DocumentLoader.h"
38 #include "Element.h"
39 #include "FocusController.h"
40 #include "Frame.h"
41 #include "FrameLoader.h"
42 #include "FrameLoaderClient.h"
43 #include "FrameTree.h"
44 #include "FrameView.h"
45 #include "GraphicsContext.h"
46 #include "HTMLNames.h"
47 #include "HTMLPlugInElement.h"
48 #include "Image.h"
49 #include "KeyboardEvent.h"
50 #include "MIMETypeRegistry.h"
51 #include "MouseEvent.h"
52 #include "NotImplemented.h"
53 #include "Page.h"
54 #include "PlatformMouseEvent.h"
55 #include "PluginDatabase.h"
56 #include "PluginDebug.h"
57 #include "PluginMainThreadScheduler.h"
58 #include "PluginPackage.h"
59 #include "ProxyServer.h"
60 #include "RenderBox.h"
61 #include "RenderObject.h"
62 #include "ScriptValue.h"
63 #include "SecurityOrigin.h"
64 #include "Settings.h"
65 #include "npruntime_impl.h"
66 #include <wtf/ASCIICType.h>
67
68 #if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
69 #include "PluginMessageThrottlerWin.h"
70 #endif
71
72 #if USE(JSC)
73 #include "JSDOMBinding.h"
74 #include "JSDOMWindow.h"
75 #include "c_instance.h"
76 #include "runtime_root.h"
77 #include <runtime/JSLock.h>
78 #include <runtime/JSValue.h>
79
80 using JSC::ExecState;
81 using JSC::JSLock;
82 using JSC::JSObject;
83 using JSC::JSValue;
84 using JSC::UString;
85 #endif
86
87 #if ENABLE(NETSCAPE_PLUGIN_API)
88
89 using std::min;
90
91 using namespace WTF;
92
93 namespace WebCore {
94
95 using namespace HTMLNames;
96
97 static int s_callingPlugin;
98
99 typedef HashMap<NPP, PluginView*> InstanceMap;
100
101 static InstanceMap& instanceMap()
102 {
103     static InstanceMap& map = *new InstanceMap;
104     return map;
105 }
106
107 static String scriptStringIfJavaScriptURL(const KURL& url)
108 {
109     if (!protocolIsJavaScript(url))
110         return String();
111
112     // This returns an unescaped string
113     return decodeURLEscapeSequences(url.string().substring(11));
114 }
115
116 PluginView* PluginView::s_currentPluginView = 0;
117
118 void PluginView::popPopupsStateTimerFired(Timer<PluginView>*)
119 {
120     popPopupsEnabledState();
121 }
122
123 IntRect PluginView::windowClipRect() const
124 {
125     // Start by clipping to our bounds.
126     IntRect clipRect(m_windowRect);
127     
128     // Take our element and get the clip rect from the enclosing layer and frame view.
129     RenderLayer* layer = m_element->renderer()->enclosingLayer();
130     FrameView* parentView = m_element->document()->view();
131     clipRect.intersect(parentView->windowClipRectForLayer(layer, true));
132
133     return clipRect;
134 }
135
136 void PluginView::setFrameRect(const IntRect& rect)
137 {
138     if (m_element->document()->printing())
139         return;
140
141     if (rect != frameRect())
142         Widget::setFrameRect(rect);
143
144     updatePluginWidget();
145
146 #if OS(WINDOWS) || OS(SYMBIAN)
147     // On Windows and Symbian, always call plugin to change geometry.
148     setNPWindowRect(rect);
149 #elif defined(XP_UNIX)
150     // On Unix, multiple calls to setNPWindow() in windowed mode causes Flash to crash
151     if (m_mode == NP_FULL || !m_isWindowed)
152         setNPWindowRect(rect);
153 #endif
154 }
155
156 void PluginView::frameRectsChanged()
157 {
158     updatePluginWidget();
159 }
160
161 void PluginView::handleEvent(Event* event)
162 {
163     if (!m_plugin || m_isWindowed)
164         return;
165
166     // Protect the plug-in from deletion while dispatching the event.
167     RefPtr<PluginView> protect(this);
168
169     if (event->isMouseEvent())
170         handleMouseEvent(static_cast<MouseEvent*>(event));
171     else if (event->isKeyboardEvent())
172         handleKeyboardEvent(static_cast<KeyboardEvent*>(event));
173 #if defined(XP_UNIX) && ENABLE(NETSCAPE_PLUGIN_API)
174     else if (event->type() == eventNames().focusoutEvent)
175         handleFocusOutEvent();
176     else if (event->type() == eventNames().focusinEvent)
177         handleFocusInEvent();
178 #endif
179 }
180
181 void PluginView::init()
182 {
183     if (m_haveInitialized)
184         return;
185
186     m_haveInitialized = true;
187
188     if (!m_plugin) {
189         ASSERT(m_status == PluginStatusCanNotFindPlugin);
190         return;
191     }
192
193     LOG(Plugins, "PluginView::init(): Initializing plug-in '%s'", m_plugin->name().utf8().data());
194
195     if (!m_plugin->load()) {
196         m_plugin = 0;
197         m_status = PluginStatusCanNotLoadPlugin;
198         return;
199     }
200
201     if (!startOrAddToUnstartedList()) {
202         m_status = PluginStatusCanNotLoadPlugin;
203         return;
204     }
205
206     m_status = PluginStatusLoadedSuccessfully;
207 }
208
209 bool PluginView::startOrAddToUnstartedList()
210 {
211     if (!m_parentFrame->page())
212         return false;
213
214     // We only delay starting the plug-in if we're going to kick off the load
215     // ourselves. Otherwise, the loader will try to deliver data before we've
216     // started the plug-in.
217     if (!m_loadManually && !m_parentFrame->page()->canStartMedia()) {
218         m_parentFrame->document()->addMediaCanStartListener(this);
219         m_isWaitingToStart = true;
220         return true;
221     }
222
223     return start();
224 }
225
226 bool PluginView::start()
227 {
228     if (m_isStarted)
229         return false;
230
231     m_isWaitingToStart = false;
232
233     PluginMainThreadScheduler::scheduler().registerPlugin(m_instance);
234
235     ASSERT(m_plugin);
236     ASSERT(m_plugin->pluginFuncs()->newp);
237
238     NPError npErr;
239     {
240         PluginView::setCurrentPluginView(this);
241 #if USE(JSC)
242         JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
243 #endif
244         setCallingPlugin(true);
245         npErr = m_plugin->pluginFuncs()->newp((NPMIMEType)m_mimeType.utf8().data(), m_instance, m_mode, m_paramCount, m_paramNames, m_paramValues, NULL);
246         setCallingPlugin(false);
247         LOG_NPERROR(npErr);
248         PluginView::setCurrentPluginView(0);
249     }
250
251     if (npErr != NPERR_NO_ERROR) {
252         m_status = PluginStatusCanNotLoadPlugin;
253         PluginMainThreadScheduler::scheduler().unregisterPlugin(m_instance);
254         return false;
255     }
256
257     m_isStarted = true;
258
259     if (!m_url.isEmpty() && !m_loadManually) {
260         FrameLoadRequest frameLoadRequest(m_parentFrame->document()->securityOrigin());
261         frameLoadRequest.resourceRequest().setHTTPMethod("GET");
262         frameLoadRequest.resourceRequest().setURL(m_url);
263         load(frameLoadRequest, false, 0);
264     }
265
266     m_status = PluginStatusLoadedSuccessfully;
267
268     if (!platformStart())
269         m_status = PluginStatusCanNotLoadPlugin;
270
271     if (m_status != PluginStatusLoadedSuccessfully)
272         return false;
273
274     return true;
275 }
276
277 void PluginView::mediaCanStart()
278 {
279     ASSERT(!m_isStarted);
280     if (!start())
281         parentFrame()->loader()->client()->dispatchDidFailToStartPlugin(this);
282 }
283
284 PluginView::~PluginView()
285 {
286     LOG(Plugins, "PluginView::~PluginView()");
287
288     ASSERT(!m_lifeSupportTimer.isActive());
289
290     // If we failed to find the plug-in, we'll return early in our constructor, and
291     // m_instance will be 0.
292     if (m_instance)
293         instanceMap().remove(m_instance);
294
295     if (m_isWaitingToStart)
296         m_parentFrame->document()->removeMediaCanStartListener(this);
297
298     stop();
299
300     deleteAllValues(m_requests);
301
302     freeStringArray(m_paramNames, m_paramCount);
303     freeStringArray(m_paramValues, m_paramCount);
304
305     platformDestroy();
306
307     m_parentFrame->script()->cleanupScriptObjectsForPlugin(this);
308
309     if (m_plugin && !(m_plugin->quirks().contains(PluginQuirkDontUnloadPlugin)))
310         m_plugin->unload();
311 }
312
313 void PluginView::stop()
314 {
315     if (!m_isStarted)
316         return;
317
318     LOG(Plugins, "PluginView::stop(): Stopping plug-in '%s'", m_plugin->name().utf8().data());
319
320     HashSet<RefPtr<PluginStream> > streams = m_streams;
321     HashSet<RefPtr<PluginStream> >::iterator end = streams.end();
322     for (HashSet<RefPtr<PluginStream> >::iterator it = streams.begin(); it != end; ++it) {
323         (*it)->stop();
324         disconnectStream((*it).get());
325     }
326
327     ASSERT(m_streams.isEmpty());
328
329     m_isStarted = false;
330
331 #if USE(JSC)
332     JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
333 #endif
334
335 #if ENABLE(NETSCAPE_PLUGIN_API)
336 #if defined(XP_WIN) && !PLATFORM(GTK)
337     // Unsubclass the window
338     if (m_isWindowed) {
339 #if OS(WINCE)
340         WNDPROC currentWndProc = (WNDPROC)GetWindowLong(platformPluginWidget(), GWL_WNDPROC);
341
342         if (currentWndProc == PluginViewWndProc)
343             SetWindowLong(platformPluginWidget(), GWL_WNDPROC, (LONG)m_pluginWndProc);
344 #else
345         WNDPROC currentWndProc = (WNDPROC)GetWindowLongPtr(platformPluginWidget(), GWLP_WNDPROC);
346
347         if (currentWndProc == PluginViewWndProc)
348             SetWindowLongPtr(platformPluginWidget(), GWLP_WNDPROC, (LONG_PTR)m_pluginWndProc);
349 #endif
350     }
351 #endif // !defined(XP_WIN) || PLATFORM(GTK)
352 #endif // ENABLE(NETSCAPE_PLUGIN_API)
353
354 #if !defined(XP_MACOSX)
355     // Clear the window
356     m_npWindow.window = 0;
357
358     if (m_plugin->pluginFuncs()->setwindow && !m_plugin->quirks().contains(PluginQuirkDontSetNullWindowHandleOnDestroy)) {
359         PluginView::setCurrentPluginView(this);
360         setCallingPlugin(true);
361         m_plugin->pluginFuncs()->setwindow(m_instance, &m_npWindow);
362         setCallingPlugin(false);
363         PluginView::setCurrentPluginView(0);
364     }
365
366 #ifdef XP_UNIX
367     if (m_isWindowed && m_npWindow.ws_info)
368            delete (NPSetWindowCallbackStruct *)m_npWindow.ws_info;
369     m_npWindow.ws_info = 0;
370 #endif
371
372 #endif // !defined(XP_MACOSX)
373
374     PluginMainThreadScheduler::scheduler().unregisterPlugin(m_instance);
375
376     NPSavedData* savedData = 0;
377     PluginView::setCurrentPluginView(this);
378     setCallingPlugin(true);
379     NPError npErr = m_plugin->pluginFuncs()->destroy(m_instance, &savedData);
380     setCallingPlugin(false);
381     LOG_NPERROR(npErr);
382     PluginView::setCurrentPluginView(0);
383
384 #if ENABLE(NETSCAPE_PLUGIN_API)
385     if (savedData) {
386         // TODO: Actually save this data instead of just discarding it
387         if (savedData->buf)
388             NPN_MemFree(savedData->buf);
389         NPN_MemFree(savedData);
390     }
391 #endif
392
393     m_instance->pdata = 0;
394 }
395
396 void PluginView::setCurrentPluginView(PluginView* pluginView)
397 {
398     s_currentPluginView = pluginView;
399 }
400
401 PluginView* PluginView::currentPluginView()
402 {
403     return s_currentPluginView;
404 }
405
406 static char* createUTF8String(const String& str)
407 {
408     CString cstr = str.utf8();
409     char* result = reinterpret_cast<char*>(fastMalloc(cstr.length() + 1));
410
411     strncpy(result, cstr.data(), cstr.length() + 1);
412
413     return result;
414 }
415
416 void PluginView::performRequest(PluginRequest* request)
417 {
418     if (!m_isStarted)
419         return;
420
421     // don't let a plugin start any loads if it is no longer part of a document that is being 
422     // displayed unless the loads are in the same frame as the plugin.
423     const String& targetFrameName = request->frameLoadRequest().frameName();
424     if (m_parentFrame->loader()->documentLoader() != m_parentFrame->loader()->activeDocumentLoader() &&
425         (targetFrameName.isNull() || m_parentFrame->tree()->find(targetFrameName) != m_parentFrame))
426         return;
427
428     KURL requestURL = request->frameLoadRequest().resourceRequest().url();
429     String jsString = scriptStringIfJavaScriptURL(requestURL);
430
431     if (jsString.isNull()) {
432         // if this is not a targeted request, create a stream for it. otherwise,
433         // just pass it off to the loader
434         if (targetFrameName.isEmpty()) {
435             RefPtr<PluginStream> stream = PluginStream::create(this, m_parentFrame.get(), request->frameLoadRequest().resourceRequest(), request->sendNotification(), request->notifyData(), plugin()->pluginFuncs(), instance(), m_plugin->quirks());
436             m_streams.add(stream);
437             stream->start();
438         } else {
439             // If the target frame is our frame, we could destroy the
440             // PluginView, so we protect it. <rdar://problem/6991251>
441             RefPtr<PluginView> protect(this);
442
443             m_parentFrame->loader()->load(request->frameLoadRequest().resourceRequest(), targetFrameName, false);
444
445             // FIXME: <rdar://problem/4807469> This should be sent when the document has finished loading
446             if (request->sendNotification()) {
447                 PluginView::setCurrentPluginView(this);
448 #if USE(JSC)
449                 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
450 #endif
451                 setCallingPlugin(true);
452                 m_plugin->pluginFuncs()->urlnotify(m_instance, requestURL.string().utf8().data(), NPRES_DONE, request->notifyData());
453                 setCallingPlugin(false);
454                 PluginView::setCurrentPluginView(0);
455             }
456         }
457         return;
458     }
459
460     // Targeted JavaScript requests are only allowed on the frame that contains the JavaScript plugin
461     // and this has been made sure in ::load.
462     ASSERT(targetFrameName.isEmpty() || m_parentFrame->tree()->find(targetFrameName) == m_parentFrame);
463     
464     // Executing a script can cause the plugin view to be destroyed, so we keep a reference to it.
465     RefPtr<PluginView> protector(this);
466     ScriptValue result = m_parentFrame->script()->executeScript(jsString, request->shouldAllowPopups());
467
468     if (targetFrameName.isNull()) {
469         String resultString;
470
471 #if USE(JSC)
472         ScriptState* scriptState = m_parentFrame->script()->globalObject(pluginWorld())->globalExec();
473 #elif USE(V8)
474         ScriptState* scriptState = 0; // Not used with V8
475 #endif
476         CString cstr;
477         if (result.getString(scriptState, resultString))
478             cstr = resultString.utf8();
479
480         RefPtr<PluginStream> stream = PluginStream::create(this, m_parentFrame.get(), request->frameLoadRequest().resourceRequest(), request->sendNotification(), request->notifyData(), plugin()->pluginFuncs(), instance(), m_plugin->quirks());
481         m_streams.add(stream);
482         stream->sendJavaScriptStream(requestURL, cstr);
483     }
484 }
485
486 void PluginView::requestTimerFired(Timer<PluginView>* timer)
487 {
488     ASSERT(timer == &m_requestTimer);
489     ASSERT(m_requests.size() > 0);
490     ASSERT(!m_isJavaScriptPaused);
491
492     PluginRequest* request = m_requests[0];
493     m_requests.remove(0);
494     
495     // Schedule a new request before calling performRequest since the call to
496     // performRequest can cause the plugin view to be deleted.
497     if (m_requests.size() > 0)
498         m_requestTimer.startOneShot(0);
499
500     performRequest(request);
501     delete request;
502 }
503
504 void PluginView::scheduleRequest(PluginRequest* request)
505 {
506     m_requests.append(request);
507
508     if (!m_isJavaScriptPaused)
509         m_requestTimer.startOneShot(0);
510 }
511
512 NPError PluginView::load(const FrameLoadRequest& frameLoadRequest, bool sendNotification, void* notifyData)
513 {
514     ASSERT(frameLoadRequest.resourceRequest().httpMethod() == "GET" || frameLoadRequest.resourceRequest().httpMethod() == "POST");
515
516     KURL url = frameLoadRequest.resourceRequest().url();
517     
518     if (url.isEmpty())
519         return NPERR_INVALID_URL;
520
521     // Don't allow requests to be made when the document loader is stopping all loaders.
522     DocumentLoader* loader = m_parentFrame->loader()->documentLoader();
523     if (!loader || loader->isStopping())
524         return NPERR_GENERIC_ERROR;
525
526     const String& targetFrameName = frameLoadRequest.frameName();
527     String jsString = scriptStringIfJavaScriptURL(url);
528
529     if (!jsString.isNull()) {
530         // Return NPERR_GENERIC_ERROR if JS is disabled. This is what Mozilla does.
531         if (!m_parentFrame->script()->canExecuteScripts(NotAboutToExecuteScript))
532             return NPERR_GENERIC_ERROR;
533
534         // For security reasons, only allow JS requests to be made on the frame that contains the plug-in.
535         if (!targetFrameName.isNull() && m_parentFrame->tree()->find(targetFrameName) != m_parentFrame)
536             return NPERR_INVALID_PARAM;
537     } else if (!m_parentFrame->document()->securityOrigin()->canDisplay(url))
538         return NPERR_GENERIC_ERROR;
539
540     PluginRequest* request = new PluginRequest(frameLoadRequest, sendNotification, notifyData, arePopupsAllowed());
541     scheduleRequest(request);
542
543     return NPERR_NO_ERROR;
544 }
545
546 static KURL makeURL(const KURL& baseURL, const char* relativeURLString)
547 {
548     String urlString = relativeURLString;
549
550     // Strip return characters.
551     urlString.replace('\n', "");
552     urlString.replace('\r', "");
553
554     return KURL(baseURL, urlString);
555 }
556
557 NPError PluginView::getURLNotify(const char* url, const char* target, void* notifyData)
558 {
559     FrameLoadRequest frameLoadRequest(m_parentFrame->document()->securityOrigin());
560
561     frameLoadRequest.setFrameName(target);
562     frameLoadRequest.resourceRequest().setHTTPMethod("GET");
563     frameLoadRequest.resourceRequest().setURL(makeURL(m_baseURL, url));
564
565     return load(frameLoadRequest, true, notifyData);
566 }
567
568 NPError PluginView::getURL(const char* url, const char* target)
569 {
570     FrameLoadRequest frameLoadRequest(m_parentFrame->document()->securityOrigin());
571
572     frameLoadRequest.setFrameName(target);
573     frameLoadRequest.resourceRequest().setHTTPMethod("GET");
574     frameLoadRequest.resourceRequest().setURL(makeURL(m_baseURL, url));
575
576     return load(frameLoadRequest, false, 0);
577 }
578
579 NPError PluginView::postURLNotify(const char* url, const char* target, uint32_t len, const char* buf, NPBool file, void* notifyData)
580 {
581     return handlePost(url, target, len, buf, file, notifyData, true, true);
582 }
583
584 NPError PluginView::postURL(const char* url, const char* target, uint32_t len, const char* buf, NPBool file)
585 {
586     // As documented, only allow headers to be specified via NPP_PostURL when using a file.
587     return handlePost(url, target, len, buf, file, 0, false, file);
588 }
589
590 NPError PluginView::newStream(NPMIMEType type, const char* target, NPStream** stream)
591 {
592     notImplemented();
593     // Unsupported
594     return NPERR_GENERIC_ERROR;
595 }
596
597 int32_t PluginView::write(NPStream* stream, int32_t len, void* buffer)
598 {
599     notImplemented();
600     // Unsupported
601     return -1;
602 }
603
604 NPError PluginView::destroyStream(NPStream* stream, NPReason reason)
605 {
606     if (!stream || PluginStream::ownerForStream(stream) != m_instance)
607         return NPERR_INVALID_INSTANCE_ERROR;
608
609     PluginStream* browserStream = static_cast<PluginStream*>(stream->ndata);
610     browserStream->cancelAndDestroyStream(reason);
611
612     return NPERR_NO_ERROR;
613 }
614
615 void PluginView::status(const char* message)
616 {
617     if (Page* page = m_parentFrame->page())
618         page->chrome()->setStatusbarText(m_parentFrame.get(), String::fromUTF8(message));
619 }
620
621 NPError PluginView::setValue(NPPVariable variable, void* value)
622 {
623     LOG(Plugins, "PluginView::setValue(%s): ", prettyNameForNPPVariable(variable, value).data());
624
625     switch (variable) {
626     case NPPVpluginWindowBool:
627         m_isWindowed = value;
628         return NPERR_NO_ERROR;
629     case NPPVpluginTransparentBool:
630         m_isTransparent = value;
631         return NPERR_NO_ERROR;
632 #if defined(XP_MACOSX)
633     case NPPVpluginDrawingModel: {
634         // Can only set drawing model inside NPP_New()
635         if (this != currentPluginView())
636            return NPERR_GENERIC_ERROR;
637
638         NPDrawingModel newDrawingModel = NPDrawingModel(uintptr_t(value));
639         switch (newDrawingModel) {
640         case NPDrawingModelCoreGraphics:
641             m_drawingModel = newDrawingModel;
642             return NPERR_NO_ERROR;
643 #ifndef NP_NO_QUICKDRAW
644         case NPDrawingModelQuickDraw:
645 #endif
646         case NPDrawingModelCoreAnimation:
647         default:
648             LOG(Plugins, "Plugin asked for unsupported drawing model: %s",
649                     prettyNameForDrawingModel(newDrawingModel));
650             return NPERR_GENERIC_ERROR;
651         }
652     }
653
654     case NPPVpluginEventModel: {
655         // Can only set event model inside NPP_New()
656         if (this != currentPluginView())
657            return NPERR_GENERIC_ERROR;
658
659         NPEventModel newEventModel = NPEventModel(uintptr_t(value));
660         switch (newEventModel) {
661 #ifndef NP_NO_CARBON
662         case NPEventModelCarbon:
663 #endif
664         case NPEventModelCocoa:
665             m_eventModel = newEventModel;
666             return NPERR_NO_ERROR;
667
668         default:
669             LOG(Plugins, "Plugin asked for unsupported event model: %s",
670                     prettyNameForEventModel(newEventModel));
671             return NPERR_GENERIC_ERROR;
672         }
673     }
674 #endif // defined(XP_MACOSX)
675
676 #if PLATFORM(QT) && defined(MOZ_PLATFORM_MAEMO) && (MOZ_PLATFORM_MAEMO >= 5)
677     case NPPVpluginWindowlessLocalBool:
678         m_renderToImage = true;
679         return NPERR_NO_ERROR;
680 #endif
681
682     default:
683         notImplemented();
684         return NPERR_GENERIC_ERROR;
685     }
686 }
687
688 void PluginView::invalidateTimerFired(Timer<PluginView>* timer)
689 {
690     ASSERT(timer == &m_invalidateTimer);
691
692     for (unsigned i = 0; i < m_invalidRects.size(); i++)
693         invalidateRect(m_invalidRects[i]);
694     m_invalidRects.clear();
695 }
696
697
698 void PluginView::pushPopupsEnabledState(bool state)
699 {
700     m_popupStateStack.append(state);
701 }
702  
703 void PluginView::popPopupsEnabledState()
704 {
705     m_popupStateStack.removeLast();
706 }
707
708 bool PluginView::arePopupsAllowed() const
709 {
710     if (!m_popupStateStack.isEmpty())
711         return m_popupStateStack.last();
712
713     return false;
714 }
715
716 void PluginView::setJavaScriptPaused(bool paused)
717 {
718     if (m_isJavaScriptPaused == paused)
719         return;
720     m_isJavaScriptPaused = paused;
721
722     if (m_isJavaScriptPaused)
723         m_requestTimer.stop();
724     else if (!m_requests.isEmpty())
725         m_requestTimer.startOneShot(0);
726 }
727
728 #if ENABLE(NETSCAPE_PLUGIN_API)
729 NPObject* PluginView::npObject()
730 {
731     NPObject* object = 0;
732
733     if (!m_isStarted || !m_plugin || !m_plugin->pluginFuncs()->getvalue)
734         return 0;
735
736     // On Windows, calling Java's NPN_GetValue can allow the message loop to
737     // run, allowing loading to take place or JavaScript to run. Protect the
738     // PluginView from destruction. <rdar://problem/6978804>
739     RefPtr<PluginView> protect(this);
740
741     NPError npErr;
742     {
743         PluginView::setCurrentPluginView(this);
744 #if USE(JSC)
745         JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
746 #endif
747         setCallingPlugin(true);
748         npErr = m_plugin->pluginFuncs()->getvalue(m_instance, NPPVpluginScriptableNPObject, &object);
749         setCallingPlugin(false);
750         PluginView::setCurrentPluginView(0);
751     }
752
753     if (npErr != NPERR_NO_ERROR)
754         return 0;
755
756     return object;
757 }
758 #endif
759
760 #if USE(JSC)
761 PassRefPtr<JSC::Bindings::Instance> PluginView::bindingInstance()
762 {
763 #if ENABLE(NETSCAPE_PLUGIN_API)
764     NPObject* object = npObject();
765     if (!object)
766         return 0;
767
768     if (hasOneRef()) {
769         // The renderer for the PluginView was destroyed during the above call, and
770         // the PluginView will be destroyed when this function returns, so we
771         // return null.
772         return 0;
773     }
774
775     RefPtr<JSC::Bindings::RootObject> root = m_parentFrame->script()->createRootObject(this);
776     RefPtr<JSC::Bindings::Instance> instance = JSC::Bindings::CInstance::create(object, root.release());
777
778     _NPN_ReleaseObject(object);
779
780     return instance.release();
781 #else
782     return 0;
783 #endif
784 }
785 #endif
786
787 void PluginView::disconnectStream(PluginStream* stream)
788 {
789     ASSERT(m_streams.contains(stream));
790
791     m_streams.remove(stream);
792 }
793
794 void PluginView::setParameters(const Vector<String>& paramNames, const Vector<String>& paramValues)
795 {
796     ASSERT(paramNames.size() == paramValues.size());
797
798     unsigned size = paramNames.size();
799     unsigned paramCount = 0;
800
801     m_paramNames = reinterpret_cast<char**>(fastMalloc(sizeof(char*) * size));
802     m_paramValues = reinterpret_cast<char**>(fastMalloc(sizeof(char*) * size));
803
804     for (unsigned i = 0; i < size; i++) {
805         if (m_plugin->quirks().contains(PluginQuirkRemoveWindowlessVideoParam) && equalIgnoringCase(paramNames[i], "windowlessvideo"))
806             continue;
807
808         if (paramNames[i] == "pluginspage")
809             m_pluginsPage = paramValues[i];
810
811         m_paramNames[paramCount] = createUTF8String(paramNames[i]);
812         m_paramValues[paramCount] = createUTF8String(paramValues[i]);
813
814         paramCount++;
815     }
816
817     m_paramCount = paramCount;
818 }
819
820 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)
821     : m_parentFrame(parentFrame)
822     , m_plugin(plugin)
823     , m_element(element)
824     , m_isStarted(false)
825     , m_url(url)
826     , m_baseURL(m_parentFrame->document()->baseURL()) // FIXME: No need for this member variable!
827     , m_status(PluginStatusLoadedSuccessfully)
828     , m_requestTimer(this, &PluginView::requestTimerFired)
829     , m_invalidateTimer(this, &PluginView::invalidateTimerFired)
830     , m_popPopupsStateTimer(this, &PluginView::popPopupsStateTimerFired)
831     , m_lifeSupportTimer(this, &PluginView::lifeSupportTimerFired)
832     , m_mode(loadManually ? NP_FULL : NP_EMBED)
833     , m_paramNames(0)
834     , m_paramValues(0)
835     , m_mimeType(mimeType)
836     , m_instance(0)
837 #if defined(XP_MACOSX)
838     , m_isWindowed(false)
839 #else
840     , m_isWindowed(true)
841 #endif
842     , m_isTransparent(false)
843     , m_haveInitialized(false)
844     , m_isWaitingToStart(false)
845 #if defined(XP_UNIX)
846     , m_needsXEmbed(false)
847 #endif
848 #if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
849     , m_pluginWndProc(0)
850     , m_lastMessage(0)
851     , m_isCallingPluginWndProc(false)
852     , m_wmPrintHDC(0)
853     , m_haveUpdatedPluginWidget(false)
854 #endif
855 #if (PLATFORM(QT) && OS(WINDOWS)) || defined(XP_MACOSX) || PLATFORM(EFL)
856     , m_window(0)
857 #endif
858 #if defined(XP_MACOSX)
859     , m_drawingModel(NPDrawingModel(-1))
860     , m_eventModel(NPEventModel(-1))
861     , m_contextRef(0)
862     , m_fakeWindow(0)
863 #endif
864 #if defined(XP_UNIX) && ENABLE(NETSCAPE_PLUGIN_API)
865     , m_hasPendingGeometryChange(true)
866     , m_drawable(0)
867     , m_visual(0)
868     , m_colormap(0)
869     , m_pluginDisplay(0)
870 #endif
871 #if PLATFORM(QT) && defined(MOZ_PLATFORM_MAEMO) && (MOZ_PLATFORM_MAEMO >= 5)
872     , m_renderToImage(false)
873 #endif
874     , m_loadManually(loadManually)
875     , m_manualStream(0)
876     , m_isJavaScriptPaused(false)
877     , m_haveCalledSetWindow(false)
878 {
879     if (!m_plugin) {
880         m_status = PluginStatusCanNotFindPlugin;
881         return;
882     }
883
884     m_instance = &m_instanceStruct;
885     m_instance->ndata = this;
886     m_instance->pdata = 0;
887
888     instanceMap().add(m_instance, this);
889
890     setParameters(paramNames, paramValues);
891
892     memset(&m_npWindow, 0, sizeof(m_npWindow));
893 #if defined(XP_MACOSX)
894     memset(&m_npCgContext, 0, sizeof(m_npCgContext));
895 #endif
896
897     resize(size);
898 }
899
900 void PluginView::focusPluginElement()
901 {
902     // Focus the plugin
903     if (Page* page = m_parentFrame->page())
904         page->focusController()->setFocusedNode(m_element, m_parentFrame);
905     else
906         m_parentFrame->document()->setFocusedNode(m_element);
907 }
908
909 void PluginView::didReceiveResponse(const ResourceResponse& response)
910 {
911     if (m_status != PluginStatusLoadedSuccessfully)
912         return;
913
914     ASSERT(m_loadManually);
915     ASSERT(!m_manualStream);
916
917     m_manualStream = PluginStream::create(this, m_parentFrame.get(), m_parentFrame->loader()->activeDocumentLoader()->request(), false, 0, plugin()->pluginFuncs(), instance(), m_plugin->quirks());
918     m_manualStream->setLoadManually(true);
919
920     m_manualStream->didReceiveResponse(0, response);
921 }
922
923 void PluginView::didReceiveData(const char* data, int length)
924 {
925     if (m_status != PluginStatusLoadedSuccessfully)
926         return;
927
928     ASSERT(m_loadManually);
929     ASSERT(m_manualStream);
930     
931     m_manualStream->didReceiveData(0, data, length);
932 }
933
934 void PluginView::didFinishLoading()
935 {
936     if (m_status != PluginStatusLoadedSuccessfully)
937         return;
938
939     ASSERT(m_loadManually);
940     ASSERT(m_manualStream);
941
942     m_manualStream->didFinishLoading(0);
943 }
944
945 void PluginView::didFail(const ResourceError& error)
946 {
947     if (m_status != PluginStatusLoadedSuccessfully)
948         return;
949
950     ASSERT(m_loadManually);
951     
952     if (m_manualStream)
953         m_manualStream->didFail(0, error);
954 }
955
956 void PluginView::setCallingPlugin(bool b) const
957 {
958     if (!m_plugin->quirks().contains(PluginQuirkHasModalMessageLoop))
959         return;
960
961     if (b)
962         ++s_callingPlugin;
963     else
964         --s_callingPlugin;
965
966     ASSERT(s_callingPlugin >= 0);
967 }
968
969 bool PluginView::isCallingPlugin()
970 {
971     return s_callingPlugin > 0;
972 }
973
974 PassRefPtr<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)
975 {
976     // if we fail to find a plugin for this MIME type, findPlugin will search for
977     // a plugin by the file extension and update the MIME type, so pass a mutable String
978     String mimeTypeCopy = mimeType;
979     PluginPackage* plugin = PluginDatabase::installedPlugins()->findPlugin(url, mimeTypeCopy);
980
981     // No plugin was found, try refreshing the database and searching again
982     if (!plugin && PluginDatabase::installedPlugins()->refresh()) {
983         mimeTypeCopy = mimeType;
984         plugin = PluginDatabase::installedPlugins()->findPlugin(url, mimeTypeCopy);
985     }
986
987     return adoptRef(new PluginView(parentFrame, size, plugin, element, url, paramNames, paramValues, mimeTypeCopy, loadManually));
988 }
989
990 void PluginView::freeStringArray(char** stringArray, int length)
991 {
992     if (!stringArray)
993         return;
994
995     for (int i = 0; i < length; i++)
996         fastFree(stringArray[i]);
997
998     fastFree(stringArray);
999 }
1000
1001 static inline bool startsWithBlankLine(const Vector<char>& buffer)
1002 {
1003     return buffer.size() > 0 && buffer[0] == '\n';
1004 }
1005
1006 static inline int locationAfterFirstBlankLine(const Vector<char>& buffer)
1007 {
1008     const char* bytes = buffer.data();
1009     unsigned length = buffer.size();
1010
1011     for (unsigned i = 0; i < length - 4; i++) {
1012         // Support for Acrobat. It sends "\n\n".
1013         if (bytes[i] == '\n' && bytes[i + 1] == '\n')
1014             return i + 2;
1015         
1016         // Returns the position after 2 CRLF's or 1 CRLF if it is the first line.
1017         if (bytes[i] == '\r' && bytes[i + 1] == '\n') {
1018             i += 2;
1019             if (i == 2)
1020                 return i;
1021             else if (bytes[i] == '\n')
1022                 // Support for Director. It sends "\r\n\n" (3880387).
1023                 return i + 1;
1024             else if (bytes[i] == '\r' && bytes[i + 1] == '\n')
1025                 // Support for Flash. It sends "\r\n\r\n" (3758113).
1026                 return i + 2;
1027         }
1028     }
1029
1030     return -1;
1031 }
1032
1033 static inline const char* findEOL(const char* bytes, unsigned length)
1034 {
1035     // According to the HTTP specification EOL is defined as
1036     // a CRLF pair. Unfortunately, some servers will use LF
1037     // instead. Worse yet, some servers will use a combination
1038     // of both (e.g. <header>CRLFLF<body>), so findEOL needs
1039     // to be more forgiving. It will now accept CRLF, LF or
1040     // CR.
1041     //
1042     // It returns NULL if EOLF is not found or it will return
1043     // a pointer to the first terminating character.
1044     for (unsigned i = 0; i < length; i++) {
1045         if (bytes[i] == '\n')
1046             return bytes + i;
1047         if (bytes[i] == '\r') {
1048             // Check to see if spanning buffer bounds
1049             // (CRLF is across reads). If so, wait for
1050             // next read.
1051             if (i + 1 == length)
1052                 break;
1053
1054             return bytes + i;
1055         }
1056     }
1057
1058     return 0;
1059 }
1060
1061 static inline String capitalizeRFC822HeaderFieldName(const String& name)
1062 {
1063     bool capitalizeCharacter = true;
1064     String result;
1065
1066     for (unsigned i = 0; i < name.length(); i++) {
1067         UChar c;
1068
1069         if (capitalizeCharacter && name[i] >= 'a' && name[i] <= 'z')
1070             c = toASCIIUpper(name[i]);
1071         else if (!capitalizeCharacter && name[i] >= 'A' && name[i] <= 'Z')
1072             c = toASCIILower(name[i]);
1073         else
1074             c = name[i];
1075
1076         if (name[i] == '-')
1077             capitalizeCharacter = true;
1078         else
1079             capitalizeCharacter = false;
1080
1081         result.append(c);
1082     }
1083
1084     return result;
1085 }
1086
1087 static inline HTTPHeaderMap parseRFC822HeaderFields(const Vector<char>& buffer, unsigned length)
1088 {
1089     const char* bytes = buffer.data();
1090     const char* eol;
1091     String lastKey;
1092     HTTPHeaderMap headerFields;
1093
1094     // Loop ove rlines until we're past the header, or we can't find any more end-of-lines
1095     while ((eol = findEOL(bytes, length))) {
1096         const char* line = bytes;
1097         int lineLength = eol - bytes;
1098         
1099         // Move bytes to the character after the terminator as returned by findEOL.
1100         bytes = eol + 1;
1101         if ((*eol == '\r') && (*bytes == '\n'))
1102             bytes++; // Safe since findEOL won't return a spanning CRLF.
1103
1104         length -= (bytes - line);
1105         if (lineLength == 0)
1106             // Blank line; we're at the end of the header
1107             break;
1108         else if (*line == ' ' || *line == '\t') {
1109             // Continuation of the previous header
1110             if (lastKey.isNull()) {
1111                 // malformed header; ignore it and continue
1112                 continue;
1113             } else {
1114                 // Merge the continuation of the previous header
1115                 String currentValue = headerFields.get(lastKey);
1116                 String newValue(line, lineLength);
1117
1118                 headerFields.set(lastKey, currentValue + newValue);
1119             }
1120         } else {
1121             // Brand new header
1122             const char* colon;
1123             for (colon = line; *colon != ':' && colon != eol; colon++) {
1124                 // empty loop
1125             }
1126             if (colon == eol) 
1127                 // malformed header; ignore it and continue
1128                 continue;
1129             else {
1130                 lastKey = capitalizeRFC822HeaderFieldName(String(line, colon - line));
1131                 String value;
1132
1133                 for (colon++; colon != eol; colon++) {
1134                     if (*colon != ' ' && *colon != '\t')
1135                         break;
1136                 }
1137                 if (colon == eol)
1138                     value = "";
1139                 else
1140                     value = String(colon, eol - colon);
1141
1142                 String oldValue = headerFields.get(lastKey);
1143                 if (!oldValue.isNull()) {
1144                     String tmp = oldValue;
1145                     tmp += ", ";
1146                     tmp += value;
1147                     value = tmp;
1148                 }
1149
1150                 headerFields.set(lastKey, value);
1151             }
1152         }
1153     }
1154
1155     return headerFields;
1156 }
1157
1158 NPError PluginView::handlePost(const char* url, const char* target, uint32_t len, const char* buf, bool file, void* notifyData, bool sendNotification, bool allowHeaders)
1159 {
1160     if (!url || !len || !buf)
1161         return NPERR_INVALID_PARAM;
1162
1163     FrameLoadRequest frameLoadRequest(m_parentFrame->document()->securityOrigin());
1164
1165     HTTPHeaderMap headerFields;
1166     Vector<char> buffer;
1167     
1168     if (file) {
1169         NPError readResult = handlePostReadFile(buffer, len, buf);
1170         if(readResult != NPERR_NO_ERROR)
1171             return readResult;
1172     } else {
1173         buffer.resize(len);
1174         memcpy(buffer.data(), buf, len);
1175     }
1176
1177     const char* postData = buffer.data();
1178     int postDataLength = buffer.size();
1179
1180     if (allowHeaders) {
1181         if (startsWithBlankLine(buffer)) {
1182             postData++;
1183             postDataLength--;
1184         } else {
1185             int location = locationAfterFirstBlankLine(buffer);
1186             if (location != -1) {
1187                 // If the blank line is somewhere in the middle of the buffer, everything before is the header
1188                 headerFields = parseRFC822HeaderFields(buffer, location);
1189                 unsigned dataLength = buffer.size() - location;
1190
1191                 // Sometimes plugins like to set Content-Length themselves when they post,
1192                 // but WebFoundation does not like that. So we will remove the header
1193                 // and instead truncate the data to the requested length.
1194                 String contentLength = headerFields.get("Content-Length");
1195
1196                 if (!contentLength.isNull())
1197                     dataLength = min(contentLength.toInt(), (int)dataLength);
1198                 headerFields.remove("Content-Length");
1199
1200                 postData += location;
1201                 postDataLength = dataLength;
1202             }
1203         }
1204     }
1205
1206     frameLoadRequest.resourceRequest().setHTTPMethod("POST");
1207     frameLoadRequest.resourceRequest().setURL(makeURL(m_baseURL, url));
1208     frameLoadRequest.resourceRequest().addHTTPHeaderFields(headerFields);
1209     frameLoadRequest.resourceRequest().setHTTPBody(FormData::create(postData, postDataLength));
1210     frameLoadRequest.setFrameName(target);
1211
1212     return load(frameLoadRequest, sendNotification, notifyData);
1213 }
1214
1215 void PluginView::invalidateWindowlessPluginRect(const IntRect& rect)
1216 {
1217     if (!isVisible())
1218         return;
1219     
1220     if (!m_element->renderer())
1221         return;
1222     RenderBox* renderer = toRenderBox(m_element->renderer());
1223     
1224     IntRect dirtyRect = rect;
1225     dirtyRect.move(renderer->borderLeft() + renderer->paddingLeft(), renderer->borderTop() + renderer->paddingTop());
1226     renderer->repaintRectangle(dirtyRect);
1227 }
1228
1229 void PluginView::paintMissingPluginIcon(GraphicsContext* context, const IntRect& rect)
1230 {
1231     static RefPtr<Image> nullPluginImage;
1232     if (!nullPluginImage)
1233         nullPluginImage = Image::loadPlatformResource("nullPlugin");
1234
1235     IntRect imageRect(frameRect().x(), frameRect().y(), nullPluginImage->width(), nullPluginImage->height());
1236
1237     int xOffset = (frameRect().width() - imageRect.width()) / 2;
1238     int yOffset = (frameRect().height() - imageRect.height()) / 2;
1239
1240     imageRect.move(xOffset, yOffset);
1241
1242     if (!rect.intersects(imageRect))
1243         return;
1244
1245     context->save();
1246     context->clip(windowClipRect());
1247     context->drawImage(nullPluginImage.get(), ColorSpaceDeviceRGB, imageRect.location());
1248     context->restore();
1249 }
1250
1251 static const char* MozillaUserAgent = "Mozilla/5.0 ("
1252 #if defined(XP_MACOSX)
1253         "Macintosh; U; Intel Mac OS X;"
1254 #elif defined(XP_WIN)
1255         "Windows; U; Windows NT 5.1;"
1256 #elif defined(XP_UNIX)
1257 // The Gtk port uses X11 plugins in Mac.
1258 #if OS(DARWIN) && PLATFORM(GTK)
1259     "X11; U; Intel Mac OS X;"
1260 #else
1261     "X11; U; Linux i686;"
1262 #endif
1263 #endif
1264         " en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0";
1265
1266 const char* PluginView::userAgent()
1267 {
1268     if (m_plugin->quirks().contains(PluginQuirkWantsMozillaUserAgent))
1269         return MozillaUserAgent;
1270
1271     if (m_userAgent.isNull())
1272         m_userAgent = m_parentFrame->loader()->userAgent(m_url).utf8();
1273
1274     return m_userAgent.data();
1275 }
1276
1277 #if ENABLE(NETSCAPE_PLUGIN_API)
1278 const char* PluginView::userAgentStatic()
1279 {
1280     return MozillaUserAgent;
1281 }
1282 #endif
1283
1284
1285 void PluginView::lifeSupportTimerFired(Timer<PluginView>*)
1286 {
1287     deref();
1288 }
1289
1290 void PluginView::keepAlive()
1291 {
1292     if (m_lifeSupportTimer.isActive())
1293         return;
1294
1295     ref();
1296     m_lifeSupportTimer.startOneShot(0);
1297 }
1298
1299 #if ENABLE(NETSCAPE_PLUGIN_API)
1300 void PluginView::keepAlive(NPP instance)
1301 {
1302     PluginView* view = instanceMap().get(instance);
1303     if (!view)
1304         return;
1305
1306     view->keepAlive();
1307 }
1308
1309 NPError PluginView::getValueStatic(NPNVariable variable, void* value)
1310 {
1311     LOG(Plugins, "PluginView::getValueStatic(%s)", prettyNameForNPNVariable(variable).data());
1312
1313     NPError result;
1314     if (platformGetValueStatic(variable, value, &result))
1315         return result;
1316
1317     return NPERR_GENERIC_ERROR;
1318 }
1319
1320 NPError PluginView::getValue(NPNVariable variable, void* value)
1321 {
1322     LOG(Plugins, "PluginView::getValue(%s)", prettyNameForNPNVariable(variable).data());
1323
1324     NPError result;
1325     if (platformGetValue(variable, value, &result))
1326         return result;
1327
1328     if (platformGetValueStatic(variable, value, &result))
1329         return result;
1330
1331     switch (variable) {
1332     case NPNVWindowNPObject: {
1333         if (m_isJavaScriptPaused)
1334             return NPERR_GENERIC_ERROR;
1335
1336         NPObject* windowScriptObject = m_parentFrame->script()->windowScriptNPObject();
1337
1338         // Return value is expected to be retained, as described here: <http://www.mozilla.org/projects/plugin/npruntime.html>
1339         if (windowScriptObject)
1340             _NPN_RetainObject(windowScriptObject);
1341
1342         void** v = (void**)value;
1343         *v = windowScriptObject;
1344
1345         return NPERR_NO_ERROR;
1346     }
1347
1348     case NPNVPluginElementNPObject: {
1349         if (m_isJavaScriptPaused)
1350             return NPERR_GENERIC_ERROR;
1351
1352         NPObject* pluginScriptObject = 0;
1353
1354         if (m_element->hasTagName(appletTag) || m_element->hasTagName(embedTag) || m_element->hasTagName(objectTag))
1355             pluginScriptObject = static_cast<HTMLPlugInElement*>(m_element)->getNPObject();
1356
1357         // Return value is expected to be retained, as described here: <http://www.mozilla.org/projects/plugin/npruntime.html>
1358         if (pluginScriptObject)
1359             _NPN_RetainObject(pluginScriptObject);
1360
1361         void** v = (void**)value;
1362         *v = pluginScriptObject;
1363
1364         return NPERR_NO_ERROR;
1365     }
1366
1367     case NPNVprivateModeBool: {
1368         Page* page = m_parentFrame->page();
1369         if (!page)
1370             return NPERR_GENERIC_ERROR;
1371         *((NPBool*)value) = !page->settings() || page->settings()->privateBrowsingEnabled();
1372         return NPERR_NO_ERROR;
1373     }
1374
1375     default:
1376         return NPERR_GENERIC_ERROR;
1377     }
1378 }
1379
1380 static Frame* getFrame(Frame* parentFrame, Element* element)
1381 {
1382     if (parentFrame)
1383         return parentFrame;
1384     
1385     Document* document = element->document();
1386     if (!document)
1387         document = element->ownerDocument();
1388     if (document)
1389         return document->frame();
1390     
1391     return 0;
1392 }
1393
1394 NPError PluginView::getValueForURL(NPNURLVariable variable, const char* url, char** value, uint32_t* len)
1395 {
1396     LOG(Plugins, "PluginView::getValueForURL(%s)", prettyNameForNPNURLVariable(variable).data());
1397
1398     NPError result = NPERR_NO_ERROR;
1399
1400     switch (variable) {
1401     case NPNURLVCookie: {
1402         KURL u(m_baseURL, url);
1403         if (u.isValid()) {
1404             Frame* frame = getFrame(parentFrame(), m_element);
1405             if (frame) {
1406                 const CString cookieStr = cookies(frame->document(), u).utf8();
1407                 if (!cookieStr.isNull()) {
1408                     const int size = cookieStr.length();
1409                     *value = static_cast<char*>(NPN_MemAlloc(size+1));
1410                     if (*value) {
1411                         memset(*value, 0, size+1);
1412                         memcpy(*value, cookieStr.data(), size+1);
1413                         if (len)
1414                             *len = size;
1415                     } else
1416                         result = NPERR_OUT_OF_MEMORY_ERROR;
1417                 }
1418             }
1419         } else
1420             result = NPERR_INVALID_URL;
1421         break;
1422     }
1423     case NPNURLVProxy: {
1424         KURL u(m_baseURL, url);
1425         if (u.isValid()) {
1426             Frame* frame = getFrame(parentFrame(), m_element);
1427             const FrameLoader* frameLoader = frame ? frame->loader() : 0;
1428             const NetworkingContext* context = frameLoader ? frameLoader->networkingContext() : 0;
1429             const CString proxyStr = toString(proxyServersForURL(u, context)).utf8();
1430             if (!proxyStr.isNull()) {
1431                 const int size = proxyStr.length();
1432                 *value = static_cast<char*>(NPN_MemAlloc(size+1));
1433                 if (*value) {
1434                     memset(*value, 0, size+1);
1435                     memcpy(*value, proxyStr.data(), size+1);
1436                     if (len)
1437                         *len = size;
1438                 } else
1439                     result = NPERR_OUT_OF_MEMORY_ERROR;
1440             }
1441         } else
1442             result = NPERR_INVALID_URL;
1443         break;
1444     }
1445     default:
1446         result = NPERR_GENERIC_ERROR;
1447         LOG(Plugins, "PluginView::getValueForURL: %s", prettyNameForNPNURLVariable(variable).data());
1448         break;
1449     }
1450
1451     return result;
1452 }
1453
1454
1455 NPError PluginView::setValueForURL(NPNURLVariable variable, const char* url, const char* value, uint32_t len)
1456 {
1457     LOG(Plugins, "PluginView::setValueForURL(%s)", prettyNameForNPNURLVariable(variable).data());
1458
1459     NPError result = NPERR_NO_ERROR;
1460
1461     switch (variable) {
1462     case NPNURLVCookie: {
1463         KURL u(m_baseURL, url);
1464         if (u.isValid()) {
1465             const String cookieStr = String::fromUTF8(value, len);
1466             Frame* frame = getFrame(parentFrame(), m_element);
1467             if (frame && !cookieStr.isEmpty())
1468                 setCookies(frame->document(), u, cookieStr);
1469         } else
1470             result = NPERR_INVALID_URL;
1471         break;
1472     }
1473     case NPNURLVProxy:
1474         LOG(Plugins, "PluginView::setValueForURL(%s): Plugins are NOT allowed to set proxy information.", prettyNameForNPNURLVariable(variable).data());
1475         result = NPERR_GENERIC_ERROR;
1476         break;
1477     default:
1478         LOG(Plugins, "PluginView::setValueForURL: %s", prettyNameForNPNURLVariable(variable).data());
1479         result = NPERR_GENERIC_ERROR;
1480         break;
1481     }
1482
1483     return result;
1484 }
1485
1486 NPError PluginView::getAuthenticationInfo(const char* protocol, const char* host, int32_t port, const char* scheme, const char* realm, char** username, uint32_t* ulen, char** password, uint32_t* plen)
1487 {
1488     LOG(Plugins, "PluginView::getAuthenticationInfo: protocol=%s, host=%s, port=%d", protocol, host, port);
1489     notImplemented();
1490     return NPERR_GENERIC_ERROR;
1491 }
1492 #endif
1493
1494 void PluginView::privateBrowsingStateChanged(bool privateBrowsingEnabled)
1495 {
1496     NPP_SetValueProcPtr setValue = m_plugin->pluginFuncs()->setvalue;
1497     if (!setValue)
1498         return;
1499
1500     PluginView::setCurrentPluginView(this);
1501 #if USE(JSC)
1502     JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
1503 #endif
1504     setCallingPlugin(true);
1505     NPBool value = privateBrowsingEnabled;
1506     setValue(m_instance, NPNVprivateModeBool, &value);
1507     setCallingPlugin(false);
1508     PluginView::setCurrentPluginView(0);
1509 }
1510
1511 } // namespace WebCore
1512
1513 #endif // ENABLE(NETSCAPE_PLUGIN_API)