node_bindings_mac.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/node_bindings_mac.h"
  5. #include <errno.h>
  6. #include <sys/select.h>
  7. #include <sys/sysctl.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include "shell/common/node_includes.h"
  11. namespace electron {
  12. NodeBindingsMac::NodeBindingsMac(BrowserEnvironment browser_env)
  13. : NodeBindings(browser_env) {}
  14. NodeBindingsMac::~NodeBindingsMac() = default;
  15. void NodeBindingsMac::PrepareMessageLoop() {
  16. int handle = uv_backend_fd(uv_loop_);
  17. // If the backend fd hasn't changed, don't proceed.
  18. if (handle == handle_)
  19. return;
  20. NodeBindings::PrepareMessageLoop();
  21. }
  22. void NodeBindingsMac::RunMessageLoop() {
  23. int handle = uv_backend_fd(uv_loop_);
  24. // If the backend fd hasn't changed, don't proceed.
  25. if (handle == handle_)
  26. return;
  27. handle_ = handle;
  28. NodeBindings::RunMessageLoop();
  29. }
  30. void NodeBindingsMac::PollEvents() {
  31. struct timeval tv;
  32. int timeout = uv_backend_timeout(uv_loop_);
  33. if (timeout != -1) {
  34. tv.tv_sec = timeout / 1000;
  35. tv.tv_usec = (timeout % 1000) * 1000;
  36. }
  37. fd_set readset;
  38. int fd = uv_backend_fd(uv_loop_);
  39. FD_ZERO(&readset);
  40. FD_SET(fd, &readset);
  41. // Wait for new libuv events.
  42. int r;
  43. do {
  44. r = select(fd + 1, &readset, nullptr, nullptr,
  45. timeout == -1 ? nullptr : &tv);
  46. } while (r == -1 && errno == EINTR);
  47. }
  48. // static
  49. NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
  50. return new NodeBindingsMac(browser_env);
  51. }
  52. } // namespace electron