1
0

xuibrowser.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include <precomp.h>
  2. #include "xuibrowser.h"
  3. const wchar_t browserXuiObjectStr[] = L"Browser"; // This is the xml tag
  4. char browserXuiSvcName[] = "Browser xui object"; // this is the name of the xuiservice
  5. XMLParamPair ScriptBrowserWnd::params[] = {
  6. {BROWSER_SETMAINMB, L"MAINMB"},
  7. {BROWSER_SETSCROLLBARS, L"SCROLLBARS"},
  8. {BROWSER_SETTARGETNAME, L"TARGETNAME"},
  9. {BROWSER_SETURL, L"URL"},
  10. };
  11. ScriptBrowserWnd::ScriptBrowserWnd()
  12. {
  13. getScriptObject()->vcpu_setInterface(browserGuid, (void *)static_cast<ScriptBrowserWnd *>(this));
  14. getScriptObject()->vcpu_setClassName(L"Browser"); // this is the script class name
  15. getScriptObject()->vcpu_setController(browserController);
  16. myxuihandle = newXuiHandle();
  17. CreateXMLParameters(myxuihandle);
  18. }
  19. void ScriptBrowserWnd::CreateXMLParameters(int master_handle)
  20. {
  21. //SCRIPTBROWSERWND_PARENT::CreateXMLParameters(master_handle);
  22. int numParams = sizeof(params) / sizeof(params[0]);
  23. hintNumberOfParams(myxuihandle, numParams);
  24. for (int i = 0;i < numParams;i++)
  25. addParam(myxuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  26. }
  27. ScriptBrowserWnd::~ScriptBrowserWnd()
  28. {
  29. if (WASABI_API_MAKI->maki_getObjectAtom(MAIN_BROWSER_ATOM_NAME) == getScriptObject())
  30. WASABI_API_MAKI->maki_setObjectAtom(MAIN_BROWSER_ATOM_NAME, NULL);
  31. }
  32. int ScriptBrowserWnd::setXuiParam(int xuihandle, int xmlattributeid, const wchar_t *xmlattributename, const wchar_t *value)
  33. {
  34. if (xuihandle != myxuihandle)
  35. return SCRIPTBROWSERWND_PARENT::setXuiParam(xuihandle, xmlattributeid, xmlattributename, value);
  36. switch (xmlattributeid)
  37. {
  38. case BROWSER_SETURL:
  39. setUrl(value);
  40. break;
  41. case BROWSER_SETMAINMB:
  42. setMainMB(WTOI(value));
  43. break;
  44. case BROWSER_SETTARGETNAME:
  45. setTargetName(value);
  46. break;
  47. case BROWSER_SETSCROLLBARS:
  48. setScrollbarsFlag(translateScrollbarFlag(value));
  49. break;
  50. default:
  51. return 0;
  52. }
  53. return 1;
  54. }
  55. int ScriptBrowserWnd::translateScrollbarFlag(const wchar_t *scrollbarflag)
  56. {
  57. if (WCSCASEEQLSAFE(scrollbarflag, L"auto")) return MiniBrowser::BROWSER_SCROLLBARS_AUTO;
  58. if (WCSCASEEQLSAFE(scrollbarflag, L"never")) return MiniBrowser::BROWSER_SCROLLBARS_NEVER;
  59. if (WCSCASEEQLSAFE(scrollbarflag, L"always")) return MiniBrowser::BROWSER_SCROLLBARS_ALWAYS;
  60. if (WCSCASEEQLSAFE(scrollbarflag, L"default")) return MiniBrowser::BROWSER_SCROLLBARS_DEFAULT; // as specified by HTML content
  61. return MiniBrowser::BROWSER_SCROLLBARS_AUTO;
  62. }
  63. void ScriptBrowserWnd::onSetVisible(int v)
  64. {
  65. SCRIPTBROWSERWND_PARENT::onSetVisible(v);
  66. if (v && !defurl.isempty() && _wcsicmp(defurl, getCurrentUrl() ? getCurrentUrl() : L""))
  67. navigateUrl(defurl);
  68. }
  69. void ScriptBrowserWnd::setMainMB(int m)
  70. {
  71. if (m)
  72. WASABI_API_MAKI->maki_setObjectAtom(MAIN_BROWSER_ATOM_NAME, getScriptObject());
  73. else
  74. if (WASABI_API_MAKI->maki_getObjectAtom(MAIN_BROWSER_ATOM_NAME) == getScriptObject())
  75. WASABI_API_MAKI->maki_setObjectAtom(MAIN_BROWSER_ATOM_NAME, NULL);
  76. }
  77. void ScriptBrowserWnd::setUrl(const wchar_t *url)
  78. {
  79. defurl = url;
  80. if (isVisible())
  81. {
  82. if (!defurl.isempty())
  83. navigateUrl(defurl);
  84. }
  85. }
  86. void ScriptBrowserWnd::setCancelIEErrorPage(bool cancel)
  87. {
  88. MiniBrowser *browser = getBrowser();
  89. if (browser) browser->minibrowser_setCancelIEErrorPage(cancel);
  90. }
  91. void ScriptBrowserWnd::Scrape()
  92. {
  93. MiniBrowser *browser = getBrowser();
  94. if (browser) browser->minibrowser_scrape();
  95. }
  96. void ScriptBrowserWnd::getDocumentTitle(wchar_t *str, size_t len)
  97. {
  98. MiniBrowser *browser = getBrowser();
  99. if (browser)
  100. browser->minibrowser_getDocumentTitle(str, len);
  101. else
  102. str[0]=0;
  103. }
  104. int ScriptBrowserWnd::navigateUrl(const wchar_t *url)
  105. {
  106. defurl = url;
  107. return SCRIPTBROWSERWND_PARENT::navigateUrl(url);
  108. }
  109. int ScriptBrowserWnd::onBeforeNavigate(const wchar_t *url, int flags, const wchar_t *frame)
  110. {
  111. if (SCRIPTBROWSERWND_PARENT::onBeforeNavigate(url, flags, frame)) return 1;
  112. scriptVar v = BrowserScriptController::browser_onBeforeNavigate(SCRIPT_CALL, getScriptObject(), MAKE_SCRIPT_STRING(url), MAKE_SCRIPT_INT(flags), MAKE_SCRIPT_STRING(frame));
  113. if (v.type != SCRIPT_VOID)
  114. return GET_SCRIPT_BOOLEAN(v);
  115. return 0;
  116. }
  117. void ScriptBrowserWnd::onDocumentComplete(const wchar_t *url)
  118. {
  119. SCRIPTBROWSERWND_PARENT::onDocumentComplete(url);
  120. if (url)
  121. BrowserScriptController::browser_onDocumentComplete(SCRIPT_CALL, getScriptObject(), MAKE_SCRIPT_STRING(url));
  122. }
  123. void ScriptBrowserWnd::onDocumentReady(const wchar_t *url)
  124. {
  125. SCRIPTBROWSERWND_PARENT::onDocumentComplete(url);
  126. if (url)
  127. BrowserScriptController::browser_onDocumentReady(SCRIPT_CALL, getScriptObject(), MAKE_SCRIPT_STRING(url));
  128. }
  129. void ScriptBrowserWnd::onNavigateError(const wchar_t *url, int status)
  130. {
  131. SCRIPTBROWSERWND_PARENT::onNavigateError(url, status);
  132. BrowserScriptController::browser_onNavigateError(SCRIPT_CALL, getScriptObject(), MAKE_SCRIPT_STRING(url), MAKE_SCRIPT_INT(status));
  133. }
  134. void ScriptBrowserWnd::onMediaLink(const wchar_t *url)
  135. {
  136. SCRIPTBROWSERWND_PARENT::onMediaLink(url);
  137. if (url)
  138. BrowserScriptController::browser_onMediaLink(SCRIPT_CALL, getScriptObject(), MAKE_SCRIPT_STRING(url));
  139. }
  140. const wchar_t * ScriptBrowserWnd::messageToMaki(wchar_t* str1, wchar_t* str2, int intval1, int intval2, int intval3)
  141. {
  142. const wchar_t* ret = SCRIPTBROWSERWND_PARENT::messageToMaki(str1, str2, intval1, intval2, intval3);
  143. if (ret) return ret;
  144. scriptVar v = BrowserScriptController::browser_messageToMaki(SCRIPT_CALL, getScriptObject(), MAKE_SCRIPT_STRING(str1), MAKE_SCRIPT_STRING(str2), MAKE_SCRIPT_INT(intval1), MAKE_SCRIPT_INT(intval2), MAKE_SCRIPT_INT(intval3));
  145. if (v.type == SCRIPT_STRING)
  146. return v.data.sdata;
  147. return 0;
  148. }
  149. const wchar_t * ScriptBrowserWnd::messageToJS(const wchar_t* str1, const wchar_t* str2, int i1, int i2, int i3)
  150. {
  151. MiniBrowser *browser = getBrowser();
  152. if (browser) return browser->minibrowser_messageToJS(str1, str2, i1, i2, i3);
  153. return 0;
  154. }
  155. // -----------------------------------------------------------------------
  156. // Script Object
  157. BrowserScriptController _browserController;
  158. BrowserScriptController *browserController = &_browserController;
  159. // -- Functions table -------------------------------------
  160. function_descriptor_struct BrowserScriptController::exportedFunction[] = {
  161. {L"gotoUrl", 1, (void*)BrowserScriptController::browser_navigateUrl },
  162. {L"navigateUrl", 1, (void*)BrowserScriptController::browser_navigateUrl },
  163. {L"back", 0, (void*)BrowserScriptController::browser_back },
  164. {L"forward", 0, (void*)BrowserScriptController::browser_forward },
  165. {L"home", 0, (void*)BrowserScriptController::browser_home},
  166. {L"stop", 0, (void*)BrowserScriptController::browser_stop},
  167. {L"refresh", 0, (void*)BrowserScriptController::browser_refresh},
  168. {L"scrape", 0, (void*)BrowserScriptController::browser_scrape},
  169. {L"setTargetName", 1, (void*)BrowserScriptController::browser_setTargetName},
  170. {L"onBeforeNavigate", 3, (void*)BrowserScriptController::browser_onBeforeNavigate},
  171. {L"onDocumentComplete", 1, (void*)BrowserScriptController::browser_onDocumentComplete},
  172. {L"onDocumentReady", 1, (void*)BrowserScriptController::browser_onDocumentReady},
  173. {L"onNavigateError", 2, (void*)BrowserScriptController::browser_onNavigateError},
  174. {L"onMediaLink", 1, (void*)BrowserScriptController::browser_onMediaLink},
  175. {L"getDocumentTitle", 0, (void*)BrowserScriptController::browser_getDocumentTitle},
  176. {L"setCancelIEErrorPage",1, (void*)BrowserScriptController::browser_setCancelIEErrorPage},
  177. {L"messageToMaki", 5, (void*)BrowserScriptController::browser_messageToMaki},
  178. {L"messageToJS", 5, (void*)BrowserScriptController::browser_messageToJS},
  179. };
  180. ScriptObject *BrowserScriptController::instantiate()
  181. {
  182. ScriptBrowserWnd *sb = new ScriptBrowserWnd;
  183. ASSERT(sb != NULL);
  184. return sb->getScriptObject();
  185. }
  186. void BrowserScriptController::destroy(ScriptObject *o)
  187. {
  188. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd *>(o->vcpu_getInterface(browserGuid));
  189. ASSERT(sb != NULL);
  190. delete sb;
  191. }
  192. void *BrowserScriptController::encapsulate(ScriptObject *o)
  193. {
  194. return NULL; // no encapsulation for browsers yet
  195. }
  196. void BrowserScriptController::deencapsulate(void *o)
  197. {}
  198. int BrowserScriptController::getNumFunctions()
  199. {
  200. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  201. }
  202. const function_descriptor_struct *BrowserScriptController::getExportedFunctions()
  203. {
  204. return exportedFunction;
  205. }
  206. scriptVar BrowserScriptController::browser_navigateUrl(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar url)
  207. {
  208. SCRIPT_FUNCTION_INIT
  209. ASSERT(url.type == SCRIPT_STRING);
  210. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  211. if (sb) sb->navigateUrl(url.data.sdata);
  212. RETURN_SCRIPT_VOID;
  213. }
  214. scriptVar BrowserScriptController::browser_back(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  215. {
  216. SCRIPT_FUNCTION_INIT
  217. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  218. if (sb) sb->back();
  219. RETURN_SCRIPT_VOID;
  220. }
  221. scriptVar BrowserScriptController::browser_forward(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  222. {
  223. SCRIPT_FUNCTION_INIT
  224. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  225. if (sb) sb->forward();
  226. RETURN_SCRIPT_VOID;
  227. }
  228. scriptVar BrowserScriptController::browser_home(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  229. {
  230. SCRIPT_FUNCTION_INIT
  231. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  232. if (sb) sb->home();
  233. RETURN_SCRIPT_VOID;
  234. }
  235. scriptVar BrowserScriptController::browser_refresh(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  236. {
  237. SCRIPT_FUNCTION_INIT
  238. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  239. if (sb) sb->refresh();
  240. RETURN_SCRIPT_VOID;
  241. }
  242. scriptVar BrowserScriptController::browser_stop(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  243. {
  244. SCRIPT_FUNCTION_INIT
  245. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  246. if (sb) sb->stop();
  247. RETURN_SCRIPT_VOID;
  248. }
  249. scriptVar BrowserScriptController::browser_scrape(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  250. {
  251. SCRIPT_FUNCTION_INIT
  252. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  253. if (sb) sb->Scrape();
  254. RETURN_SCRIPT_VOID;
  255. }
  256. scriptVar BrowserScriptController::browser_setCancelIEErrorPage(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar cancel)
  257. {
  258. SCRIPT_FUNCTION_INIT
  259. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  260. if (sb) sb->setCancelIEErrorPage(!!cancel.data.idata);
  261. RETURN_SCRIPT_VOID;
  262. }
  263. scriptVar BrowserScriptController::browser_setTargetName(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar name)
  264. {
  265. SCRIPT_FUNCTION_INIT
  266. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  267. if (sb)
  268. sb->setTargetName(GET_SCRIPT_STRING(name));
  269. RETURN_SCRIPT_VOID;
  270. }
  271. scriptVar BrowserScriptController::browser_onBeforeNavigate(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar url, scriptVar flags, scriptVar framename)
  272. {
  273. SCRIPT_FUNCTION_INIT
  274. PROCESS_HOOKS3(o, browserController, url, flags, framename);
  275. SCRIPT_FUNCTION_CHECKABORTEVENT;
  276. SCRIPT_EXEC_EVENT3(o, url, flags, framename);
  277. }
  278. scriptVar BrowserScriptController::browser_messageToMaki(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar str1, scriptVar str2, scriptVar i1, scriptVar i2, scriptVar i3)
  279. {
  280. SCRIPT_FUNCTION_INIT
  281. PROCESS_HOOKS5(o, browserController, str1, str2, i1, i2, i3);
  282. SCRIPT_FUNCTION_CHECKABORTEVENT;
  283. SCRIPT_EXEC_EVENT5(o, str1, str2, i1, i2, i3);
  284. }
  285. scriptVar BrowserScriptController::browser_messageToJS(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar str1, scriptVar str2, scriptVar i1, scriptVar i2, scriptVar i3)
  286. {
  287. SCRIPT_FUNCTION_INIT
  288. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  289. if (sb)
  290. {
  291. const wchar_t * ret = sb->messageToJS(str1.data.sdata, str2.data.sdata, i1.data.idata, i2.data.idata, i3.data.idata);
  292. if (ret)
  293. return MAKE_SCRIPT_STRING(ret);
  294. }
  295. return MAKE_SCRIPT_STRING(L"");
  296. }
  297. scriptVar BrowserScriptController::browser_onDocumentComplete(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar url)
  298. {
  299. SCRIPT_FUNCTION_INIT
  300. PROCESS_HOOKS1(o, browserController, url);
  301. SCRIPT_FUNCTION_CHECKABORTEVENT;
  302. SCRIPT_EXEC_EVENT1(o, url);
  303. }
  304. scriptVar BrowserScriptController::browser_onDocumentReady(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar url)
  305. {
  306. SCRIPT_FUNCTION_INIT
  307. PROCESS_HOOKS1(o, browserController, url);
  308. SCRIPT_FUNCTION_CHECKABORTEVENT;
  309. SCRIPT_EXEC_EVENT1(o, url);
  310. }
  311. scriptVar BrowserScriptController::browser_onNavigateError(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar url, scriptVar status)
  312. {
  313. SCRIPT_FUNCTION_INIT
  314. PROCESS_HOOKS2(o, browserController, url, status);
  315. SCRIPT_FUNCTION_CHECKABORTEVENT;
  316. SCRIPT_EXEC_EVENT2(o, url, status);
  317. }
  318. scriptVar BrowserScriptController::browser_onMediaLink(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar url)
  319. {
  320. SCRIPT_FUNCTION_INIT
  321. PROCESS_HOOKS1(o, browserController, url);
  322. SCRIPT_FUNCTION_CHECKABORTEVENT;
  323. SCRIPT_EXEC_EVENT1(o, url);
  324. }
  325. static wchar_t docTitle[1024];
  326. /*string*/ scriptVar BrowserScriptController::browser_getDocumentTitle(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  327. SCRIPT_FUNCTION_INIT
  328. ScriptBrowserWnd *sb = static_cast<ScriptBrowserWnd*>(o->vcpu_getInterface(browserGuid));
  329. docTitle[0]=0;
  330. if (sb) sb->getDocumentTitle(docTitle, 1024);
  331. return MAKE_SCRIPT_STRING(docTitle);
  332. }