VirtualBox

Changeset 2976

Show
Ignore:
Timestamp:
06/01/07 15:47:51 (2 years ago)
Author:
vboxsync
Message:

Main:
- XPCOM: Implemented storing compreg.data and xpti.data in the VirtualBox home directory (defect #1947).
- Cleaned up the COM/XPCOM layer API (removed tons of duplicate definitions and several pieces of duplcate code).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/include/VBox/com/com.h

    r2672 r2976  
    2525 
    2626#include "VBox/com/defs.h" 
     27#include "VBox/com/string.h" 
    2728 
    2829namespace com 
     
    4243 *  No COM calls may be made after this method returns. 
    4344 */ 
    44 void Shutdown(); 
     45HRESULT Shutdown(); 
    4546 
    4647/** 
     
    5556void GetInterfaceNameByIID (const GUID &aIID, BSTR *aName); 
    5657 
     58/**  
     59 *  Returns the VirtualBox user home directory. 
     60 * 
     61 *  On failure, this function will return a path that caused a failure (or a 
     62 *  null string if the faiulre is not path-related). 
     63 * 
     64 *  On success, this function will try to create the returned directory if it 
     65 *  doesn't exist yet. This may also fail with the corresponding status code. 
     66 *  
     67 *  @param aDir     Where to return the directory to. 
     68 *  @return         VBox status code. 
     69 */ 
     70int GetVBoxUserHomeDirectory (Utf8Str &aDir); 
     71 
    5772}; // namespace com 
    5873 
  • trunk/include/VBox/com/defs.h

    r612 r2976  
    138138#define E_ABORT             NS_ERROR_ABORT 
    139139#define E_FAIL              NS_ERROR_FAILURE 
     140/* Note: a better analog for E_ACCESSDENIED would probably be 
     141 * NS_ERROR_NOT_AVAILABLE, but we want binary compatibility for now. */ 
    140142#define E_ACCESSDENIED      ((nsresult) 0x80070005L) 
    141143 
  • trunk/include/VBox/com/ptr.h

    r1472 r2976  
    2424#define __VBox_com_ptr_h__ 
    2525 
    26 #if defined (__WIN__
     26#if !defined (VBOX_WITH_XPCOM
    2727 
    2828#include <atlbase.h> 
     
    3232#endif  
    3333 
    34 #else // !defined (__WIN__) 
     34#else /* !defined (VBOX_WITH_XPCOM) */ 
    3535 
    3636#include <nsXPCOM.h> 
     
    4646    "@mozilla.org/ipc/dconnect-service;1" 
    4747 
    48 #endif // !defined (__WIN__) 
     48#endif /* !defined (VBOX_WITH_XPCOM) */ 
    4949 
    5050#include <VBox/com/defs.h> 
     
    8585    class NoAddRefRelease : public I { 
    8686        private: 
    87 #ifdef __WIN__ 
     87#if !defined (VBOX_WITH_XPCOM) 
    8888            STDMETHOD_(ULONG, AddRef)() = 0; 
    8989            STDMETHOD_(ULONG, Release)() = 0; 
    90 #else 
     90#else /* !defined (VBOX_WITH_XPCOM) */ 
    9191            NS_IMETHOD_(nsrefcnt) AddRef(void) = 0; 
    9292            NS_IMETHOD_(nsrefcnt) Release(void) = 0; 
    93 #endif 
     93#endif /* !defined (VBOX_WITH_XPCOM) */ 
    9494    }; 
    9595 
     
    255255        HRESULT rc; 
    256256        I *obj = NULL; 
    257 #if defined (__WIN__
     257#if !defined (VBOX_WITH_XPCOM
    258258        rc = CoCreateInstance (clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF (I), 
    259259                               (void **) &obj); 
    260 #else 
     260#else /* !defined (VBOX_WITH_XPCOM) */ 
    261261        nsCOMPtr <nsIComponentManager> manager; 
    262262        rc = NS_GetComponentManager (getter_AddRefs (manager)); 
     
    264264            rc = manager->CreateInstance (clsid, nsnull, NS_GET_IID (I), 
    265265                                          (void **) &obj); 
    266 #endif 
     266#endif /* !defined (VBOX_WITH_XPCOM) */ 
    267267        *this = obj; 
    268268        if (SUCCEEDED (rc)) 
     
    282282    HRESULT createLocalObject (const CLSID &clsid) 
    283283    { 
    284 #if defined (__WIN__
     284#if !defined (VBOX_WITH_XPCOM
    285285        HRESULT rc; 
    286286        I *obj = NULL; 
     
    291291            obj->Release(); 
    292292        return rc; 
    293 #else 
     293#else /* !defined (VBOX_WITH_XPCOM) */ 
    294294        return createInprocObject (clsid); 
    295 #endif 
     295#endif /* !defined (VBOX_WITH_XPCOM) */ 
    296296    } 
    297297 
     
    410410    HRESULT createObject() { 
    411411        HRESULT rc; 
    412 #if defined (__WIN__
     412#if !defined (VBOX_WITH_XPCOM
    413413#   ifdef VBOX_COM_OUTOFPROC_MODULE 
    414414        CComObjectNoLock <C> *obj = new CComObjectNoLock <C>(); 
     
    424424        rc = CComObject <C>::CreateInstance (&obj); 
    425425#   endif 
    426 #else 
     426#else /* !defined (VBOX_WITH_XPCOM) */ 
    427427        CComObject <C> *obj = new CComObject <C>(); 
    428428        if (obj) { 
     
    431431            rc = E_OUTOFMEMORY; 
    432432        } 
    433 #endif 
     433#endif /* !defined (VBOX_WITH_XPCOM) */ 
    434434        *this = obj; 
    435435        return rc; 
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.cpp

    r2920 r2976  
    60746074    else 
    60756075    { 
    6076         // check if an alternative VBox Home directory is set 
    6077         Utf8Str homeDir = getenv ("VBOX_USER_HOME"); 
    6078         if (!homeDir) 
    6079         { 
    6080             // compose the config directory (full path) 
    6081             char home [RTPATH_MAX]; 
    6082             RTPathUserHome (home, RTPATH_MAX); 
    6083             homeDir = Utf8StrFmt ("%s%c%s", home, RTPATH_DELIMITER, ".VirtualBox"); 
    6084         } 
     6076        Utf8Str homeDir; 
     6077        GetVBoxUserHomeDirectory (homeDir); 
    60856078 
    60866079        RTPrintf ("Updating settings files in the following VirtualBox Home Directory:\n" 
  • trunk/src/VBox/Frontends/VirtualBox/include/COMDefs.h

    r2671 r2976  
    2828#define __COMDefs_h__ 
    2929 
     30 
     31/* Both VBox/com/assert.h and qglobal.h contain a definition of ASSERT. 
     32 * Either of them can be already included here, so try to shut them up.  */ 
     33#undef ASSERT 
     34 
     35#include <VBox/com/com.h> 
     36 
     37#undef ASSERT 
     38 
    3039#include <qglobal.h> 
    3140#include <qstring.h> 
     
    3544 
    3645/* 
    37  * common COM / XPCOM includes and defines 
     46 * Additional COM / XPCOM defines and includes 
    3847 */ 
    3948 
    40 #if defined(Q_OS_WIN32) 
    41  
    42     #include <objbase.h> 
    43     /* for _ATL_IIDOF */ 
    44     #include <atldef.h> 
    45  
    46     #include <VBox/types.h> 
    47  
    48     /* these are XPCOM only */ 
    49     #define NS_DECL_ISUPPORTS 
    50  
    51     /* makes interface getter/setter names (n must be capitalized) */ 
    52     #define COMGETTER(n)    get_##n 
    53     #define COMSETTER(n)    put_##n 
    54  
    55     #define IN_BSTRPARAM    BSTR 
    56     #define IN_GUIDPARAM    GUID 
    57  
    58     /* const reference to IID of the interface */ 
    59     #define COM_IIDOF(I)    _ATL_IIDOF (I) 
    60  
    61 #else 
    62  
    63     #include <VBox/types.h> 
    64  
    65     #include <nsMemory.h> 
    66     #include <nsIComponentManager.h> 
    67     #include <ipcIDConnectService.h> 
    68  
    69     class nsIComponentManager; 
    70     class nsIEventQueue; 
    71     class ipcIDConnectService; 
    72  
    73     typedef nsCID   CLSID; 
    74     typedef nsIID   IID; 
    75  
    76     class XPCOMEventQSocketListener; 
    77  
    78     #define STDMETHOD(a) NS_IMETHOD a 
    79     #define STDMETHODIMP NS_IMETHODIMP 
    80  
    81     #define HRESULT     nsresult 
    82     #define SUCCEEDED   NS_SUCCEEDED 
    83     #define FAILED      NS_FAILED 
    84  
    85     /// @todo error code mappings 
    86     #define S_OK            NS_OK 
    87     #define E_UNEXPECTED    (HRESULT)0x8000FFFFL 
    88     #define E_NOTIMPL       (HRESULT)0x80004001L 
    89     #define E_OUTOFMEMORY   (HRESULT)0x8007000EL 
    90     #define E_INVALIDARG    (HRESULT)0x80070057L 
    91     #define E_NOINTERFACE   (HRESULT)0x80004002L 
    92     #define E_POINTER       (HRESULT)0x80004003L 
    93     #define E_HANDLE        (HRESULT)0x80070006L 
    94     #define E_ABORT         (HRESULT)0x80004004L 
    95     #define E_FAIL          (HRESULT)0x80004005L 
    96     #define E_ACCESSDENIED  (HRESULT)0x80070005L 
    97  
    98     #define IUnknown    nsISupports 
    99  
    100     #define BOOL        PRBool 
    101     #define BYTE        PRUint8 
    102     #define SHORT       PRInt16 
    103     #define USHORT      PRUint16 
    104     #define LONG        PRInt32 
    105     #define ULONG       PRUint32 
    106     #define LONG64      PRInt64 
    107     #define ULONG64     PRUint64 
    108  
    109     #define BSTR        PRUnichar* 
    110     #define LPBSTR      BSTR* 
    111     #define OLECHAR     wchar_t 
    112     #define GUID        nsID 
    113  
    114     #define IN_BSTRPARAM    const BSTR 
    115     #define IN_GUIDPARAM    const nsID & 
    116  
    117     /* makes interface getter/setter names (n must be capitalized) */ 
    118     #define COMGETTER(n)    Get##n 
    119     #define COMSETTER(n)    Set##n 
    120  
    121     /* const reference to IID of the interface */ 
    122     #define COM_IIDOF(I)    NS_GET_IID (I) 
    123  
    124     /* helper functions (defined in the Runtime3 library) */ 
    125     extern "C" { 
    126         BSTR SysAllocString (const OLECHAR* sz); 
    127         BSTR SysAllocStringByteLen (char *psz, unsigned int len); 
    128         BSTR SysAllocStringLen (const OLECHAR *pch, unsigned int cch); 
    129         void SysFreeString (BSTR bstr); 
    130         int SysReAllocString (BSTR *pbstr, const OLECHAR *psz); 
    131         int SysReAllocStringLen (BSTR *pbstr, const OLECHAR *psz, unsigned int cch); 
    132         unsigned int SysStringByteLen (BSTR bstr); 
    133         unsigned int SysStringLen (BSTR bstr); 
    134     } 
    135  
    136 #endif 
     49#define IN_BSTRPARAM    INPTR BSTR 
     50#define IN_GUIDPARAM    INPTR GUIDPARAM 
     51 
     52#if !defined (VBOX_WITH_XPCOM) 
     53 
     54#else /* !defined (VBOX_WITH_XPCOM) */ 
     55 
     56#include <nsXPCOM.h> 
     57#include <nsMemory.h> 
     58#include <nsIComponentManager.h> 
     59 
     60class XPCOMEventQSocketListener; 
     61 
     62#endif /* !defined (VBOX_WITH_XPCOM) */ 
     63 
    13764 
    13865/* VirtualBox interfaces declarations */ 
    139 #if defined(Q_OS_WIN32
     66#if !defined (VBOX_WITH_XPCOM
    14067    #include <VirtualBox.h> 
    141 #else 
     68#else /* !defined (VBOX_WITH_XPCOM) */ 
    14269    #include <VirtualBox_XPCOM.h> 
    143 #endif 
     70#endif /* !defined (VBOX_WITH_XPCOM) */ 
    14471 
    14572#include "VBoxDefs.h" 
     73 
    14674 
    14775///////////////////////////////////////////////////////////////////////////// 
     
    217145    static HRESULT cleanupCOM(); 
    218146 
     147#if !defined (VBOX_WITH_XPCOM) 
     148 
    219149    /** Converts a GUID value to QUuid */ 
    220 #if defined (Q_OS_WIN32
    221     static QUuid toQUuid (const GUID &id)
     150    static QUuid toQUuid (const GUID &id
     151   
    222152        return QUuid (id.Data1, id.Data2, id.Data3, 
    223153                      id.Data4[0], id.Data4[1], id.Data4[2], id.Data4[3], 
    224154                      id.Data4[4], id.Data4[5], id.Data4[6], id.Data4[7]); 
    225155    } 
    226 #else 
    227     static QUuid toQUuid (const nsID &id) { 
     156 
     157#else /* !defined (VBOX_WITH_XPCOM) */ 
     158 
     159    /** Converts a GUID value to QUuid */ 
     160    static QUuid toQUuid (const nsID &id) 
     161    { 
    228162        return QUuid (id.m0, id.m1, id.m2, 
    229163                      id.m3[0], id.m3[1], id.m3[2], id.m3[3], 
    230164                      id.m3[4], id.m3[5], id.m3[6], id.m3[7]); 
    231165    } 
    232 #endif 
     166 
     167#endif /* !defined (VBOX_WITH_XPCOM) */ 
    233168 
    234169    /** 
     
    251186    COMBase() : mRC (S_OK) {}; 
    252187 
    253 #if !defined (Q_OS_WIN32) 
    254     static nsIComponentManager *gComponentManager; 
    255     static nsIEventQueue* gEventQ; 
    256     static ipcIDConnectService *gDConnectService; 
    257     static PRUint32 gVBoxServerID; 
    258  
    259     static XPCOMEventQSocketListener *gSocketListener; 
     188#if defined (VBOX_WITH_XPCOM) 
     189    static XPCOMEventQSocketListener *sSocketListener; 
    260190#endif 
    261191 
     
    264194    { 
    265195    public: 
     196 
    266197        BSTRIn (const QString &s) : bstr (SysAllocString ((const OLECHAR *) s.ucs2())) {} 
    267         ~BSTRIn() { 
     198 
     199        ~BSTRIn() 
     200        { 
    268201            if (bstr) 
    269202                SysFreeString (bstr); 
    270203        } 
     204 
    271205        operator BSTR() const { return bstr; } 
    272206 
    273207    private: 
     208 
    274209        BSTR bstr; 
    275210    }; 
     
    279214    { 
    280215    public: 
     216 
    281217        BSTROut (QString &s) : str (s), bstr (0) {} 
    282         ~BSTROut() { 
     218 
     219        ~BSTROut() 
     220        { 
    283221            if (bstr) { 
    284222                str = QString::fromUcs2 (bstr); 
     
    286224            } 
    287225        } 
     226 
    288227        operator BSTR *() { return &bstr; } 
    289228 
    290229    private: 
     230 
    291231        QString &str; 
    292232        BSTR bstr; 
     
    298238    { 
    299239    public: 
     240 
    300241        ENUMOut (CE &e) : ce (e), ve ((VE) 0) {} 
    301242        ~ENUMOut() { ce = (CE) ve; } 
     
    303244 
    304245    private: 
     246 
    305247        CE &ce; 
    306248        VE ve; 
    307249    }; 
    308250 
    309 #if defined (Q_OS_WIN32
     251#if !defined (VBOX_WITH_XPCOM
    310252 
    311253    /** Adapter to pass QUuid as input GUID params */ 
    312     GUID GUIDIn (const QUuid &uuid) const { return uuid; } 
     254    static GUID GUIDIn (const QUuid &uuid) { return uuid; } 
    313255 
    314256    /** Adapter to pass QUuid as output GUID params */ 
     
    316258    { 
    317259    public: 
    318         GUIDOut (QUuid &id) : uuid (id) { 
     260 
     261        GUIDOut (QUuid &id) : uuid (id) 
     262        { 
    319263            ::memset (&guid, 0, sizeof (GUID)); 
    320264        } 
    321         ~GUIDOut() { 
     265 
     266        ~GUIDOut() 
     267        { 
    322268            uuid = QUuid ( 
    323269                guid.Data1, guid.Data2, guid.Data3, 
    324270                guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], 
    325                 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7] 
    326             ); 
    327         } 
     271                guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); 
     272        } 
     273 
    328274        operator GUID *() { return &guid; } 
    329275 
    330276    private: 
     277 
    331278        QUuid &uuid; 
    332279        GUID guid; 
    333280    }; 
    334281 
    335 #else 
     282#else /* !defined (VBOX_WITH_XPCOM) */ 
    336283 
    337284    /** Adapter to pass QUuid as input GUID params */ 
    338     static const nsID &GUIDIn (const QUuid &uuid) { 
     285    static const nsID &GUIDIn (const QUuid &uuid) 
     286    { 
    339287        return *(const nsID *) &uuid; 
    340288    } 
     
    344292    { 
    345293    public: 
     294 
    346295        GUIDOut (QUuid &id) : uuid (id), nsid (0) {} 
    347         ~GUIDOut() { 
    348             if (nsid) { 
     296 
     297        ~GUIDOut() 
     298        { 
     299            if (nsid) 
     300            { 
    349301                uuid = QUuid ( 
    350302                    nsid->m0, nsid->m1, nsid->m2, 
    351303                    nsid->m3[0], nsid->m3[1], nsid->m3[2], nsid->m3[3], 
    352                     nsid->m3[4], nsid->m3[5], nsid->m3[6], nsid->m3[7] 
    353                 ); 
     304                    nsid->m3[4], nsid->m3[5], nsid->m3[6], nsid->m3[7]); 
    354305                nsMemory::Free (nsid); 
    355306            } 
    356307        } 
     308 
    357309        operator nsID **() { return &nsid; } 
    358310 
    359311    private: 
     312 
    360313        QUuid &uuid; 
    361314        nsID *nsid; 
    362315    }; 
    363316 
    364 #endif 
     317#endif /* !defined (VBOX_WITH_XPCOM) */ 
    365318 
    366319    void fetchErrorInfo (IUnknown * /*callee*/, const GUID * /*calleeIID*/) const {} 
     
    394347    COMBaseWithEI() : COMBase () {}; 
    395348 
    396     void fetchErrorInfo (IUnknown *callee, const GUID *calleeIID) const { 
     349    void fetchErrorInfo (IUnknown *callee, const GUID *calleeIID) const 
     350    { 
    397351        mErrInfo.fetchFromCurrentThread (callee, calleeIID); 
    398352    } 
     
    484438        if (!mIface) 
    485439        { 
    486 #if defined (Q_OS_WIN32) 
     440#if !defined (VBOX_WITH_XPCOM) 
     441 
    487442            B::mRC = CoCreateInstance (clsid, NULL, CLSCTX_ALL, 
    488                                        _ATL_IIDOF (I), (void**) &mIface); 
    489 #else 
    490             /* first, try to create an instance within the in-proc server 
    491              * (for compatibility with Win32) */ 
    492             B::mRC = B::gComponentManager-> 
    493                 CreateInstance (clsid, nsnull, NS_GET_IID (I), (void**) &mIface); 
    494             if (FAILED (B::mRC) && B::gDConnectService && B::gVBoxServerID) 
    495             { 
    496                 /* now try the out-of-proc server if it exists */ 
    497                 B::mRC = B::gDConnectService-> 
    498                     CreateInstance (B::gVBoxServerID, clsid, 
    499                                     NS_GET_IID (I), (void**) &mIface); 
    500             } 
    501 #endif 
     443                                       _ATL_IIDOF (I), (void **) &mIface); 
     444 
     445#else /* !defined (VBOX_WITH_XPCOM) */ 
     446 
     447            nsCOMPtr <nsIComponentManager> manager; 
     448            B::mRC = NS_GetComponentManager (getter_AddRefs (manager)); 
     449            if (SUCCEEDED (B::mRC)) 
     450                B::mRC = manager->CreateInstance (clsid, nsnull, NS_GET_IID (I), 
     451                                                  (void **) &mIface); 
     452 
     453#endif /* !defined (VBOX_WITH_XPCOM) */ 
     454 
    502455            /* fetch error info, but don't assert if it's missing -- many other 
    503456             * reasons can lead to an error (w/o providing error info), not only 
     
    524477        B::mRC = S_OK; 
    525478        if (i) 
    526 #if defined (Q_OS_WIN32
    527             B::mRC = i->QueryInterface (_ATL_IIDOF (I), (void**) &mIface); 
    528 #else 
    529             B::mRC = i->QueryInterface (NS_GET_IID (I), (void**) &mIface); 
    530 #endif 
     479#if !defined (VBOX_WITH_XPCOM
     480            B::mRC = i->QueryInterface (_ATL_IIDOF (I), (void **) &mIface); 
     481#else /* !defined (VBOX_WITH_XPCOM) */ 
     482            B::mRC = i->QueryInterface (NS_GET_IID (I), (void **) &mIface); 
     483#endif /* !defined (VBOX_WITH_XPCOM) */ 
    531484        release (old_iface); 
    532485    }; 
     
    575528        mIface = NULL; 
    576529        if (that.mIface) 
    577 #if defined (Q_OS_WIN32
     530#if !defined (VBOX_WITH_XPCOM
    578531            mRC = that.mIface->QueryInterface (_ATL_IIDOF (IUnknown), (void**) &mIface); 
    579 #else 
     532#else /* !defined (VBOX_WITH_XPCOM) */ 
    580533            mRC = that.mIface->QueryInterface (NS_GET_IID (IUnknown), (void**) &mIface); 
    581 #endif 
    582         if (SUCCEEDED (mRC)) { 
     534#endif /* !defined (VBOX_WITH_XPCOM) */ 
     535        if (SUCCEEDED (mRC)) 
     536        { 
    583537            mRC = that.lastRC(); 
    584538            mErrInfo = that.errorInfo(); 
    585539        } 
    586540    } 
     541 
    587542    /* specialization for CUnknown */ 
    588     CUnknown (const CUnknown &that) : CInterface <IUnknown, COMBaseWithEI> () { 
     543    CUnknown (const CUnknown &that) : CInterface <IUnknown, COMBaseWithEI> () 
     544    { 
    589545        mIface = that.mIface; 
    590546        addref (mIface); 
     
    593549 
    594550    template <class C> 
    595     CUnknown &operator= (const C &that) { 
     551    CUnknown &operator= (const C &that) 
     552    { 
    596553        /* be aware of self (from COM point of view) assignment */ 
    597554        IUnknown *old_iface = mIface; 
    598555        mIface = NULL; 
    599556        mRC = S_OK; 
    600 #if defined (Q_OS_WIN32
     557#if !defined (VBOX_WITH_XPCOM
    601558        if (that.mIface) 
    602559            mRC = that.mIface->QueryInterface (_ATL_IIDOF (IUnknown), (void**) &mIface); 
    603 #else 
     560#else /* !defined (VBOX_WITH_XPCOM) */ 
    604561        if (that.mIface) 
    605562            mRC = that.mIface->QueryInterface (NS_GET_IID (IUnknown), (void**) &mIface); 
    606 #endif 
    607         if (SUCCEEDED (mRC)) { 
     563#endif /* !defined (VBOX_WITH_XPCOM) */ 
     564        if (SUCCEEDED (mRC)) 
     565        { 
    608566            mRC = that.lastRC(); 
    609567            mErrInfo = that.errorInfo(); 
     
    612570        return *this; 
    613571    } 
     572 
    614573    /* specialization for CUnknown */ 
    615     CUnknown &operator= (const CUnknown &that) { 
     574    CUnknown &operator= (const CUnknown &that) 
     575    { 
    616576        attach (that.mIface); 
    617577        COMBaseWithEI::operator= (that); 
  • trunk/src/VBox/Frontends/VirtualBox/src/COMDefs.cpp

    r2671 r2976  
    2323#include "COMDefs.h" 
    2424 
    25 #if defined (Q_OS_WIN32) 
    26  
    27 // for CComPtr/CComQIPtr 
    28 #include <atlcomcli.h> 
    29 #include <VBox/com/assert.h> 
    30  
    31 #else // !defined (Q_OS_WIN32) 
     25#if !defined (VBOX_WITH_XPCOM) 
     26 
     27#else /* !defined (VBOX_WITH_XPCOM) */ 
    3228 
    3329#include <qobject.h> 
    34 #include <qapplication.h> 
    35 #include <qfile.h> 
    3630#include <qsocketnotifier.h> 
    37 #ifdef DEBUG 
    38     #include <qfileinfo.h> 
    39 #endif 
    40  
    41 #include <nsXPCOMGlue.h> 
    42 #include <nsIServiceManager.h> 
    43 #include <nsIComponentRegistrar.h> 
    44 // for NS_InitXPCOM2 with bin dir parameter 
    45 #include <nsEmbedString.h> 
    46 #include <nsIFile.h> 
    47 #include <nsILocalFile.h> 
    48 // for dconnect 
    49 #include <ipcIService.h> 
    50 #include <ipcCID.h> 
    51 // XPCOM headers still do not define this, so define by hand 
    52 #define IPC_DCONNECTSERVICE_CONTRACTID \ 
    53     "@mozilla.org/ipc/dconnect-service;1" 
    54 // for event queue management 
     31 
    5532#include <nsEventQueueUtils.h> 
    5633#include <nsIEventQueue.h> 
    5734 
    58 // for IID to name resolution 
    59 #include <nsIInterfaceInfo.h> 
    60 #include <nsIInterfaceInfoManager.h> 
    61  
    6235// for exception fetching 
    6336#include <nsIExceptionService.h> 
    6437 
    65 #undef ASSERT 
    66 #include <VBox/com/assert.h> 
    6738#include <iprt/env.h> 
    6839#include <iprt/path.h> 
    6940#include <iprt/param.h> 
    7041#include <iprt/err.h> 
    71  
    72 nsIComponentManager *COMBase::gComponentManager = nsnull; 
    73 nsIEventQueue* COMBase::gEventQ = nsnull; 
    74 ipcIDConnectService *COMBase::gDConnectService = nsnull; 
    75 PRUint32 COMBase::gVBoxServerID = 0; 
    7642 
    7743/* Mac OS X (Carbon mode) and OS/2 will notify the native queue 
     
    8046   very silly. */ 
    8147# if !defined (Q_OS_MAC)  && !defined (Q_OS_OS2) 
    82 XPCOMEventQSocketListener *COMBase::gSocketListener = 0; 
     48XPCOMEventQSocketListener *COMBase::sSocketListener = 0; 
     49 
    8350# endif 
    8451 
     
    10370    } 
    10471 
     72    virtual ~XPCOMEventQSocketListener() 
     73    { 
     74        delete mNotifier; 
     75    } 
     76 
    10577public slots: 
    10678 
     
    11082 
    11183    QSocketNotifier *mNotifier; 
    112     nsIEventQueue *mEventQ; 
     84    nsCOMPtr <nsIEventQueue> mEventQ; 
    11385}; 
    11486 
    115 #endif // !defined (Q_OS_WIN32) 
     87#endif /* !defined (VBOX_WITH_XPCOM) */ 
    11688 
    11789/** 
     
    12294    LogFlowFuncEnter(); 
    12395 
    124 #if defined (Q_OS_WIN32) 
     96    HRESULT rc = S_OK; 
     97 
     98#if !defined (VBOX_WITH_XPCOM) 
    12599 
    126100    /* disable this damn CoInitialize* somehow made by Qt during 
     
    128102     * why does it do this) */ 
    129103    CoUninitialize(); 
    130     CoInitializeEx (NULL, COINIT_MULTITHREADED | 
    131                           COINIT_DISABLE_OLE1DDE | 
    132                           COINIT_SPEED_OVER_MEMORY); 
    133  
    134     LogFlowFuncLeave(); 
    135     return S_OK; 
    136  
    137 #else 
    138  
    139     if (gComponentManager) 
    140     { 
    141         LogFlowFuncLeave(); 
    142         return S_OK; 
    143     } 
    144  
    145     /* 
    146      * Set VBOX_XPCOM_HOME if not present like we do in the common glue code. 
    147      * (XPCOMGlueStartup will query this.) 
    148      */ 
    149     if (!RTEnvExist ("VBOX_XPCOM_HOME")) 
    150     { 
    151         /* get the executable path */ 
    152         char szPathProgram [RTPATH_MAX]; 
    153         int rcVBox = RTPathProgram (szPathProgram, sizeof (szPathProgram)); 
    154         if (RT_SUCCESS (rcVBox)) 
    155             RTEnvSet ("VBOX_XPCOM_HOME", szPathProgram); 
    156     } 
    157  
    158     HRESULT rc; 
    159     XPCOMGlueStartup (nsnull); 
    160  
    161     nsCOMPtr <nsIServiceManager> serviceManager; 
    162  
    163     /* create a file object containing the path to the executable */ 
    164     QCString appDir; 
     104 
     105#endif /* !defined (VBOX_WITH_XPCOM) */ 
     106 
     107    rc = com::Initialize(); 
     108 
     109#if defined (VBOX_WITH_XPCOM) 
     110 
     111#if !defined (__DARWIN__) && !defined (__OS2__) 
     112 
     113    if (NS_SUCCEEDED (rc)) 
     114    { 
     115        nsCOMPtr <nsIEventQueue> eventQ; 
     116        rc = NS_GetMainEventQ (getter_AddRefs (eventQ)); 
     117        if (NS_SUCCEEDED (rc)) 
     118        { 
    165119#ifdef DEBUG 
    166     appDir = getenv ("VIRTUALBOX_APP_HOME"); 
    167     if (!appDir.isNull()) 
    168         appDir = QFile::encodeName (QFileInfo (QFile::decodeName (appDir)).absFilePath()); 
    169     else 
     120            BOOL isNative = FALSE; 
     121            eventQ->IsQueueNative (&isNative); 
     122            AssertMsg (isNative, ("The event queue must be native")); 
    170123#endif 
    171     appDir = QFile::encodeName (qApp->applicationDirPath()); 
    172     nsCOMPtr <nsILocalFile> lfAppDir; 
    173     rc = NS_NewNativeLocalFile (nsEmbedCString (appDir.data()), PR_FALSE, 
    174                                 getter_AddRefs (lfAppDir)); 
    175     if (SUCCEEDED (rc)) 
    176     { 
    177         nsCOMPtr <nsIFile> fAppDir = do_QueryInterface (lfAppDir, &rc); 
    178         if (SUCCEEDED( rc )) 
    179         { 
    180             /* initialize XPCOM and get the service manager */ 
    181             rc = NS_InitXPCOM2 (getter_AddRefs (serviceManager), fAppDir, nsnull); 
    182         } 
    183     } 
    184  
    185     if (SUCCEEDED (rc)) 
    186     { 
    187         /* get the registrar */ 
    188         nsCOMPtr <nsIComponentRegistrar> registrar = 
    189             do_QueryInterface (serviceManager, &rc); 
    190         if (SUCCEEDED (rc)) 
    191         { 
    192             /* autoregister components from a component directory */ 
    193             registrar->AutoRegister (nsnull); 
    194  
    195             /* get the component manager */ 
    196             rc = registrar->QueryInterface (NS_GET_IID (nsIComponentManager), 
    197                                             (void**) &gComponentManager); 
    198             if (SUCCEEDED (rc)) 
     124            BOOL isOnMainThread = FALSE; 
     125            rc = eventQ->IsOnCurrentThread (&isOnMainThread); 
     126            if (NS_SUCCEEDED (rc) && isOnMainThread) 
    199127            { 
    200                 /* get the main thread's event queue (afaik, the 
    201                  * dconnect service always gets created upon XPCOM 
    202                  * startup, so it will use the main (this) thread's 
    203                  * event queue to receive IPC events) */ 
    204                 rc = NS_GetMainEventQ (&gEventQ); 
    205 #ifdef DEBUG 
    206                 BOOL isNative = FALSE; 
    207                 gEventQ->IsQueueNative (&isNative); 
    208                 AssertMsg (isNative, ("The event queue must be native")); 
    209 #endif 
    210 # if !defined (__DARWIN__) && !defined (__OS2__) 
    211                 gSocketListener = new XPCOMEventQSocketListener (gEventQ); 
    212 # endif 
    213  
    214 /// @todo remove the below code and corresponding variables etc. when 
    215 /// the server autostart feature is finished and well tested. 
    216 /// 
    217 //                /* get the IPC service */ 
    218 //                 nsCOMPtr <ipcIService> ipcServ = 
    219 //                     do_GetService (IPC_SERVICE_CONTRACTID, serviceManager, &rc); 
    220 //                 if (SUCCEEDED (rc)) 
    221 //                 { 
    222 //                     /* get the VirtualBox out-of-proc server ID */ 
    223 //                     rc = ipcServ->ResolveClientName ("VirtualBoxServer", 
    224 //                                                      &gVBoxServerID); 
    225 //                     if (SUCCEEDED (rc)) 
    226 //                     { 
    227 //                         /* get the DConnect service */ 
    228 //                         rc = serviceManager-> 
    229 //                             GetServiceByContractID (IPC_DCONNECTSERVICE_CONTRACTID, 
    230 //                                                     NS_GET_IID (ipcIDConnectService), 
    231 //                                                     (void **) &gDConnectService); 
    232 //                     } 
    233 //                 } 
     128                sSocketListener = new XPCOMEventQSocketListener (eventQ); 
    234129            } 
    235130        } 
    236131    } 
    237132 
     133#endif 
     134 
     135#endif /* defined (VBOX_WITH_XPCOM) */ 
     136 
    238137    if (FAILED (rc)) 
    239138        cleanupCOM(); 
     139 
     140    AssertComRC (rc); 
    240141 
    241142    LogFlowFunc (("rc=%08X\n", rc)); 
     
    243144    return rc; 
    244145 
    245 #endif 
    246146} 
    247147 
    248148/** 
    249  *  Initializes COM/XPCOM. 
     149 *  Cleans up COM/XPCOM. 
    250150 */ 
    251151HRESULT COMBase::cleanupCOM() 
     
    253153    LogFlowFuncEnter(); 
    254154 
    255 #if defined (Q_OS_WIN32) 
    256     CoUninitialize(); 
    257 #else 
    258     if (gComponentManager) 
    259     { 
    260         PRBool isOnCurrentThread = true; 
    261         if (gEventQ) 
    262             gEventQ->IsOnCurrentThread (&isOnCurrentThread)
    263  
    264         if (isOnCurrentThread
     155    HRESULT rc = S_OK; 
     156 
     157#if defined (VBOX_WITH_XPCOM) 
     158 
     159    /* scope the code to make smart references are released before calling 
     160     * com::Shutdown() */ 
     161    { 
     162        nsCOMPtr <nsIEventQueue> eventQ
     163        rc = NS_GetMainEventQ (getter_AddRefs (eventQ)); 
     164        if (NS_SUCCEEDED (rc)
    265165        { 
    266             LogFlowFunc (("Doing cleanup...\n")); 
     166            BOOL isOnMainThread = FALSE; 
     167            rc = eventQ->IsOnCurrentThread (&isOnMainThread); 
     168            if (NS_SUCCEEDED (rc) && isOnMainThread) 
     169            { 
    267170# if !defined (__DARWIN__) && !defined (__OS2__) 
    268             if (gSocketListener) 
    269                 delete gSocketListener; 
     171                if (sSocketListener) 
     172                { 
     173                    delete sSocketListener; 
     174                    sSocketListener = NULL; 
     175                } 
    270176# endif 
    271             if (gDConnectService) 
    272             { 
    273                 gDConnectService->Release(); 
    274                 gDConnectService = nsnull; 
    275177            } 
    276             if (gEventQ) 
    277             { 
    278                 gEventQ->Release(); 
    279                 gEventQ = nsnull; 
    280             } 
    281             gComponentManager->Release(); 
    282             gComponentManager = nsnull; 
    283             /* note: gComponentManager = nsnull indicates that we're 
    284              * cleaned up */ 
    285             NS_ShutdownXPCOM (nsnull); 
    286             XPCOMGlueShutdown(); 
    287178        } 
    288179    } 
    289 #endif 
    290  
     180 
     181#endif /* defined (VBOX_WITH_XPCOM) */ 
     182 
     183    HRESULT rc2 = com::Shutdown(); 
     184    if (SUCCEEDED (rc)) 
     185        rc = rc2; 
     186 
     187    AssertComRC (rc); 
     188 
     189    LogFlowFunc (("rc=%08X\n", rc)); 
    291190    LogFlowFuncLeave(); 
    292     return S_OK
     191    return rc
    293192} 
    294193 
     
    358257    HRESULT rc = E_FAIL; 
    359258 
    360 #if defined (__WIN__
     259#if !defined (VBOX_WITH_XPCOM
    361260 
    362261    if (callee) 
     
    405304    } 
    406305 
    407 #else // !defined (__WIN__) 
     306#else /* !defined (VBOX_WITH_XPCOM) */ 
    408307 
    409308    nsCOMPtr <nsIExceptionService> es; 
     
    458357    AssertComRC (rc); 
    459358 
    460 #endif // !defined (__WIN__) 
     359#endif /* !defined (VBOX_WITH_XPCOM) */ 
    461360 
    462361    if (callee && calleeIID && mIsBasicAvailable) 
     
    472371    QString name; 
    473372 
    474 #if defined (__WIN__) 
    475  
    476     LONG rc; 
    477     LPOLESTR iidStr = NULL; 
    478     if (StringFromIID (id, &iidStr) == S_OK) 
    479     { 
    480         HKEY ifaceKey; 
    481         rc = RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Interface", 0, KEY_QUERY_VALUE, &ifaceKey); 
    482         if (rc == ERROR_SUCCESS) 
    483         { 
    484             HKEY iidKey; 
    485             rc = RegOpenKeyExW (ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey); 
    486             if (rc == ERROR_SUCCESS) 
    487             { 
    488                 // determine the size and type 
    489                 DWORD sz, type; 
    490                 rc = RegQueryValueExW (iidKey, NULL, NULL, &type, NULL, &sz); 
    491                 if (rc == ERROR_SUCCESS && type == REG_SZ) 
    492                 { 
    493                     // query the value to BSTR 
    494                     BSTR bstrName = SysAllocStringLen (NULL, (sz + 1) / sizeof (TCHAR) + 1); 
    495                     rc = RegQueryValueExW (iidKey, NULL, NULL, NULL, (LPBYTE) bstrName, &sz); 
    496                     if (rc == ERROR_SUCCESS) 
    497                     { 
    498                         name = QString::fromUcs2 (bstrName); 
    499                     } 
    500                     SysFreeString (bstrName); 
    501                 } 
    502                 RegCloseKey (iidKey); 
    503             } 
    504             RegCloseKey (ifaceKey); 
    505         } 
    506         CoTaskMemFree (iidStr); 
    507     } 
    508  
    509 #else 
    510  
    511     nsresult rv; 
    512     nsCOMPtr <nsIInterfaceInfoManager> iim = 
    513         do_GetService (NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv); 
    514     if (NS_SUCCEEDED (rv)) 
    515     { 
    516         nsCOMPtr <nsIInterfaceInfo> iinfo; 
    517         rv = iim->GetInfoForIID (&COMBase::GUIDIn (id), getter_AddRefs (iinfo)); 
    518         if (NS_SUCCEEDED (rv)) 
    519         { 
    520             const char *iname = NULL; 
    521             iinfo->GetNameShared (&iname); 
    522             name = QString::fromLocal8Bit (iname); 
    523         } 
    524     } 
    525  
    526 #endif 
     373    com::GetInterfaceNameByIID (COMBase::GUIDIn (id), COMBase::BSTROut (name)); 
    527374 
    528375    return name; 
    529376} 
    530377 
    531 #if !defined (Q_OS_WIN32
     378#if defined (VBOX_WITH_XPCOM
    532379#include "COMDefs.moc" 
    533380#endif 
  • trunk/src/VBox/Main/VirtualBoxImpl.cpp

    r2610 r2976