cobjsafe.cpp
上传用户:xhy777
上传日期:2007-02-14
资源大小:24088k
文件大小:5k
源码类别:

系统编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. // a default isafetyobject that we generally would use...  marks 
  4. // deals with IDispatch 
  5. HRESULT CObjectSafety::GetInterfaceSafetyOptions(REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
  6. {
  7.     if (IsEqualIID(riid, IID_IDispatch))
  8.     {
  9.         *pdwEnabledOptions = _dwSafetyOptions;
  10.     }
  11.     else
  12.     {
  13.         ::DefaultGetSafetyOptions(riid, pdwSupportedOptions, pdwEnabledOptions);
  14.     }
  15.     return S_OK;
  16. }
  17. HRESULT CObjectSafety::SetInterfaceSafetyOptions(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions)
  18. {
  19.     if (dwOptionSetMask & ~(INTERFACESAFE_FOR_UNTRUSTED_CALLER |
  20.                             INTERFACESAFE_FOR_UNTRUSTED_DATA))
  21.     {
  22.         return E_INVALIDARG;
  23.     }
  24.     if (IsEqualIID(riid, IID_IDispatch))
  25.     {
  26.         _dwSafetyOptions = (_dwSafetyOptions & ~dwOptionSetMask) |
  27.                            (dwEnabledOptions & dwOptionSetMask);
  28.         return S_OK;
  29.     }
  30.     else
  31.     {
  32.         return ::DefaultSetSafetyOptions(riid, dwOptionSetMask, dwEnabledOptions);
  33.     }
  34. }
  35. // *** IObjectSafety
  36. //
  37. // A couple static functions called by sitemap (and webbrowser).
  38. // These are static so anyone else in this dll who has an OC
  39. // that's always safe can just call them.
  40. //
  41. // These functions say we are safe for these three interfaces we implement
  42. //  IID_IDispatch
  43. //  IID_IPersistStream
  44. //  IID_IPersistPropertyBag
  45. //
  46. // The WebBrowser OC handles IDispatch differently.
  47. //
  48. HRESULT DefaultGetSafetyOptions(REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
  49. {
  50.     *pdwSupportedOptions = 0;
  51.     *pdwEnabledOptions = 0;
  52.     if (IsEqualIID(riid, IID_IDispatch) ||
  53.         IsEqualIID(riid, IID_IPersistStream) ||
  54.         IsEqualIID(riid, IID_IPersistStreamInit) ||
  55.         IsEqualIID(riid, IID_IPersistPropertyBag))
  56.     {
  57.         *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
  58.         *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
  59.     }
  60.     return S_OK;
  61. }
  62. HRESULT DefaultSetSafetyOptions(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions)
  63. {
  64.     if (dwOptionSetMask & ~(INTERFACESAFE_FOR_UNTRUSTED_CALLER |
  65.                             INTERFACESAFE_FOR_UNTRUSTED_DATA))
  66.     {
  67.         return E_INVALIDARG;
  68.     }
  69.     if (IsEqualIID(riid, IID_IDispatch) ||
  70.         IsEqualIID(riid, IID_IPersistStream) ||
  71.         IsEqualIID(riid, IID_IPersistStreamInit) ||
  72.         IsEqualIID(riid, IID_IPersistPropertyBag))
  73.     {
  74.         return S_OK;
  75.     }
  76.     return E_FAIL;
  77. }
  78. // When CWebBrowserOC is in the safe for scripting mode, we can't give out
  79. // anyone else's IDispatch that is not also safe for scripting.
  80. // This function encapsulates the basic functionality needed by both
  81. // MakeSafeScripting and MakeSafeForInitializing (which we don't use)
  82. BOOL MakeSafeFor(
  83. IUnknown *punk,                 // object to test for safety
  84. REFCATID catid,                 // category of safety
  85. REFIID riid,                    // interface on which safety is desired
  86. DWORD dwXSetMask,               // options to set
  87. DWORD dwXOptions                // options to make safe for
  88.                                     // (either INTERFACESAFE_FOR_UNTRUSTED_CALLER or
  89.                                     //  INTERFACESAFE_FOR_UNTRUSTED_DATA)
  90. )
  91. {
  92.     HRESULT hres;
  93.     // first try IObjectSafety
  94.     IObjectSafety *posafe;
  95.     if (SUCCEEDED(punk->QueryInterface(IID_IObjectSafety, (LPVOID*) &posafe)))
  96.     {
  97.         hres = posafe->SetInterfaceSafetyOptions(riid, dwXSetMask, dwXOptions);
  98.         posafe->Release();
  99.         if (SUCCEEDED(hres))
  100.             return TRUE;
  101.     }
  102.     // check the registry for "safe for scripting" component category
  103.     // we need the classid -- get it thru IPersist
  104.     CLSID clsid;
  105.     IPersist *ppersist;
  106.     hres = punk->QueryInterface(IID_IPersist, (LPVOID*) &ppersist);
  107.     if (SUCCEEDED(hres))
  108.     {
  109.         hres = ppersist->GetClassID(&clsid);
  110.         ppersist->Release();
  111.     }
  112.     if (FAILED(hres))
  113.     {
  114.         return FALSE;
  115.     }
  116.     // Create the category manager
  117.     ICatInformation *pcatinfo;
  118.     hres = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  119.                             NULL, CLSCTX_INPROC_SERVER,
  120.                             IID_ICatInformation, (LPVOID*) &pcatinfo);
  121.     if (FAILED(hres))
  122.         return FALSE;
  123.     // Ask if the object belongs to the specified category
  124.     CATID rgcatid[1];
  125.     rgcatid[0] = catid;
  126.     hres = pcatinfo->IsClassOfCategories(clsid, 1, rgcatid, 0, NULL);
  127.     pcatinfo->Release();
  128.     return (hres==S_OK) ? TRUE : FALSE;;
  129. }
  130. HRESULT MakeSafeForScripting(IUnknown** ppDisp)
  131. {
  132.     HRESULT hres = S_OK;
  133.     if (!MakeSafeFor(*ppDisp, CATID_SafeForScripting, IID_IDispatch,
  134.                        INTERFACESAFE_FOR_UNTRUSTED_CALLER,
  135.                        INTERFACESAFE_FOR_UNTRUSTED_CALLER))
  136.     {
  137.         (*ppDisp)->Release();
  138.         *ppDisp = NULL;
  139.         hres = E_FAIL;
  140.     }
  141.     return hres;
  142. }