Back to index

lightning-sunbird  0.9+nobinonly
nsJVMPluginTagInfo.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsJVMPluginTagInfo.h"
00039 #include "nsIPluginTagInfo2.h"
00040 #include "plstr.h"
00041 #include "nsCRT.h"      // mixing metaphors with plstr.h!
00042 #ifdef XP_UNIX
00043 #undef Bool
00044 #endif
00045 
00046 static NS_DEFINE_IID(kIJVMPluginTagInfoIID, NS_IJVMPLUGINTAGINFO_IID);
00047 static NS_DEFINE_IID(kIPluginTagInfo2IID, NS_IPLUGINTAGINFO2_IID);
00048 
00050 // nsJVMPluginTagInfo
00052 
00053 nsJVMPluginTagInfo::nsJVMPluginTagInfo(nsISupports* outer, nsIPluginTagInfo2* info)
00054     : fPluginTagInfo(info), fSimulatedCodebase(NULL), fSimulatedCode(NULL)
00055 {
00056     NS_INIT_AGGREGATED(outer);
00057 }
00058 
00059 nsJVMPluginTagInfo::~nsJVMPluginTagInfo(void)
00060 {
00061     if (fSimulatedCodebase)
00062         PL_strfree(fSimulatedCodebase);
00063 
00064     if (fSimulatedCode)
00065         PL_strfree(fSimulatedCode);
00066 }
00067 
00068 NS_IMPL_AGGREGATED(nsJVMPluginTagInfo)
00069 
00070 NS_METHOD
00071 nsJVMPluginTagInfo::AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr)
00072 {
00073         if(!aInstancePtr)
00074             return NS_ERROR_INVALID_POINTER;
00075 
00076     if (aIID.Equals(kIJVMPluginTagInfoIID))
00077             *aInstancePtr = NS_STATIC_CAST(nsIJVMPluginTagInfo*, this);
00078         else if (aIID.Equals(NS_GET_IID(nsISupports)))
00079             *aInstancePtr = GetInner();
00080         else  {
00081             *aInstancePtr = nsnull;
00082                 return NS_NOINTERFACE;
00083         }
00084         
00085         NS_ADDREF((nsISupports*)aInstancePtr);
00086         return NS_OK;
00087 }
00088 
00089 
00090 static void
00091 oji_StandardizeCodeAttribute(char* buf)
00092 {
00093     // strip off the ".class" suffix
00094     char* cp;
00095 
00096     if ((cp = PL_strrstr(buf, ".class")) != NULL)
00097         *cp = '\0';
00098 
00099     // Convert '/' to '.'
00100     cp = buf;
00101     while ((*cp) != '\0') {
00102         if ((*cp) == '/')
00103             (*cp) = '.';
00104 
00105         ++cp;
00106     }
00107 }
00108 
00109 NS_METHOD
00110 nsJVMPluginTagInfo::GetCode(const char* *result)
00111 {
00112     if (fSimulatedCode) {
00113         *result = fSimulatedCode;
00114         return NS_OK;
00115     }
00116 
00117     const char* code;
00118     nsresult err = fPluginTagInfo->GetAttribute("code", &code);
00119     if (err == NS_OK && code) {
00120         fSimulatedCode = PL_strdup(code);
00121         oji_StandardizeCodeAttribute(fSimulatedCode);
00122         *result = fSimulatedCode;
00123         return NS_OK;
00124     }
00125 
00126     const char* classid;
00127     err = fPluginTagInfo->GetAttribute("classid", &classid);
00128     if (err == NS_OK && classid && PL_strncasecmp(classid, "java:", 5) == 0) {
00129         fSimulatedCode = PL_strdup(classid + 5); // skip "java:"
00130         oji_StandardizeCodeAttribute(fSimulatedCode);
00131         *result = fSimulatedCode;
00132         return NS_OK;
00133     }
00134 
00135     // XXX what about "javaprogram:" and "javabean:"?
00136     return NS_ERROR_FAILURE;
00137 }
00138 
00139 NS_METHOD
00140 nsJVMPluginTagInfo::GetCodeBase(const char* *result)
00141 {
00142     // If we've already cached and computed the value, use it...
00143     if (fSimulatedCodebase) {
00144         *result = fSimulatedCodebase;
00145         return NS_OK;
00146     }
00147 
00148     // See if it's supplied as an attribute...
00149     const char* codebase;
00150     nsresult err = fPluginTagInfo->GetAttribute("codebase", &codebase);
00151     if (err == NS_OK && codebase != NULL) {
00152         *result = codebase;
00153         return NS_OK;
00154     }
00155 
00156     // Okay, we'll need to simulate it from the layout tag's base URL.
00157     const char* docBase;
00158     err = fPluginTagInfo->GetDocumentBase(&docBase);
00159     if (err != NS_OK) return err;
00160     codebase = (const char*) docBase;
00161 
00162     if ((fSimulatedCodebase = PL_strdup(codebase)) != NULL) {
00163         char* lastSlash = PL_strrchr(fSimulatedCodebase, '/');
00164 
00165         // chop of the filename from the original document base URL to
00166         // generate the codebase.
00167         if (lastSlash != NULL)
00168             *(lastSlash + 1) = '\0';
00169     }
00170     
00171     *result = fSimulatedCodebase;
00172     return NS_OK;
00173 }
00174 
00175 NS_METHOD
00176 nsJVMPluginTagInfo::GetArchive(const char* *result)
00177 {
00178     return fPluginTagInfo->GetAttribute("archive", result);
00179 }
00180 
00181 NS_METHOD
00182 nsJVMPluginTagInfo::GetName(const char* *result)
00183 {
00184     const char* attrName;
00185     nsPluginTagType type;
00186     nsresult err = fPluginTagInfo->GetTagType(&type);
00187     if (err != NS_OK) return err;
00188     switch (type) {
00189       case nsPluginTagType_Applet:
00190         attrName = "name";
00191         break;
00192       default:
00193         attrName = "id";
00194         break;
00195     }
00196     return fPluginTagInfo->GetAttribute(attrName, result);
00197 }
00198 
00199 NS_METHOD
00200 nsJVMPluginTagInfo::GetMayScript(PRBool *result)
00201 {
00202     const char* attr;
00203     *result = PR_FALSE;
00204 
00205     nsresult err = fPluginTagInfo->GetAttribute("mayscript", &attr);
00206     if (err) return err;
00207 
00208     if (PL_strcasecmp(attr, "true") == 0)
00209     {
00210        *result = PR_TRUE;
00211     }
00212     return NS_OK;
00213 }
00214 
00215 NS_METHOD
00216 nsJVMPluginTagInfo::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr,
00217                            nsIPluginTagInfo2* info)
00218 {
00219         if(!aInstancePtr)
00220             return NS_ERROR_INVALID_POINTER;
00221 
00222     if (outer && !aIID.Equals(NS_GET_IID(nsISupports)))
00223         return NS_ERROR_INVALID_ARG;
00224 
00225     nsJVMPluginTagInfo* jvmTagInfo = new nsJVMPluginTagInfo(outer, info);
00226     if (jvmTagInfo == NULL)
00227         return NS_ERROR_OUT_OF_MEMORY;
00228 
00229         nsresult result = jvmTagInfo->AggregatedQueryInterface(aIID, aInstancePtr);
00230         if (NS_FAILED(result)) goto error;
00231 
00232     result = jvmTagInfo->QueryInterface(kIPluginTagInfo2IID,
00233                                             (void**)&jvmTagInfo->fPluginTagInfo);
00234     if (NS_FAILED(result)) goto error;
00235     return result;
00236 
00237   error:
00238     delete jvmTagInfo;
00239     return result;
00240 }
00241 
00243