1
0

JSAPI2_Application.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "JSAPI2_Application.h"
  2. #include "main.h"
  3. #include "api.h"
  4. #include "language.h"
  5. #include "JSAPI.h"
  6. #include "JSAPI2_Security.h"
  7. JSAPI2::ApplicationAPI::ApplicationAPI(const wchar_t *_key, JSAPI::ifc_info *_info)
  8. {
  9. info = _info;
  10. key = _key;
  11. refCount = 1;
  12. }
  13. #define DISP_TABLE \
  14. CHECK_ID(LaunchURL)\
  15. CHECK_ID(version)\
  16. CHECK_ID(versionstring)\
  17. CHECK_ID(language)\
  18. CHECK_ID(languagepack)\
  19. CHECK_ID(settingspath)\
  20. #define CHECK_ID(str) JSAPI_DISP_ENUMIFY(str),
  21. enum {
  22. DISP_TABLE
  23. };
  24. #undef CHECK_ID
  25. #define CHECK_ID(str)\
  26. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, rgszNames[i], -1, L## #str, -1))\
  27. { rgdispid[i] = JSAPI_DISP_ENUMIFY(str); continue; }
  28. HRESULT JSAPI2::ApplicationAPI::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  29. {
  30. bool unknowns = false;
  31. for (unsigned int i = 0;i != cNames;i++)
  32. {
  33. DISP_TABLE
  34. rgdispid[i] = DISPID_UNKNOWN;
  35. unknowns = true;
  36. }
  37. if (unknowns)
  38. return DISP_E_UNKNOWNNAME;
  39. else
  40. return S_OK;
  41. }
  42. HRESULT JSAPI2::ApplicationAPI::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  43. {
  44. return E_NOTIMPL;
  45. }
  46. HRESULT JSAPI2::ApplicationAPI::GetTypeInfoCount(unsigned int FAR * pctinfo)
  47. {
  48. return E_NOTIMPL;
  49. }
  50. void CALLBACK OpenURLAPC(ULONG_PTR param);
  51. HRESULT JSAPI2::ApplicationAPI::LaunchURL(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  52. {
  53. JSAPI_VERIFY_METHOD(wFlags);
  54. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 2);
  55. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  56. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 2, VT_BOOL, puArgErr);
  57. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  58. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  59. if (security.GetActionAuthorization(L"application", L"launchurl", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  60. {
  61. const wchar_t *url = JSAPI_PARAM(pdispparams, 1).bstrVal;
  62. wchar_t scheme[16]=L"";
  63. DWORD size = 16;
  64. // make sure it's an HTTP url
  65. if (PathIsURLW(url) && !UrlIsFileUrlW(url) && UrlGetPartW(url, scheme, &size, URL_PART_SCHEME, 0) == S_OK
  66. && (!_wcsicmp(scheme, L"http") || !_wcsicmp(scheme, L"https")))
  67. {
  68. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  69. if (JSAPI_PARAM_OPTIONAL(pdispparams, 2, boolVal, VARIANT_FALSE) == VARIANT_TRUE)
  70. ShellExecuteW(NULL, L"open", url, NULL, L".", 0);
  71. else
  72. QueueUserAPC(OpenURLAPC, hMainThread, (ULONG_PTR)_wcsdup(url));
  73. }
  74. }
  75. return S_OK;
  76. }
  77. HRESULT JSAPI2::ApplicationAPI::version(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  78. {
  79. if (wFlags & DISPATCH_PROPERTYGET)
  80. {
  81. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  82. JSAPI_SET_RESULT(pvarResult, lVal, ((APP_VERSION_NUM&0xf000)>>12) * 100 + ((APP_VERSION_NUM&0xf0)>>4) * 10 + (APP_VERSION_NUM&0xf));
  83. return S_OK;
  84. }
  85. else
  86. return DISP_E_MEMBERNOTFOUND;
  87. }
  88. #define WIDEN2(x) L ## x
  89. #define WIDEN(x) WIDEN2(x)
  90. HRESULT JSAPI2::ApplicationAPI::versionstring(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  91. {
  92. if (wFlags & DISPATCH_PROPERTYGET)
  93. {
  94. VariantInit(pvarResult);
  95. V_VT(pvarResult) = VT_BSTR;
  96. V_BSTR(pvarResult) = SysAllocString(WIDEN(APP_VERSION));
  97. return S_OK;
  98. }
  99. else
  100. return DISP_E_MEMBERNOTFOUND;
  101. }
  102. HRESULT JSAPI2::ApplicationAPI::language(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  103. {
  104. if (wFlags & DISPATCH_PROPERTYGET)
  105. {
  106. VariantInit(pvarResult);
  107. V_VT(pvarResult) = VT_BSTR;
  108. V_BSTR(pvarResult) = SysAllocString(WASABI_API_LNG->GetLanguageIdentifier(LANG_LANG_CODE));
  109. return S_OK;
  110. }
  111. else
  112. return DISP_E_MEMBERNOTFOUND;
  113. }
  114. HRESULT JSAPI2::ApplicationAPI::languagepack(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  115. {
  116. if (wFlags & DISPATCH_PROPERTYGET)
  117. {
  118. VariantInit(pvarResult);
  119. V_VT(pvarResult) = VT_BSTR;
  120. V_BSTR(pvarResult) = SysAllocString(WASABI_API_LNG->GetLanguageIdentifier(LANG_IDENT_STR));
  121. return S_OK;
  122. }
  123. else
  124. return DISP_E_MEMBERNOTFOUND;
  125. }
  126. HRESULT JSAPI2::ApplicationAPI::settingspath(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  127. {
  128. if (wFlags & DISPATCH_PROPERTYGET)
  129. {
  130. VariantInit(pvarResult);
  131. V_VT(pvarResult) = VT_BSTR;
  132. V_BSTR(pvarResult) = SysAllocString(WASABI_API_APP->path_getUserSettingsPath());
  133. return S_OK;
  134. }
  135. else
  136. return DISP_E_MEMBERNOTFOUND;
  137. }
  138. #undef CHECK_ID
  139. #define CHECK_ID(str) case JSAPI_DISP_ENUMIFY(str): return str(wFlags, pdispparams, pvarResult, puArgErr);
  140. HRESULT JSAPI2::ApplicationAPI::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  141. {
  142. switch (dispid)
  143. {
  144. DISP_TABLE
  145. }
  146. return DISP_E_MEMBERNOTFOUND;
  147. }
  148. STDMETHODIMP JSAPI2::ApplicationAPI::QueryInterface(REFIID riid, PVOID *ppvObject)
  149. {
  150. if (!ppvObject)
  151. return E_POINTER;
  152. else if (IsEqualIID(riid, IID_IDispatch))
  153. *ppvObject = (IDispatch *)this;
  154. else if (IsEqualIID(riid, IID_IUnknown))
  155. *ppvObject = this;
  156. else
  157. {
  158. *ppvObject = NULL;
  159. return E_NOINTERFACE;
  160. }
  161. AddRef();
  162. return S_OK;
  163. }
  164. ULONG JSAPI2::ApplicationAPI::AddRef(void)
  165. {
  166. return InterlockedIncrement(&refCount);
  167. }
  168. ULONG JSAPI2::ApplicationAPI::Release(void)
  169. {
  170. LONG lRef = InterlockedDecrement(&refCount);
  171. if (lRef == 0) delete this;
  172. return lRef;
  173. }