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