Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
13 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. ScriptWrapper_ClientSide.cpp
  5. Abstract:
  6. File for implementation of CPCHScriptWrapper_ServerSide class,
  7. a generic wrapper for remoting scripting engines.
  8. Revision History:
  9. Davide Massarenti created 03/28/2000
  10. ********************************************************************/
  11. #include "stdafx.h"
  12. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  13. CPCHScriptWrapper_ServerSide::CPCHScriptWrapper_ServerSide()
  14. {
  15. // CComPtr<IPCHActiveScriptSite> m_Browser;
  16. // CComPtr<IActiveScript> m_Script;
  17. // CComPtr<IActiveScriptParse> m_ScriptParse;
  18. }
  19. CPCHScriptWrapper_ServerSide::~CPCHScriptWrapper_ServerSide()
  20. {
  21. }
  22. HRESULT CPCHScriptWrapper_ServerSide::FinalConstructInner( /*[in]*/ const CLSID* pWrappedCLSID, /*[in]*/ BSTR bstrURL )
  23. {
  24. __HCP_FUNC_ENTRY( "CPCHScriptWrapper_ServerSide::FinalConstructInner" );
  25. HRESULT hr;
  26. CComPtr<IUnknown> unk;
  27. m_bstrURL = bstrURL;
  28. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CoCreateInstance( *pWrappedCLSID, NULL, CLSCTX_ALL, IID_IUnknown, (void**)&unk ));
  29. __MPC_EXIT_IF_METHOD_FAILS(hr, unk->QueryInterface( IID_IActiveScript , (void**)&m_Script ));
  30. __MPC_EXIT_IF_METHOD_FAILS(hr, unk->QueryInterface( IID_IActiveScriptParse, (void**)&m_ScriptParse ));
  31. hr = S_OK;
  32. __HCP_FUNC_CLEANUP;
  33. __HCP_FUNC_EXIT(hr);
  34. }
  35. void CPCHScriptWrapper_ServerSide::FinalRelease()
  36. {
  37. m_Browser .Release();
  38. m_Script .Release();
  39. m_ScriptParse.Release();
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////
  42. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_SetScriptSite( /*[in]*/ IPCHActiveScriptSite* pass )
  43. {
  44. m_Browser = pass;
  45. return S_OK;
  46. }
  47. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_SetScriptState( /*[in] */ SCRIPTSTATE ss )
  48. {
  49. if(m_Script == NULL) return E_FAIL;
  50. return m_Script->SetScriptState( ss );
  51. }
  52. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_GetScriptState( /*[out]*/ SCRIPTSTATE *pssState )
  53. {
  54. if(m_Script == NULL) return E_FAIL;
  55. return m_Script->GetScriptState( pssState );
  56. }
  57. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_Close()
  58. {
  59. if(m_Script == NULL) return E_FAIL;
  60. return m_Script->Close();
  61. }
  62. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_AddNamedItem( /*[in]*/ BSTR bstrName ,
  63. /*[in]*/ DWORD dwFlags )
  64. {
  65. if(m_Script == NULL) return E_FAIL;
  66. return m_Script->AddNamedItem( bstrName, dwFlags );
  67. }
  68. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_AddTypeLib( /*[in]*/ BSTR bstrTypeLib ,
  69. /*[in]*/ DWORD dwMajor ,
  70. /*[in]*/ DWORD dwMinor ,
  71. /*[in]*/ DWORD dwFlags )
  72. {
  73. GUID guidTypeLib;
  74. if(m_Script == NULL) return E_FAIL;
  75. ::CLSIDFromString( bstrTypeLib, &guidTypeLib );
  76. return m_Script->AddTypeLib( guidTypeLib, dwMajor, dwMinor, dwFlags );
  77. }
  78. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_GetScriptDispatch( /*[in ]*/ BSTR pstrItemName ,
  79. /*[out]*/ IDispatch* *ppdisp )
  80. {
  81. if(m_Script == NULL) return E_FAIL;
  82. return m_Script->GetScriptDispatch( pstrItemName, ppdisp );
  83. }
  84. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_GetCurrentScriptThreadID( /*[out]*/ SCRIPTTHREADID *pstidThread )
  85. {
  86. if(m_Script == NULL) return E_FAIL;
  87. return m_Script->GetCurrentScriptThreadID( pstidThread );
  88. }
  89. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_GetScriptThreadID( /*[in ]*/ DWORD dwWin32ThreadId ,
  90. /*[out]*/ SCRIPTTHREADID *pstidThread )
  91. {
  92. if(m_Script == NULL) return E_FAIL;
  93. return m_Script->GetScriptThreadID( dwWin32ThreadId, pstidThread );
  94. }
  95. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_GetScriptThreadState( /*[in ]*/ SCRIPTTHREADID stidThread ,
  96. /*[out]*/ SCRIPTTHREADSTATE *pstsState )
  97. {
  98. if(m_Script == NULL) return E_FAIL;
  99. return m_Script->GetScriptThreadState( stidThread, pstsState );
  100. }
  101. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_InterruptScriptThread( /*[in]*/ SCRIPTTHREADID stidThread ,
  102. /*[in]*/ DWORD dwFlags )
  103. {
  104. if(m_Script == NULL) return E_FAIL;
  105. return m_Script->InterruptScriptThread( stidThread, NULL, dwFlags );
  106. }
  107. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_InitNew()
  108. {
  109. HRESULT hr;
  110. if(m_Script == NULL) return E_FAIL;
  111. if(m_ScriptParse == NULL) return E_FAIL;
  112. if(FAILED(hr = m_ScriptParse->InitNew())) return hr;
  113. return m_Script->SetScriptSite( this );
  114. }
  115. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_AddScriptlet( /*[in ]*/ BSTR bstrDefaultName ,
  116. /*[in ]*/ BSTR bstrCode ,
  117. /*[in ]*/ BSTR bstrItemName ,
  118. /*[in ]*/ BSTR bstrSubItemName ,
  119. /*[in ]*/ BSTR bstrEventName ,
  120. /*[in ]*/ BSTR bstrDelimiter ,
  121. /*[in ]*/ DWORD_PTR dwSourceContextCookie ,
  122. /*[in ]*/ ULONG ulStartingLineNumber ,
  123. /*[in ]*/ DWORD dwFlags ,
  124. /*[out]*/ BSTR *pbstrName )
  125. {
  126. if(m_ScriptParse == NULL) return E_FAIL;
  127. return m_ScriptParse->AddScriptlet( bstrDefaultName ,
  128. bstrCode ,
  129. bstrItemName ,
  130. bstrSubItemName ,
  131. bstrEventName ,
  132. bstrDelimiter ,
  133. dwSourceContextCookie ,
  134. ulStartingLineNumber ,
  135. dwFlags ,
  136. pbstrName ,
  137. NULL );
  138. }
  139. STDMETHODIMP CPCHScriptWrapper_ServerSide::Remote_ParseScriptText( /*[in ]*/ BSTR bstrCode ,
  140. /*[in ]*/ BSTR bstrItemName ,
  141. /*[in ]*/ IUnknown* punkContext ,
  142. /*[in ]*/ BSTR bstrDelimiter ,
  143. /*[in ]*/ DWORD_PTR dwSourceContextCookie ,
  144. /*[in ]*/ ULONG ulStartingLineNumber ,
  145. /*[in ]*/ DWORD dwFlags ,
  146. /*[out]*/ VARIANT* pvarResult )
  147. {
  148. __HCP_FUNC_ENTRY( "CPCHScriptWrapper_ServerSide::Remote_ParseScriptText" );
  149. HRESULT hr;
  150. CComBSTR bstrRealCode;
  151. CPCHScriptWrapper_ServerSide::HeaderList lst;
  152. if(m_ScriptParse == NULL) __MPC_SET_ERROR_AND_EXIT(hr, E_FAIL);
  153. __MPC_EXIT_IF_METHOD_FAILS(hr, CPCHScriptWrapper_ServerSide::ProcessBody( bstrCode, bstrRealCode, lst ));
  154. hr = m_ScriptParse->ParseScriptText( bstrRealCode ,
  155. bstrItemName ,
  156. punkContext ,
  157. bstrDelimiter ,
  158. dwSourceContextCookie ,
  159. ulStartingLineNumber ,
  160. dwFlags ,
  161. pvarResult ,
  162. NULL );
  163. __HCP_FUNC_CLEANUP;
  164. __HCP_FUNC_EXIT(hr);
  165. }
  166. ////////////////////////////////////////////////////////////////////////////////
  167. STDMETHODIMP CPCHScriptWrapper_ServerSide::GetLCID( /*[out]*/ LCID *plcid )
  168. {
  169. HRESULT hr;
  170. CComBSTR bstr;
  171. CComVariant v;
  172. if(m_Browser == NULL) return E_FAIL;
  173. if(FAILED(hr = m_Browser->Remote_GetLCID( &bstr ))) return hr;
  174. v = bstr; v.ChangeType( VT_I4 );
  175. if(plcid) *plcid = v.lVal;
  176. return S_OK;
  177. }
  178. STDMETHODIMP CPCHScriptWrapper_ServerSide::GetItemInfo( /*[in ]*/ LPCOLESTR pstrName ,
  179. /*[in ]*/ DWORD dwReturnMask ,
  180. /*[out]*/ IUnknown* *ppiunkItem ,
  181. /*[out]*/ ITypeInfo* *ppti )
  182. {
  183. HRESULT hr;
  184. CComBSTR bstrName( pstrName );
  185. CComPtr<IUnknown> unk;
  186. CComPtr<ITypeInfo> pti;
  187. if(m_Browser == NULL) return E_FAIL;
  188. if(FAILED(hr = m_Browser->Remote_GetItemInfo( bstrName, dwReturnMask, &unk, &pti ))) return hr;
  189. if(ppiunkItem) *ppiunkItem = unk.Detach();
  190. if(ppti ) *ppti = pti.Detach();
  191. return S_OK;
  192. }
  193. STDMETHODIMP CPCHScriptWrapper_ServerSide::GetDocVersionString( /*[out]*/ BSTR *pbstrVersion )
  194. {
  195. if(m_Browser == NULL) return E_FAIL;
  196. return m_Browser->Remote_GetDocVersionString( pbstrVersion );
  197. }
  198. STDMETHODIMP CPCHScriptWrapper_ServerSide::OnScriptTerminate( /*[in]*/ const VARIANT* pvarResult ,
  199. /*[in]*/ const EXCEPINFO* pexcepinfo )
  200. {
  201. if(m_Browser == NULL) return E_FAIL;
  202. return m_Browser->Remote_OnScriptTerminate( (VARIANT*)pvarResult );
  203. }
  204. STDMETHODIMP CPCHScriptWrapper_ServerSide::OnStateChange( /*[in]*/ SCRIPTSTATE ssScriptState )
  205. {
  206. if(m_Browser == NULL) return E_FAIL;
  207. return m_Browser->Remote_OnStateChange( ssScriptState );
  208. }
  209. STDMETHODIMP CPCHScriptWrapper_ServerSide::OnScriptError( /*[in]*/ IActiveScriptError *pscripterror )
  210. {
  211. if(m_Browser == NULL) return E_FAIL;
  212. return m_Browser->Remote_OnScriptError( pscripterror );
  213. }
  214. STDMETHODIMP CPCHScriptWrapper_ServerSide::OnEnterScript( void )
  215. {
  216. if(m_Browser == NULL) return E_FAIL;
  217. return m_Browser->Remote_OnEnterScript();
  218. }
  219. STDMETHODIMP CPCHScriptWrapper_ServerSide::OnLeaveScript( void )
  220. {
  221. if(m_Browser == NULL) return E_FAIL;
  222. return m_Browser->Remote_OnLeaveScript();
  223. }
  224. ////////////////////////////////////////////////////////////////////////////////
  225. bool CPCHScriptWrapper_ServerSide::KeyValue::operator==( /*[in]*/ LPCOLESTR szKey ) const
  226. {
  227. return MPC::StrICmp( m_strKey, szKey ) == 0;
  228. }
  229. HRESULT CPCHScriptWrapper_ServerSide::ProcessBody( /*[in ]*/ BSTR bstrCode ,
  230. /*[out]*/ CComBSTR& bstrRealCode ,
  231. /*[out]*/ HeaderList& lst )
  232. {
  233. __HCP_FUNC_ENTRY( "CPCHScriptWrapper_ServerSide::ProcessBody" );
  234. HRESULT hr;
  235. LPCWSTR szLineStart = SAFEBSTR( bstrCode );
  236. LPCWSTR szLineEnd;
  237. LPCWSTR szLineNext;
  238. bool fSkipEmpty = true;
  239. lst.clear();
  240. while(szLineStart[0])
  241. {
  242. HeaderIter it;
  243. LPCWSTR szColon;
  244. LPCWSTR szEndLF;
  245. LPCWSTR szEndCR;
  246. szColon = wcschr( szLineStart, ':' );
  247. szEndLF = wcschr( szLineStart, '\n' );
  248. szEndCR = wcschr( szLineStart, '\r' );
  249. if(szEndLF == NULL)
  250. {
  251. if(szEndCR == NULL) break; // No end of line, exit.
  252. szLineEnd = szEndCR;
  253. szLineNext = szEndCR+1;
  254. }
  255. else if(szEndCR == NULL)
  256. {
  257. szLineEnd = szEndLF;
  258. szLineNext = szEndLF+1;
  259. }
  260. else if(szEndCR+1 == szEndLF) // \r\n
  261. {
  262. szLineEnd = szEndCR;
  263. szLineNext = szEndLF+1;
  264. }
  265. else if(szEndLF < szEndCR)
  266. {
  267. szLineEnd = szEndLF;
  268. szLineNext = szEndLF+1;
  269. }
  270. else
  271. {
  272. szLineEnd = szEndCR;
  273. szLineNext = szEndCR+1;
  274. }
  275. if(szEndLF == szLineStart || szEndCR == szLineStart) // Empty line, skip it and exit.
  276. {
  277. if(fSkipEmpty)
  278. {
  279. szLineStart = szLineNext;
  280. continue;
  281. }
  282. szLineStart = szLineNext;
  283. break;
  284. }
  285. if(szColon == NULL) break; // No colon, so it's not an header field, exit
  286. it = lst.insert( lst.end() );
  287. it->m_strKey = MPC::wstring( szLineStart, szColon );
  288. it->m_strValue = MPC::wstring( szColon+1 , szLineEnd );
  289. fSkipEmpty = false;
  290. szLineStart = szLineNext;
  291. }
  292. bstrRealCode = szLineStart;
  293. hr = S_OK;
  294. __HCP_FUNC_EXIT(hr);
  295. }