The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

732 lines
22KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class CommandReceiver
  18. {
  19. public:
  20. struct Responder
  21. {
  22. virtual ~Responder() {}
  23. virtual void handleCommand (const String& cmd, const var& param) = 0;
  24. virtual void receiverHadError() = 0;
  25. };
  26. CommandReceiver (Responder* responderToUse, int inputChannelToUse)
  27. : responder (responderToUse), inChannel (inputChannelToUse)
  28. {
  29. setBlocking (inChannel, false);
  30. }
  31. static void setBlocking (int fd, bool shouldBlock)
  32. {
  33. int flags = fcntl (fd, F_GETFL);
  34. fcntl (fd, F_SETFL, (shouldBlock ? (flags & ~O_NONBLOCK)
  35. : (flags | O_NONBLOCK)));
  36. }
  37. int getFd() const { return inChannel; }
  38. void tryNextRead()
  39. {
  40. while (true)
  41. {
  42. size_t len = (receivingLength ? sizeof (size_t) : bufferLength.len);
  43. if (! receivingLength)
  44. buffer.realloc (len);
  45. char* dst = (receivingLength ? bufferLength.data : buffer.getData());
  46. ssize_t actual = read (inChannel, &dst[pos], static_cast<size_t> (len - pos));
  47. if (actual < 0)
  48. {
  49. if (errno == EINTR)
  50. continue;
  51. break;
  52. }
  53. pos += static_cast<size_t> (actual);
  54. if (pos == len)
  55. {
  56. pos = 0;
  57. if (! receivingLength)
  58. parseJSON (String (buffer.getData(), bufferLength.len));
  59. receivingLength = (! receivingLength);
  60. }
  61. }
  62. if (errno != EAGAIN && errno != EWOULDBLOCK && responder != nullptr)
  63. responder->receiverHadError();
  64. }
  65. static void sendCommand (int outChannel, const String& cmd, const var& params)
  66. {
  67. DynamicObject::Ptr obj = new DynamicObject;
  68. obj->setProperty (getCmdIdentifier(), cmd);
  69. if (! params.isVoid())
  70. obj->setProperty (getParamIdentifier(), params);
  71. String json (JSON::toString (var (obj)));
  72. size_t jsonLength = static_cast<size_t> (json.length());
  73. size_t len = sizeof (size_t) + jsonLength;
  74. HeapBlock<char> buffer (len);
  75. char* dst = buffer.getData();
  76. memcpy (dst, &jsonLength, sizeof (size_t));
  77. dst += sizeof (size_t);
  78. memcpy (dst, json.toRawUTF8(), jsonLength);
  79. ssize_t ret;
  80. do
  81. {
  82. ret = write (outChannel, buffer.getData(), len);
  83. } while (ret == -1 && errno == EINTR);
  84. }
  85. private:
  86. void parseJSON (const String& json)
  87. {
  88. var object (JSON::fromString (json));
  89. if (! object.isVoid())
  90. {
  91. String cmd (object.getProperty (getCmdIdentifier(), var()).toString());
  92. var params (object.getProperty (getParamIdentifier(), var()));
  93. if (responder != nullptr)
  94. responder->handleCommand (cmd, params);
  95. }
  96. }
  97. static Identifier getCmdIdentifier() { static Identifier Id ("cmd"); return Id; }
  98. static Identifier getParamIdentifier() { static Identifier Id ("params"); return Id; }
  99. Responder* responder;
  100. int inChannel;
  101. size_t pos = 0;
  102. bool receivingLength = true;
  103. union { char data [sizeof (size_t)]; size_t len; } bufferLength;
  104. HeapBlock<char> buffer;
  105. };
  106. //==============================================================================
  107. class GtkChildProcess : private CommandReceiver::Responder
  108. {
  109. public:
  110. //==============================================================================
  111. GtkChildProcess (int inChannel, int outChannelToUse)
  112. : outChannel (outChannelToUse), receiver (this, inChannel)
  113. {}
  114. void entry()
  115. {
  116. CommandReceiver::setBlocking (outChannel, true);
  117. gtk_init (nullptr, nullptr);
  118. WebKitSettings* settings = webkit_settings_new();
  119. webkit_settings_set_hardware_acceleration_policy (settings, WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER);
  120. GtkWidget *plug;
  121. plug = gtk_plug_new(0);
  122. GtkWidget* container;
  123. container = gtk_scrolled_window_new (nullptr, nullptr);
  124. GtkWidget* webviewWidget = webkit_web_view_new_with_settings (settings);
  125. webview = WEBKIT_WEB_VIEW (webviewWidget);
  126. gtk_container_add (GTK_CONTAINER (container), webviewWidget);
  127. gtk_container_add (GTK_CONTAINER (plug), container);
  128. webkit_web_view_load_uri (webview, "about:blank");
  129. g_signal_connect (webview, "decide-policy",
  130. G_CALLBACK (decidePolicyCallback), this);
  131. g_signal_connect (webview, "load-changed",
  132. G_CALLBACK (loadChangedCallback), this);
  133. gtk_widget_show_all (plug);
  134. unsigned long wID = (unsigned long) gtk_plug_get_id (GTK_PLUG (plug));
  135. ssize_t ret;
  136. do {
  137. ret = write (outChannel, &wID, sizeof (wID));
  138. } while (ret == -1 && errno == EINTR);
  139. g_unix_fd_add (receiver.getFd(), G_IO_IN, pipeReadyStatic, this);
  140. receiver.tryNextRead();
  141. gtk_main();
  142. }
  143. void goToURL (const var& params)
  144. {
  145. static Identifier urlIdentifier ("url");
  146. String url (params.getProperty (urlIdentifier, var()).toString());
  147. webkit_web_view_load_uri (webview, url.toRawUTF8());
  148. }
  149. void handleDecisionResponse (const var& params)
  150. {
  151. WebKitPolicyDecision* decision
  152. = (WebKitPolicyDecision*) ((int64) params.getProperty ("decision_id", var (0)));
  153. bool allow = params.getProperty ("allow", var (false));
  154. if (decision != nullptr && decisions.contains (decision))
  155. {
  156. if (allow)
  157. webkit_policy_decision_use (decision);
  158. else
  159. webkit_policy_decision_ignore (decision);
  160. decisions.removeAllInstancesOf (decision);
  161. g_object_unref (decision);
  162. }
  163. }
  164. //==============================================================================
  165. void handleCommand (const String& cmd, const var& params) override
  166. {
  167. if (cmd == "quit") quit();
  168. else if (cmd == "goToURL") goToURL (params);
  169. else if (cmd == "goBack") webkit_web_view_go_back (webview);
  170. else if (cmd == "goForward") webkit_web_view_go_forward (webview);
  171. else if (cmd == "refresh") webkit_web_view_reload (webview);
  172. else if (cmd == "stop") webkit_web_view_stop_loading (webview);
  173. else if (cmd == "decision") handleDecisionResponse (params);
  174. }
  175. void receiverHadError() override
  176. {
  177. exit (-1);
  178. }
  179. //==============================================================================
  180. bool pipeReady (gint fd, GIOCondition)
  181. {
  182. if (fd == receiver.getFd())
  183. {
  184. receiver.tryNextRead();
  185. return true;
  186. }
  187. return false;
  188. }
  189. void quit()
  190. {
  191. exit (-1);
  192. }
  193. bool onNavigation (String frameName,
  194. WebKitNavigationAction* action,
  195. WebKitPolicyDecision* decision)
  196. {
  197. if (decision != nullptr && frameName.isEmpty())
  198. {
  199. g_object_ref (decision);
  200. decisions.add (decision);
  201. DynamicObject::Ptr params = new DynamicObject;
  202. params->setProperty ("url", String (webkit_uri_request_get_uri (webkit_navigation_action_get_request (action))));
  203. params->setProperty ("decision_id", (int64) decision);
  204. CommandReceiver::sendCommand (outChannel, "pageAboutToLoad", var (params));
  205. return true;
  206. }
  207. return false;
  208. }
  209. bool onNewWindow (String /*frameName*/,
  210. WebKitNavigationAction* action,
  211. WebKitPolicyDecision* decision)
  212. {
  213. if (decision != nullptr)
  214. {
  215. DynamicObject::Ptr params = new DynamicObject;
  216. params->setProperty ("url", String (webkit_uri_request_get_uri (webkit_navigation_action_get_request (action))));
  217. CommandReceiver::sendCommand (outChannel, "newWindowAttemptingToLoad", var (params));
  218. // never allow new windows
  219. webkit_policy_decision_ignore (decision);
  220. return true;
  221. }
  222. return false;
  223. }
  224. void onLoadChanged (WebKitLoadEvent loadEvent)
  225. {
  226. if (loadEvent == WEBKIT_LOAD_FINISHED)
  227. {
  228. DynamicObject::Ptr params = new DynamicObject;
  229. params->setProperty ("url", String (webkit_web_view_get_uri (webview)));
  230. CommandReceiver::sendCommand (outChannel, "pageFinishedLoading", var (params));
  231. }
  232. }
  233. bool onDecidePolicy (WebKitPolicyDecision* decision,
  234. WebKitPolicyDecisionType decisionType)
  235. {
  236. switch (decisionType)
  237. {
  238. case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
  239. {
  240. WebKitNavigationPolicyDecision* navigationDecision = WEBKIT_NAVIGATION_POLICY_DECISION (decision);
  241. const char* frameName = webkit_navigation_policy_decision_get_frame_name (navigationDecision);
  242. return onNavigation (String (frameName != nullptr ? frameName : ""),
  243. webkit_navigation_policy_decision_get_navigation_action (navigationDecision),
  244. decision);
  245. }
  246. break;
  247. case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
  248. {
  249. WebKitNavigationPolicyDecision* navigationDecision = WEBKIT_NAVIGATION_POLICY_DECISION (decision);
  250. const char* frameName = webkit_navigation_policy_decision_get_frame_name (navigationDecision);
  251. return onNewWindow (String (frameName != nullptr ? frameName : ""),
  252. webkit_navigation_policy_decision_get_navigation_action (navigationDecision),
  253. decision);
  254. }
  255. break;
  256. case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
  257. {
  258. WebKitResponsePolicyDecision *response = WEBKIT_RESPONSE_POLICY_DECISION (decision);
  259. // for now just always allow response requests
  260. ignoreUnused (response);
  261. webkit_policy_decision_use (decision);
  262. return true;
  263. }
  264. break;
  265. default:
  266. break;
  267. }
  268. return false;
  269. }
  270. private:
  271. static gboolean pipeReadyStatic (gint fd, GIOCondition condition, gpointer user)
  272. {
  273. return (reinterpret_cast<GtkChildProcess*> (user)->pipeReady (fd, condition) ? TRUE : FALSE);
  274. }
  275. static gboolean decidePolicyCallback (WebKitWebView*,
  276. WebKitPolicyDecision* decision,
  277. WebKitPolicyDecisionType decisionType,
  278. gpointer user)
  279. {
  280. GtkChildProcess& owner = *reinterpret_cast<GtkChildProcess*> (user);
  281. return (owner.onDecidePolicy (decision, decisionType) ? TRUE : FALSE);
  282. }
  283. static void loadChangedCallback (WebKitWebView*,
  284. WebKitLoadEvent loadEvent,
  285. gpointer user)
  286. {
  287. GtkChildProcess& owner = *reinterpret_cast<GtkChildProcess*> (user);
  288. owner.onLoadChanged (loadEvent);
  289. }
  290. int outChannel;
  291. CommandReceiver receiver;
  292. WebKitWebView* webview = nullptr;
  293. Array<WebKitPolicyDecision*> decisions;
  294. };
  295. //==============================================================================
  296. class WebBrowserComponent::Pimpl : private Thread, private CommandReceiver::Responder
  297. {
  298. public:
  299. Pimpl (WebBrowserComponent& parent)
  300. : Thread ("Webview"), owner (parent)
  301. {}
  302. ~Pimpl()
  303. {
  304. quit();
  305. }
  306. //==============================================================================
  307. void init()
  308. {
  309. launchChild();
  310. int ret = pipe (threadControl);
  311. ignoreUnused (ret);
  312. jassert (ret == 0);
  313. CommandReceiver::setBlocking (inChannel, true);
  314. CommandReceiver::setBlocking (outChannel, true);
  315. CommandReceiver::setBlocking (threadControl[0], false);
  316. CommandReceiver::setBlocking (threadControl[1], true);
  317. unsigned long windowHandle;
  318. ssize_t actual = read (inChannel, &windowHandle, sizeof (windowHandle));
  319. if (actual != sizeof (windowHandle))
  320. {
  321. killChild();
  322. return;
  323. }
  324. receiver = new CommandReceiver (this, inChannel);
  325. startThread();
  326. xembed = new XEmbedComponent (windowHandle);
  327. owner.addAndMakeVisible (xembed);
  328. }
  329. void quit()
  330. {
  331. if (isThreadRunning())
  332. {
  333. signalThreadShouldExit();
  334. char ignore = 0;
  335. ssize_t ret;
  336. do
  337. {
  338. ret = write (threadControl[1], &ignore, 1);
  339. } while (ret == -1 && errno == EINTR);
  340. waitForThreadToExit (-1);
  341. receiver = nullptr;
  342. }
  343. if (childProcess != 0)
  344. {
  345. CommandReceiver::sendCommand (outChannel, "quit", var());
  346. killChild();
  347. }
  348. }
  349. //==============================================================================
  350. void goToURL (const String& url, const StringArray* headers, const MemoryBlock* postData)
  351. {
  352. DynamicObject::Ptr params = new DynamicObject;
  353. params->setProperty ("url", url);
  354. if (headers != nullptr)
  355. params->setProperty ("headers", var (*headers));
  356. if (postData != nullptr)
  357. params->setProperty ("postData", var (*postData));
  358. CommandReceiver::sendCommand (outChannel, "goToURL", var (params));
  359. }
  360. void goBack() { CommandReceiver::sendCommand (outChannel, "goBack", var()); }
  361. void goForward() { CommandReceiver::sendCommand (outChannel, "goForward", var()); }
  362. void refresh() { CommandReceiver::sendCommand (outChannel, "refresh", var()); }
  363. void stop() { CommandReceiver::sendCommand (outChannel, "stop", var()); }
  364. void resized()
  365. {
  366. if (xembed != nullptr)
  367. xembed->setBounds (owner.getLocalBounds());
  368. }
  369. private:
  370. //==============================================================================
  371. void killChild()
  372. {
  373. if (childProcess != 0)
  374. {
  375. xembed = nullptr;
  376. kill (childProcess, SIGTERM);
  377. int status = 0;
  378. while (! WIFEXITED(status))
  379. waitpid (childProcess, &status, 0);
  380. childProcess = 0;
  381. }
  382. }
  383. void launchChild()
  384. {
  385. int ret;
  386. int inPipe[2], outPipe[2];
  387. ret = pipe (inPipe);
  388. ignoreUnused (ret); jassert (ret == 0);
  389. ret = pipe (outPipe);
  390. ignoreUnused (ret); jassert (ret == 0);
  391. int pid = fork();
  392. if (pid == 0)
  393. {
  394. close (inPipe[0]);
  395. close (outPipe[1]);
  396. GtkChildProcess child (outPipe[0], inPipe[1]);
  397. child.entry();
  398. exit (0);
  399. }
  400. close (inPipe[1]);
  401. close (outPipe[0]);
  402. inChannel = inPipe[0];
  403. outChannel = outPipe[1];
  404. childProcess = pid;
  405. }
  406. void run() override
  407. {
  408. while (! threadShouldExit())
  409. {
  410. if (shouldExit())
  411. return;
  412. receiver->tryNextRead();
  413. fd_set set;
  414. FD_ZERO (&set);
  415. FD_SET (threadControl[0], &set);
  416. FD_SET (receiver->getFd(), &set);
  417. int max_fd = jmax (threadControl[0], receiver->getFd());
  418. int result = 0;
  419. while (result == 0 || (result < 0 && errno == EINTR))
  420. result = select (max_fd + 1, &set, NULL, NULL, NULL);
  421. if (result < 0)
  422. break;
  423. }
  424. }
  425. bool shouldExit()
  426. {
  427. char ignore;
  428. ssize_t result = read (threadControl[0], &ignore, 1);
  429. return (result != -1 || (errno != EAGAIN && errno != EWOULDBLOCK));
  430. }
  431. //==============================================================================
  432. void handleCommandOnMessageThread (const String& cmd, const var& params)
  433. {
  434. String url (params.getProperty ("url", var()).toString());
  435. if (cmd == "pageAboutToLoad") handlePageAboutToLoad (url, params);
  436. else if (cmd == "pageFinishedLoading") owner.pageFinishedLoading (url);
  437. else if (cmd == "windowCloseRequest") owner.windowCloseRequest();
  438. else if (cmd == "newWindowAttemptingToLoad") owner.newWindowAttemptingToLoad (url);
  439. threadBlocker.signal();
  440. }
  441. void handlePageAboutToLoad (const String& url, const var& inputParams)
  442. {
  443. int64 decision_id = inputParams.getProperty ("decision_id", var (0));
  444. if (decision_id != 0)
  445. {
  446. DynamicObject::Ptr params = new DynamicObject;
  447. params->setProperty ("decision_id", decision_id);
  448. params->setProperty ("allow", owner.pageAboutToLoad (url));
  449. CommandReceiver::sendCommand (outChannel, "decision", var (params));
  450. }
  451. }
  452. void handleCommand (const String& cmd, const var& params) override
  453. {
  454. threadBlocker.reset();
  455. (new HandleOnMessageThread (this, cmd, params))->post();
  456. // wait until the command has executed on the message thread
  457. // this ensures that Pimpl can never be deleted while the
  458. // message has not been executed yet
  459. threadBlocker.wait (-1);
  460. }
  461. void receiverHadError() override {}
  462. //==============================================================================
  463. struct HandleOnMessageThread : public CallbackMessage
  464. {
  465. HandleOnMessageThread (Pimpl* pimpl, const String& cmdToUse, const var& params)
  466. : owner (pimpl), cmdToSend (cmdToUse), paramsToSend (params)
  467. {}
  468. void messageCallback() override
  469. {
  470. owner->handleCommandOnMessageThread (cmdToSend, paramsToSend);
  471. }
  472. Pimpl* owner;
  473. String cmdToSend;
  474. var paramsToSend;
  475. };
  476. private:
  477. WebBrowserComponent& owner;
  478. ScopedPointer<CommandReceiver> receiver;
  479. int childProcess = 0, inChannel = 0, outChannel = 0;
  480. int threadControl[2];
  481. ScopedPointer<XEmbedComponent> xembed;
  482. WaitableEvent threadBlocker;
  483. };
  484. //==============================================================================
  485. WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
  486. : browser (new Pimpl (*this)),
  487. blankPageShown (false),
  488. unloadPageWhenBrowserIsHidden (unloadPageWhenBrowserIsHidden_)
  489. {
  490. setOpaque (true);
  491. browser->init();
  492. }
  493. WebBrowserComponent::~WebBrowserComponent()
  494. {
  495. if (browser != nullptr)
  496. {
  497. delete browser;
  498. browser = nullptr;
  499. }
  500. }
  501. //==============================================================================
  502. void WebBrowserComponent::goToURL (const String& url,
  503. const StringArray* headers,
  504. const MemoryBlock* postData)
  505. {
  506. lastURL = url;
  507. if (headers != nullptr)
  508. lastHeaders = *headers;
  509. else
  510. lastHeaders.clear();
  511. if (postData != nullptr)
  512. lastPostData = *postData;
  513. else
  514. lastPostData.reset();
  515. blankPageShown = false;
  516. browser->goToURL (url, headers, postData);
  517. }
  518. void WebBrowserComponent::stop()
  519. {
  520. browser->stop();
  521. }
  522. void WebBrowserComponent::goBack()
  523. {
  524. lastURL.clear();
  525. blankPageShown = false;
  526. browser->goBack();
  527. }
  528. void WebBrowserComponent::goForward()
  529. {
  530. lastURL.clear();
  531. browser->goForward();
  532. }
  533. void WebBrowserComponent::refresh()
  534. {
  535. browser->refresh();
  536. }
  537. //==============================================================================
  538. void WebBrowserComponent::paint (Graphics& g)
  539. {
  540. g.fillAll (Colours::white);
  541. }
  542. void WebBrowserComponent::checkWindowAssociation()
  543. {
  544. }
  545. void WebBrowserComponent::reloadLastURL()
  546. {
  547. if (lastURL.isNotEmpty())
  548. {
  549. goToURL (lastURL, &lastHeaders, &lastPostData);
  550. lastURL.clear();
  551. }
  552. }
  553. void WebBrowserComponent::parentHierarchyChanged()
  554. {
  555. checkWindowAssociation();
  556. }
  557. void WebBrowserComponent::resized()
  558. {
  559. if (browser != nullptr)
  560. browser->resized();
  561. }
  562. void WebBrowserComponent::visibilityChanged()
  563. {
  564. checkWindowAssociation();
  565. }
  566. void WebBrowserComponent::focusGained (FocusChangeType)
  567. {
  568. }