WebClient.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifdef ATOMIC_PLATFORM_WINDOWS
  23. #include <windows.h>
  24. #undef LoadString
  25. #endif
  26. #include <include/cef_app.h>
  27. #include <include/cef_client.h>
  28. #include <include/cef_browser.h>
  29. #include <include/wrapper/cef_helpers.h>
  30. #include <include/base/cef_bind.h>
  31. #include <include/wrapper/cef_closure_task.h>
  32. #include "include/wrapper/cef_message_router.h"
  33. #include <Atomic/Core/ProcessUtils.h>
  34. #include <Atomic/Core/CoreEvents.h>
  35. #include <Atomic/IO/Log.h>
  36. #include <Atomic/Input/Input.h>
  37. #include <Atomic/Graphics/Graphics.h>
  38. #include "Internal/WebAppBrowser.h"
  39. #include "WebBrowserHost.h"
  40. #include "WebMessageHandler.h"
  41. #include "WebClient.h"
  42. #include "WebKeyboard.h"
  43. #include "WebViewEvents.h"
  44. #include "WebString.h"
  45. #include <SDL/include/SDL.h>
  46. #include <ThirdParty/SDL/include/SDL_syswm.h>
  47. namespace Atomic
  48. {
  49. #ifdef ATOMIC_PLATFORM_OSX
  50. void* GetNSWindowContentView(void* window);
  51. #endif
  52. class WebClientPrivate : public CefClient,
  53. public CefLifeSpanHandler,
  54. public CefLoadHandler,
  55. public CefDisplayHandler,
  56. public CefRequestHandler,
  57. public CefKeyboardHandler
  58. {
  59. friend class WebClient;
  60. public:
  61. WebClientPrivate(WebClient* client)
  62. {
  63. webClient_ = client;
  64. webBrowserHost_ = webClient_->GetSubsystem<WebBrowserHost>();
  65. CefMessageRouterConfig config;
  66. config.js_query_function = "atomicQuery";
  67. config.js_cancel_function = "atomicQueryCancel";
  68. browserSideRouter_ = CefMessageRouterBrowserSide::Create(config);
  69. }
  70. virtual ~WebClientPrivate()
  71. {
  72. }
  73. CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE
  74. {
  75. if (webClient_->renderHandler_.Null())
  76. return nullptr;
  77. return webClient_->renderHandler_->GetCEFRenderHandler();
  78. }
  79. CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
  80. {
  81. return this;
  82. }
  83. CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE
  84. {
  85. return this;
  86. }
  87. CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE
  88. {
  89. return this;
  90. }
  91. CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE
  92. {
  93. return this;
  94. }
  95. CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE
  96. {
  97. return this;
  98. }
  99. // CefKeyboardHandler
  100. virtual bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
  101. const CefKeyEvent& event,
  102. CefEventHandle os_event,
  103. bool* is_keyboard_shortcut) OVERRIDE
  104. {
  105. return false;
  106. }
  107. // CefRequestHandler methods
  108. void OnRenderViewReady(CefRefPtr<CefBrowser> browser) OVERRIDE
  109. {
  110. }
  111. bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
  112. CefRefPtr<CefFrame> frame,
  113. CefRefPtr<CefRequest> request,
  114. bool is_redirect) OVERRIDE
  115. {
  116. CEF_REQUIRE_UI_THREAD();
  117. browserSideRouter_->OnBeforeBrowse(browser, frame);
  118. return false;
  119. }
  120. bool OnProcessMessageReceived(
  121. CefRefPtr<CefBrowser> browser,
  122. CefProcessId source_process,
  123. CefRefPtr<CefProcessMessage> message) OVERRIDE
  124. {
  125. CEF_REQUIRE_UI_THREAD();
  126. const CefString& message_name = message->GetName();
  127. if (message_name == "atomic_eval_javascript_result")
  128. {
  129. if (webClient_.Null())
  130. return false;
  131. unsigned evalID = (unsigned) message->GetArgumentList()->GetInt(0);
  132. bool result = message->GetArgumentList()->GetBool(1);
  133. String value;
  134. ConvertCEFString(message->GetArgumentList()->GetString(2), value);
  135. webClient_->EvalJavaScriptResult(evalID, result, value);
  136. return true;
  137. }
  138. if (browserSideRouter_->OnProcessMessageReceived(browser, source_process, message))
  139. {
  140. return true;
  141. }
  142. return false;
  143. }
  144. void OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
  145. TerminationStatus status) OVERRIDE
  146. {
  147. CEF_REQUIRE_UI_THREAD();
  148. browserSideRouter_->OnRenderProcessTerminated(browser);
  149. }
  150. // CefLoadHandler
  151. void OnLoadStart(CefRefPtr<CefBrowser> browser,
  152. CefRefPtr<CefFrame> frame) OVERRIDE
  153. {
  154. if (webClient_.Null() || !frame->IsMain())
  155. return;
  156. VariantMap eventData;
  157. eventData[WebViewLoadStart::P_CLIENT] = webClient_;
  158. CefString cefURL = frame->GetURL();
  159. String url;
  160. ConvertCEFString(cefURL, url);
  161. eventData[WebViewLoadStart::P_URL] = url;
  162. webClient_->SendEvent(E_WEBVIEWLOADSTART, eventData);
  163. }
  164. void OnLoadEnd(CefRefPtr<CefBrowser> browser,
  165. CefRefPtr<CefFrame> frame,
  166. int httpStatusCode) OVERRIDE
  167. {
  168. if (webClient_.Null() || !frame->IsMain())
  169. return;
  170. VariantMap eventData;
  171. eventData[WebViewLoadEnd::P_CLIENT] = webClient_;
  172. CefString cefURL = frame->GetURL();
  173. String url;
  174. ConvertCEFString(cefURL, url);
  175. eventData[WebViewLoadEnd::P_URL] = url;
  176. webClient_->SendEvent(E_WEBVIEWLOADEND, eventData);
  177. }
  178. void OnLoadError(CefRefPtr<CefBrowser> browser,
  179. CefRefPtr<CefFrame> frame,
  180. ErrorCode errorCode,
  181. const CefString& errorText,
  182. const CefString& failedUrl) OVERRIDE
  183. {
  184. if (webClient_.Null())
  185. return;
  186. }
  187. void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
  188. bool isLoading,
  189. bool canGoBack,
  190. bool canGoForward) OVERRIDE
  191. {
  192. if (webClient_.Null())
  193. return;
  194. VariantMap eventData;
  195. eventData[WebViewLoadStateChange::P_CLIENT] = webClient_;
  196. eventData[WebViewLoadStateChange::P_LOADING] = isLoading;
  197. eventData[WebViewLoadStateChange::P_CANGOBACK] = canGoBack;
  198. eventData[WebViewLoadStateChange::P_CANGOFORWARD] = canGoForward;
  199. webClient_->SendEvent(E_WEBVIEWLOADSTATECHANGE, eventData);
  200. }
  201. // CefDisplayHandler
  202. void OnAddressChange(CefRefPtr<CefBrowser> browser,
  203. CefRefPtr<CefFrame> frame,
  204. const CefString& url) OVERRIDE
  205. {
  206. if (webClient_.Null() || !frame->IsMain())
  207. return;
  208. VariantMap eventData;
  209. eventData[WebViewAddressChange::P_CLIENT] = webClient_;
  210. String _url;
  211. ConvertCEFString(url, _url);
  212. eventData[WebViewAddressChange::P_URL] = _url;
  213. webClient_->SendEvent(E_WEBVIEWADDRESSCHANGE, eventData);
  214. }
  215. void OnTitleChange(CefRefPtr<CefBrowser> browser,
  216. const CefString& title) OVERRIDE
  217. {
  218. if (webClient_.Null())
  219. return;
  220. VariantMap eventData;
  221. eventData[WebViewTitleChange::P_CLIENT] = webClient_;
  222. String _title;
  223. ConvertCEFString(title, _title);
  224. eventData[WebViewTitleChange::P_TITLE] = _title;
  225. webClient_->SendEvent(E_WEBVIEWTITLECHANGE, eventData);
  226. }
  227. ///
  228. // Called to display a console message. Return true to stop the message from
  229. // being output to the console.
  230. ///
  231. /*--cef(optional_param=message,optional_param=source)--*/
  232. virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
  233. const CefString& message,
  234. const CefString& source,
  235. int line) OVERRIDE
  236. {
  237. if (webClient_.Null())
  238. return false;
  239. String _message;
  240. ConvertCEFString(message, _message);
  241. String _source;
  242. ConvertCEFString(source, _source);
  243. LOGINFOF("WebViewJS: %s (%s:%i)", _message.CString(), _source.CString(), line);
  244. return false;
  245. }
  246. bool CreateBrowser(const String& initialURL, int width, int height)
  247. {
  248. if (browser_.get())
  249. {
  250. LOGERROR("WebClient::CreateBrowser - Browser already created");
  251. return false;
  252. }
  253. if (webClient_->renderHandler_.Null())
  254. {
  255. LOGERROR("WebClient::CreateBrowser - No render handler specified");
  256. return false;
  257. }
  258. CefWindowInfo windowInfo;
  259. CefBrowserSettings browserSettings;
  260. browserSettings.webgl = STATE_ENABLED;
  261. browserSettings.file_access_from_file_urls = STATE_ENABLED;
  262. browserSettings.universal_access_from_file_urls = STATE_ENABLED;
  263. windowInfo.width = width;
  264. windowInfo.height = height;
  265. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  266. if (graphics)
  267. {
  268. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  269. SDL_SysWMinfo info;
  270. SDL_VERSION(&info.version);
  271. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  272. {
  273. #ifdef ATOMIC_PLATFORM_OSX
  274. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  275. windowInfo.SetAsWindowless(view, false);
  276. #endif
  277. #ifdef ATOMIC_PLATFORM_WINDOWS
  278. windowInfo.SetAsWindowless(info.info.win.window, false);
  279. #endif
  280. }
  281. }
  282. else
  283. {
  284. #ifndef ATOMIC_PLATFORM_LINUX
  285. // headless
  286. windowInfo.SetAsWindowless(nullptr, false);
  287. #endif
  288. }
  289. // TODO: There seems to be a CEF bug when loading a string into a browser
  290. // which was created with an empty URL, this workaround gets things going
  291. // NOTE: I also tried loading the string, delaying 5 seconds and still won't
  292. // load a string until a URL has been passed into the view
  293. String _initialURL = initialLoadString_.Length() ? "x" : initialURL;
  294. webClient_->renderHandler_->SetSize(width, height);
  295. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  296. _initialURL.CString(), browserSettings, nullptr);
  297. if (!browser.get())
  298. return false;
  299. browser_ = browser;
  300. if (initialLoadString_.Length())
  301. {
  302. webClient_->LoadString(initialLoadString_, initialLoadStringURL_);
  303. }
  304. return true;
  305. }
  306. // CefLifeSpanHandler methods:
  307. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  308. {
  309. CEF_REQUIRE_UI_THREAD();
  310. }
  311. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  312. {
  313. return false;
  314. }
  315. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  316. {
  317. CEF_REQUIRE_UI_THREAD();
  318. browser_ = nullptr;
  319. }
  320. void CloseBrowser(bool force_close)
  321. {
  322. if (!CefCurrentlyOn(TID_UI))
  323. {
  324. // Execute on the UI thread.
  325. CefPostTask(TID_UI,
  326. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  327. return;
  328. }
  329. if (!browser_.get())
  330. return;
  331. browser_->GetHost()->CloseBrowser(force_close);
  332. }
  333. void SetInitialLoadString(const String& loadString, const String& url)
  334. {
  335. initialLoadString_ = loadString;
  336. initialLoadStringURL_ = url;
  337. }
  338. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  339. private:
  340. String initialLoadString_;
  341. String initialLoadStringURL_;
  342. CefRefPtr<CefBrowser> browser_;
  343. WeakPtr<WebBrowserHost> webBrowserHost_;
  344. WeakPtr<WebClient> webClient_;
  345. CefRefPtr<CefMessageRouterBrowserSide> browserSideRouter_;
  346. };
  347. WebClient::WebClient(Context* context) : Object(context)
  348. {
  349. d_ = new WebClientPrivate(this);
  350. SubscribeToEvent(E_WEBVIEWGLOBALPROPERTIESCHANGED, HANDLER(WebClient, HandleWebViewGlobalPropertiesChanged));
  351. }
  352. WebClient::~WebClient()
  353. {
  354. if (d_)
  355. {
  356. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Begin();
  357. while (itr != messageHandlers_.End())
  358. {
  359. CefMessageRouterBrowserSide::Handler* handler = static_cast<CefMessageRouterBrowserSide::Handler*>((*itr)->GetCefHandler());
  360. d_->browserSideRouter_->RemoveHandler(handler);
  361. itr++;
  362. }
  363. d_->CloseBrowser(true);
  364. }
  365. renderHandler_ = 0;
  366. //d_->Release();
  367. }
  368. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier, int clickCount) const
  369. {
  370. if (!d_->browser_.get())
  371. return;
  372. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  373. CefMouseEvent mevent;
  374. mevent.x = x;
  375. mevent.y = y;
  376. mevent.modifiers = 0;
  377. //MBT_LEFT = 0,
  378. //MBT_MIDDLE,
  379. //MBT_RIGHT,
  380. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, clickCount);
  381. }
  382. void WebClient::SendMousePressEvent(int x, int y, unsigned button, unsigned modifier, int clickCount) const
  383. {
  384. SendMouseClickEvent(x, y, button, false, modifier, clickCount);
  385. SendMouseClickEvent(x, y, button, true, modifier, clickCount);
  386. }
  387. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  388. {
  389. if (!d_->browser_.get())
  390. return;
  391. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  392. CefMouseEvent mevent;
  393. mevent.x = x;
  394. mevent.y = y;
  395. mevent.modifiers = 0;
  396. Input* input = GetSubsystem<Input>();
  397. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  398. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  399. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  400. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  401. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  402. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  403. host->SendMouseMoveEvent(mevent, mouseLeave);
  404. }
  405. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  406. {
  407. if (!d_->browser_.get())
  408. return;
  409. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  410. CefMouseEvent mevent;
  411. mevent.x = x;
  412. mevent.y = y;
  413. mevent.modifiers = 0;
  414. deltaY = -deltaY * 5;
  415. #ifndef ATOMIC_PLATFORM_OSX
  416. deltaY *= 5;
  417. #endif
  418. host->SendMouseWheelEvent(mevent, deltaX, deltaY);
  419. }
  420. /*
  421. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  422. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  423. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  424. EVENTFLAG_ALT_DOWN = 1 << 3,
  425. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  426. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  427. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  428. // Mac OS-X command key.
  429. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  430. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  431. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  432. EVENTFLAG_IS_LEFT = 1 << 10,
  433. EVENTFLAG_IS_RIGHT = 1 << 11,
  434. } cef_event_flags_t;
  435. */
  436. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  437. {
  438. if (!d_->browser_.get())
  439. return;
  440. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  441. CefKeyEvent keyEvent;
  442. if (!ConvertKeyEvent(GetSubsystem<Input>(), eventType, eventData, keyEvent))
  443. return;
  444. host->SendKeyEvent(keyEvent);
  445. #ifdef ATOMIC_PLATFORM_WINDOWS
  446. // RETURN KEY: We need to send both keydown and char for return key
  447. // this allows it to be used both to confirm entry on popups,
  448. // while also being used for text input
  449. if (keyEvent.windows_key_code == 13)
  450. {
  451. keyEvent.type = KEYEVENT_CHAR;
  452. host->SendKeyEvent(keyEvent);
  453. }
  454. #endif
  455. #ifdef ATOMIC_PLATFORM_OSX
  456. // RETURN KEY: We need to send both keydown and char for return key
  457. // this allows it to be used both to confirm entry on popups,
  458. // while also being used for text input
  459. if (keyEvent.native_key_code == 36)
  460. {
  461. keyEvent.type = KEYEVENT_CHAR;
  462. host->SendKeyEvent(keyEvent);
  463. }
  464. // Send an empty key event on OSX, which seems to fix
  465. // keyboard problems on OSX with cefclient
  466. // ./cefclient --off-screen-rendering-enabled
  467. // return does not work at all on cef client with offscreen
  468. // bad interaction with arrow keys (for example here, after
  469. // hitting arrow keys, return/text takes a couple presses to register
  470. memset((void*)&keyEvent, 0, sizeof(keyEvent));
  471. keyEvent.type = KEYEVENT_KEYDOWN;
  472. keyEvent.modifiers = 0;
  473. keyEvent.native_key_code = 0;
  474. host->SendKeyEvent(keyEvent);
  475. #endif
  476. }
  477. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  478. {
  479. if (!d_->browser_.get())
  480. return;
  481. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  482. CefKeyEvent keyEvent;
  483. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  484. return;
  485. host->SendKeyEvent(keyEvent);
  486. }
  487. void WebClient::SendFocusEvent(bool focus)
  488. {
  489. if (!d_->browser_.get())
  490. return;
  491. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  492. host->SendFocusEvent(focus);
  493. }
  494. // Javascript
  495. void WebClient::ExecuteJavaScript(const String& script)
  496. {
  497. if (!d_->browser_.get())
  498. return;
  499. d_->browser_->GetMainFrame()->ExecuteJavaScript(CefString(script.CString()), "", 0);
  500. }
  501. void WebClient::EvalJavaScript(unsigned evalID, const String& script)
  502. {
  503. if (!d_->browser_.get())
  504. return;
  505. // Create the message object.
  506. CefRefPtr<CefProcessMessage> msg= CefProcessMessage::Create("atomic_eval_javascript");
  507. // Retrieve the argument list object.
  508. CefRefPtr<CefListValue> args = msg->GetArgumentList();
  509. // Populate the argument values.
  510. args->SetInt(0, (int) evalID);
  511. args->SetString(1, CefString(script.CString()));
  512. // Send the process message to the render process.
  513. // Use PID_BROWSER instead when sending a message to the browser process.
  514. d_->browser_->SendProcessMessage(PID_RENDERER, msg);
  515. }
  516. void WebClient::EvalJavaScriptResult(unsigned evalID, bool result, const String& value)
  517. {
  518. using namespace WebViewJSEvalResult;
  519. VariantMap eventData;
  520. eventData[P_CLIENT] = this;
  521. eventData[P_EVALID] = evalID;
  522. eventData[P_RESULT] = result;
  523. eventData[P_VALUE] = value;
  524. SendEvent(E_WEBVIEWJSEVALRESULT, eventData);
  525. }
  526. void WebClient::AddMessageHandler(WebMessageHandler* handler, bool first)
  527. {
  528. SharedPtr<WebMessageHandler> _handler(handler);
  529. if (handler->GetWebClient())
  530. {
  531. LOGWARNING("WebClient::AddMessageHandler - message handler already added to another client");
  532. return;
  533. }
  534. if (messageHandlers_.Contains(_handler))
  535. {
  536. LOGWARNING("WebClient::AddMessageHandler - message handler already added to this client");
  537. return;
  538. }
  539. _handler->SetWebClient(this);
  540. messageHandlers_.Push(_handler);
  541. d_->browserSideRouter_->AddHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()), first);
  542. }
  543. void WebClient::RemoveMessageHandler(WebMessageHandler* handler)
  544. {
  545. SharedPtr<WebMessageHandler> _handler(handler);
  546. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Find(_handler);
  547. if (itr == messageHandlers_.End())
  548. {
  549. LOGWARNING("WebClient::RemoveMessageHandler - message handler not found");
  550. return;
  551. }
  552. d_->browserSideRouter_->RemoveHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()));
  553. messageHandlers_.Erase(itr);
  554. }
  555. // Navigation
  556. void WebClient::LoadURL(const String& url)
  557. {
  558. if (!d_->browser_.get())
  559. {
  560. return;
  561. }
  562. CefString _url(url.CString());
  563. d_->browser_->GetMainFrame()->LoadURL(_url);
  564. }
  565. void WebClient::LoadString(const String& source, const String& url)
  566. {
  567. if (!d_->browser_.get())
  568. {
  569. d_->SetInitialLoadString(source, url);
  570. return;
  571. }
  572. // We need to make sure global properties are updated when loading web content from source string
  573. // This is handled differently internally then we requests
  574. UpdateGlobalProperties();
  575. d_->browser_->GetMainFrame()->LoadString(source.CString(), url.CString());
  576. }
  577. void WebClient::GoBack()
  578. {
  579. if (!d_->browser_.get())
  580. return;
  581. d_->browser_->GoBack();
  582. }
  583. void WebClient::GoForward()
  584. {
  585. if (!d_->browser_.get())
  586. return;
  587. d_->browser_->GoForward();
  588. }
  589. bool WebClient::IsLoading()
  590. {
  591. if (!d_->browser_.get())
  592. return false;
  593. return d_->browser_->IsLoading();
  594. }
  595. void WebClient::Reload()
  596. {
  597. if (!d_->browser_.get())
  598. return;
  599. d_->browser_->Reload();
  600. }
  601. void WebClient::ShortcutCut()
  602. {
  603. if (!d_->browser_.get())
  604. return;
  605. d_->browser_->GetFocusedFrame()->Cut();
  606. }
  607. void WebClient::ShortcutCopy()
  608. {
  609. if (!d_->browser_.get())
  610. return;
  611. d_->browser_->GetFocusedFrame()->Copy();
  612. }
  613. void WebClient::ShortcutPaste()
  614. {
  615. if (!d_->browser_.get())
  616. return;
  617. d_->browser_->GetFocusedFrame()->Paste();
  618. }
  619. void WebClient::ShortcutSelectAll()
  620. {
  621. if (!d_->browser_.get())
  622. return;
  623. d_->browser_->GetFocusedFrame()->SelectAll();
  624. }
  625. void WebClient::ShortcutUndo()
  626. {
  627. if (!d_->browser_.get())
  628. return;
  629. d_->browser_->GetFocusedFrame()->Undo();
  630. }
  631. void WebClient::ShortcutRedo()
  632. {
  633. if (!d_->browser_.get())
  634. return;
  635. d_->browser_->GetFocusedFrame()->Redo();
  636. }
  637. void WebClient::ShortcutDelete()
  638. {
  639. if (!d_->browser_.get())
  640. return;
  641. d_->browser_->GetFocusedFrame()->Delete();
  642. }
  643. void WebClient::WasResized()
  644. {
  645. if (!d_->browser_.get())
  646. return;
  647. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  648. host->WasResized();;
  649. }
  650. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  651. {
  652. bool result = d_->CreateBrowser(initialURL, width, height);
  653. return result;
  654. }
  655. void WebClient::UpdateGlobalProperties()
  656. {
  657. if (!d_->browser_.get())
  658. return;
  659. CefRefPtr<CefDictionaryValue> globalProps;
  660. if (!WebAppBrowser::CreateGlobalProperties(globalProps))
  661. return;
  662. // Create the message object.
  663. CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("atomic_set_globalproperties");
  664. // Retrieve the argument list object.
  665. CefRefPtr<CefListValue> args = msg->GetArgumentList();
  666. args->SetDictionary(0, globalProps);
  667. // Send the process message to the render process.
  668. if (!d_->browser_->SendProcessMessage(PID_RENDERER, msg))
  669. {
  670. LOGERROR("WebClient::UpdateGlobalProperties - Failed to send message");
  671. }
  672. }
  673. void WebClient::HandleWebViewGlobalPropertiesChanged(StringHash eventType, VariantMap& eventData)
  674. {
  675. UpdateGlobalProperties();
  676. }
  677. void WebClient::SetSize(int width, int height)
  678. {
  679. if (renderHandler_.Null())
  680. return;
  681. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  682. return;
  683. renderHandler_->SetSize(width, height);
  684. WasResized();
  685. }
  686. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  687. {
  688. handler->SetWebClient(this);
  689. renderHandler_ = handler;
  690. }
  691. CefClient* WebClient::GetCefClient()
  692. {
  693. return d_;
  694. }
  695. }