Back to index

lightning-sunbird  0.9+nobinonly
nsProxiedService.h
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  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsProxiedService_h_
00040 #define nsProxiedService_h_
00041 
00042 #include "nsIServiceManager.h"
00043 #include "nsIProxyObjectManager.h"
00044 
00046 // NS_WITH_PROXIED_SERVICE: macro to make using services that need to be proxied
00047 //                                   before using them easier. 
00048 // Now you can replace this:
00049 // {
00050 //      nsresult rv;
00051 //      nsCOMPtr<nsIMyService> pIMyService = 
00052 //               do_GetService(kMyServiceCID, &rv);
00053 //      if(NS_FAILED(rv))
00054 //          return;
00055 //      nsCOMPtr<nsIProxyObjectManager> pIProxyObjectManager = 
00056 //               do_GetService(kProxyObjectManagerCID, &rv);
00057 //      if(NS_FAILED(rv))
00058 //          return;
00059 //      nsIMyService pIProxiedObject = NULL;
00060 //      rv = pIProxyObjectManager->GetProxyForObject(pIProxyQueue, 
00061 //                                                              NS_GET_IID(nsIMyService), 
00062 //                                                              pIMyService, PROXY_SYNC,
00063 //                                                              (void**)&pIProxiedObject);
00064 //      pIProxiedObject->DoIt(...);  // Executed on same thread as pIProxyQueue
00065 //      ...
00066 //      pIProxiedObject->Release();  // Must be done as not managed for you.
00067 //      }
00068 //  with this:
00069 //      {
00070 //      nsresult rv;
00071 //      NS_WITH_PROXIED_SERVICE(nsIMyService, pIMyService, kMyServiceCID, 
00072 //                                      pIProxyQueue, &rv);
00073 //      if(NS_FAILED(rv))
00074 //          return;
00075 //      pIMyService->DoIt(...);  // Executed on the same thread as pIProxyQueue
00076 //      }
00077 // and the automatic destructor will take care of releasing the service and
00078 // the proxied object for you. 
00079 // 
00080 // Note that this macro requires you to link with the xpcom DLL to pick up the
00081 // static member functions from nsServiceManager.
00082 
00083 #define NS_WITH_PROXIED_SERVICE(T, var, cid, Q, rvAddr)     \
00084     nsProxiedService _serv##var(cid, NS_GET_IID(T), Q, PR_FALSE, rvAddr);     \
00085     T* var = (T*)(nsISupports*)_serv##var;
00086 
00087 #define NS_WITH_ALWAYS_PROXIED_SERVICE(T, var, cid, Q, rvAddr)     \
00088     nsProxiedService _serv##var(cid, NS_GET_IID(T), Q, PR_TRUE, rvAddr);       \
00089     T* var = (T*)(nsISupports*)_serv##var;
00090 
00092 // nsProxiedService
00094 
00095 class nsProxiedService
00096 {
00097 public:
00098     nsProxiedService(const nsCID &aClass, const nsIID &aIID, 
00099                      nsIEventQueue* aEventQ, PRBool always, nsresult* rv)
00100     {
00101         nsCOMPtr<nsISupports> svc = do_GetService(aClass, rv);
00102         if (NS_SUCCEEDED(*rv))
00103             InitProxy(svc, aIID, aEventQ, always, rv);
00104     }
00105 
00106     nsProxiedService(const char* aContractID, const nsIID &aIID, 
00107                      nsIEventQueue* aEventQ, PRBool always, nsresult* rv)
00108     {
00109         nsCOMPtr<nsISupports> svc = do_GetService(aContractID, rv);
00110         if (NS_SUCCEEDED(*rv))
00111             InitProxy(svc, aIID, aEventQ, always, rv);
00112     }
00113     
00114     operator nsISupports*() const
00115     {
00116         return mProxiedService;
00117     }
00118 
00119 private:
00120 
00121     void InitProxy(nsISupports *aObj, const nsIID &aIID,
00122                    nsIEventQueue* aEventQ, PRBool always, nsresult*rv)
00123     {
00124         PRInt32 proxyType = PROXY_SYNC;
00125         if (always)
00126             proxyType |= PROXY_ALWAYS;
00127 
00128         *rv = NS_GetProxyForObject(aEventQ, 
00129                                    aIID, 
00130                                    aObj,
00131                                    proxyType, 
00132                                    getter_AddRefs(mProxiedService));
00133     }
00134 
00135     nsCOMPtr<nsISupports> mProxiedService;
00136 };
00137 
00138 #endif // nsProxiedService_h_