1
0

serviceHelper.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. #include "main.h"
  2. #include "./serviceHelper.h"
  3. #include "./navigation.h"
  4. #include "./api__ml_online.h"
  5. #include "./resource.h"
  6. #include "./serviceHost.h"
  7. #include <ifc_omservice.h>
  8. #include <ifc_omfilestorage.h>
  9. #include <ifc_omwebstorage.h>
  10. #include <ifc_omserviceeditor.h>
  11. #include <ifc_omserviceeventmngr.h>
  12. #include <ifc_omservicecommand.h>
  13. #include <ifc_omserviceenum.h>
  14. #include <ifc_omxmlserviceenum.h>
  15. #include <ifc_omservicecopier.h>
  16. #include <ifc_mlnavigationhelper.h>
  17. #include <browserUiCommon.h>
  18. #include <vector>
  19. #include <wininet.h>
  20. #include <strsafe.h>
  21. typedef std::vector<ifc_omstorageasync*> AsyncList;
  22. struct SERVICEHELPER
  23. {
  24. SERVICEHELPER()
  25. : discoverAsync(NULL)
  26. {}
  27. ifc_omstorageasync *discoverAsync;
  28. AsyncList versionChecks;
  29. CRITICAL_SECTION lock;
  30. };
  31. static SERVICEHELPER *serviceHelper = NULL;
  32. static void ServiceHelper_Lock()
  33. {
  34. if (NULL != serviceHelper)
  35. EnterCriticalSection(&serviceHelper->lock);
  36. }
  37. static void ServiceHelper_Unlock()
  38. {
  39. if (NULL != serviceHelper)
  40. LeaveCriticalSection(&serviceHelper->lock);
  41. }
  42. static void CALLBACK ServiceHelper_Uninitialize()
  43. {
  44. if (NULL == serviceHelper)
  45. return;
  46. ServiceHelper_Lock();
  47. ifc_omstorage *storage = NULL;;
  48. if (SUCCEEDED(ServiceHelper_QueryWebStorage(&storage)))
  49. {
  50. if (NULL != serviceHelper->discoverAsync)
  51. {
  52. storage->RequestAbort(serviceHelper->discoverAsync, TRUE);
  53. storage->EndLoad(serviceHelper->discoverAsync, NULL);
  54. serviceHelper->discoverAsync->Release();
  55. serviceHelper->discoverAsync = NULL;
  56. }
  57. size_t index = serviceHelper->versionChecks.size();
  58. while(index--)
  59. {
  60. ifc_omstorageasync *async = serviceHelper->versionChecks[index];
  61. storage->RequestAbort(async, TRUE);
  62. storage->EndLoad(async, NULL);
  63. }
  64. serviceHelper->versionChecks.clear();
  65. storage->Release();
  66. }
  67. ServiceHelper_Unlock();
  68. DeleteCriticalSection(&serviceHelper->lock);
  69. //free(serviceHelper);
  70. delete serviceHelper;
  71. serviceHelper = NULL;
  72. }
  73. HRESULT ServiceHelper_Initialize()
  74. {
  75. if (NULL != serviceHelper)
  76. return S_FALSE;
  77. //serviceHelper = (SERVICEHELPER*)calloc(1, sizeof(SERVICEHELPER));
  78. serviceHelper = new SERVICEHELPER();
  79. if (NULL == serviceHelper)
  80. return E_OUTOFMEMORY;
  81. InitializeCriticalSection(&serviceHelper->lock);
  82. Plugin_RegisterUnloadCallback(ServiceHelper_Uninitialize);
  83. return S_OK;
  84. }
  85. HRESULT ServiceHelper_QueryStorage(ifc_omstorage **storage)
  86. {
  87. if (NULL == storage) return E_POINTER;
  88. if (NULL == OMSERVICEMNGR)
  89. {
  90. *storage = NULL;
  91. return E_UNEXPECTED;
  92. }
  93. return OMSERVICEMNGR->QueryStorage(&SUID_OmStorageIni, storage);
  94. }
  95. HRESULT ServiceHelper_QueryWebStorage(ifc_omstorage **storage)
  96. {
  97. if (NULL == storage) return E_POINTER;
  98. if (NULL == OMSERVICEMNGR)
  99. {
  100. *storage = NULL;
  101. return E_UNEXPECTED;
  102. }
  103. return OMSERVICEMNGR->QueryStorage(&SUID_OmStorageUrl, storage);
  104. }
  105. HRESULT ServiceHelper_Create(UINT serviceId, LPCWSTR pszName, LPCWSTR pszIcon, LPCWSTR pszUrl, UINT flags, UINT generation, BOOL fSave, ifc_omservice **serviceOut)
  106. {
  107. if (NULL == serviceOut)
  108. return E_POINTER;
  109. *serviceOut = NULL;
  110. if (NULL == OMSERVICEMNGR)
  111. return E_UNEXPECTED;
  112. ServiceHost *serviceHost;
  113. if (FAILED(ServiceHost::GetCachedInstance(&serviceHost)))
  114. serviceHost = NULL;
  115. HRESULT hr = OMSERVICEMNGR->CreateService(serviceId, serviceHost, serviceOut);
  116. if (SUCCEEDED(hr))
  117. {
  118. ifc_omserviceeditor *editor;
  119. if (SUCCEEDED((*serviceOut)->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
  120. {
  121. WCHAR szBuffer[4096] = {0};
  122. editor->BeginUpdate();
  123. if (NULL != pszName && IS_INTRESOURCE(pszName))
  124. {
  125. if (NULL != WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)pszName, szBuffer, ARRAYSIZE(szBuffer)))
  126. editor->SetName(szBuffer, FALSE);
  127. }
  128. else
  129. editor->SetName(pszName, FALSE);
  130. if (NULL != pszIcon && IS_INTRESOURCE(pszIcon))
  131. {
  132. if (SUCCEEDED(Plugin_MakeResourcePath(szBuffer, ARRAYSIZE(szBuffer), RT_RCDATA, pszIcon, RESPATH_COMPACT)))
  133. editor->SetIcon(szBuffer, FALSE);
  134. }
  135. else
  136. editor->SetIcon(pszIcon, FALSE);
  137. if (NULL != pszUrl && IS_INTRESOURCE(pszUrl))
  138. {
  139. if (SUCCEEDED(Plugin_MakeResourcePath(szBuffer, ARRAYSIZE(szBuffer), RT_HTML, pszUrl, RESPATH_TARGETIE | RESPATH_COMPACT)))
  140. editor->SetUrl(szBuffer, FALSE);
  141. }
  142. else
  143. editor->SetUrl(pszUrl, FALSE);
  144. editor->SetGeneration(generation);
  145. editor->SetFlags(flags, 0xFFFFFFFF);
  146. if (FALSE != fSave)
  147. {
  148. hr = ServiceHelper_Save(*serviceOut);
  149. if (FAILED(hr))
  150. {
  151. (*serviceOut)->Release();
  152. *serviceOut = NULL;
  153. }
  154. }
  155. else
  156. {
  157. editor->SetModified(0, (UINT)-1);
  158. }
  159. editor->EndUpdate();
  160. editor->Release();
  161. }
  162. }
  163. if (NULL != serviceHost)
  164. serviceHost->Release();
  165. return hr;
  166. }
  167. HRESULT ServiceHelper_Save(ifc_omservice *service)
  168. {
  169. if (NULL == service)
  170. return E_INVALIDARG;
  171. if (NULL == OMSERVICEMNGR)
  172. return E_UNEXPECTED;
  173. if (S_OK == ServiceHelper_IsSpecial(service))
  174. return S_FALSE;
  175. HRESULT hr;
  176. ifc_omstorage *storage;
  177. hr = ServiceHelper_QueryStorage(&storage);
  178. if (SUCCEEDED(hr))
  179. {
  180. UINT serviceFlags;
  181. if (SUCCEEDED(service->GetFlags(&serviceFlags)) && 0 != (SVCF_AUTOUPGRADE & serviceFlags))
  182. {
  183. hr = storage->Save(&service, 1, ifc_omstorage::saveClearModified, NULL);
  184. if (SUCCEEDED(hr))
  185. {
  186. ifc_omserviceeditor *editor;
  187. if (SUCCEEDED(service->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
  188. {
  189. editor->BeginUpdate();
  190. editor->SetFlags(0, SVCF_AUTOUPGRADE);
  191. editor->SetModified(0, ifc_omserviceeditor::modifiedFlags);
  192. editor->EndUpdate();
  193. editor->Release();
  194. }
  195. }
  196. }
  197. else
  198. {
  199. hr = storage->Save(&service, 1, ifc_omstorage::saveClearModified | ifc_omstorage::saveModifiedOnly, NULL);
  200. }
  201. storage->Release();
  202. }
  203. return hr;
  204. }
  205. HRESULT ServiceHelper_Delete(ifc_omservice *service, UINT flags)
  206. {
  207. if (NULL == service)
  208. return E_INVALIDARG;
  209. if (NULL == OMSERVICEMNGR)
  210. return E_UNEXPECTED;
  211. if (S_OK == ServiceHelper_IsSpecial(service))
  212. return S_FALSE;
  213. HRESULT hr;
  214. if (S_OK == ServiceHelper_IsSubscribed(service))
  215. {
  216. hr = ServiceHelper_Subscribe(service, FALSE, ((SHF_NOTIFY | SHF_VERBAL) & flags));
  217. if (S_OK != hr) return hr;
  218. }
  219. ifc_omstorage *storage = NULL;
  220. hr = ServiceHelper_QueryStorage(&storage);
  221. if (SUCCEEDED(hr) && storage)
  222. {
  223. hr = storage->Delete(&service, 1, NULL);
  224. storage->Release();
  225. }
  226. return hr;
  227. }
  228. HRESULT ServiceHelper_SetFlags(ifc_omservice *service, UINT flags, UINT flagsMask)
  229. {
  230. if (NULL ==service)
  231. return E_INVALIDARG;
  232. UINT serviceFlags;
  233. HRESULT hr = service->GetFlags(&serviceFlags);
  234. if (FAILED(hr)) return hr;
  235. if ((flags & flagsMask) == (serviceFlags & flagsMask))
  236. return S_FALSE;
  237. ifc_omserviceeditor *editor;
  238. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  239. if (SUCCEEDED(hr))
  240. {
  241. editor->SetFlags(flags, flagsMask);
  242. editor->Release();
  243. }
  244. return hr;
  245. }
  246. HRESULT ServiceHelper_IsSpecial(ifc_omservice *service)
  247. {
  248. if (NULL == service) return E_INVALIDARG;
  249. UINT flags;
  250. HRESULT hr = service->GetFlags(&flags);
  251. if (FAILED(hr)) return hr;
  252. return (0 != (SVCF_SPECIAL & flags)) ? S_OK : S_FALSE;
  253. }
  254. HRESULT ServiceHelper_IsSubscribed(ifc_omservice *service)
  255. {
  256. if (NULL == service) return E_INVALIDARG;
  257. //return S_OK;
  258. UINT flags;
  259. HRESULT hr = service->GetFlags(&flags);
  260. if (FAILED(hr)) return hr;
  261. return (0 != (SVCF_SUBSCRIBED & flags)) ? S_OK : S_FALSE;
  262. }
  263. HRESULT ServiceHelper_IsModified(ifc_omservice *service)
  264. {
  265. if (NULL == service) return E_INVALIDARG;
  266. HRESULT hr;
  267. ifc_omserviceeditor *editor;
  268. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  269. if (SUCCEEDED(hr))
  270. {
  271. UINT modified;
  272. hr = editor->GetModified(&modified);
  273. if (SUCCEEDED(hr))
  274. {
  275. hr = (0 == modified) ? S_FALSE : S_OK;
  276. }
  277. editor->Release();
  278. }
  279. return hr;
  280. }
  281. HRESULT ServiceHelper_MarkModified(ifc_omservice *service, UINT modifiedFlag, UINT modifiedMask)
  282. {
  283. if (NULL == service)
  284. return E_INVALIDARG;
  285. HRESULT hr;
  286. ifc_omserviceeditor *editor;
  287. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  288. if (SUCCEEDED(hr))
  289. {
  290. hr = editor->SetModified(modifiedFlag, modifiedMask);
  291. editor->Release();
  292. }
  293. return hr;
  294. }
  295. HRESULT ServiceHelper_Load(ifc_omserviceenum **enumerator)
  296. {
  297. if (NULL == enumerator)
  298. return E_POINTER;
  299. HRESULT hr;
  300. ifc_omstorage *storage;
  301. hr = ServiceHelper_QueryStorage(&storage);
  302. if (SUCCEEDED(hr))
  303. {
  304. ServiceHost *serviceHost;
  305. if (FAILED(ServiceHost::GetCachedInstance(&serviceHost)))
  306. serviceHost = NULL;
  307. hr = storage->Load(L"*.ini", serviceHost, enumerator);
  308. if (NULL != serviceHost)
  309. serviceHost->Release();
  310. storage->Release();
  311. }
  312. if (FAILED(hr))
  313. *enumerator = NULL;
  314. return hr;
  315. }
  316. HRESULT ServiceHelper_UpdateIcon(ifc_omserviceeditor *editor, LPCWSTR pszImage)
  317. {
  318. WCHAR szBuffer[8192];
  319. szBuffer[0] = L'\0';
  320. if (NULL == editor)
  321. return E_INVALIDARG;
  322. ifc_omservice *service;
  323. if (SUCCEEDED(editor->QueryInterface(IFC_OmService, (void**)&service)))
  324. {
  325. if (FAILED(service->GetIcon(szBuffer, ARRAYSIZE(szBuffer))))
  326. szBuffer[0] = L'\0';
  327. service->Release();
  328. }
  329. HRESULT hr = editor->SetIcon(pszImage, FALSE);
  330. if (FAILED(hr) || S_FALSE == hr) return hr;
  331. if (L'\0' != szBuffer)
  332. {
  333. ifc_mlnavigationhelper *navHelper;
  334. if (SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
  335. {
  336. navHelper->ReleaseIndex(szBuffer);
  337. navHelper->Release();
  338. }
  339. }
  340. return S_OK;
  341. }
  342. HRESULT ServiceHelper_Find(UINT serviceId, ifc_omservice **serviceOut)
  343. {
  344. if (NULL == serviceOut) return E_POINTER;
  345. *serviceOut = NULL;
  346. if (0 == serviceId)
  347. return E_INVALIDARG;
  348. Navigation *navigation;
  349. if (SUCCEEDED(Plugin_GetNavigation(&navigation)))
  350. {
  351. HNAVITEM hItem = navigation->FindService(serviceId, serviceOut);
  352. navigation->Release();
  353. if (NULL != hItem)
  354. return S_OK;
  355. }
  356. ifc_omserviceenum *enumerator;
  357. HRESULT hr = ServiceHelper_Load(&enumerator);
  358. if (SUCCEEDED(hr))
  359. {
  360. ifc_omservice *service;
  361. hr = S_FALSE;
  362. while (S_OK == enumerator->Next(1, &service, NULL))
  363. {
  364. if (service->GetId() == serviceId)
  365. {
  366. *serviceOut = service;
  367. hr = S_OK;
  368. break;
  369. }
  370. service->Release();
  371. }
  372. enumerator->Release();
  373. }
  374. return hr;
  375. }
  376. HRESULT ServiceHelper_SetRating(ifc_omservice *service, UINT rating, UINT flags)
  377. {
  378. if (NULL == service)
  379. return E_POINTER;
  380. ifc_omserviceeditor *editor;
  381. HRESULT hr;
  382. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  383. if (SUCCEEDED(hr))
  384. {
  385. hr = editor->SetRating(rating);
  386. if (S_OK == hr)
  387. {
  388. if (0 != (SHF_NOTIFY & flags))
  389. {
  390. // send notification
  391. WCHAR szUrl[2048] = {0}, szClient[128] = {0};
  392. if (NULL == OMBROWSERMNGR || FAILED(OMBROWSERMNGR->GetClientId(szClient, ARRAYSIZE(szClient))))
  393. szClient[0] = L'\0';
  394. hr = StringCchPrintf(szUrl, ARRAYSIZE(szUrl),
  395. L"http://services.winamp.com/svc/rating?svc_id=%u&unique_id=%s&rating=%d",
  396. service->GetId(), szClient, rating*2);
  397. if (SUCCEEDED(hr))
  398. {
  399. hr = ServiceHelper_PostNotificationUrl(szUrl);
  400. }
  401. }
  402. if (0 != (SHF_SAVE & flags))
  403. ServiceHelper_Save(service);
  404. }
  405. editor->Release();
  406. }
  407. return hr;
  408. }
  409. HRESULT ServiceHelper_PostNotificationUrl(LPCWSTR pszUrl)
  410. {
  411. HRESULT hr;
  412. ifc_omstorage *storage;
  413. hr = ServiceHelper_QueryWebStorage(&storage);
  414. if (SUCCEEDED(hr))
  415. {
  416. ifc_omstorageasync *async;
  417. hr = storage->BeginLoad(pszUrl, NULL, NULL, NULL, &async);
  418. if(SUCCEEDED(hr))
  419. {
  420. async->Release();
  421. }
  422. storage->Release();
  423. }
  424. return hr;
  425. }
  426. HRESULT ServiceHelper_ResetPermissions(ifc_omservice *service, UINT flags)
  427. {
  428. if (NULL == service)
  429. return E_INVALIDARG;
  430. if (0 != (SHF_VERBAL & flags))
  431. {
  432. WCHAR szText[1024] = {0}, szFormat[512] = {0}, szName[128] = {0};
  433. service->GetName(szName, ARRAYSIZE(szName));
  434. WASABI_API_LNGSTRINGW_BUF(IDS_MESSAGEBOX_POLICYRESET, szFormat, ARRAYSIZE(szFormat));
  435. StringCchPrintf(szText, ARRAYSIZE(szText), szFormat, szName);
  436. if (IDNO == Plugin_MessageBox(szText, MAKEINTRESOURCE(IDS_MESSAGEBOX_POLICYRESET_CAPTION),
  437. MB_ICONQUESTION | MB_YESNO))
  438. {
  439. return S_FALSE;
  440. }
  441. }
  442. if (NULL == AGAVE_API_JSAPI2_SECURITY)
  443. return E_UNEXPECTED;
  444. WCHAR szBuffer[64] = {0};
  445. if (FAILED(StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer), L"%u", service->GetId())))
  446. return E_FAIL;
  447. AGAVE_API_JSAPI2_SECURITY->ResetAuthorization(szBuffer);
  448. return S_OK;
  449. }
  450. HRESULT ServiceHelper_IsPreAuthorized(ifc_omservice *service)
  451. {
  452. if (NULL == service)
  453. return E_INVALIDARG;
  454. UINT serviceFlags;
  455. HRESULT hr = service->GetFlags(&serviceFlags);
  456. if (SUCCEEDED(hr))
  457. {
  458. hr = (0 != (SVCF_PREAUTHORIZED & serviceFlags)) ? S_OK : S_FALSE;
  459. }
  460. return hr;
  461. }
  462. HRESULT ServiceHelper_Subscribe(ifc_omservice *service, BOOL subscribe, UINT flags)
  463. {
  464. if (NULL == service)
  465. return E_POINTER;
  466. if ((TRUE == subscribe) == (S_OK == ServiceHelper_IsSubscribed(service)))
  467. return S_FALSE;
  468. HRESULT hr;
  469. if (FALSE == subscribe && 0 != (SHF_VERBAL & flags))
  470. {
  471. WCHAR szText[1024] = {0}, szFormat[512] = {0}, szName[128] = {0};
  472. if (FAILED(service->GetName(szName, ARRAYSIZE(szName))))
  473. szName[0] = L'\0';
  474. WASABI_API_LNGSTRINGW_BUF(IDS_MESSAGEBOX_UNSUBSCRIBE, szFormat, ARRAYSIZE(szFormat));
  475. StringCchPrintf(szText,ARRAYSIZE(szText), szFormat, szName);
  476. if (IDNO == Plugin_MessageBox(szText, MAKEINTRESOURCE(IDS_MESSAGEBOX_UNSUBSCRIBE_CAPTION),
  477. MB_ICONQUESTION | MB_YESNO))
  478. {
  479. return S_FALSE;
  480. }
  481. }
  482. ifc_omserviceeditor *editor = NULL;
  483. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  484. if (SUCCEEDED(hr) && editor)
  485. {
  486. hr = editor->SetFlags((FALSE == subscribe) ? 0 : SVCF_SUBSCRIBED, SVCF_SUBSCRIBED);
  487. if (S_OK == hr)
  488. {
  489. if (0 != (SHF_SAVE & flags))
  490. ServiceHelper_Save(service);
  491. }
  492. editor->Release();
  493. if (FALSE == subscribe)
  494. ServiceHelper_Delete(service, 0);
  495. }
  496. if (S_OK == hr)
  497. {
  498. if (NULL == AGAVE_API_JSAPI2_SECURITY)
  499. {
  500. WCHAR szBuffer[64] = {0};
  501. if (SUCCEEDED(StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer), L"%u", service->GetId())))
  502. {
  503. AGAVE_API_JSAPI2_SECURITY->ResetAuthorization(szBuffer);
  504. }
  505. }
  506. if (0 != (SHF_NOTIFY & flags))
  507. {
  508. LPWSTR pszUrl;
  509. UINT serviceId = service->GetId();
  510. LPCWSTR action = (FALSE != subscribe) ? L"add" : L"remove";;
  511. if (NULL != action && SUCCEEDED(Plugin_BuildActionUrl(&pszUrl, action, &serviceId, 1)))
  512. {
  513. hr = ServiceHelper_PostNotificationUrl(pszUrl);
  514. Plugin_FreeString(pszUrl);
  515. }
  516. }
  517. }
  518. return hr;
  519. }
  520. HRESULT ServiceHelper_ResetSubscription(UINT flags)
  521. {
  522. if (S_OK == ServiceHelper_IsDiscovering())
  523. {
  524. if (0 != (SHF_VERBAL & flags))
  525. {
  526. Plugin_MessageBox(MAKEINTRESOURCE(IDS_MESSAGEBOX_DISCOVERBUSY),
  527. MAKEINTRESOURCE(IDS_MESSAGEBOX_DISCOVERBUSY_CAPTION),
  528. MB_ICONASTERISK | MB_OK);
  529. }
  530. return E_PENDING;
  531. }
  532. if (0 != (SHF_VERBAL & flags))
  533. {
  534. if (IDNO == Plugin_MessageBox(MAKEINTRESOURCE(IDS_MESSAGEBOX_RESETTODEFAULT),
  535. MAKEINTRESOURCE(IDS_MESSAGEBOX_RESETTODEFAULT_CAPTION),
  536. MB_ICONQUESTION | MB_YESNO))
  537. {
  538. return S_FALSE;
  539. }
  540. }
  541. HRESULT hr;
  542. Navigation *navigation;
  543. if (SUCCEEDED(Plugin_GetNavigation(&navigation)))
  544. {
  545. navigation->DeleteAll();
  546. navigation->Release();
  547. }
  548. ifc_omstorage *storage;
  549. hr = ServiceHelper_QueryStorage(&storage);
  550. if (SUCCEEDED(hr))
  551. {
  552. ifc_omserviceenum *enumerator;
  553. hr = ServiceHelper_Load(&enumerator);
  554. if (SUCCEEDED(hr))
  555. {
  556. ifc_omservice *service;
  557. std::vector<UINT> removeList;
  558. while(S_OK == enumerator->Next(1, &service, NULL))
  559. {
  560. if (S_OK == ServiceHelper_IsSubscribed(service))
  561. removeList.push_back(service->GetId());
  562. ServiceHelper_ResetPermissions(service, 0);
  563. storage->Delete(&service, 1, NULL);
  564. service->Release();
  565. }
  566. enumerator->Release();
  567. if (0 != removeList.size())
  568. {
  569. LPWSTR pszUrl;
  570. if (SUCCEEDED(Plugin_BuildActionUrl(&pszUrl, L"remove", removeList.data(), removeList.size())))
  571. {
  572. ServiceHelper_PostNotificationUrl(pszUrl);
  573. Plugin_FreeString(pszUrl);
  574. }
  575. }
  576. }
  577. storage->Release();
  578. }
  579. if (SUCCEEDED(hr))
  580. {
  581. // discover new services
  582. hr = ServiceHelper_BeginDiscover(L"http://services.winamp.com/svc/default.php");
  583. }
  584. return hr;
  585. }
  586. HRESULT ServiceHelper_IsDiscovering()
  587. {
  588. HRESULT hr;
  589. ServiceHelper_Lock();
  590. if (NULL == serviceHelper)
  591. {
  592. hr = E_UNEXPECTED;
  593. }
  594. else
  595. {
  596. hr = (NULL != serviceHelper->discoverAsync) ? S_OK : S_FALSE;
  597. }
  598. ServiceHelper_Unlock();
  599. return hr;
  600. }
  601. static void CALLBACK ServiceHelper_DiscoverComplete(ifc_omstorageasync *result)
  602. {
  603. if (NULL == result)
  604. return;
  605. SERVICEHELPER *helper;
  606. if (FAILED(result->GetData((void**)&helper)) || NULL == helper)
  607. return;
  608. ServiceHelper_Lock();
  609. ifc_omstorage *webStorage;
  610. HRESULT hr = ServiceHelper_QueryWebStorage(&webStorage);
  611. if (SUCCEEDED(hr))
  612. {
  613. ifc_omserviceenum *serviceEnum;
  614. if (SUCCEEDED(webStorage->EndLoad(result, &serviceEnum)))
  615. {
  616. Navigation *navigation;
  617. if (FAILED(Plugin_GetNavigation(&navigation)))
  618. navigation = NULL;
  619. ifc_omstorage *localStorage;
  620. if (SUCCEEDED(ServiceHelper_QueryStorage(&localStorage)))
  621. {
  622. std::vector<UINT> registerList;
  623. ifc_omservice *service;
  624. while(S_OK == serviceEnum->Next(1, &service, NULL))
  625. {
  626. ifc_omserviceeditor *editor;
  627. if (SUCCEEDED(service->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
  628. {
  629. if (SUCCEEDED(editor->SetFlags(SVCF_SUBSCRIBED, SVCF_SUBSCRIBED)))
  630. registerList.push_back(service->GetId());
  631. editor->Release();
  632. }
  633. if (SUCCEEDED(localStorage->Save(&service, 1, ifc_omstorage::saveClearModified, NULL)))
  634. {
  635. navigation->CreateItemAsync(service);
  636. }
  637. service->Release();
  638. }
  639. if (0 != registerList.size())
  640. {
  641. LPWSTR pszUrl;
  642. if (SUCCEEDED(Plugin_BuildActionUrl(&pszUrl, L"add", registerList.data(), registerList.size())))
  643. {
  644. ServiceHelper_PostNotificationUrl(pszUrl);
  645. Plugin_FreeString(pszUrl);
  646. }
  647. }
  648. }
  649. if (NULL != navigation)
  650. navigation->Release();
  651. serviceEnum->Release();
  652. }
  653. webStorage->Release();
  654. }
  655. if (serviceHelper->discoverAsync == result)
  656. {
  657. serviceHelper->discoverAsync->Release();
  658. serviceHelper->discoverAsync = NULL;
  659. }
  660. ServiceHelper_Unlock();
  661. }
  662. HRESULT ServiceHelper_BeginDiscover(LPCWSTR address)
  663. {
  664. ifc_omstorage *storage;
  665. HRESULT hr = ServiceHelper_QueryWebStorage(&storage);
  666. if (FAILED(hr)) return hr;
  667. ServiceHost *serviceHost;
  668. if (FAILED(ServiceHost::GetCachedInstance(&serviceHost)))
  669. serviceHost = NULL;
  670. ServiceHelper_Lock();
  671. if (NULL == serviceHelper->discoverAsync)
  672. hr = storage->BeginLoad(address, serviceHost, ServiceHelper_DiscoverComplete, serviceHelper, &serviceHelper->discoverAsync);
  673. else
  674. hr = E_PENDING;
  675. ServiceHelper_Unlock();
  676. storage->Release();
  677. if (NULL != serviceHost)
  678. serviceHost->Release();
  679. return hr;
  680. }
  681. HRESULT ServiceHelper_GetDetailsUrl(LPWSTR pszBuffer, UINT cchBufferMax, ifc_omservice *service, BOOL fLite)
  682. {
  683. if (NULL == pszBuffer)
  684. return E_POINTER;
  685. if (0 == service)
  686. return E_INVALIDARG;
  687. HRESULT hr;
  688. size_t remaining = cchBufferMax;
  689. LPWSTR cursor = pszBuffer;
  690. hr = StringCchPrintfEx(cursor, remaining, &cursor, &remaining, STRSAFE_NULL_ON_FAILURE,
  691. L"http://services.winamp.com/svc/details?svc_id=%u", service->GetId());
  692. WCHAR szClient[128] = {0};
  693. if (SUCCEEDED(hr) &&
  694. NULL != OMBROWSERMNGR &&
  695. SUCCEEDED(OMBROWSERMNGR->GetClientId(szClient, ARRAYSIZE(szClient))) &&
  696. L'\0' != szClient[0])
  697. {
  698. hr = StringCchPrintfEx(cursor, remaining, &cursor, &remaining, STRSAFE_NULL_ON_FAILURE,
  699. L"&unique_id=%s", szClient);
  700. }
  701. if (FALSE != fLite && SUCCEEDED(hr))
  702. {
  703. hr = StringCchCopyEx(cursor, remaining, L"&detail=lite", &cursor, &remaining, STRSAFE_NULL_ON_FAILURE);
  704. }
  705. return hr;
  706. }
  707. static void CALLBACK ServiceHelper_VersionCheckComplete(ifc_omstorageasync *result)
  708. {
  709. if (NULL == result)
  710. return;
  711. ifc_omservice *target;
  712. if (FAILED(result->GetData((void**)&target)) || NULL == target)
  713. return;
  714. ServiceHelper_Lock();
  715. size_t index = serviceHelper->versionChecks.size();
  716. while(index--)
  717. {
  718. if (result == serviceHelper->versionChecks[index])
  719. {
  720. auto it = serviceHelper->versionChecks.begin() + index;
  721. if (it < serviceHelper->versionChecks.end())
  722. {
  723. it = serviceHelper->versionChecks.erase(it);
  724. }
  725. break;
  726. }
  727. }
  728. ServiceHelper_Unlock();
  729. UINT originalFlags;
  730. if (FAILED(target->GetFlags(&originalFlags)))
  731. originalFlags = 0;
  732. ifc_omserviceeditor *editor;
  733. if (SUCCEEDED(target->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
  734. editor->BeginUpdate();
  735. else
  736. editor = NULL;
  737. ifc_omstorage *storage;
  738. HRESULT hr = ServiceHelper_QueryWebStorage(&storage);
  739. if (SUCCEEDED(hr))
  740. {
  741. ifc_omserviceenum *serviceEnum;
  742. if (SUCCEEDED(storage->EndLoad(result, &serviceEnum)))
  743. {
  744. ifc_omservice *source = NULL;
  745. while(S_OK == serviceEnum->Next(1, &source, NULL))
  746. {
  747. if (source->GetId() == target->GetId())
  748. break;
  749. source->Release();
  750. source = NULL;
  751. }
  752. ifc_omxmlserviceenum *xmlServiceEnum;
  753. if (SUCCEEDED(serviceEnum->QueryInterface(IFC_OmXmlServiceEnum, (void**)&xmlServiceEnum)))
  754. {
  755. UINT statusCode;
  756. if (SUCCEEDED(xmlServiceEnum->GetStatusCode(&statusCode)))
  757. {
  758. switch(statusCode)
  759. {
  760. case 410: // force remove
  761. if (NULL != target)
  762. {
  763. WCHAR szText[1024] = {0}, szFormat[512] = {0}, szName[128] = {0};
  764. target->GetName(szName, ARRAYSIZE(szName));
  765. WASABI_API_LNGSTRINGW_BUF(IDS_MESSAGEBOX_FORCEREMOVE, szFormat, ARRAYSIZE(szFormat));
  766. StringCchPrintf(szText,ARRAYSIZE(szText), szFormat, szName);
  767. Plugin_MessageBox(szText, MAKEINTRESOURCE(IDS_MESSAGEBOX_FORCEREMOVE_CAPTION), MB_ICONINFORMATION | MB_OK);
  768. ServiceHelper_Delete(target, 0);
  769. target->Release();
  770. target = NULL;
  771. }
  772. break;
  773. case 200:
  774. if (NULL != source)
  775. {
  776. ifc_omservicecopier *copier;
  777. if (SUCCEEDED(source->QueryInterface(IFC_OmServiceCopier, (void**)&copier)))
  778. {
  779. UINT targetFlags, sourceFlags;
  780. if (FAILED(target->GetFlags(&targetFlags))) targetFlags = 0;
  781. if (SUCCEEDED(source->GetFlags(&sourceFlags)))
  782. targetFlags |= sourceFlags;
  783. copier->CopyTo(target, NULL);
  784. copier->Release();
  785. editor->SetFlags(targetFlags, targetFlags);
  786. }
  787. }
  788. break;
  789. }
  790. }
  791. xmlServiceEnum->Release();
  792. }
  793. if (NULL != source)
  794. source->Release();
  795. serviceEnum->Release();
  796. }
  797. storage->Release();
  798. }
  799. if (NULL != target && NULL != AGAVE_API_JSAPI2_SECURITY)
  800. {
  801. WCHAR szBuffer[64] = {0};
  802. if (SUCCEEDED(StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), L"%u", target->GetId())))
  803. {
  804. UINT flags = 0;
  805. if (SUCCEEDED(target->GetFlags(&flags)))
  806. {
  807. bool bypassEnabled = (0 != (SVCF_PREAUTHORIZED & flags));
  808. AGAVE_API_JSAPI2_SECURITY->SetBypass(szBuffer, bypassEnabled);
  809. }
  810. }
  811. }
  812. if (NULL != editor)
  813. {
  814. editor->SetFlags(SVCF_VALIDATED, SVCF_VALIDATED | SVCF_VERSIONCHECK);
  815. if (NULL != target)
  816. {
  817. UINT targetFlags;
  818. if (FAILED(target->GetFlags(&targetFlags)))
  819. targetFlags = 0;
  820. if ((targetFlags & ~ifc_omservice::RuntimeFlagsMask) == (originalFlags & ~ifc_omservice::RuntimeFlagsMask))
  821. editor->SetModified(0, ifc_omserviceeditor::modifiedFlags);
  822. }
  823. editor->EndUpdate();
  824. editor->Release();
  825. }
  826. if (NULL != target)
  827. {
  828. ifc_omserviceeventmngr *eventManager;
  829. if (SUCCEEDED(target->QueryInterface(IFC_OmServiceEventMngr, (void**)&eventManager)))
  830. {
  831. eventManager->Signal_CommandStateChange(&CMDGROUP_SERVICE, SVCCOMMAND_BLOCKNAV);
  832. eventManager->Release();
  833. }
  834. ServiceHelper_Save(target);
  835. target->Release();
  836. }
  837. }
  838. HRESULT ServiceHelper_BeginVersionCheck(ifc_omservice *service)
  839. {
  840. if (NULL == service)
  841. return E_INVALIDARG;
  842. if (NULL == serviceHelper)
  843. return E_UNEXPECTED;
  844. HRESULT hr = S_OK;
  845. ServiceHelper_Lock();
  846. size_t index = serviceHelper->versionChecks.size();
  847. while(index--)
  848. {
  849. ifc_omstorageasync *async = serviceHelper->versionChecks[index];
  850. UINT serviceId;
  851. if (SUCCEEDED(async->GetData((void**)&serviceId)))
  852. {
  853. if (serviceId == service->GetId())
  854. {
  855. hr = S_FALSE;
  856. }
  857. }
  858. }
  859. if (S_OK == hr)
  860. {
  861. ifc_omstorage *storage;
  862. HRESULT hr = ServiceHelper_QueryWebStorage(&storage);
  863. if (SUCCEEDED(hr))
  864. {
  865. WCHAR szAddress[2048] = {0};
  866. hr = ServiceHelper_GetDetailsUrl(szAddress, ARRAYSIZE(szAddress), service, TRUE);
  867. if (SUCCEEDED(hr))
  868. {
  869. ServiceHost *serviceHost;
  870. if (FAILED(ServiceHost::GetCachedInstance(&serviceHost)))
  871. serviceHost = NULL;
  872. ifc_omserviceeditor *editor;
  873. if (FAILED(service->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
  874. editor = NULL;
  875. else
  876. {
  877. editor->BeginUpdate();
  878. editor->SetFlags(SVCF_VERSIONCHECK, SVCF_VERSIONCHECK);
  879. }
  880. service->AddRef();
  881. serviceHelper->versionChecks.push_back(NULL);
  882. //ifc_omstorageasync **pAsync = serviceHelper->versionChecks.end() - 1;
  883. ifc_omstorageasync** pAsync = (serviceHelper->versionChecks.size() ?
  884. &serviceHelper->versionChecks.at(serviceHelper->versionChecks.size() - 1) : 0);
  885. hr = storage->BeginLoad(szAddress, serviceHost, ServiceHelper_VersionCheckComplete, service, pAsync);
  886. if (FAILED(hr))
  887. {
  888. service->Release();
  889. serviceHelper->versionChecks.pop_back();
  890. if (NULL != editor)
  891. editor->SetFlags(0, SVCF_VERSIONCHECK);
  892. }
  893. if (NULL != serviceHost)
  894. serviceHost->Release();
  895. if (NULL != editor)
  896. {
  897. editor->EndUpdate();
  898. editor->Release();
  899. }
  900. }
  901. storage->Release();
  902. }
  903. }
  904. ServiceHelper_Unlock();
  905. return hr;
  906. }
  907. static void CALLBACK ServiceHelper_UpdateOperationInfoApc(ULONG_PTR param)
  908. {
  909. HWND hBrowser = (HWND)param;
  910. if (NULL != hBrowser && IsWindow(hBrowser))
  911. {
  912. ServiceHelper_UpdateOperationInfo(hBrowser);
  913. }
  914. }
  915. HRESULT ServiceHelper_UpdateOperationInfo(HWND hBrowser)
  916. {
  917. if (NULL == hBrowser)
  918. return E_INVALIDARG;
  919. DWORD currentTID = GetCurrentThreadId();
  920. DWORD winampTID = GetWindowThreadProcessId(Plugin_GetWinamp(), NULL);
  921. if (NULL != winampTID && winampTID != currentTID)
  922. {
  923. HRESULT hr;
  924. if (NULL != OMUTILITY)
  925. hr = OMUTILITY->PostMainThreadCallback(ServiceHelper_UpdateOperationInfoApc, (ULONG_PTR)hBrowser);
  926. else
  927. hr = E_FAIL;
  928. return hr;
  929. }
  930. ifc_omservice *service;
  931. if (FALSE == BrowserControl_GetService(hBrowser, &service))
  932. return E_FAIL;
  933. UINT flags;
  934. if (SUCCEEDED(service->GetFlags(&flags)))
  935. {
  936. WCHAR szText[128] = {0}, szTitle[128] = {0};
  937. OPERATIONINFO operation = {0};
  938. operation.cbSize = sizeof(operation);
  939. operation.mask = NBCOM_FLAGS;
  940. if (0 == (SVCF_VERSIONCHECK & flags))
  941. {
  942. operation.flags = NBCOF_HIDEWIDGET;
  943. }
  944. else
  945. {
  946. operation.mask |= (NBCOM_TITLE | NBCOM_TEXT);
  947. operation.flags = NBCOF_SHOWWIDGET;
  948. WASABI_API_LNGSTRINGW_BUF(IDS_SERVICE_CHECKINGVERSION, szTitle, ARRAYSIZE(szTitle));
  949. operation.title = szTitle;
  950. WASABI_API_LNGSTRINGW_BUF(IDS_PLEASE_WAIT, szText, ARRAYSIZE(szText));
  951. operation.text = szText;
  952. }
  953. BrowserControl_ShowOperation(hBrowser, &operation);
  954. }
  955. service->Release();
  956. return S_OK;
  957. }
  958. static void CALLBACK ServiceHelper_ShowWindowTimer(UINT_PTR eventId, DWORD elapsedMs, ULONG_PTR data)
  959. {
  960. Plugin_KillTimer(eventId);
  961. HWND hTarget = (HWND)data;
  962. if (hTarget == Plugin_GetLibrary())
  963. PostMessage(hTarget, WM_ML_IPC, 0, ML_IPC_ENSURE_VISIBLE);
  964. else
  965. ShowWindow(hTarget, SW_SHOWNORMAL);
  966. }
  967. HRESULT ServiceHelper_ShowService(UINT serviceId, UINT showMode)
  968. {
  969. HRESULT hr;
  970. BOOL serviceFound = FALSE;
  971. ifc_omservice *service;
  972. if (0 != serviceId && ROOTSERVICE_ID != serviceId &&
  973. S_OK == ServiceHelper_Find(serviceId, &service) &&
  974. NULL != service)
  975. {
  976. if (S_OK == ServiceHelper_IsSubscribed(service))
  977. serviceFound = TRUE;
  978. service->Release();
  979. }
  980. WCHAR szBuffer[INTERNET_MAX_URL_LENGTH] = {0};
  981. LPCWSTR pszUrl = NULL;
  982. if (FALSE == serviceFound)
  983. {
  984. if (0 != serviceId && ROOTSERVICE_ID != serviceId &&
  985. SUCCEEDED(StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer),
  986. L"http://client.winamp.com/service/detail/gallery/%u", serviceId)))
  987. {
  988. pszUrl = szBuffer;
  989. }
  990. serviceId = ROOTSERVICE_ID;
  991. }
  992. Navigation *navigation;
  993. hr = Plugin_GetNavigation(&navigation);
  994. if (FAILED(hr)) return hr;
  995. if (SHOWMODE_POPUP == showMode)
  996. {
  997. showMode = SHOWMODE_ENSUREVISIBLE;
  998. if (FALSE != serviceFound)
  999. {
  1000. HNAVITEM hItem = navigation->FindService(serviceId, NULL);
  1001. if (NULL != hItem)
  1002. {
  1003. HWND hPopup;
  1004. hr = navigation->CreatePopup(hItem, &hPopup);
  1005. if (SUCCEEDED(hr))
  1006. {
  1007. Plugin_SetTimer(100, ServiceHelper_ShowWindowTimer, (ULONG_PTR)hPopup);
  1008. showMode = SHOWMODE_POPUP;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. if (SHOWMODE_POPUP != showMode)
  1014. {
  1015. hr = navigation->ShowService(serviceId, pszUrl);
  1016. if (SUCCEEDED(hr))
  1017. {
  1018. if (SHOWMODE_ENSUREVISIBLE == showMode)
  1019. Plugin_SetTimer(100, ServiceHelper_ShowWindowTimer, (ULONG_PTR)Plugin_GetLibrary());
  1020. }
  1021. }
  1022. navigation->Release();
  1023. return hr;
  1024. }