Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / embedding / browser / activex / src / pluginhostctrl / npn.cpp
blobebfc247294f48de5b4c3a199177abf7de3fe2169
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Adam Lock <adamlock@eircom.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "stdafx.h"
40 #include "npn.h"
41 #include "Pluginhostctrl.h"
43 #include "nsPluginHostWnd.h"
45 static NPError
46 _OpenURL(NPP npp, const char *szURL, const char *szTarget, void *pNotifyData, const char *pData, uint32 len, NPBool isFile)
48 if (!npp)
50 return NPERR_INVALID_INSTANCE_ERROR;
53 void *postData = NULL;
54 uint32 postDataLen = 0;
55 if (pData)
57 if (!isFile)
59 postData = (void *) pData;
60 postDataLen = len;
62 else
64 // TODO read the file specified in the postdata param into memory
68 nsPluginHostWnd *pWnd = (nsPluginHostWnd *) npp->ndata;
69 ATLASSERT(pWnd);
71 // Other window targets
72 if (szTarget)
74 CComPtr<IWebBrowserApp> cpBrowser;
75 pWnd->GetWebBrowserApp(&cpBrowser);
76 if (!cpBrowser)
78 return NPERR_GENERIC_ERROR;
81 CComBSTR url(szURL);
83 HRESULT hr;
85 // Test if the input URL has a schema which means it's relative,
86 // otherwise consider it to be relative to the current URL
88 WCHAR szSchema[10];
89 const DWORD cbSchema = sizeof(szSchema) / sizeof(szSchema[0]);
90 DWORD cbSchemaUsed = 0;
92 memset(szSchema, 0, cbSchema);
93 hr = CoInternetParseUrl(url.m_str, PARSE_SCHEMA, 0,
94 szSchema, cbSchema, &cbSchemaUsed, 0);
96 if (hr != S_OK || cbSchemaUsed == 0)
98 // Convert relative URLs to absolute, so that they can be loaded
99 // by the Browser
101 CComBSTR bstrCurrentURL;
102 cpBrowser->get_LocationURL(&bstrCurrentURL);
104 if (bstrCurrentURL.Length())
106 USES_CONVERSION;
107 DWORD cbNewURL = (url.Length() + bstrCurrentURL.Length() + 1) * sizeof(WCHAR);
108 DWORD cbNewURLUsed = 0;
109 WCHAR *pszNewURL = (WCHAR *) calloc(cbNewURL, 1);
110 ATLASSERT(pszNewURL);
112 CoInternetCombineUrl(
113 bstrCurrentURL.m_str,
114 url.m_str,
116 pszNewURL,
117 cbNewURL,
118 &cbNewURLUsed,
121 ATLASSERT(cbNewURLUsed < cbNewURL);
123 url = pszNewURL;
124 free(pszNewURL);
128 CComVariant vFlags;
129 CComVariant vTarget(szTarget);
130 CComVariant vPostData;
131 CComVariant vHeaders;
133 // Initialise postdata
134 if (postData)
136 // According to the documentation.
137 // The post data specified by PostData is passed as a SAFEARRAY
138 // structure. The variant should be of type VT_ARRAY and point to
139 // a SAFEARRAY. The SAFEARRAY should be of element type VT_UI1,
140 // dimension one, and have an element count equal to the number of
141 // bytes of post data.
143 SAFEARRAYBOUND saBound[1];
144 saBound[0].lLbound = 0;
145 saBound[0].cElements = len;
146 vPostData.vt = VT_ARRAY | VT_UI1;
147 vPostData.parray = SafeArrayCreate(VT_UI1, 1, saBound);
148 SafeArrayLock(vPostData.parray);
149 memcpy(vPostData.parray->pvData, postData, postDataLen);
150 SafeArrayUnlock(vPostData.parray);
153 cpBrowser->Navigate(url, &vFlags, &vTarget, &vPostData, &vHeaders);
154 // TODO listen to navigation & send a URL notify to plugin when completed
155 return NPERR_NO_ERROR;
158 USES_CONVERSION;
159 HRESULT hr = pWnd->OpenURLStream(A2CT(szURL), pNotifyData, postData, postDataLen);
160 return SUCCEEDED(hr) ? NPERR_NO_ERROR : NPERR_GENERIC_ERROR;
164 NPError NP_EXPORT
165 NPN_GetURL(NPP npp, const char* relativeURL, const char* target)
167 return _OpenURL(npp, relativeURL, target, NULL, NULL, 0, FALSE);
170 ////////////////////////////////////////////////////////////////////////
171 NPError NP_EXPORT
172 NPN_GetURLNotify(NPP npp,
173 const char* relativeURL,
174 const char* target,
175 void* notifyData)
177 if (strncmp("javascript:", relativeURL, 11) == 0)
178 return NPERR_INVALID_URL;
180 return _OpenURL(npp, relativeURL, target, notifyData, NULL, 0, FALSE);
184 ////////////////////////////////////////////////////////////////////////
185 NPError NP_EXPORT
186 NPN_PostURLNotify(NPP npp,
187 const char *relativeURL,
188 const char *target,
189 uint32 len,
190 const char *buf,
191 NPBool file,
192 void *notifyData)
194 return _OpenURL(npp, relativeURL, target, notifyData, buf, len, file);
198 ////////////////////////////////////////////////////////////////////////
199 NPError NP_EXPORT
200 NPN_PostURL(NPP npp,
201 const char *relativeURL,
202 const char *target,
203 uint32 len,
204 const char *buf,
205 NPBool file)
207 return _OpenURL(npp, relativeURL, target, NULL, buf, len, file);
211 ////////////////////////////////////////////////////////////////////////
212 NPError NP_EXPORT
213 NPN_NewStream(NPP npp, NPMIMEType type, const char* window, NPStream* *result)
215 if (!npp)
217 return NPERR_INVALID_INSTANCE_ERROR;
220 return NPERR_GENERIC_ERROR;
224 ////////////////////////////////////////////////////////////////////////
225 int32 NP_EXPORT
226 NPN_Write(NPP npp, NPStream *pstream, int32 len, void *buffer)
228 if (!npp)
230 return NPERR_INVALID_INSTANCE_ERROR;
233 return NPERR_GENERIC_ERROR;
237 ////////////////////////////////////////////////////////////////////////
238 NPError NP_EXPORT
239 NPN_DestroyStream(NPP npp, NPStream *pstream, NPError reason)
241 if (!npp)
243 return NPERR_INVALID_INSTANCE_ERROR;
246 return NPERR_GENERIC_ERROR;
250 ////////////////////////////////////////////////////////////////////////
251 void NP_EXPORT
252 NPN_Status(NPP npp, const char *message)
254 if (!npp)
256 return;
259 nsPluginHostWnd *pWnd = (nsPluginHostWnd *) npp->ndata;
260 ATLASSERT(pWnd);
262 // Update the status bar in the browser
263 CComPtr<IWebBrowserApp> cpBrowser;
264 pWnd->GetWebBrowserApp(&cpBrowser);
265 if (cpBrowser)
267 USES_CONVERSION;
268 cpBrowser->put_StatusText(A2OLE(message));
273 ////////////////////////////////////////////////////////////////////////
274 void * NP_EXPORT
275 NPN_MemAlloc (uint32 size)
277 return malloc(size);
281 ////////////////////////////////////////////////////////////////////////
282 void NP_EXPORT
283 NPN_MemFree (void *ptr)
285 if (ptr)
287 free(ptr);
292 ////////////////////////////////////////////////////////////////////////
293 uint32 NP_EXPORT
294 NPN_MemFlush(uint32 size)
296 return 0;
300 ////////////////////////////////////////////////////////////////////////
301 void NP_EXPORT
302 NPN_ReloadPlugins(NPBool reloadPages)
307 ////////////////////////////////////////////////////////////////////////
308 void NP_EXPORT
309 NPN_InvalidateRect(NPP npp, NPRect *invalidRect)
311 if (!npp)
313 return;
316 nsPluginHostWnd *pWnd = (nsPluginHostWnd *) npp->ndata;
317 ATLASSERT(pWnd);
319 RECT *prc = NULL;
320 RECT rcInvalid;
321 if (invalidRect)
323 prc = &rcInvalid;
324 prc->left = invalidRect->left;
325 prc->bottom = invalidRect->bottom;
326 prc->top = invalidRect->top;
327 prc->right = invalidRect->right;
329 pWnd->InvalidateRect(prc, FALSE);
333 ////////////////////////////////////////////////////////////////////////
334 void NP_EXPORT
335 NPN_InvalidateRegion(NPP npp, NPRegion invalidRegion)
337 if (!npp)
339 return;
341 nsPluginHostWnd *pWnd = (nsPluginHostWnd *) npp->ndata;
342 ATLASSERT(pWnd);
344 pWnd->InvalidateRgn(invalidRegion, FALSE);
348 ////////////////////////////////////////////////////////////////////////
349 void NP_EXPORT
350 NPN_ForceRedraw(NPP npp)
352 if (!npp)
354 return;
356 nsPluginHostWnd *pWnd = (nsPluginHostWnd *) npp->ndata;
357 ATLASSERT(pWnd);
358 pWnd->InvalidateRect(NULL, FALSE);
361 ////////////////////////////////////////////////////////////////////////
362 NPError NP_EXPORT
363 NPN_GetValue(NPP npp, NPNVariable variable, void *result)
365 if (!npp)
367 return NPERR_INVALID_INSTANCE_ERROR;
370 if (!result)
372 return NPERR_INVALID_PARAM;
375 nsPluginHostWnd *pWnd = (nsPluginHostWnd *) npp->ndata;
376 ATLASSERT(pWnd);
378 CComPtr<IWebBrowserApp> cpBrowser;
379 pWnd->GetWebBrowserApp(&cpBrowser);
381 // Test the variable
382 switch (variable)
384 case NPNVnetscapeWindow:
385 *((HWND *) result) = pWnd->m_hWnd;
386 break;
387 case NPNVjavascriptEnabledBool:
388 // TODO
389 *((NPBool *) result) = FALSE;
390 break;
391 case NPNVasdEnabledBool:
392 *((NPBool *) result) = FALSE;
393 break;
394 case NPNVisOfflineBool:
395 *((NPBool *) result) = FALSE;
396 if (cpBrowser)
398 CComQIPtr<IWebBrowser2> cpBrowser2 = cpBrowser;
399 if (cpBrowser2)
401 VARIANT_BOOL bOffline = VARIANT_FALSE;
402 cpBrowser2->get_Offline(&bOffline);
403 *((NPBool *) result) = (bOffline == VARIANT_TRUE) ? TRUE : FALSE;
406 break;
407 default:
408 return NPERR_GENERIC_ERROR;
410 return NPERR_NO_ERROR;
414 ////////////////////////////////////////////////////////////////////////
415 NPError NP_EXPORT
416 NPN_SetValue(NPP npp, NPPVariable variable, void *result)
418 if (!npp)
420 return NPERR_INVALID_INSTANCE_ERROR;
423 nsPluginHostWnd *pWnd = (nsPluginHostWnd *) npp->ndata;
424 ATLASSERT(pWnd);
426 switch (variable)
428 case NPPVpluginWindowBool:
430 pWnd->SetPluginWindowless((result == NULL) ? true : false);
432 return NPERR_NO_ERROR;
433 case NPPVpluginTransparentBool:
435 pWnd->SetPluginTransparent((result != NULL) ? true : false);
437 return NPERR_NO_ERROR;
440 return NPERR_GENERIC_ERROR;
444 ////////////////////////////////////////////////////////////////////////
445 NPError NP_EXPORT
446 NPN_RequestRead(NPStream *pstream, NPByteRange *rangeList)
448 if (!pstream || !rangeList || !pstream->ndata)
450 return NPERR_INVALID_PARAM;
453 return NPERR_GENERIC_ERROR;
456 ////////////////////////////////////////////////////////////////////////
457 JRIEnv* NP_EXPORT
458 NPN_GetJavaEnv(void)
460 return NULL;
464 ////////////////////////////////////////////////////////////////////////
465 const char * NP_EXPORT
466 NPN_UserAgent(NPP npp)
468 return "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6";
472 ////////////////////////////////////////////////////////////////////////
473 java_lang_Class* NP_EXPORT
474 NPN_GetJavaClass(void* handle)
476 return NULL;
480 ////////////////////////////////////////////////////////////////////////
481 jref NP_EXPORT
482 NPN_GetJavaPeer(NPP npp)
484 return NULL;