1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #include "nsPluginInstancePeer.h"
41 #include "nsIPluginInstance.h"
50 #include "nsIJVMManager.h"
52 #include "nsIServiceManager.h"
54 #include "nsIDocument.h"
55 #include "nsPIDOMWindow.h"
56 #include "nsIScriptGlobalObject.h"
57 #include "nsIScriptContext.h"
58 #include "nsIDirectoryService.h"
59 #include "nsDirectoryServiceDefs.h"
60 #include "nsIFileStreams.h"
61 #include "nsNetUtil.h"
68 nsPluginInstancePeerImpl::nsPluginInstancePeerImpl()
75 nsPluginInstancePeerImpl::~nsPluginInstancePeerImpl()
78 NS_IF_RELEASE(mOwner
);
80 if (nsnull
!= mMIMEType
) {
81 PR_Free((void *)mMIMEType
);
86 static NS_DEFINE_IID(kIPluginTagInfoIID
, NS_IPLUGINTAGINFO_IID
);
87 static NS_DEFINE_IID(kIPluginTagInfo2IID
, NS_IPLUGINTAGINFO2_IID
);
89 static NS_DEFINE_IID(kIJVMPluginTagInfoIID
, NS_IJVMPLUGINTAGINFO_IID
);
91 NS_IMPL_ISUPPORTS7(nsPluginInstancePeerImpl
,
92 nsIPluginInstancePeer
,
93 nsIPluginInstancePeer2
,
94 nsIWindowlessPluginInstancePeer
,
98 nsPIPluginInstancePeer
)
100 NS_IMPL_ISUPPORTS6(nsPluginInstancePeerImpl
,
101 nsIPluginInstancePeer
,
102 nsIPluginInstancePeer2
,
103 nsIWindowlessPluginInstancePeer
,
106 nsPIPluginInstancePeer
)
110 nsPluginInstancePeerImpl::GetValue(nsPluginInstancePeerVariable variable
,
114 return NS_ERROR_FAILURE
;
116 return mOwner
->GetValue(variable
, value
);
120 nsPluginInstancePeerImpl::GetMIMEType(nsMIMEType
*result
)
122 if (nsnull
== mMIMEType
)
131 nsPluginInstancePeerImpl::GetMode(nsPluginMode
*result
)
133 if (nsnull
!= mOwner
)
134 return mOwner
->GetMode(result
);
136 return NS_ERROR_FAILURE
;
139 // nsPluginStreamToFile
140 // --------------------
141 // Used to handle NPN_NewStream() - writes the stream as received by the plugin
142 // to a file and at completion (NPN_DestroyStream), tells the browser to load it into
143 // a plugin-specified target
145 static NS_DEFINE_IID(kIOutputStreamIID
, NS_IOUTPUTSTREAM_IID
);
147 class nsPluginStreamToFile
: public nsIOutputStream
151 nsPluginStreamToFile(const char* target
, nsIPluginInstanceOwner
* owner
);
152 virtual ~nsPluginStreamToFile();
155 NS_DECL_NSIOUTPUTSTREAM
160 nsCOMPtr
<nsILocalFile
> mTempFile
;
161 nsCOMPtr
<nsIOutputStream
> mOutputStream
;
162 nsIPluginInstanceOwner
* mOwner
;
165 NS_IMPL_ADDREF(nsPluginStreamToFile
)
166 NS_IMPL_RELEASE(nsPluginStreamToFile
)
168 nsPluginStreamToFile::nsPluginStreamToFile(const char* target
,
169 nsIPluginInstanceOwner
* owner
)
170 : mTarget(PL_strdup(target
)),
174 nsCOMPtr
<nsIFile
> pluginTmp
;
175 rv
= NS_GetSpecialDirectory(NS_OS_TEMP_DIR
, getter_AddRefs(pluginTmp
));
176 if (NS_FAILED(rv
)) return;
178 mTempFile
= do_QueryInterface(pluginTmp
, &rv
);
179 if (NS_FAILED(rv
)) return;
181 // need to create a file with a unique name - use target as the basis
182 rv
= mTempFile
->AppendNative(nsDependentCString(target
));
183 if (NS_FAILED(rv
)) return;
185 // Yes, make it unique.
186 rv
= mTempFile
->CreateUnique(nsIFile::NORMAL_FILE_TYPE
, 0700);
187 if (NS_FAILED(rv
)) return;
190 rv
= NS_NewLocalFileOutputStream(getter_AddRefs(mOutputStream
), mTempFile
, -1, 00600);
194 mOutputStream
->Close();
196 // construct the URL we'll use later in calls to GetURL()
197 NS_GetURLSpecFromFile(mTempFile
, mFileURL
);
200 printf("File URL = %s\n", mFileURL
.get());
204 nsPluginStreamToFile::~nsPluginStreamToFile()
206 // should we be deleting mTempFile here?
207 if (nsnull
!= mTarget
)
212 nsPluginStreamToFile::QueryInterface(const nsIID
& aIID
,
213 void** aInstancePtrResult
)
215 NS_PRECONDITION(nsnull
!= aInstancePtrResult
, "null pointer");
217 if (nsnull
== aInstancePtrResult
)
218 return NS_ERROR_NULL_POINTER
;
220 if (aIID
.Equals(kIOutputStreamIID
)) {
221 *aInstancePtrResult
= (void *)((nsIOutputStream
*)this);
226 return NS_NOINTERFACE
;
230 nsPluginStreamToFile::Flush()
236 nsPluginStreamToFile::Write(const char* aBuf
, PRUint32 aCount
,
237 PRUint32
*aWriteCount
)
239 PRUint32 actualCount
;
240 mOutputStream
->Write(aBuf
, aCount
, &actualCount
);
241 mOutputStream
->Flush();
242 mOwner
->GetURL(mFileURL
.get(), mTarget
, nsnull
, 0, nsnull
, 0);
248 nsPluginStreamToFile::WriteFrom(nsIInputStream
*inStr
, PRUint32 count
,
251 NS_NOTREACHED("WriteFrom");
252 return NS_ERROR_NOT_IMPLEMENTED
;
256 nsPluginStreamToFile::WriteSegments(nsReadSegmentFun reader
, void * closure
,
257 PRUint32 count
, PRUint32
*_retval
)
259 NS_NOTREACHED("WriteSegments");
260 return NS_ERROR_NOT_IMPLEMENTED
;
264 nsPluginStreamToFile::IsNonBlocking(PRBool
*aNonBlocking
)
266 *aNonBlocking
= PR_FALSE
;
271 nsPluginStreamToFile::Close(void)
273 mOwner
->GetURL(mFileURL
.get(), mTarget
, nsnull
, 0, nsnull
, 0);
277 // end of nsPluginStreamToFile
280 nsPluginInstancePeerImpl::NewStream(nsMIMEType type
, const char* target
,
281 nsIOutputStream
* *result
)
284 nsPluginStreamToFile
* stream
= new nsPluginStreamToFile(target
, mOwner
);
286 return NS_ERROR_OUT_OF_MEMORY
;
288 rv
= stream
->QueryInterface(kIOutputStreamIID
, (void **)result
);
294 nsPluginInstancePeerImpl::ShowStatus(const char* message
)
296 if (nsnull
!= mOwner
)
297 return mOwner
->ShowStatus(message
);
299 return NS_ERROR_FAILURE
;
303 nsPluginInstancePeerImpl::GetAttributes(PRUint16
& n
, const char*const*& names
,
304 const char*const*& values
)
306 if (nsnull
!= mOwner
) {
307 nsIPluginTagInfo
*tinfo
;
310 rv
= mOwner
->QueryInterface(kIPluginTagInfoIID
, (void **)&tinfo
);
313 rv
= tinfo
->GetAttributes(n
, names
, values
);
324 return NS_ERROR_FAILURE
;
329 nsPluginInstancePeerImpl::GetAttribute(const char* name
, const char* *result
)
331 if (nsnull
!= mOwner
) {
332 nsIPluginTagInfo
*tinfo
;
335 rv
= mOwner
->QueryInterface(kIPluginTagInfoIID
, (void **)&tinfo
);
338 rv
= tinfo
->GetAttribute(name
, result
);
346 return NS_ERROR_FAILURE
;
351 nsPluginInstancePeerImpl::GetDOMElement(nsIDOMElement
* *result
)
353 if (mOwner
== nsnull
) {
355 return NS_ERROR_FAILURE
;
358 nsIPluginTagInfo2
*tinfo
;
361 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
364 rv
= tinfo
->GetDOMElement(result
);
372 nsPluginInstancePeerImpl::GetTagType(nsPluginTagType
*result
)
374 if (nsnull
!= mOwner
) {
375 nsIPluginTagInfo2
*tinfo
;
378 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
381 rv
= tinfo
->GetTagType(result
);
388 *result
= nsPluginTagType_Unknown
;
389 return NS_ERROR_FAILURE
;
394 nsPluginInstancePeerImpl::GetTagText(const char* *result
)
396 if (nsnull
!= mOwner
) {
397 nsIPluginTagInfo2
*tinfo
;
400 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
403 rv
= tinfo
->GetTagText(result
);
411 return NS_ERROR_FAILURE
;
416 nsPluginInstancePeerImpl::GetParameters(PRUint16
& n
, const char*const*& names
,
417 const char*const*& values
)
419 if (nsnull
!= mOwner
) {
420 nsIPluginTagInfo2
*tinfo
;
423 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
426 rv
= tinfo
->GetParameters(n
, names
, values
);
437 return NS_ERROR_FAILURE
;
442 nsPluginInstancePeerImpl::GetParameter(const char* name
, const char* *result
)
444 if (nsnull
!= mOwner
) {
445 nsIPluginTagInfo2
*tinfo
;
448 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
451 rv
= tinfo
->GetParameter(name
, result
);
459 return NS_ERROR_FAILURE
;
464 nsPluginInstancePeerImpl::GetDocumentBase(const char* *result
)
466 if (nsnull
!= mOwner
) {
467 nsIPluginTagInfo2
*tinfo
;
470 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
473 rv
= tinfo
->GetDocumentBase(result
);
481 return NS_ERROR_FAILURE
;
486 nsPluginInstancePeerImpl::GetDocumentEncoding(const char* *result
)
488 if (nsnull
!= mOwner
) {
489 nsIPluginTagInfo2
*tinfo
;
492 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
495 rv
= tinfo
->GetDocumentEncoding(result
);
503 return NS_ERROR_FAILURE
;
508 nsPluginInstancePeerImpl::GetAlignment(const char* *result
)
510 if (nsnull
!= mOwner
) {
511 nsIPluginTagInfo2
*tinfo
;
514 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
517 rv
= tinfo
->GetAlignment(result
);
525 return NS_ERROR_FAILURE
;
530 nsPluginInstancePeerImpl::GetWidth(PRUint32
*result
)
532 if (nsnull
!= mOwner
) {
533 nsIPluginTagInfo2
*tinfo
;
536 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
539 rv
= tinfo
->GetWidth(result
);
547 return NS_ERROR_FAILURE
;
552 nsPluginInstancePeerImpl::GetHeight(PRUint32
*result
)
554 if (nsnull
!= mOwner
) {
555 nsIPluginTagInfo2
*tinfo
;
558 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
561 rv
= tinfo
->GetHeight(result
);
569 return NS_ERROR_FAILURE
;
574 nsPluginInstancePeerImpl::GetBorderVertSpace(PRUint32
*result
)
576 if (nsnull
!= mOwner
) {
577 nsIPluginTagInfo2
*tinfo
;
580 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
583 rv
= tinfo
->GetBorderVertSpace(result
);
591 return NS_ERROR_FAILURE
;
596 nsPluginInstancePeerImpl::GetBorderHorizSpace(PRUint32
*result
)
598 if (nsnull
!= mOwner
) {
599 nsIPluginTagInfo2
*tinfo
;
602 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
605 rv
= tinfo
->GetBorderHorizSpace(result
);
613 return NS_ERROR_FAILURE
;
618 nsPluginInstancePeerImpl::GetUniqueID(PRUint32
*result
)
620 if (nsnull
!= mOwner
) {
621 nsIPluginTagInfo2
*tinfo
;
624 rv
= mOwner
->QueryInterface(kIPluginTagInfo2IID
, (void **)&tinfo
);
627 rv
= tinfo
->GetUniqueID(result
);
635 return NS_ERROR_FAILURE
;
640 nsPluginInstancePeerImpl::GetCode(const char* *result
)
643 if (nsnull
!= mOwner
) {
644 nsIJVMPluginTagInfo
*tinfo
;
647 rv
= mOwner
->QueryInterface(kIJVMPluginTagInfoIID
, (void **)&tinfo
);
650 rv
= tinfo
->GetCode(result
);
659 return NS_ERROR_FAILURE
;
666 nsPluginInstancePeerImpl::GetCodeBase(const char* *result
)
669 if (nsnull
!= mOwner
) {
670 nsIJVMPluginTagInfo
*tinfo
;
673 rv
= mOwner
->QueryInterface(kIJVMPluginTagInfoIID
, (void **)&tinfo
);
676 rv
= tinfo
->GetCodeBase(result
);
685 return NS_ERROR_FAILURE
;
692 nsPluginInstancePeerImpl::GetArchive(const char* *result
)
695 if (nsnull
!= mOwner
) {
696 nsIJVMPluginTagInfo
*tinfo
;
699 rv
= mOwner
->QueryInterface(kIJVMPluginTagInfoIID
, (void **)&tinfo
);
702 rv
= tinfo
->GetArchive(result
);
711 return NS_ERROR_FAILURE
;
718 nsPluginInstancePeerImpl::GetName(const char* *result
)
721 if (nsnull
!= mOwner
) {
722 nsIJVMPluginTagInfo
*tinfo
;
725 rv
= mOwner
->QueryInterface(kIJVMPluginTagInfoIID
, (void **)&tinfo
);
728 rv
= tinfo
->GetName(result
);
737 return NS_ERROR_FAILURE
;
744 nsPluginInstancePeerImpl::GetMayScript(PRBool
*result
)
747 if (nsnull
!= mOwner
) {
748 nsIJVMPluginTagInfo
*tinfo
;
751 rv
= mOwner
->QueryInterface(kIJVMPluginTagInfoIID
, (void **)&tinfo
);
754 rv
= tinfo
->GetMayScript(result
);
763 return NS_ERROR_FAILURE
;
770 nsPluginInstancePeerImpl::SetWindowSize(PRUint32 width
, PRUint32 height
)
776 nsPluginInstancePeerImpl::GetJSWindow(JSObject
* *outJSWindow
)
779 nsresult rv
= NS_ERROR_FAILURE
;
780 nsCOMPtr
<nsIDocument
> document
;
782 rv
= mOwner
->GetDocument(getter_AddRefs(document
));
784 if (NS_SUCCEEDED(rv
) && document
) {
785 nsPIDOMWindow
*win
= document
->GetWindow();
787 nsCOMPtr
<nsIScriptGlobalObject
> global
= do_QueryInterface(win
);
789 *outJSWindow
= global
->GetGlobalJSObject();
797 nsPluginInstancePeerImpl::GetJSThread(PRUint32
*outThreadID
)
799 *outThreadID
= mThreadID
;
804 nsPluginInstancePeerImpl::GetJSContext(JSContext
* *outContext
)
807 nsresult rv
= NS_ERROR_FAILURE
;
808 nsCOMPtr
<nsIDocument
> document
;
810 rv
= mOwner
->GetDocument(getter_AddRefs(document
));
812 if (NS_SUCCEEDED(rv
) && document
) {
813 nsIScriptGlobalObject
*global
= document
->GetScriptGlobalObject();
816 nsIScriptContext
*context
= global
->GetContext();
819 *outContext
= (JSContext
*) context
->GetNativeContext();
828 nsPluginInstancePeerImpl::Initialize(nsIPluginInstanceOwner
*aOwner
,
829 const nsMIMEType aMIMEType
)
832 NS_IF_ADDREF(mOwner
);
834 aOwner
->GetInstance(mInstance
);
835 NS_IF_RELEASE(mInstance
);
837 if (nsnull
!= aMIMEType
) {
838 mMIMEType
= (nsMIMEType
)PR_Malloc(PL_strlen(aMIMEType
) + 1);
840 if (nsnull
!= mMIMEType
)
841 PL_strcpy((char *)mMIMEType
, aMIMEType
);
844 // record the thread we were created in.
845 mThreadID
= NS_PTR_TO_INT32(PR_GetCurrentThread());
851 nsPluginInstancePeerImpl::SetOwner(nsIPluginInstanceOwner
*aOwner
)
853 // get rid of the previous owner
854 NS_IF_RELEASE(mOwner
);
857 NS_IF_ADDREF(mOwner
);
859 aOwner
->GetInstance(mInstance
);
860 NS_IF_RELEASE(mInstance
);
865 nsPluginInstancePeerImpl::GetOwner(nsIPluginInstanceOwner
**aOwner
)
867 NS_ENSURE_ARG_POINTER(aOwner
);
869 NS_IF_ADDREF(mOwner
);
870 return (mOwner
) ? NS_OK
: NS_ERROR_FAILURE
;
874 nsPluginInstancePeerImpl::InvalidateRect(nsPluginRect
*invalidRect
)
877 return NS_ERROR_FAILURE
;
879 return mOwner
->InvalidateRect(invalidRect
);
883 nsPluginInstancePeerImpl::InvalidateRegion(nsPluginRegion invalidRegion
)
886 return NS_ERROR_FAILURE
;
888 return mOwner
->InvalidateRegion(invalidRegion
);
892 nsPluginInstancePeerImpl::ForceRedraw(void)
895 return NS_ERROR_FAILURE
;
897 return mOwner
->ForceRedraw();