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 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 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 "HTTPHeaderNames.h"
48#include "Image.h"
49#include "JSDOMBinding.h"
50#include "JSDOMWindow.h"
51#include "KeyboardEvent.h"
52#include "MIMETypeRegistry.h"
53#include "MouseEvent.h"
54#include "NotImplemented.h"
55#include "Page.h"
56#include "PlatformMouseEvent.h"
57#include "PluginDatabase.h"
58#include "PluginDebug.h"
59#include "PluginMainThreadScheduler.h"
60#include "PluginPackage.h"
61#include "ProxyServer.h"
62#include "RenderBox.h"
63#include "RenderObject.h"
64#include "ScriptController.h"
65#include "SecurityOrigin.h"
66#include "Settings.h"
67#include "UserGestureIndicator.h"
68#include "WheelEvent.h"
69#include "c_instance.h"
70#include "npruntime_impl.h"
71#include "runtime_root.h"
72#include <bindings/ScriptValue.h>
73#include <runtime/JSCJSValue.h>
74#include <runtime/JSLock.h>
75#include <wtf/ASCIICType.h>
76#include <wtf/text/WTFString.h>
77
78#if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
79#include "PluginMessageThrottlerWin.h"
80#endif
81
82using JSC::ExecState;
83using JSC::JSLock;
84using JSC::JSObject;
85using JSC::JSValue;
86
87#if ENABLE(NETSCAPE_PLUGIN_API)
88
89using std::min;
90
91using namespace WTF;
92
93namespace WebCore {
94
95using namespace HTMLNames;
96
97static int s_callingPlugin;
98
99typedef HashMap<NPP, PluginView*> InstanceMap;
100
101static InstanceMap& instanceMap()
102{
103    static InstanceMap& map = *new InstanceMap;
104    return map;
105}
106
107static String scriptStringIfJavaScriptURL(const URL& 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
116PluginView* PluginView::s_currentPluginView = 0;
117
118void PluginView::popPopupsStateTimerFired(Timer<PluginView>*)
119{
120    popPopupsEnabledState();
121}
122
123IntRect 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    FrameView* parentView = m_element->document().view();
130    clipRect.intersect(parentView->windowClipRectForFrameOwner(m_element, true));
131
132    return clipRect;
133}
134
135void PluginView::setFrameRect(const IntRect& rect)
136{
137    if (m_element->document().printing())
138        return;
139
140    if (rect != frameRect())
141        Widget::setFrameRect(rect);
142
143    updatePluginWidget();
144
145#if OS(WINDOWS)
146    // On Windows always call plugin to change geometry.
147    setNPWindowRect(rect);
148#elif defined(XP_UNIX)
149    // On Unix, multiple calls to setNPWindow() in windowed mode causes Flash to crash
150    if (m_mode == NP_FULL || !m_isWindowed)
151        setNPWindowRect(rect);
152#endif
153}
154
155void PluginView::frameRectsChanged()
156{
157    updatePluginWidget();
158}
159
160void PluginView::clipRectChanged()
161{
162    updatePluginWidget();
163}
164
165void PluginView::handleEvent(Event* event)
166{
167    if (!m_plugin || m_isWindowed)
168        return;
169
170    // Protect the plug-in from deletion while dispatching the event.
171    RefPtr<PluginView> protect(this);
172
173    if (event->isMouseEvent())
174        handleMouseEvent(static_cast<MouseEvent*>(event));
175    else if (event->isKeyboardEvent())
176        handleKeyboardEvent(static_cast<KeyboardEvent*>(event));
177#if defined(XP_MACOSX)
178    else if (event->type() == eventNames().wheelEvent || event->type() == eventNames().mousewheelEvent)
179        handleWheelEvent(static_cast<WheelEvent*>(event));
180#endif
181    else if (event->type() == eventNames().contextmenuEvent)
182        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.
183#if defined(XP_UNIX) && ENABLE(NETSCAPE_PLUGIN_API)
184    else if (event->type() == eventNames().focusoutEvent)
185        handleFocusOutEvent();
186    else if (event->type() == eventNames().focusinEvent)
187        handleFocusInEvent();
188#endif
189}
190
191void PluginView::init()
192{
193    if (m_haveInitialized)
194        return;
195
196    m_haveInitialized = true;
197
198    if (!m_plugin) {
199        ASSERT(m_status == PluginStatusCanNotFindPlugin);
200        return;
201    }
202
203    LOG(Plugins, "PluginView::init(): Initializing plug-in '%s'", m_plugin->name().utf8().data());
204
205    if (!m_plugin->load()) {
206        m_plugin = 0;
207        m_status = PluginStatusCanNotLoadPlugin;
208        return;
209    }
210
211    if (!startOrAddToUnstartedList()) {
212        m_status = PluginStatusCanNotLoadPlugin;
213        return;
214    }
215
216    m_status = PluginStatusLoadedSuccessfully;
217}
218
219bool PluginView::startOrAddToUnstartedList()
220{
221    if (!m_parentFrame->page())
222        return false;
223
224    // We only delay starting the plug-in if we're going to kick off the load
225    // ourselves. Otherwise, the loader will try to deliver data before we've
226    // started the plug-in.
227    if (!m_loadManually && !m_parentFrame->page()->canStartMedia()) {
228        m_parentFrame->document()->addMediaCanStartListener(this);
229        m_isWaitingToStart = true;
230        return true;
231    }
232
233    return start();
234}
235
236bool PluginView::start()
237{
238    if (m_isStarted)
239        return false;
240
241    m_isWaitingToStart = false;
242
243    PluginMainThreadScheduler::scheduler().registerPlugin(m_instance);
244
245    ASSERT(m_plugin);
246    ASSERT(m_plugin->pluginFuncs()->newp);
247
248    NPError npErr;
249    {
250        PluginView::setCurrentPluginView(this);
251        JSC::JSLock::DropAllLocks dropAllLocks(JSDOMWindowBase::commonVM());
252        setCallingPlugin(true);
253        npErr = m_plugin->pluginFuncs()->newp((NPMIMEType)m_mimeType.utf8().data(), m_instance, m_mode, m_paramCount, m_paramNames, m_paramValues, NULL);
254        setCallingPlugin(false);
255        LOG_NPERROR(npErr);
256        PluginView::setCurrentPluginView(0);
257    }
258
259    if (npErr != NPERR_NO_ERROR) {
260        m_status = PluginStatusCanNotLoadPlugin;
261        PluginMainThreadScheduler::scheduler().unregisterPlugin(m_instance);
262        return false;
263    }
264
265    m_isStarted = true;
266
267    if (!m_url.isEmpty() && !m_loadManually) {
268        FrameLoadRequest frameLoadRequest(m_parentFrame->document()->securityOrigin());
269        frameLoadRequest.resourceRequest().setHTTPMethod("GET");
270        frameLoadRequest.resourceRequest().setURL(m_url);
271        load(frameLoadRequest, false, 0);
272    }
273
274    m_status = PluginStatusLoadedSuccessfully;
275
276    if (!platformStart())
277        m_status = PluginStatusCanNotLoadPlugin;
278
279    if (m_status != PluginStatusLoadedSuccessfully)
280        return false;
281
282    return true;
283}
284
285void PluginView::mediaCanStart()
286{
287    ASSERT(!m_isStarted);
288    if (!start())
289        parentFrame()->loader().client().dispatchDidFailToStartPlugin(this);
290}
291
292PluginView::~PluginView()
293{
294    LOG(Plugins, "PluginView::~PluginView()");
295
296    ASSERT(!m_lifeSupportTimer.isActive());
297
298    // If we failed to find the plug-in, we'll return early in our constructor, and
299    // m_instance will be 0.
300    if (m_instance)
301        instanceMap().remove(m_instance);
302
303    if (m_isWaitingToStart)
304        m_parentFrame->document()->removeMediaCanStartListener(this);
305
306    stop();
307
308    freeStringArray(m_paramNames, m_paramCount);
309    freeStringArray(m_paramValues, m_paramCount);
310
311    platformDestroy();
312
313    m_parentFrame->script().cleanupScriptObjectsForPlugin(this);
314
315    if (m_plugin && !(m_plugin->quirks().contains(PluginQuirkDontUnloadPlugin)))
316        m_plugin->unload();
317}
318
319void PluginView::stop()
320{
321    if (!m_isStarted)
322        return;
323
324    LOG(Plugins, "PluginView::stop(): Stopping plug-in '%s'", m_plugin->name().utf8().data());
325
326    HashSet<RefPtr<PluginStream> > streams = m_streams;
327    HashSet<RefPtr<PluginStream> >::iterator end = streams.end();
328    for (HashSet<RefPtr<PluginStream> >::iterator it = streams.begin(); it != end; ++it) {
329        (*it)->stop();
330        disconnectStream((*it).get());
331    }
332
333    ASSERT(m_streams.isEmpty());
334
335    m_isStarted = false;
336
337    JSC::JSLock::DropAllLocks dropAllLocks(JSDOMWindowBase::commonVM());
338
339#if ENABLE(NETSCAPE_PLUGIN_API)
340#if defined(XP_WIN) && !PLATFORM(GTK)
341    // Unsubclass the window
342    if (m_isWindowed) {
343#if OS(WINCE)
344        WNDPROC currentWndProc = (WNDPROC)GetWindowLong(platformPluginWidget(), GWL_WNDPROC);
345
346        if (currentWndProc == PluginViewWndProc)
347            SetWindowLong(platformPluginWidget(), GWL_WNDPROC, (LONG)m_pluginWndProc);
348#else
349        WNDPROC currentWndProc = (WNDPROC)GetWindowLongPtr(platformPluginWidget(), GWLP_WNDPROC);
350
351        if (currentWndProc == PluginViewWndProc)
352            SetWindowLongPtr(platformPluginWidget(), GWLP_WNDPROC, (LONG_PTR)m_pluginWndProc);
353#endif
354    }
355#endif // !defined(XP_WIN) || PLATFORM(GTK)
356#endif // ENABLE(NETSCAPE_PLUGIN_API)
357
358#if !defined(XP_MACOSX)
359    // Clear the window
360    m_npWindow.window = 0;
361
362    if (m_plugin->pluginFuncs()->setwindow && !m_plugin->quirks().contains(PluginQuirkDontSetNullWindowHandleOnDestroy)) {
363        PluginView::setCurrentPluginView(this);
364        setCallingPlugin(true);
365        m_plugin->pluginFuncs()->setwindow(m_instance, &m_npWindow);
366        setCallingPlugin(false);
367        PluginView::setCurrentPluginView(0);
368    }
369
370#ifdef XP_UNIX
371    delete static_cast<NPSetWindowCallbackStruct*>(m_npWindow.ws_info);
372    m_npWindow.ws_info = 0;
373#endif
374
375#endif // !defined(XP_MACOSX)
376
377    PluginMainThreadScheduler::scheduler().unregisterPlugin(m_instance);
378
379    NPSavedData* savedData = 0;
380    PluginView::setCurrentPluginView(this);
381    setCallingPlugin(true);
382    NPError npErr = m_plugin->pluginFuncs()->destroy(m_instance, &savedData);
383    setCallingPlugin(false);
384    LOG_NPERROR(npErr);
385    PluginView::setCurrentPluginView(0);
386
387#if ENABLE(NETSCAPE_PLUGIN_API)
388    if (savedData) {
389        // TODO: Actually save this data instead of just discarding it
390        if (savedData->buf)
391            NPN_MemFree(savedData->buf);
392        NPN_MemFree(savedData);
393    }
394#endif
395
396    m_instance->pdata = 0;
397}
398
399void PluginView::setCurrentPluginView(PluginView* pluginView)
400{
401    s_currentPluginView = pluginView;
402}
403
404PluginView* PluginView::currentPluginView()
405{
406    return s_currentPluginView;
407}
408
409static char* createUTF8String(const String& str)
410{
411    CString cstr = str.utf8();
412    const size_t cstrLength = cstr.length();
413    char* result = reinterpret_cast<char*>(fastMalloc(cstrLength + 1));
414
415    memcpy(result, cstr.data(), cstrLength);
416    result[cstrLength] = '\0';
417
418    return result;
419}
420
421void PluginView::performRequest(PluginRequest* request)
422{
423    if (!m_isStarted)
424        return;
425
426    // don't let a plugin start any loads if it is no longer part of a document that is being
427    // displayed unless the loads are in the same frame as the plugin.
428    const String& targetFrameName = request->frameLoadRequest().frameName();
429    if (m_parentFrame->loader().documentLoader() != m_parentFrame->loader().activeDocumentLoader() && (targetFrameName.isNull() || m_parentFrame->tree().find(targetFrameName) != m_parentFrame))
430        return;
431
432    URL requestURL = request->frameLoadRequest().resourceRequest().url();
433    String jsString = scriptStringIfJavaScriptURL(requestURL);
434
435    UserGestureIndicator gestureIndicator(request->shouldAllowPopups() ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
436
437    if (jsString.isNull()) {
438        // if this is not a targeted request, create a stream for it. otherwise,
439        // just pass it off to the loader
440        if (targetFrameName.isEmpty()) {
441            RefPtr<PluginStream> stream = PluginStream::create(this, m_parentFrame.get(), request->frameLoadRequest().resourceRequest(), request->sendNotification(), request->notifyData(), plugin()->pluginFuncs(), instance(), m_plugin->quirks());
442            m_streams.add(stream);
443            stream->start();
444        } else {
445            // If the target frame is our frame, we could destroy the
446            // PluginView, so we protect it. <rdar://problem/6991251>
447            RefPtr<PluginView> protect(this);
448
449            FrameLoadRequest frameRequest(m_parentFrame.get(), request->frameLoadRequest().resourceRequest());
450            frameRequest.setFrameName(targetFrameName);
451            frameRequest.setShouldCheckNewWindowPolicy(true);
452            m_parentFrame->loader().load(frameRequest);
453
454            // FIXME: <rdar://problem/4807469> This should be sent when the document has finished loading
455            if (request->sendNotification()) {
456                PluginView::setCurrentPluginView(this);
457                JSC::JSLock::DropAllLocks dropAllLocks(JSDOMWindowBase::commonVM());
458                setCallingPlugin(true);
459                m_plugin->pluginFuncs()->urlnotify(m_instance, requestURL.string().utf8().data(), NPRES_DONE, request->notifyData());
460                setCallingPlugin(false);
461                PluginView::setCurrentPluginView(0);
462            }
463        }
464        return;
465    }
466
467    // Targeted JavaScript requests are only allowed on the frame that contains the JavaScript plugin
468    // and this has been made sure in ::load.
469    ASSERT(targetFrameName.isEmpty() || m_parentFrame->tree().find(targetFrameName) == m_parentFrame);
470
471    // Executing a script can cause the plugin view to be destroyed, so we keep a reference to it.
472    RefPtr<PluginView> protector(this);
473    Deprecated::ScriptValue result = m_parentFrame->script().executeScript(jsString, request->shouldAllowPopups());
474
475    if (targetFrameName.isNull()) {
476        String resultString;
477
478        JSC::ExecState* scriptState = m_parentFrame->script().globalObject(pluginWorld())->globalExec();
479        CString cstr;
480        if (result.getString(scriptState, resultString))
481            cstr = resultString.utf8();
482
483        RefPtr<PluginStream> stream = PluginStream::create(this, m_parentFrame.get(), request->frameLoadRequest().resourceRequest(), request->sendNotification(), request->notifyData(), plugin()->pluginFuncs(), instance(), m_plugin->quirks());
484        m_streams.add(stream);
485        stream->sendJavaScriptStream(requestURL, cstr);
486    }
487}
488
489void PluginView::requestTimerFired(Timer<PluginView>* timer)
490{
491    ASSERT_UNUSED(timer, timer == &m_requestTimer);
492    ASSERT(!m_requests.isEmpty());
493    ASSERT(!m_isJavaScriptPaused);
494
495    OwnPtr<PluginRequest> request = m_requests[0].release();
496    m_requests.remove(0);
497
498    // Schedule a new request before calling performRequest since the call to
499    // performRequest can cause the plugin view to be deleted.
500    if (!m_requests.isEmpty())
501        m_requestTimer.startOneShot(0);
502
503    performRequest(request.get());
504}
505
506void PluginView::scheduleRequest(PassOwnPtr<PluginRequest> request)
507{
508    m_requests.append(request);
509
510    if (!m_isJavaScriptPaused)
511        m_requestTimer.startOneShot(0);
512}
513
514NPError PluginView::load(const FrameLoadRequest& frameLoadRequest, bool sendNotification, void* notifyData)
515{
516    ASSERT(frameLoadRequest.resourceRequest().httpMethod() == "GET" || frameLoadRequest.resourceRequest().httpMethod() == "POST");
517
518    URL url = frameLoadRequest.resourceRequest().url();
519
520    if (url.isEmpty())
521        return NPERR_INVALID_URL;
522
523    // Don't allow requests to be made when the document loader is stopping all loaders.
524    DocumentLoader* loader = m_parentFrame->loader().documentLoader();
525    if (!loader || loader->isStopping())
526        return NPERR_GENERIC_ERROR;
527
528    const String& targetFrameName = frameLoadRequest.frameName();
529    String jsString = scriptStringIfJavaScriptURL(url);
530
531    if (!jsString.isNull()) {
532        // Return NPERR_GENERIC_ERROR if JS is disabled. This is what Mozilla does.
533        if (!m_parentFrame->script().canExecuteScripts(NotAboutToExecuteScript))
534            return NPERR_GENERIC_ERROR;
535
536        // For security reasons, only allow JS requests to be made on the frame that contains the plug-in.
537        if (!targetFrameName.isNull() && m_parentFrame->tree().find(targetFrameName) != m_parentFrame)
538            return NPERR_INVALID_PARAM;
539    } else if (!m_parentFrame->document()->securityOrigin()->canDisplay(url))
540        return NPERR_GENERIC_ERROR;
541
542    scheduleRequest(adoptPtr(new PluginRequest(frameLoadRequest, sendNotification, notifyData, arePopupsAllowed())));
543
544    return NPERR_NO_ERROR;
545}
546
547static URL makeURL(const URL& baseURL, const char* relativeURLString)
548{
549    String urlString = relativeURLString;
550
551    // Strip return characters.
552    urlString.replaceWithLiteral('\n', "");
553    urlString.replaceWithLiteral('\r', "");
554
555    return URL(baseURL, urlString);
556}
557
558NPError PluginView::getURLNotify(const char* url, const char* target, void* notifyData)
559{
560    FrameLoadRequest frameLoadRequest(m_parentFrame->document()->securityOrigin());
561
562    frameLoadRequest.setFrameName(target);
563    frameLoadRequest.resourceRequest().setHTTPMethod("GET");
564    frameLoadRequest.resourceRequest().setURL(makeURL(m_parentFrame->document()->baseURL(), url));
565
566    return load(frameLoadRequest, true, notifyData);
567}
568
569NPError PluginView::getURL(const char* url, const char* target)
570{
571    FrameLoadRequest frameLoadRequest(m_parentFrame->document()->securityOrigin());
572
573    frameLoadRequest.setFrameName(target);
574    frameLoadRequest.resourceRequest().setHTTPMethod("GET");
575    frameLoadRequest.resourceRequest().setURL(makeURL(m_parentFrame->document()->baseURL(), url));
576
577    return load(frameLoadRequest, false, 0);
578}
579
580NPError PluginView::postURLNotify(const char* url, const char* target, uint32_t len, const char* buf, NPBool file, void* notifyData)
581{
582    return handlePost(url, target, len, buf, file, notifyData, true, true);
583}
584
585NPError PluginView::postURL(const char* url, const char* target, uint32_t len, const char* buf, NPBool file)
586{
587    // As documented, only allow headers to be specified via NPP_PostURL when using a file.
588    return handlePost(url, target, len, buf, file, 0, false, file);
589}
590
591NPError PluginView::newStream(NPMIMEType, const char* /* target */, NPStream**)
592{
593    notImplemented();
594    // Unsupported
595    return NPERR_GENERIC_ERROR;
596}
597
598int32_t PluginView::write(NPStream*, int32_t /* len */, void* /* buffer */)
599{
600    notImplemented();
601    // Unsupported
602    return -1;
603}
604
605NPError PluginView::destroyStream(NPStream* stream, NPReason reason)
606{
607    if (!stream || PluginStream::ownerForStream(stream) != m_instance)
608        return NPERR_INVALID_INSTANCE_ERROR;
609
610    PluginStream* browserStream = static_cast<PluginStream*>(stream->ndata);
611    browserStream->cancelAndDestroyStream(reason);
612
613    return NPERR_NO_ERROR;
614}
615
616void PluginView::status(const char* message)
617{
618    if (Page* page = m_parentFrame->page())
619        page->chrome().setStatusbarText(m_parentFrame.get(), String::fromUTF8(message));
620}
621
622NPError PluginView::setValue(NPPVariable variable, void* value)
623{
624    LOG(Plugins, "PluginView::setValue(%s): ", prettyNameForNPPVariable(variable, value).data());
625
626    switch (variable) {
627    case NPPVpluginWindowBool:
628        m_isWindowed = value;
629        return NPERR_NO_ERROR;
630    case NPPVpluginTransparentBool:
631        m_isTransparent = value;
632        return NPERR_NO_ERROR;
633#if defined(XP_MACOSX)
634    case NPPVpluginDrawingModel: {
635        // Can only set drawing model inside NPP_New()
636        if (this != currentPluginView())
637           return NPERR_GENERIC_ERROR;
638
639        NPDrawingModel newDrawingModel = NPDrawingModel(uintptr_t(value));
640        switch (newDrawingModel) {
641        case NPDrawingModelCoreGraphics:
642            return NPERR_NO_ERROR;
643        case NPDrawingModelCoreAnimation:
644        default:
645            LOG(Plugins, "Plugin asked for unsupported drawing model: %s",
646                    prettyNameForDrawingModel(newDrawingModel));
647            return NPERR_GENERIC_ERROR;
648        }
649    }
650
651    case NPPVpluginEventModel: {
652        // Can only set event model inside NPP_New()
653        if (this != currentPluginView())
654           return NPERR_GENERIC_ERROR;
655
656        NPEventModel newEventModel = NPEventModel(uintptr_t(value));
657        switch (newEventModel) {
658        case NPEventModelCocoa:
659            return NPERR_NO_ERROR;
660        default:
661            LOG(Plugins, "Plugin asked for unsupported event model: %s",
662                    prettyNameForEventModel(newEventModel));
663            return NPERR_GENERIC_ERROR;
664        }
665    }
666#endif // defined(XP_MACOSX)
667
668    default:
669        notImplemented();
670        return NPERR_GENERIC_ERROR;
671    }
672}
673
674void PluginView::invalidateTimerFired(Timer<PluginView>* timer)
675{
676    ASSERT_UNUSED(timer, timer == &m_invalidateTimer);
677
678    for (unsigned i = 0; i < m_invalidRects.size(); i++)
679        invalidateRect(m_invalidRects[i]);
680    m_invalidRects.clear();
681}
682
683
684void PluginView::pushPopupsEnabledState(bool state)
685{
686    m_popupStateStack.append(state);
687}
688
689void PluginView::popPopupsEnabledState()
690{
691    m_popupStateStack.removeLast();
692}
693
694bool PluginView::arePopupsAllowed() const
695{
696    if (!m_popupStateStack.isEmpty())
697        return m_popupStateStack.last();
698
699    return false;
700}
701
702void PluginView::setJavaScriptPaused(bool paused)
703{
704    if (m_isJavaScriptPaused == paused)
705        return;
706    m_isJavaScriptPaused = paused;
707
708    if (m_isJavaScriptPaused)
709        m_requestTimer.stop();
710    else if (!m_requests.isEmpty())
711        m_requestTimer.startOneShot(0);
712}
713
714#if ENABLE(NETSCAPE_PLUGIN_API)
715NPObject* PluginView::npObject()
716{
717    NPObject* object = 0;
718
719    if (!m_isStarted || !m_plugin || !m_plugin->pluginFuncs()->getvalue)
720        return 0;
721
722    // On Windows, calling Java's NPN_GetValue can allow the message loop to
723    // run, allowing loading to take place or JavaScript to run. Protect the
724    // PluginView from destruction. <rdar://problem/6978804>
725    RefPtr<PluginView> protect(this);
726
727    NPError npErr;
728    {
729        PluginView::setCurrentPluginView(this);
730        JSC::JSLock::DropAllLocks dropAllLocks(JSDOMWindowBase::commonVM());
731        setCallingPlugin(true);
732        npErr = m_plugin->pluginFuncs()->getvalue(m_instance, NPPVpluginScriptableNPObject, &object);
733        setCallingPlugin(false);
734        PluginView::setCurrentPluginView(0);
735    }
736
737    if (npErr != NPERR_NO_ERROR)
738        return 0;
739
740    return object;
741}
742#endif
743
744PassRefPtr<JSC::Bindings::Instance> PluginView::bindingInstance()
745{
746#if ENABLE(NETSCAPE_PLUGIN_API)
747    NPObject* object = npObject();
748    if (!object)
749        return 0;
750
751    if (hasOneRef()) {
752        // The renderer for the PluginView was destroyed during the above call, and
753        // the PluginView will be destroyed when this function returns, so we
754        // return null.
755        return 0;
756    }
757
758    RefPtr<JSC::Bindings::RootObject> root = m_parentFrame->script().createRootObject(this);
759    RefPtr<JSC::Bindings::Instance> instance = JSC::Bindings::CInstance::create(object, root.release());
760
761    _NPN_ReleaseObject(object);
762
763    return instance.release();
764#else
765    return 0;
766#endif
767}
768
769void PluginView::disconnectStream(PluginStream* stream)
770{
771    ASSERT(m_streams.contains(stream));
772
773    m_streams.remove(stream);
774}
775
776void PluginView::setParameters(const Vector<String>& paramNames, const Vector<String>& paramValues)
777{
778    ASSERT(paramNames.size() == paramValues.size());
779
780    unsigned size = paramNames.size();
781    unsigned paramCount = 0;
782
783    m_paramNames = reinterpret_cast<char**>(fastMalloc(sizeof(char*) * size));
784    m_paramValues = reinterpret_cast<char**>(fastMalloc(sizeof(char*) * size));
785
786    for (unsigned i = 0; i < size; i++) {
787        if (m_plugin->quirks().contains(PluginQuirkRemoveWindowlessVideoParam) && equalIgnoringCase(paramNames[i], "windowlessvideo"))
788            continue;
789
790        if (paramNames[i] == "pluginspage")
791            m_pluginsPage = paramValues[i];
792
793        m_paramNames[paramCount] = createUTF8String(paramNames[i]);
794        m_paramValues[paramCount] = createUTF8String(paramValues[i]);
795
796        paramCount++;
797    }
798
799    m_paramCount = paramCount;
800}
801
802PluginView::PluginView(Frame* parentFrame, const IntSize& size, PluginPackage* plugin, HTMLPlugInElement* element, const URL& url, const Vector<String>& paramNames, const Vector<String>& paramValues, const String& mimeType, bool loadManually)
803    : m_parentFrame(parentFrame)
804    , m_plugin(plugin)
805    , m_element(element)
806    , m_isStarted(false)
807    , m_url(url)
808    , m_status(PluginStatusLoadedSuccessfully)
809    , m_requestTimer(this, &PluginView::requestTimerFired)
810    , m_invalidateTimer(this, &PluginView::invalidateTimerFired)
811    , m_popPopupsStateTimer(this, &PluginView::popPopupsStateTimerFired)
812    , m_lifeSupportTimer(this, &PluginView::lifeSupportTimerFired)
813    , m_mode(loadManually ? NP_FULL : NP_EMBED)
814    , m_paramNames(0)
815    , m_paramValues(0)
816    , m_mimeType(mimeType)
817    , m_instance(0)
818#if defined(XP_MACOSX)
819    , m_isWindowed(false)
820    , m_updatedCocoaTextInputRequested(false)
821    , m_keyDownSent(false)
822    , m_disregardKeyUpCounter(0)
823#else
824    , m_isWindowed(true)
825#endif
826    , m_isTransparent(false)
827    , m_haveInitialized(false)
828    , m_isWaitingToStart(false)
829#if defined(XP_UNIX)
830    , m_needsXEmbed(false)
831#endif
832#if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
833    , m_pluginWndProc(0)
834    , m_lastMessage(0)
835    , m_isCallingPluginWndProc(false)
836    , m_wmPrintHDC(0)
837    , m_haveUpdatedPluginWidget(false)
838#endif
839#if PLATFORM(EFL)
840    , m_window(0)
841#endif
842#if defined(XP_MACOSX)
843    , m_contextRef(0)
844#endif
845#if defined(XP_UNIX) && ENABLE(NETSCAPE_PLUGIN_API)
846    , m_hasPendingGeometryChange(true)
847    , m_drawable(0)
848    , m_visual(0)
849    , m_colormap(0)
850    , m_pluginDisplay(0)
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 URL& 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(HTTPHeaderName::ContentLength);
1168
1169                if (!contentLength.isNull())
1170                    dataLength = min(contentLength.toInt(), (int)dataLength);
1171                headerFields.remove(HTTPHeaderName::ContentLength);
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().setHTTPHeaderFields(WTF::move(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->usesEphemeralSession();
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    return element->document().frame();
1375}
1376
1377NPError PluginView::getValueForURL(NPNURLVariable variable, const char* url, char** value, uint32_t* len)
1378{
1379    LOG(Plugins, "PluginView::getValueForURL(%s)", prettyNameForNPNURLVariable(variable).data());
1380
1381    NPError result = NPERR_NO_ERROR;
1382
1383    switch (variable) {
1384    case NPNURLVCookie: {
1385        URL u(m_parentFrame->document()->baseURL(), url);
1386        if (u.isValid()) {
1387            Frame* frame = getFrame(parentFrame(), m_element);
1388            if (frame) {
1389                const CString cookieStr = cookies(frame->document(), u).utf8();
1390                if (!cookieStr.isNull()) {
1391                    const int size = cookieStr.length();
1392                    *value = static_cast<char*>(NPN_MemAlloc(size+1));
1393                    if (*value) {
1394                        memset(*value, 0, size+1);
1395                        memcpy(*value, cookieStr.data(), size+1);
1396                        if (len)
1397                            *len = size;
1398                    } else
1399                        result = NPERR_OUT_OF_MEMORY_ERROR;
1400                }
1401            }
1402        } else
1403            result = NPERR_INVALID_URL;
1404        break;
1405    }
1406    case NPNURLVProxy: {
1407        URL u(m_parentFrame->document()->baseURL(), url);
1408        if (u.isValid()) {
1409            Frame* frame = getFrame(parentFrame(), m_element);
1410            const FrameLoader* frameLoader = frame ? &frame->loader() : 0;
1411            const NetworkingContext* context = frameLoader ? frameLoader->networkingContext() : 0;
1412            const CString proxyStr = toString(proxyServersForURL(u, context)).utf8();
1413            if (!proxyStr.isNull()) {
1414                const int size = proxyStr.length();
1415                *value = static_cast<char*>(NPN_MemAlloc(size+1));
1416                if (*value) {
1417                    memset(*value, 0, size+1);
1418                    memcpy(*value, proxyStr.data(), size+1);
1419                    if (len)
1420                        *len = size;
1421                } else
1422                    result = NPERR_OUT_OF_MEMORY_ERROR;
1423            }
1424        } else
1425            result = NPERR_INVALID_URL;
1426        break;
1427    }
1428    default:
1429        result = NPERR_GENERIC_ERROR;
1430        LOG(Plugins, "PluginView::getValueForURL: %s", prettyNameForNPNURLVariable(variable).data());
1431        break;
1432    }
1433
1434    return result;
1435}
1436
1437
1438NPError PluginView::setValueForURL(NPNURLVariable variable, const char* url, const char* value, uint32_t len)
1439{
1440    LOG(Plugins, "PluginView::setValueForURL(%s)", prettyNameForNPNURLVariable(variable).data());
1441
1442    NPError result = NPERR_NO_ERROR;
1443
1444    switch (variable) {
1445    case NPNURLVCookie: {
1446        URL u(m_parentFrame->document()->baseURL(), url);
1447        if (u.isValid()) {
1448            const String cookieStr = String::fromUTF8(value, len);
1449            Frame* frame = getFrame(parentFrame(), m_element);
1450            if (frame && !cookieStr.isEmpty())
1451                setCookies(frame->document(), u, cookieStr);
1452        } else
1453            result = NPERR_INVALID_URL;
1454        break;
1455    }
1456    case NPNURLVProxy:
1457        LOG(Plugins, "PluginView::setValueForURL(%s): Plugins are NOT allowed to set proxy information.", prettyNameForNPNURLVariable(variable).data());
1458        result = NPERR_GENERIC_ERROR;
1459        break;
1460    default:
1461        LOG(Plugins, "PluginView::setValueForURL: %s", prettyNameForNPNURLVariable(variable).data());
1462        result = NPERR_GENERIC_ERROR;
1463        break;
1464    }
1465
1466    return result;
1467}
1468
1469NPError 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 */)
1470{
1471#if LOG_DISABLED
1472    UNUSED_PARAM(protocol);
1473    UNUSED_PARAM(host);
1474    UNUSED_PARAM(port);
1475#endif
1476    LOG(Plugins, "PluginView::getAuthenticationInfo: protocol=%s, host=%s, port=%d", protocol, host, port);
1477    notImplemented();
1478    return NPERR_GENERIC_ERROR;
1479}
1480#endif
1481
1482void PluginView::privateBrowsingStateChanged(bool privateBrowsingEnabled)
1483{
1484    NPP_SetValueProcPtr setValue = m_plugin->pluginFuncs()->setvalue;
1485    if (!setValue)
1486        return;
1487
1488    PluginView::setCurrentPluginView(this);
1489    JSC::JSLock::DropAllLocks dropAllLocks(JSDOMWindowBase::commonVM());
1490    setCallingPlugin(true);
1491    NPBool value = privateBrowsingEnabled;
1492    setValue(m_instance, NPNVprivateModeBool, &value);
1493    setCallingPlugin(false);
1494    PluginView::setCurrentPluginView(0);
1495}
1496
1497} // namespace WebCore
1498
1499#endif // ENABLE(NETSCAPE_PLUGIN_API)
1500