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.

863 lines
15 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. RmsObjct.cpp
  5. Abstract:
  6. Implementation of CRmsComObject
  7. Author:
  8. Brian Dodd [brian] 15-Nov-1996
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "RmsObjct.h"
  13. /////////////////////////////////////////////////////////////////////////////
  14. //
  15. CRmsComObject::CRmsComObject(void)
  16. /*++
  17. Routine Description:
  18. CRmsComObject constructor
  19. Arguments:
  20. None
  21. Return Value:
  22. None
  23. --*/
  24. {
  25. // Default values
  26. (void) CoCreateGuid( &m_objectId );
  27. m_ObjectType = RmsObjectUnknown;
  28. m_IsEnabled = TRUE;
  29. m_State = 0;
  30. m_StatusCode = S_OK;
  31. m_Name = OLESTR("Object");
  32. m_Description = OLESTR("");
  33. memset( &m_Permit, 0, sizeof( SECURITY_DESCRIPTOR ) );
  34. m_findBy = RmsFindByUnknown;
  35. }
  36. HRESULT
  37. CRmsComObject::CompareTo(
  38. IN IUnknown *pCollectable,
  39. OUT SHORT *pResult)
  40. /*++
  41. Implements:
  42. CRmsComObject::CompareTo
  43. --*/
  44. {
  45. HRESULT hr = E_FAIL;
  46. SHORT result = 1;
  47. WsbTraceIn( OLESTR("CRmsComObject::CompareTo"), OLESTR("") );
  48. try {
  49. // Validate arguments - Okay if pResult is NULL
  50. WsbAssertPointer( pCollectable );
  51. // We need the IRmsComObject interface to get the value of the object.
  52. CComQIPtr<IRmsComObject, &IID_IRmsComObject> pObject = pCollectable;
  53. WsbAssertPointer( pObject );
  54. switch ( m_findBy ) {
  55. case RmsFindByObjectId:
  56. default:
  57. {
  58. GUID objectId;
  59. // Get objectId.
  60. WsbAffirmHr( pObject->GetObjectId( &objectId ));
  61. if ( m_objectId == objectId ) {
  62. // Object IDs match
  63. hr = S_OK;
  64. result = 0;
  65. }
  66. else {
  67. hr = S_FALSE;
  68. result = 1;
  69. }
  70. }
  71. break;
  72. }
  73. }
  74. WsbCatch( hr );
  75. if ( SUCCEEDED(hr) && (0 != pResult) ){
  76. *pResult = result;
  77. }
  78. WsbTraceOut( OLESTR("CRmsComObject::CompareTo"),
  79. OLESTR("hr = <%ls>, result = <%ls>"),
  80. WsbHrAsString( hr ), WsbPtrToShortAsString( pResult ) );
  81. return hr;
  82. }
  83. HRESULT
  84. CRmsComObject::GetSizeMax(
  85. OUT ULARGE_INTEGER* pcbSize)
  86. /*++
  87. Implements:
  88. IPersistStream::GetSizeMax
  89. --*/
  90. {
  91. HRESULT hr = E_NOTIMPL;
  92. WsbTraceIn(OLESTR("CRmsComObject::GetSizeMax"), OLESTR(""));
  93. // try {
  94. // WsbAssert(0 != pcbSize, E_POINTER);
  95. // // Get max size
  96. // pcbSize->QuadPart = WsbPersistSizeOf(GUID) + // m_objectId
  97. // WsbPersistSizeOf(LONG) + // m_findBy
  98. // WsbPersistSizeOf(LONG) + // m_state
  99. // WsbPersistSizeOf(HRESULT); // m_errCode
  100. //// WsbPersistSizeOf(SECURITY_DESCRIPTOR); // m_permit
  101. // } WsbCatch(hr);
  102. WsbTraceOut(OLESTR("CRmsComObject::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pcbSize));
  103. return hr;
  104. }
  105. HRESULT
  106. CRmsComObject::Load(
  107. IN IStream* pStream)
  108. /*++
  109. Implements:
  110. IPersistStream::Load
  111. --*/
  112. {
  113. HRESULT hr = S_OK;
  114. WsbTraceIn(OLESTR("CRmsComObject::Load"), OLESTR(""));
  115. try {
  116. WsbAssertPointer( pStream );
  117. USHORT usTemp;
  118. ULONG ulTemp;
  119. // Read value
  120. WsbAffirmHr(WsbLoadFromStream(pStream, &m_objectId));
  121. WsbAffirmHr(WsbLoadFromStream(pStream, &usTemp));
  122. m_ObjectType = (RmsObject)usTemp;
  123. WsbAffirmHr(WsbLoadFromStream(pStream, &m_IsEnabled));
  124. WsbAffirmHr(WsbLoadFromStream(pStream, &m_State));
  125. WsbAffirmHr(WsbLoadFromStream(pStream, &ulTemp));
  126. m_StatusCode = (HRESULT)ulTemp;
  127. m_Name.Free(); // Clean out any thing previously held
  128. WsbAffirmHr(WsbBstrFromStream(pStream, &m_Name));
  129. m_Description.Free();
  130. WsbAffirmHr(WsbBstrFromStream(pStream, &m_Description));
  131. // WsbAffirmHr(WsbLoadFromStream(pStream, &m_permit));
  132. WsbAffirmHr(WsbLoadFromStream(pStream, &usTemp));
  133. m_findBy = (RmsFindBy)usTemp;
  134. } WsbCatch(hr);
  135. WsbTraceOut(OLESTR("CRmsComObject::Load"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  136. return hr;
  137. }
  138. HRESULT
  139. CRmsComObject::Save(
  140. IN IStream* pStream,
  141. IN BOOL clearDirty)
  142. /*++
  143. Implements:
  144. IPersistStream::Save
  145. --*/
  146. {
  147. HRESULT hr = S_OK;
  148. ULONG ulBytes = 0;
  149. WsbTraceIn(OLESTR("CRmsComObject::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  150. try {
  151. WsbAssertPointer( pStream );
  152. // Read value
  153. WsbAffirmHr(WsbSaveToStream(pStream, m_objectId));
  154. WsbAffirmHr(WsbSaveToStream(pStream, (USHORT) m_ObjectType));
  155. WsbAffirmHr(WsbSaveToStream(pStream, m_IsEnabled));
  156. WsbAffirmHr(WsbSaveToStream(pStream, m_State));
  157. WsbAffirmHr(WsbSaveToStream(pStream, (ULONG) m_StatusCode));
  158. WsbAffirmHr(WsbBstrToStream(pStream, m_Name));
  159. WsbAffirmHr(WsbBstrToStream(pStream, m_Description));
  160. // WsbAffirmHr(WsbSaveToStream(pStream, m_permit));
  161. WsbAffirmHr(WsbSaveToStream(pStream, (USHORT) m_findBy));
  162. } WsbCatch(hr);
  163. WsbTraceOut(OLESTR("CRmsComObject::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  164. return hr;
  165. }
  166. HRESULT
  167. CRmsComObject::Test(
  168. OUT USHORT *pPassed,
  169. OUT USHORT *pFailed)
  170. /*++
  171. Implements:
  172. IWsbTestable::Test
  173. --*/
  174. {
  175. HRESULT hr = S_OK;
  176. CComPtr<IRmsMediaSet> pMediaSet1;
  177. CComPtr<IRmsMediaSet> pMediaSet2;
  178. CComPtr<IPersistFile> pFile1;
  179. CComPtr<IPersistFile> pFile2;
  180. LONG i;
  181. LONG longWork1;
  182. LONG longWork2;
  183. HRESULT hresultVal1 = 11111111;
  184. HRESULT hresultWork1;
  185. // SECURITY_DESCRIPTOR permitVal1;
  186. // SECURITY_DESCRIPTOR permitWork1;
  187. WsbTraceIn(OLESTR("CRmsComObject::Test"), OLESTR(""));
  188. try {
  189. // Get the MediaSet interface.
  190. hr = S_OK;
  191. try {
  192. WsbAssertHr(((IUnknown*) (IRmsMediaSet*) this)->QueryInterface(IID_IRmsMediaSet, (void**) &pMediaSet1));
  193. // Test SetState & GetState
  194. for (i = RmsStateUnknown; i < RmsStateError; i++){
  195. longWork1 = i;
  196. SetState (longWork1);
  197. GetState (&longWork2);
  198. if (longWork1 == longWork2){
  199. (*pPassed)++;
  200. } else {
  201. (*pFailed)++;
  202. }
  203. }
  204. // Test GetErrCode
  205. m_StatusCode = hresultVal1;
  206. GetStatusCode(&hresultWork1);
  207. if(hresultVal1 == hresultWork1){
  208. (*pPassed)++;
  209. } else {
  210. (*pFailed)++;
  211. }
  212. // Test SetPermissions & GetPermissions
  213. // SetPermissions(permitVal1);
  214. // GetPermissions(&permitWork1);
  215. // if((permitVal1 == permitWork1)){
  216. // (*pPassed)++;
  217. // } else {
  218. // (*pFailed)++;
  219. // }
  220. } WsbCatch(hr);
  221. // Tally up the results
  222. hr = S_OK;
  223. if (*pFailed) {
  224. hr = S_FALSE;
  225. }
  226. } WsbCatch(hr);
  227. WsbTraceOut(OLESTR("CRmsComObject::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  228. return hr;
  229. }
  230. STDMETHODIMP
  231. CRmsComObject::InterfaceSupportsErrorInfo(
  232. IN REFIID riid)
  233. /*++
  234. Implements:
  235. ISupportsErrorInfo::InterfaceSupportsErrorInfo
  236. --*/
  237. {
  238. static const IID* arr[] =
  239. {
  240. &IID_IRmsServer,
  241. &IID_IRmsLibrary,
  242. &IID_IRmsDriveClass,
  243. &IID_IRmsCartridge,
  244. &IID_IRmsDrive,
  245. &IID_IRmsStorageSlot,
  246. &IID_IRmsMediumChanger,
  247. &IID_IRmsIEPort,
  248. &IID_IRmsMediaSet,
  249. &IID_IRmsRequest,
  250. &IID_IRmsPartition,
  251. &IID_IRmsComObject,
  252. &IID_IRmsChangerElement,
  253. &IID_IRmsDevice,
  254. &IID_IRmsStorageInfo,
  255. &IID_IRmsNTMS,
  256. };
  257. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  258. {
  259. if (InlineIsEqualGUID(*arr[i],riid))
  260. return S_OK;
  261. }
  262. return S_FALSE;
  263. }
  264. STDMETHODIMP
  265. CRmsComObject::GetObjectId(
  266. OUT GUID *pObjectId)
  267. /*++
  268. Implements:
  269. IRmsComObject::GetObjectId
  270. --*/
  271. {
  272. HRESULT hr = S_OK;
  273. try {
  274. WsbAssertPointer( pObjectId );
  275. *pObjectId = m_objectId;
  276. } WsbCatch(hr);
  277. return hr;
  278. }
  279. STDMETHODIMP
  280. CRmsComObject::SetObjectId(
  281. IN GUID objectId)
  282. /*++
  283. Implements:
  284. IRmsComObject::SetObjectId
  285. --*/
  286. {
  287. m_objectId = objectId;
  288. return S_OK;
  289. }
  290. STDMETHODIMP
  291. CRmsComObject::GetObjectType(
  292. OUT LONG *pType)
  293. /*++
  294. Implements:
  295. IRmsComObject::GetObjectType
  296. --*/
  297. {
  298. HRESULT hr = S_OK;
  299. try {
  300. WsbAssertPointer( pType );
  301. *pType = m_ObjectType;
  302. } WsbCatch(hr);
  303. return hr;
  304. }
  305. STDMETHODIMP
  306. CRmsComObject::SetObjectType(
  307. IN LONG type)
  308. /*++
  309. Implements:
  310. IRmsComObject::SetObjectType
  311. --*/
  312. {
  313. m_ObjectType = (RmsObject) type;
  314. return S_OK;
  315. }
  316. STDMETHODIMP
  317. CRmsComObject::IsEnabled(void)
  318. /*++
  319. Implements:
  320. IRmsComObject::IsEnabled
  321. --*/
  322. {
  323. return (m_IsEnabled) ? S_OK : S_FALSE;
  324. }
  325. STDMETHODIMP
  326. CRmsComObject::Enable()
  327. /*++
  328. Implements:
  329. IRmsComObject::Enable
  330. --*/
  331. {
  332. HRESULT hr = S_OK;
  333. try {
  334. m_IsEnabled = TRUE;
  335. WsbAffirmHr(SetStatusCode(S_OK));
  336. // Log an Event
  337. WsbLogEvent(RMS_MESSAGE_OBJECT_ENABLED, 0, NULL, (WCHAR *)m_Name, NULL );
  338. } WsbCatch(hr);
  339. return hr;
  340. }
  341. STDMETHODIMP
  342. CRmsComObject::Disable(
  343. IN HRESULT reason)
  344. /*++
  345. Implements:
  346. IRmsComObject::Disable
  347. --*/
  348. {
  349. HRESULT hr = S_OK;
  350. try {
  351. m_IsEnabled = FALSE;
  352. WsbAffirmHr(SetStatusCode(reason));
  353. // Log an Event
  354. WsbLogEvent(RMS_MESSAGE_OBJECT_DISABLED, 0, NULL, (WCHAR *)m_Name, WsbHrAsString(reason), NULL );
  355. } WsbCatch(hr);
  356. return hr;
  357. }
  358. STDMETHODIMP
  359. CRmsComObject::GetState(
  360. OUT LONG *pState)
  361. /*++
  362. Implements:
  363. IRmsComObject::GetState
  364. --*/
  365. {
  366. HRESULT hr = S_OK;
  367. try {
  368. WsbAssertPointer( pState );
  369. *pState = m_State;
  370. WsbTrace(OLESTR("GetState: Object <0x%08x> - Enabled = <%ls>; State = <%d>; StatusCode = <%ls>.\n"),
  371. this, WsbBoolAsString(m_IsEnabled), m_State, WsbHrAsString(m_StatusCode));
  372. } WsbCatch(hr);
  373. return hr;
  374. }
  375. STDMETHODIMP
  376. CRmsComObject::SetState(
  377. IN LONG state)
  378. /*++
  379. Implements:
  380. IRmsComObject::SetState
  381. --*/
  382. {
  383. HRESULT hr = S_OK;
  384. try {
  385. m_State = state;
  386. WsbAffirmHr(SetStatusCode(S_OK));
  387. } WsbCatch(hr);
  388. return hr;
  389. }
  390. STDMETHODIMP
  391. CRmsComObject::GetStatusCode(
  392. OUT HRESULT *pResult)
  393. /*++
  394. Implements:
  395. IRmsComObject::GetStatusCode
  396. --*/
  397. {
  398. HRESULT hr = S_OK;
  399. try {
  400. WsbAssertPointer( pResult );
  401. *pResult = m_StatusCode;
  402. } WsbCatch(hr);
  403. return hr;
  404. }
  405. STDMETHODIMP
  406. CRmsComObject::SetStatusCode(
  407. IN HRESULT result
  408. )
  409. /*++
  410. Implements:
  411. IRmsComObject::SetStatusCode
  412. --*/
  413. {
  414. HRESULT hr = S_OK;
  415. try {
  416. m_StatusCode = result;
  417. WsbAffirmHr(adviseOfStatusChange());
  418. } WsbCatch(hr);
  419. return hr;
  420. }
  421. STDMETHODIMP
  422. CRmsComObject::GetName(
  423. OUT BSTR *pName)
  424. /*++
  425. Implements:
  426. IRmsComObject::GetName
  427. --*/
  428. {
  429. HRESULT hr = S_OK;
  430. try {
  431. WsbAssertPointer( pName );
  432. WsbAffirmHr( m_Name.CopyToBstr(pName) );
  433. } WsbCatch( hr );
  434. return hr;
  435. }
  436. STDMETHODIMP
  437. CRmsComObject::SetName(
  438. IN BSTR name)
  439. /*++
  440. Implements:
  441. IRmsComObject::SetName
  442. --*/
  443. {
  444. m_Name = name;
  445. return S_OK;
  446. }
  447. STDMETHODIMP
  448. CRmsComObject::GetDescription(
  449. OUT BSTR *pDesc)
  450. /*++
  451. Implements:
  452. IRmsComObject::GetDescription
  453. --*/
  454. {
  455. HRESULT hr = S_OK;
  456. try {
  457. WsbAssertPointer( pDesc );
  458. WsbAffirmHr( m_Name.CopyToBstr(pDesc) );
  459. } WsbCatch( hr );
  460. return hr;
  461. }
  462. STDMETHODIMP
  463. CRmsComObject::SetDescription(
  464. IN BSTR desc)
  465. /*++
  466. Implements:
  467. IRmsComObject::SetDescription
  468. --*/
  469. {
  470. m_Description = desc;
  471. return S_OK;
  472. }
  473. STDMETHODIMP
  474. CRmsComObject::GetPermissions(
  475. OUT SECURITY_DESCRIPTOR *lpPermit)
  476. /*++
  477. Implements:
  478. IRmsComObject::GetPermissions
  479. --*/
  480. {
  481. HRESULT hr = S_OK;
  482. try {
  483. WsbAssertPointer( lpPermit );
  484. *lpPermit = m_Permit;
  485. } WsbCatch(hr);
  486. return hr;
  487. }
  488. STDMETHODIMP
  489. CRmsComObject::SetPermissions(
  490. IN SECURITY_DESCRIPTOR permit)
  491. /*++
  492. Implements:
  493. IRmsComObject::GetPermissions
  494. --*/
  495. {
  496. m_Permit = permit;
  497. return S_OK;
  498. }
  499. STDMETHODIMP
  500. CRmsComObject::GetFindBy(
  501. OUT LONG *pFindBy)
  502. /*++
  503. Implements:
  504. IRmsComObject::GetFindBy
  505. --*/
  506. {
  507. HRESULT hr = S_OK;
  508. try {
  509. WsbAssertPointer( pFindBy );
  510. *pFindBy = m_findBy;
  511. } WsbCatch(hr);
  512. return hr;
  513. }
  514. STDMETHODIMP
  515. CRmsComObject::SetFindBy(
  516. IN LONG findBy)
  517. /*++
  518. Implements:
  519. IRmsComObject::SetFindBy
  520. --*/
  521. {
  522. m_findBy = (RmsFindBy) findBy;
  523. return S_OK;
  524. }
  525. HRESULT
  526. CRmsComObject::adviseOfStatusChange(void)
  527. /*++
  528. Routine Description:
  529. Notifies of object state changes.
  530. Arguments:
  531. None
  532. Return Value:
  533. S_OK - Success.
  534. --*/
  535. {
  536. HRESULT hr = S_OK;
  537. WsbTraceIn( OLESTR("CRmsComObject::adviseOfStatusChange"), OLESTR(""));
  538. try {
  539. CONNECTDATA pConnectData;
  540. CComPtr<IConnectionPoint> pCP;
  541. CComPtr<IConnectionPointContainer> pCPC;
  542. CComPtr<IEnumConnections> pConnection;
  543. CComPtr<IRmsSinkEveryEvent> pSink;
  544. WsbTrace(OLESTR("Object <0x%08x> - Enabled = <%ls>; State = <%d>; StatusCode = <%ls>.\n"),
  545. this, WsbBoolAsString(m_IsEnabled), m_State, WsbHrAsString(m_StatusCode));
  546. // Tell everyone the new state of the object.
  547. WsbAffirmHr(((IUnknown*)(IRmsComObject*) this)->QueryInterface(IID_IConnectionPointContainer, (void**) &pCPC));
  548. WsbAffirmHr(pCPC->FindConnectionPoint(IID_IRmsSinkEveryEvent, &pCP));
  549. WsbAffirmHr(pCP->EnumConnections(&pConnection));
  550. while(pConnection->Next(1, &pConnectData, 0) == S_OK) {
  551. try {
  552. WsbAffirmHr((pConnectData.pUnk)->QueryInterface(IID_IRmsSinkEveryEvent, (void**) &pSink));
  553. WsbAffirmHr(pSink->ProcessObjectStatusChange( m_IsEnabled, m_State, m_StatusCode ));
  554. } WsbCatch(hr);
  555. (pConnectData.pUnk)->Release();
  556. pSink=0;
  557. }
  558. } WsbCatch(hr);
  559. // We don't care if the sink has problems!
  560. hr = S_OK;
  561. WsbTraceOut(OLESTR("CRmsComObject::adviseOfStatusChange"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  562. return hr;
  563. }