Application.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibImageDecoderClient/Client.h>
  8. #include <LibWebView/Application.h>
  9. #include <LibWebView/WebContentClient.h>
  10. namespace WebView {
  11. Application* Application::s_the = nullptr;
  12. Application::Application(int, char**)
  13. {
  14. VERIFY(!s_the);
  15. s_the = this;
  16. m_process_manager.on_process_exited = [this](Process&& process) {
  17. process_did_exit(move(process));
  18. };
  19. }
  20. Application::~Application()
  21. {
  22. s_the = nullptr;
  23. }
  24. int Application::exec()
  25. {
  26. int ret = m_event_loop.exec();
  27. m_in_shutdown = true;
  28. return ret;
  29. }
  30. void Application::add_child_process(WebView::Process&& process)
  31. {
  32. m_process_manager.add_process(move(process));
  33. }
  34. #if defined(AK_OS_MACH)
  35. void Application::set_process_mach_port(pid_t pid, Core::MachPort&& port)
  36. {
  37. m_process_manager.set_process_mach_port(pid, move(port));
  38. }
  39. #endif
  40. Optional<Process&> Application::find_process(pid_t pid)
  41. {
  42. return m_process_manager.find_process(pid);
  43. }
  44. void Application::update_process_statistics()
  45. {
  46. m_process_manager.update_all_process_statistics();
  47. }
  48. String Application::generate_process_statistics_html()
  49. {
  50. return m_process_manager.generate_html();
  51. }
  52. void Application::process_did_exit(Process&& process)
  53. {
  54. if (m_in_shutdown)
  55. return;
  56. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Process {} died, type: {}", process.pid(), process_name_from_type(process.type()));
  57. switch (process.type()) {
  58. case ProcessType::ImageDecoder:
  59. if (auto client = process.client<ImageDecoderClient::Client>(); client.has_value()) {
  60. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Restart ImageDecoder process");
  61. if (auto on_death = move(client->on_death)) {
  62. on_death();
  63. }
  64. }
  65. break;
  66. case ProcessType::RequestServer:
  67. dbgln_if(WEBVIEW_PROCESS_DEBUG, "FIXME: Restart request server");
  68. break;
  69. case ProcessType::WebContent:
  70. if (auto client = process.client<WebContentClient>(); client.has_value()) {
  71. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Restart WebContent process");
  72. if (auto on_web_content_process_crash = move(client->on_web_content_process_crash))
  73. on_web_content_process_crash();
  74. }
  75. break;
  76. case ProcessType::WebWorker:
  77. dbgln_if(WEBVIEW_PROCESS_DEBUG, "WebWorker {} died, not sure what to do.", process.pid());
  78. break;
  79. case ProcessType::Chrome:
  80. dbgln("Invalid process type to be dying: Chrome");
  81. VERIFY_NOT_REACHED();
  82. }
  83. }
  84. }