OSDN Git Service

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