Back to index

lightning-sunbird  0.9+nobinonly
nsPluginHostCtrl.h
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 // nsPluginHostCtrl.h : Declaration of the nsPluginHostCtrl
00039 
00040 #ifndef __PLUGINHOSTCTRL_H_
00041 #define __PLUGINHOSTCTRL_H_
00042 
00043 #include "resource.h"       // main symbols
00044 #include <atlctl.h>
00045 
00046 #include "nsPluginWnd.h"
00047 
00048 class nsURLDataCallback;
00049 
00050 template <class Entry>
00051 class nsSimpleArray
00052 {
00053     Entry *m_pData;
00054     unsigned long m_nSize;
00055     unsigned long m_nMaxSize;
00056     unsigned long m_nExpandArrayBy;
00057 public:
00058     nsSimpleArray(unsigned long nExpandArrayBy = 10) :
00059       m_pData(NULL),
00060       m_nSize(0),
00061       m_nMaxSize(0),
00062       m_nExpandArrayBy(nExpandArrayBy)
00063     {
00064     }
00065     
00066     virtual ~nsSimpleArray()
00067     {
00068         Empty();
00069     }
00070 
00071     Entry ElementAt(unsigned long aIndex) const
00072     {
00073         ATLASSERT(aIndex < m_nSize);
00074         return m_pData[aIndex];
00075     }
00076 
00077     Entry operator[](unsigned long aIndex) const
00078     {
00079         return ElementAt(aIndex);
00080     }
00081 
00082     void AppendElement(Entry entry)
00083     {
00084         if (!m_pData)
00085         {
00086             m_nMaxSize = m_nExpandArrayBy;
00087             m_pData = (Entry *) malloc(sizeof(Entry) * m_nMaxSize);
00088         }
00089         else if (m_nSize == m_nMaxSize)
00090         {
00091             m_nMaxSize += m_nExpandArrayBy;
00092             m_pData = (Entry *) realloc(m_pData, sizeof(Entry) * m_nMaxSize);
00093         }
00094         ATLASSERT(m_pData);
00095         if (m_pData)
00096         {
00097             m_pData[m_nSize++] = entry;
00098         }
00099     }
00100 
00101     void RemoveElementAt(unsigned long nIndex)
00102     {
00103         ATLASSERT(aIndex < m_nSize);
00104         if (nIndex < m_nSize)
00105         {
00106             m_nSize--;
00107             if (m_nSize > 0)
00108             {
00109                 m_pData[nIndex] = m_pData[m_nSize - 1];
00110                 m_nSize--;
00111             }
00112         }
00113     }
00114 
00115     void RemoveElement(Entry entry)
00116     {
00117         unsigned long i = 0;
00118         while (i < m_nSize)
00119         {
00120             if (m_pData[i] == entry)
00121             {
00122                 m_nSize--;
00123                 if (m_nSize > 0)
00124                 {
00125                     m_pData[i] = m_pData[m_nSize - 1];
00126                     m_nSize--;
00127                 }
00128                 continue;
00129             }
00130             i++;
00131         }
00132     }
00133 
00134     void Empty()
00135     {
00136         if (m_pData)
00137         {
00138             free(m_pData);
00139             m_pData = NULL;
00140             m_nSize = m_nMaxSize = 0;
00141         }
00142     }
00143     BOOL IsEmpty() const { return m_nSize == 0 ? TRUE : FALSE; }
00144     unsigned long Count() const { return m_nSize; }
00145 };
00146 
00147 
00148 #define PLUGINS_FROM_IE       0x1
00149 #define PLUGINS_FROM_NS4X     0x2
00150 #define PLUGINS_FROM_NS6X     0x4
00151 #define PLUGINS_FROM_MOZILLA  0x8
00152 
00154 // nsPluginHostCtrl
00155 class ATL_NO_VTABLE nsPluginHostCtrl : 
00156        public CComObjectRootEx<CComSingleThreadModel>,
00157        public CStockPropImpl<nsPluginHostCtrl, IMozPluginHostCtrl, &IID_IMozPluginHostCtrl, &LIBID_PLUGINHOSTCTRLLib>,
00158        public CComControl<nsPluginHostCtrl>,
00159        public IPersistStreamInitImpl<nsPluginHostCtrl>,
00160        public IOleControlImpl<nsPluginHostCtrl>,
00161        public IOleObjectImpl<nsPluginHostCtrl>,
00162        public IOleInPlaceActiveObjectImpl<nsPluginHostCtrl>,
00163        public IViewObjectExImpl<nsPluginHostCtrl>,
00164        public IOleInPlaceObjectWindowlessImpl<nsPluginHostCtrl>,
00165        public ISupportErrorInfo,
00166        public IConnectionPointContainerImpl<nsPluginHostCtrl>,
00167        public IPersistStorageImpl<nsPluginHostCtrl>,
00168     public IPersistPropertyBagImpl<nsPluginHostCtrl>,
00169        public ISpecifyPropertyPagesImpl<nsPluginHostCtrl>,
00170        public IQuickActivateImpl<nsPluginHostCtrl>,
00171        public IDataObjectImpl<nsPluginHostCtrl>,
00172        public IProvideClassInfo2Impl<&CLSID_MozPluginHostCtrl, &DIID__IMozPluginHostCtrlEvents, &LIBID_PLUGINHOSTCTRLLib>,
00173        public IPropertyNotifySinkCP<nsPluginHostCtrl>,
00174        public CComCoClass<nsPluginHostCtrl, &CLSID_MozPluginHostCtrl>
00175 {
00176 protected:
00177     virtual ~nsPluginHostCtrl();
00178 
00179 public:
00180        nsPluginHostCtrl();
00181 
00182 DECLARE_REGISTRY_RESOURCEID(IDR_PLUGINHOSTCTRL)
00183 
00184 DECLARE_PROTECT_FINAL_CONSTRUCT()
00185 
00186 BEGIN_COM_MAP(nsPluginHostCtrl)
00187        COM_INTERFACE_ENTRY(IMozPluginHostCtrl)
00188        COM_INTERFACE_ENTRY2(IDispatch, IMozPluginHostCtrl)
00189        COM_INTERFACE_ENTRY(IViewObjectEx)
00190        COM_INTERFACE_ENTRY(IViewObject2)
00191        COM_INTERFACE_ENTRY(IViewObject)
00192        COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
00193        COM_INTERFACE_ENTRY(IOleInPlaceObject)
00194        COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
00195        COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
00196        COM_INTERFACE_ENTRY(IOleControl)
00197        COM_INTERFACE_ENTRY(IOleObject)
00198        COM_INTERFACE_ENTRY(IPersistStreamInit)
00199        COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
00200     COM_INTERFACE_ENTRY(IPersistPropertyBag)
00201        COM_INTERFACE_ENTRY(ISupportErrorInfo)
00202        COM_INTERFACE_ENTRY(IConnectionPointContainer)
00203 //     COM_INTERFACE_ENTRY(ISpecifyPropertyPages)
00204        COM_INTERFACE_ENTRY(IQuickActivate)
00205        COM_INTERFACE_ENTRY(IPersistStorage)
00206        COM_INTERFACE_ENTRY(IDataObject)
00207        COM_INTERFACE_ENTRY(IProvideClassInfo)
00208        COM_INTERFACE_ENTRY(IProvideClassInfo2)
00209 END_COM_MAP()
00210 
00211 BEGIN_PROP_MAP(nsPluginHostCtrl)
00212        PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
00213        PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
00214        PROP_ENTRY("HWND", DISPID_HWND, CLSID_NULL)
00215        PROP_ENTRY("Text", DISPID_TEXT, CLSID_NULL)
00216     // Mozilla plugin host control properties
00217     PROP_ENTRY("type", 1, CLSID_NULL)
00218     PROP_ENTRY("src", 2, CLSID_NULL)
00219     PROP_ENTRY("pluginspage", 3, CLSID_NULL)
00220        // Example entries
00221        // PROP_ENTRY("Property Description", dispid, clsid)
00222        // PROP_PAGE(CLSID_StockColorPage)
00223 END_PROP_MAP()
00224 
00225 BEGIN_CONNECTION_POINT_MAP(nsPluginHostCtrl)
00226        CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink)
00227 END_CONNECTION_POINT_MAP()
00228 
00229 BEGIN_MSG_MAP(nsPluginHostCtrl)
00230        MESSAGE_HANDLER(WM_CREATE, OnCreate)
00231        MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
00232        MESSAGE_HANDLER(WM_SIZE, OnSize)
00233        MESSAGE_HANDLER(WM_PAINT, OnPaint)
00234        CHAIN_MSG_MAP(CComControl<nsPluginHostCtrl>)
00235 END_MSG_MAP()
00236 
00237 
00238 // Handler prototypes:
00239 //  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
00240 //  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
00241 //  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
00242 
00243 
00244 
00245 // ISupportsErrorInfo
00246        STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
00247        {
00248               static const IID* arr[] = 
00249               {
00250                      &IID_IMozPluginHostCtrl,
00251               };
00252               for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
00253               {
00254                      if (InlineIsEqualGUID(*arr[i], riid))
00255                             return S_OK;
00256               }
00257               return S_FALSE;
00258        }
00259 
00260 // IViewObjectEx
00261        DECLARE_VIEW_STATUS(0)
00262 
00263 // IPersistPropertyBag override
00264        STDMETHOD(Load)(LPPROPERTYBAG pPropBag, LPERRORLOG pErrorLog);
00265 
00266 // IMozPluginHostCtrl
00267 public:
00268        STDMETHOD(get_PluginSource)(/*[out, retval]*/ BSTR *pVal);
00269        STDMETHOD(put_PluginSource)(/*[in]*/ BSTR newVal);
00270        STDMETHOD(get_PluginContentType)(/*[out, retval]*/ BSTR *pVal);
00271        STDMETHOD(put_PluginContentType)(/*[in]*/ BSTR newVal);
00272        STDMETHOD(get_PluginsPage)(/*[out, retval]*/ BSTR *pVal);
00273        STDMETHOD(put_PluginsPage)(/*[in]*/ BSTR newVal);
00274 
00275 // Windows message handlers
00276 public:
00277        LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
00278        LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
00279        LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
00280        LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
00281 
00282 public:
00283        CComBSTR m_bstrText;
00284 
00285 protected:
00286     CComBSTR m_bstrContentType;
00287     CComBSTR m_bstrSource;
00288     CComBSTR m_bstrPluginsPage;
00289 
00290     // Array of plugins
00291     struct PluginInfo
00292     {
00293         TCHAR *szPluginPath;
00294         TCHAR *szPluginName;
00295         TCHAR *szMIMEType;
00296     };
00297 
00298     nsSimpleArray<PluginInfo *> m_Plugins;
00299 
00300     // Array of names and values to pass to the new plugin
00301     unsigned long m_nArgs;
00302     unsigned long m_nArgsMax;
00303     char **m_pszArgNames;
00304     char **m_pszArgValues;
00305 
00306     // Array of loaded plugins which is shared amongst instances of this control
00307     struct LoadedPluginInfo
00308     {
00309         TCHAR *szFullPath; // Path + plugin name
00310         HINSTANCE hInstance;
00311         DWORD nRefCount;
00312         NPPluginFuncs NPPFuncs;
00313     };
00314 
00315     static nsSimpleArray<LoadedPluginInfo *> m_LoadedPlugins;
00316 
00317     LoadedPluginInfo *m_pLoadedPlugin;
00318 
00319     NPWindow m_NPWindow;
00320 
00321     static NPNetscapeFuncs g_NPNFuncs;
00322     static HRESULT InitPluginCallbacks();
00323 
00324     HRESULT CreatePluginList(unsigned long ulFlags);
00325     HRESULT CreatePluginListFrom(const TCHAR *szPluginsDir);
00326     HRESULT CleanupPluginList();
00327     HRESULT GetPluginInfo(const TCHAR * pszPluginPath, PluginInfo *pInfo);
00328 
00329 public:
00330     NPP_t m_NPP;
00331     BOOL  m_bPluginIsAlive;
00332     BOOL  m_bCreatePluginFromStreamData;
00333     nsPluginWnd m_wndPlugin;
00334 
00335     // Struct holding pointers to the functions within the plugin
00336     NPPluginFuncs m_NPPFuncs;
00337 
00338     HRESULT GetWebBrowserApp(IWebBrowserApp **pBrowser);
00339     HRESULT GetBaseURL(TCHAR **szBaseURL);
00340     
00341     HRESULT AddPluginParam(const char *szName, const char *szValue);
00342     HRESULT LoadPluginByContentType(const TCHAR *pszContentType);
00343        HRESULT LoadPlugin(const TCHAR *pszPluginPath);
00344     HRESULT FindPluginPathByContentType(const TCHAR *pszContentType, TCHAR **ppszPluginPath);
00345     HRESULT UnloadPlugin();
00346 
00347     HRESULT OpenURLStream(const TCHAR *szURL, void *pNotifyData, const void *pPostData, unsigned long nDataLength);
00348 
00349     HRESULT CreatePluginInstance();
00350     HRESULT DestroyPluginInstance();
00351     HRESULT SizeToFitPluginInstance();
00352 };
00353 
00354 #endif //__PLUGINHOSTCTRL_H_