ComponentManagerBase.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "ComponentManagerBase.h"
  2. #include "foundation/error.h"
  3. #include "nx/nxuri.h"
  4. ComponentManagerBase::ComponentManagerBase()
  5. {
  6. phase=PHASE_INITIALIZE;
  7. service_api=0;
  8. component_sync=0;
  9. framework_guid = INVALID_GUID;
  10. application_guid = INVALID_GUID;
  11. }
  12. int ComponentManagerBase::LateLoad(ifc_component *component)
  13. {
  14. int ret;
  15. if (phase >= PHASE_REGISTERED)
  16. {
  17. ret = component->RegisterServices(service_api);
  18. if (ret != NErr_Success)
  19. {
  20. int ret2 = component->Quit(service_api);
  21. if (ret2 == NErr_TryAgain)
  22. {
  23. component_sync->Wait(1);
  24. }
  25. return ret;
  26. }
  27. }
  28. if (phase >= PHASE_LOADING)
  29. {
  30. ret = component->OnLoading(service_api);
  31. if (ret != NErr_Success)
  32. {
  33. int ret2 = component->Quit(service_api);
  34. if (ret2 == NErr_TryAgain)
  35. {
  36. component_sync->Wait(1);
  37. }
  38. return ret;
  39. }
  40. }
  41. if (phase >= PHASE_LOADED)
  42. {
  43. ret = component->OnLoaded(service_api);
  44. if (ret != NErr_Success)
  45. {
  46. int ret2 = component->Quit(service_api);
  47. if (ret2 == NErr_TryAgain)
  48. {
  49. component_sync->Wait(1);
  50. }
  51. return ret;
  52. }
  53. }
  54. return NErr_Success;
  55. }
  56. void ComponentManagerBase::SetServiceAPI(api_service *_service_api)
  57. {
  58. this->service_api = _service_api;
  59. service_api->QueryInterface(&component_sync);
  60. }
  61. int ComponentManagerBase::Load()
  62. {
  63. if (phase != PHASE_INITIALIZE)
  64. return NErr_Error;
  65. int ret;
  66. /* RegisterServices phase */
  67. for (ComponentList::iterator itr=components.begin();itr!=components.end();)
  68. {
  69. ifc_component *component = *itr;
  70. //ComponentList::iterator next=itr;
  71. //next++;
  72. ret = component->RegisterServices(service_api);
  73. if (ret != NErr_Success)
  74. {
  75. int ret2 = component->Quit(service_api);
  76. if (ret2 == NErr_TryAgain)
  77. {
  78. component_sync->Wait(1);
  79. }
  80. NXURIRelease(component->component_info.filename);
  81. CloseComponent(component);
  82. itr = components.erase(itr);
  83. }
  84. else
  85. {
  86. itr++; // = next;
  87. }
  88. }
  89. phase = PHASE_REGISTERED;
  90. /* OnLoading phase */
  91. for (ComponentList::iterator itr=components.begin();itr!=components.end();)
  92. {
  93. ifc_component *component = *itr;
  94. //ComponentList::iterator next=itr;
  95. //next++;
  96. ret = component->OnLoading(service_api);
  97. if (ret != NErr_Success)
  98. {
  99. int ret2 = component->Quit(service_api);
  100. if (ret2 == NErr_TryAgain)
  101. {
  102. component_sync->Wait(1);
  103. }
  104. NXURIRelease(component->component_info.filename);
  105. CloseComponent(component);
  106. itr = components.erase(itr);
  107. }
  108. else
  109. {
  110. itr++; // = next;
  111. }
  112. }
  113. phase = PHASE_LOADING;
  114. /* OnLoaded phase */
  115. for (ComponentList::iterator itr = components.begin(); itr != components.end();)
  116. {
  117. ifc_component* component = *itr;
  118. //ComponentList::iterator next = itr;
  119. //next++;
  120. ret = component->OnLoaded(service_api);
  121. if (ret != NErr_Success)
  122. {
  123. int ret2 = component->Quit(service_api);
  124. if (ret2 == NErr_TryAgain)
  125. {
  126. component_sync->Wait(1);
  127. }
  128. NXURIRelease(component->component_info.filename);
  129. CloseComponent(component);
  130. itr = components.erase(itr);
  131. }
  132. else
  133. {
  134. itr++; // = next;
  135. }
  136. }
  137. phase = PHASE_LOADED;
  138. return NErr_Success;
  139. }
  140. int ComponentManagerBase::AddComponent(ifc_component *component)
  141. {
  142. int err;
  143. if (NULL == component)
  144. return NErr_BadParameter;
  145. if (phase > PHASE_LOADED)
  146. return NErr_Error;
  147. if (component->component_info.wasabi_version != wasabi2_component_version
  148. || component->component_info.nx_api_version != nx_api_version
  149. || component->component_info.nx_platform_guid != nx_platform_guid)
  150. {
  151. return NErr_IncompatibleVersion;
  152. }
  153. if (component->component_info.framework_guid != INVALID_GUID
  154. && framework_guid != component->component_info.framework_guid)
  155. {
  156. return NErr_IncompatibleVersion;
  157. }
  158. if (component->component_info.application_guid != INVALID_GUID
  159. && application_guid != component->component_info.application_guid)
  160. {
  161. return NErr_IncompatibleVersion;
  162. }
  163. for (ComponentList::iterator itr = components.begin(); itr != components.end(); itr++)
  164. {
  165. ifc_component *registered_component = *itr;
  166. if (registered_component->component_info.component_guid == component->component_info.component_guid)
  167. return NErr_Error;
  168. }
  169. err = component->Initialize(service_api);
  170. if (NErr_Success != err)
  171. return err;
  172. /* if the component was added late, we'll need to run some extra stages */
  173. err = LateLoad(component);
  174. if (NErr_Success != err)
  175. return err;
  176. components.push_back(component);
  177. return NErr_Success;
  178. }
  179. void ComponentManagerBase::SetFrameworkGUID(GUID guid)
  180. {
  181. framework_guid = guid;
  182. }
  183. GUID ComponentManagerBase::GetFrameworkGUID()
  184. {
  185. return framework_guid;
  186. }
  187. void ComponentManagerBase::SetApplicationGUID(GUID guid)
  188. {
  189. application_guid = guid;
  190. }
  191. GUID ComponentManagerBase::GetApplicationGUID()
  192. {
  193. return application_guid;
  194. }