Back to index

lightning-sunbird  0.9+nobinonly
npn.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 the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Adam Lock <adamlock@netscape.com>
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 "stdafx.h"
00039 
00040 #include "npn.h"
00041 #include "Pluginhostctrl.h"
00042 
00043 #include "nsPluginHostCtrl.h"
00044 
00045 static NPError
00046 _OpenURL(NPP npp, const char *szURL, const char *szTarget, void *pNotifyData, const char *pData, uint32 len, NPBool isFile)
00047 {
00048     if (!npp)
00049     {
00050         return NPERR_INVALID_INSTANCE_ERROR;
00051     }
00052 
00053     void *postData = NULL;
00054     uint32 postDataLen = 0;
00055     if (pData)
00056     {
00057         if (!isFile)
00058         {
00059             postData = (void *) pData;
00060             postDataLen = len;
00061         }
00062         else
00063         {
00064             // TODO read the file specified in the postdata param into memory
00065         }
00066     }
00067 
00068     nsPluginHostCtrl *pCtrl = (nsPluginHostCtrl *) npp->ndata;
00069     ATLASSERT(pCtrl);
00070 
00071     // Other window targets
00072     if (szTarget)
00073     {
00074         CComPtr<IWebBrowserApp> cpBrowser;
00075         pCtrl->GetWebBrowserApp(&cpBrowser);
00076         if (!cpBrowser)
00077         {
00078             return NPERR_GENERIC_ERROR;
00079         }
00080 
00081         CComBSTR url(szURL);
00082         
00083         HRESULT hr;
00084 
00085         // Test if the input URL has a schema which means it's relative,
00086         // otherwise consider it to be relative to the current URL
00087 
00088         WCHAR szSchema[10];
00089         const DWORD cbSchema = sizeof(szSchema) / sizeof(szSchema[0]);
00090         DWORD cbSchemaUsed = 0;
00091 
00092         memset(szSchema, 0, cbSchema);
00093         hr = CoInternetParseUrl(url.m_str, PARSE_SCHEMA, 0,
00094             szSchema, cbSchema, &cbSchemaUsed, 0);
00095 
00096         if (hr != S_OK || cbSchemaUsed == 0)
00097         {
00098             // Convert relative URLs to absolute, so that they can be loaded
00099             // by the Browser
00100 
00101             CComBSTR bstrCurrentURL;
00102             cpBrowser->get_LocationURL(&bstrCurrentURL);
00103         
00104             if (bstrCurrentURL.Length())
00105             {
00106                 USES_CONVERSION;
00107                 DWORD cbNewURL = (url.Length() + bstrCurrentURL.Length() + 1) * sizeof(WCHAR);
00108                 DWORD cbNewURLUsed = 0;
00109                 WCHAR *pszNewURL = (WCHAR *) calloc(cbNewURL, 1);
00110                 ATLASSERT(pszNewURL);
00111 
00112                 CoInternetCombineUrl(
00113                     bstrCurrentURL.m_str,
00114                     url.m_str,
00115                     0,
00116                     pszNewURL,
00117                     cbNewURL,
00118                     &cbNewURLUsed,
00119                     0);
00120 
00121                 ATLASSERT(cbNewURLUsed < cbNewURL);
00122 
00123                 url = pszNewURL;
00124                 free(pszNewURL);
00125             }
00126         }
00127 
00128         CComVariant vFlags;
00129         CComVariant vTarget(szTarget);
00130         CComVariant vPostData;
00131         CComVariant vHeaders;
00132 
00133         // Initialise postdata
00134         if (postData)
00135         {
00136             // According to the documentation.
00137             // The post data specified by PostData is passed as a SAFEARRAY
00138             // structure. The variant should be of type VT_ARRAY and point to
00139             // a SAFEARRAY. The SAFEARRAY should be of element type VT_UI1,
00140             // dimension one, and have an element count equal to the number of
00141             // bytes of post data.
00142 
00143             SAFEARRAYBOUND saBound[1];
00144             saBound[0].lLbound = 0;
00145             saBound[0].cElements = len;
00146             vPostData.vt = VT_ARRAY | VT_UI1;
00147             vPostData.parray = SafeArrayCreate(VT_UI1, 1, saBound);
00148             SafeArrayLock(vPostData.parray);
00149             memcpy(vPostData.parray->pvData, postData, postDataLen);
00150             SafeArrayUnlock(vPostData.parray);
00151         }
00152 
00153         cpBrowser->Navigate(url, &vFlags, &vTarget, &vPostData, &vHeaders);
00154         // TODO listen to navigation & send a URL notify to plugin when completed
00155         return NPERR_NO_ERROR;
00156     }
00157 
00158     USES_CONVERSION;
00159     HRESULT hr = pCtrl->OpenURLStream(A2CT(szURL), pNotifyData, postData, postDataLen);
00160     return SUCCEEDED(hr) ? NPERR_NO_ERROR : NPERR_GENERIC_ERROR;
00161 }
00162 
00163 
00164 NPError NP_EXPORT
00165 NPN_GetURL(NPP npp, const char* relativeURL, const char* target)
00166 {
00167     return _OpenURL(npp, relativeURL, target, NULL, NULL, 0, FALSE);
00168 }
00169 
00171 NPError NP_EXPORT
00172 NPN_GetURLNotify(NPP         npp, 
00173               const char* relativeURL, 
00174               const char* target, 
00175               void*       notifyData)
00176 {
00177     return _OpenURL(npp, relativeURL, target, notifyData, NULL, 0, FALSE);
00178 }
00179 
00180 
00182 NPError NP_EXPORT
00183 NPN_PostURLNotify(NPP         npp,
00184                const char *relativeURL,
00185                const char *target,
00186                uint32     len,
00187                const char *buf,
00188                NPBool     file,
00189                void       *notifyData)
00190 {
00191     return _OpenURL(npp, relativeURL, target, notifyData, buf, len, file);
00192 }
00193 
00194 
00196 NPError NP_EXPORT
00197 NPN_PostURL(NPP npp,
00198          const char *relativeURL,
00199          const char *target,
00200          uint32     len,
00201          const char *buf,
00202          NPBool     file)
00203 {
00204     return _OpenURL(npp, relativeURL, target, NULL, buf, len, file);
00205 }
00206 
00207 
00209 NPError NP_EXPORT
00210 NPN_NewStream(NPP npp, NPMIMEType type, const char* window, NPStream* *result)
00211 {
00212     if (!npp)
00213     {
00214         return NPERR_INVALID_INSTANCE_ERROR;
00215     }
00216 
00217     return NPERR_GENERIC_ERROR;
00218 }
00219 
00220 
00222 int32 NP_EXPORT
00223 NPN_Write(NPP npp, NPStream *pstream, int32 len, void *buffer)
00224 {
00225     if (!npp)
00226     {
00227         return NPERR_INVALID_INSTANCE_ERROR;
00228     }
00229 
00230     return NPERR_GENERIC_ERROR;
00231 }
00232 
00233 
00235 NPError NP_EXPORT
00236 NPN_DestroyStream(NPP npp, NPStream *pstream, NPError reason)
00237 {
00238     if (!npp)
00239     {
00240         return NPERR_INVALID_INSTANCE_ERROR;
00241     }
00242 
00243     return NPERR_GENERIC_ERROR;
00244 }
00245 
00246 
00248 void NP_EXPORT
00249 NPN_Status(NPP npp, const char *message)
00250 {
00251     if (!npp)
00252     {
00253         return;
00254     }
00255 
00256     nsPluginHostCtrl *pCtrl = (nsPluginHostCtrl *) npp->ndata;
00257     ATLASSERT(pCtrl);
00258 
00259     // Update the status bar in the browser
00260     CComPtr<IWebBrowserApp> cpBrowser;
00261     pCtrl->GetWebBrowserApp(&cpBrowser);
00262     if (cpBrowser)
00263     {
00264         USES_CONVERSION;
00265         cpBrowser->put_StatusText(A2OLE(message));
00266     }
00267 }
00268 
00269 
00271 void * NP_EXPORT
00272 NPN_MemAlloc (uint32 size)
00273 {
00274     return malloc(size);
00275 }
00276 
00277 
00279 void NP_EXPORT
00280 NPN_MemFree (void *ptr)
00281 {
00282     if (ptr)
00283     {
00284         free(ptr);
00285     }
00286 }
00287 
00288 
00290 uint32 NP_EXPORT
00291 NPN_MemFlush(uint32 size)
00292 {
00293     return 0;
00294 }
00295 
00296 
00298 void NP_EXPORT
00299 NPN_ReloadPlugins(NPBool reloadPages)
00300 {
00301 }
00302 
00303 
00305 void NP_EXPORT
00306 NPN_InvalidateRect(NPP npp, NPRect *invalidRect)
00307 {
00308     if (!npp)
00309     {
00310         return;
00311     }
00312 
00313     // TODO - windowless plugins
00314 }
00315 
00316 
00318 void NP_EXPORT
00319 NPN_InvalidateRegion(NPP npp, NPRegion invalidRegion)
00320 {
00321     if (!npp)
00322     {
00323         return;
00324     }
00325     // TODO - windowless plugins
00326 }
00327 
00328 
00330 void NP_EXPORT
00331 NPN_ForceRedraw(NPP npp)
00332 {
00333     if (!npp)
00334     {
00335         return;
00336     }
00337     // TODO - windowless plugins
00338 }
00339 
00341 NPError NP_EXPORT
00342 NPN_GetValue(NPP npp, NPNVariable variable, void *result)
00343 {
00344     if (!npp)
00345     {
00346         return NPERR_INVALID_INSTANCE_ERROR;
00347     }
00348 
00349     if (!result)
00350     {
00351         return NPERR_INVALID_PARAM;
00352     }
00353 
00354     nsPluginHostCtrl *pCtrl = (nsPluginHostCtrl *) npp->ndata;
00355     ATLASSERT(pCtrl);
00356 
00357     CComPtr<IWebBrowserApp> cpBrowser;
00358     pCtrl->GetWebBrowserApp(&cpBrowser);
00359 
00360     // Test the variable
00361     if (variable == NPNVnetscapeWindow)
00362     {
00363         *((HWND *) result) = pCtrl->m_wndPlugin.m_hWnd;
00364     }
00365     else if (variable == NPNVjavascriptEnabledBool)
00366     {
00367         // TODO
00368         *((NPBool *) result) = TRUE;
00369     }
00370     else if (variable == NPNVasdEnabledBool) // Smart update
00371     {
00372         *((NPBool *) result) = FALSE;
00373     }
00374     else if (variable == NPNVisOfflineBool)
00375     {
00376         *((NPBool *) result) = FALSE;
00377         if (cpBrowser)
00378         {
00379             CComQIPtr<IWebBrowser2> cpBrowser2 = cpBrowser;
00380             if (cpBrowser2)
00381             {
00382                 VARIANT_BOOL bOffline = VARIANT_FALSE;
00383                 cpBrowser2->get_Offline(&bOffline);
00384                 *((NPBool *) result) = (bOffline == VARIANT_TRUE) ? TRUE : FALSE;
00385             }
00386         }
00387     }
00388     else
00389     {
00390         return NPERR_GENERIC_ERROR;
00391     }
00392 
00393     return NPERR_NO_ERROR;
00394 }
00395 
00396 
00398 NPError NP_EXPORT
00399 NPN_SetValue(NPP npp, NPPVariable variable, void *result)
00400 {
00401     if (!npp)
00402     {
00403         return NPERR_INVALID_INSTANCE_ERROR;
00404     }
00405 
00406     // TODO windowless
00407     // NPPVpluginWindowBool
00408     // NPPVpluginTransparentBool
00409 
00410     return NPERR_GENERIC_ERROR;
00411 }
00412 
00413 
00415 NPError NP_EXPORT
00416 NPN_RequestRead(NPStream *pstream, NPByteRange *rangeList)
00417 {
00418     if (!pstream || !rangeList || !pstream->ndata)
00419     {
00420         return NPERR_INVALID_PARAM;
00421     }
00422 
00423     return NPERR_GENERIC_ERROR;
00424 }
00425 
00427 JRIEnv* NP_EXPORT
00428 NPN_GetJavaEnv(void)
00429 {
00430     return NULL;
00431 }
00432 
00433 
00435 const char * NP_EXPORT
00436 NPN_UserAgent(NPP npp)
00437 {
00438     return NULL;
00439 }
00440 
00441 
00443 java_lang_Class* NP_EXPORT
00444 NPN_GetJavaClass(void* handle)
00445 {
00446     return NULL;
00447 }
00448 
00449 
00451 jref NP_EXPORT
00452 NPN_GetJavaPeer(NPP npp)
00453 {
00454     return NULL;
00455 }
00456 
00457