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