Back to index

lightning-sunbird  0.9+nobinonly
nsLiveConnect.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1998
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038     nsLiveConnect.cpp
00039  */
00040 
00041 #include "nsLiveconnect.h"
00042 #include "MRJMonitor.h"
00043 #include "MRJPlugin.h"
00044 #include "nsIPluginManager.h"
00045 
00046 #include <string.h>
00047 
00048 extern nsIPluginManager* thePluginManager;
00049 
00050 const InterfaceInfo nsLiveconnect::sInterfaces[] = {
00051     { NS_ILIVECONNECT_IID, INTERFACE_OFFSET(nsLiveconnect, nsILiveconnect) },
00052     { NS_IPLUGINSTREAMLISTENER_IID, INTERFACE_OFFSET(nsLiveconnect, nsIPluginStreamListener) },
00053 };
00054 const UInt32 nsLiveconnect::kInterfaceCount = sizeof(sInterfaces) / sizeof(InterfaceInfo);
00055 
00056 nsLiveconnect::nsLiveconnect()
00057     :   SupportsMixin(this, sInterfaces, kInterfaceCount),
00058         mJavaScriptMonitor(NULL), mScript(NULL), mResult(NULL)
00059 {
00060 }
00061 
00062 nsLiveconnect::~nsLiveconnect()
00063 {
00064     delete mJavaScriptMonitor;
00065 }
00066 
00067 static char* u2c(const jchar *ustr, jsize length)
00068 {
00069     char* result = new char[length + 1];
00070     if (result != NULL) {
00071         char* cstr = result;
00072         while (length-- > 0) {
00073             *cstr++ = (char) *ustr++;
00074         }
00075         *cstr = '\0';
00076     }
00077     return result;
00078 }
00079 
00080 const char* kJavaScriptPrefix = "javascript:";
00081 
00082 NS_METHOD   
00083 nsLiveconnect::Eval(JNIEnv *env, jsobject obj, const jchar *script, jsize length, void* principalsArray[], 
00084                     int numPrincipals, nsISupports *securitySupports, jobject *outResult)
00085 {
00086     MRJPluginInstance* pluginInstance = (MRJPluginInstance*) obj;
00087     nsIPluginStreamListener* listener = this;
00088 
00089     if (!mJavaScriptMonitor) {
00090         mJavaScriptMonitor = new MRJMonitor(pluginInstance->getSession());
00091         if (!mJavaScriptMonitor)
00092             return NS_ERROR_OUT_OF_MEMORY;
00093     }
00094 
00095     mJavaScriptMonitor->enter();
00096 
00097     while (mScript != NULL) {
00098         // some other thread is evaluating a script.
00099         mJavaScriptMonitor->wait();
00100     }
00101 
00102     nsresult rv = NS_OK;    
00103     // convert the script to ASCII, construct a "javascript:" URL.
00104     char* cscript = u2c(script, length);
00105     if (!cscript) {
00106         rv = NS_ERROR_OUT_OF_MEMORY;
00107     } else {
00108         mScript = new char[strlen(kJavaScriptPrefix) + length + 1];
00109         if (!mScript) {
00110             rv = NS_ERROR_OUT_OF_MEMORY;
00111         } else {
00112             strcpy(mScript, kJavaScriptPrefix);
00113             strcat(mScript, cscript);
00114             delete[] cscript;
00115             rv = thePluginManager->GetURL((nsIPluginInstance*)pluginInstance, mScript, NULL, listener);
00116 
00117             // need to block until the result is ready.
00118             mJavaScriptMonitor->wait();
00119 
00120             // default result is NULL, in case JavaScript returns undefined value.
00121             *outResult = NULL;
00122 
00123             // result should now be ready, convert it to a Java string and return.
00124             if (mResult != NULL) {
00125                 *outResult = env->NewStringUTF(mResult);
00126                 delete[] mResult;
00127                 mResult = NULL;
00128             }
00129 
00130             delete[] mScript;
00131             mScript = NULL;
00132         }
00133     }
00134 
00135     mJavaScriptMonitor->notifyAll();
00136 
00137     mJavaScriptMonitor->exit();
00138 
00139     return rv;
00140 }
00141 
00142 NS_METHOD nsLiveconnect::OnDataAvailable(nsIPluginStreamInfo* pluginInfo, nsIInputStream* input, PRUint32 length)
00143 {
00144     // hopefully all our data is available.
00145     mResult = new char[length + 1];
00146     if (mResult != NULL) {
00147         if (input->Read(mResult, length, &length) == NS_OK) {
00148             // We've delayed processing the applet tag, because we
00149             // don't know the location of the current document yet.
00150             mResult[length] = '\0';
00151         }
00152     }
00153     return NS_OK;
00154 }
00155 
00156 NS_METHOD nsLiveconnect::OnStopBinding(nsIPluginStreamInfo* pluginInfo, nsresult status)
00157 {
00158     // the stream has been closed, notify any waiting Java threads.
00159     mJavaScriptMonitor->notifyAll();
00160     return NS_OK;
00161 }