245 const auto& prefix = incoming.
prefix;
246 if (prefix.category != MESSAGE_CATEGORY_COMMAND)
248 log::engineWarn(
"Cannot handle application operation: expected a command.");
251 if (prefix.operationId == COMMAND_QUIT)
253 if (prefix.destinationKind != PROCESS_KIND_ENGINE || prefix.destinationId != 0 ||
254 prefix.senderKind != PROCESS_KIND_ELECTRON_MAIN || prefix.senderId != 0 ||
255 incoming.
header.payload_type() != PAYLOAD_TYPE_NONE ||
256 incoming.
header.message_type() != MESSAGE_TYPE_NONE ||
267 if (prefix.operationId == COMMAND_LOG_ENTRY)
272 if (prefix.operationId == COMMAND_LOADING_READY_TO_SHOW)
274 if (prefix.destinationKind != PROCESS_KIND_ENGINE || prefix.destinationId != 0 ||
276 incoming.
header.payload_type() != PAYLOAD_TYPE_NONE || incoming.
header.message_type() != MESSAGE_TYPE_NONE ||
282 ipc.loadingReadyToShow();
285 if (prefix.operationId == COMMAND_MAIN_FRAME_READY)
287 if (prefix.destinationKind != PROCESS_KIND_ENGINE || prefix.destinationId != 0 ||
288 prefix.senderKind != PROCESS_KIND_ELECTRON_MAIN || prefix.senderId != 0 ||
289 incoming.
header.payload_type() != PAYLOAD_TYPE_NONE || incoming.
header.message_type() != MESSAGE_TYPE_NONE ||
292 log::engineError(
"Cannot record main frame readiness: invalid announcement.");
295 ipc.mainFrameIsReady();
298 if (prefix.operationId == COMMAND_STARTUP_FAILED)
300 StartupFailure failure;
301 if (prefix.destinationKind != PROCESS_KIND_ENGINE || prefix.destinationId != 0 ||
302 prefix.senderKind != PROCESS_KIND_ELECTRON_MAIN || prefix.senderId != 0 ||
303 incoming.
header.payload_type() != PAYLOAD_TYPE_PROTOBUF ||
304 incoming.
header.message_type() != MESSAGE_TYPE_STARTUP_FAILURE || incoming.
header.has_target_project() ||
305 !failure.ParseFromArray(incoming.
payload(),
static_cast<int>(incoming.
payloadSize())) || failure.error().empty())
310 ipc.startupFailed(failure.error());
313 if (prefix.operationId == COMMAND_NOTIFY)
318 if (prefix.operationId == COMMAND_APPLICATION_STYLE_REQUEST)
320 ApplicationStyle request;
321 if (prefix.destinationKind != PROCESS_KIND_ENGINE || prefix.destinationId != 0 ||
323 incoming.
header.has_target_project() || incoming.
header.payload_type() != PAYLOAD_TYPE_PROTOBUF ||
324 incoming.
header.message_type() != MESSAGE_TYPE_APPLICATION_STYLE ||
325 !request.ParseFromArray(incoming.
payload(),
static_cast<int>(incoming.
payloadSize())) ||
327 request.css().find(
'\0') != std::string::npos)
332 const auto renderer = prefix.senderId;
334 const auto deliver = [&
ipc, renderer](
const ApplicationStyle&
style)
339 options.
messageType = MESSAGE_TYPE_APPLICATION_STYLE;
340 options.
flags = MESSAGE_FLAG_NO_CONSOLE;
341 ipc.sendBuilt(options);
346 ApplicationStyle result;
347 result.set_save(request.save());
348 result.set_edit(request.edit());
349 std::string css = request.css();
351 if (status == errors::NONE) result.set_css(css);
352 else result.set_error(request.save() ?
"Could not save button stylesheet. See the Engine log."
353 :
"Could not load button stylesheet. See the Engine log.");
357 log::engineError(
"Cannot handle application stylesheet: application worker is stopping.");
358 ApplicationStyle result;
359 result.set_save(request.save());
360 result.set_edit(request.edit());
361 result.set_error(
"Application stylesheet is unavailable during shutdown.");
366 if (prefix.operationId == COMMAND_APPLICATION_PREFERENCES_REQUEST ||
367 prefix.operationId == COMMAND_APPLICATION_PREFERENCE_CHANGE)
369 const bool changing = prefix.operationId == COMMAND_APPLICATION_PREFERENCE_CHANGE;
370 ApplicationPreferenceChange change;
371 if (prefix.destinationKind != PROCESS_KIND_ENGINE || prefix.destinationId != 0 ||
373 incoming.
header.has_target_project() ||
374 (changing && (incoming.
header.payload_type() != PAYLOAD_TYPE_PROTOBUF ||
375 incoming.
header.message_type() != MESSAGE_TYPE_APPLICATION_PREFERENCE_CHANGE ||
376 !change.ParseFromArray(incoming.
payload(),
static_cast<int>(incoming.
payloadSize())) ||
377 change.change_case() == ApplicationPreferenceChange::CHANGE_NOT_SET)) ||
378 (!changing && (incoming.
header.payload_type() != PAYLOAD_TYPE_NONE ||
379 incoming.
header.message_type() != MESSAGE_TYPE_NONE || incoming.
payloadSize() != 0)))
381 log::engineError(
"Cannot handle application preferences: invalid request.");
384 const auto renderer = prefix.senderId;
387 F_err result = errors::NONE;
390 switch (change.change_case())
392 case ApplicationPreferenceChange::kDefaultSaveLocation:
394 const auto& folder = change.default_save_location();
395 const std::filesystem::path path(folder);
396 std::error_code error;
397 if ((!folder.empty() && (!path.is_absolute() || folder.find(
'\0') != std::string::npos ||
398 !std::filesystem::is_directory(path, error))) || error)
400 log::engineError(
"Cannot set default project location: folder is unavailable.");
401 result = errors::INVALID_MESSAGE;
404 result =
set(
"application.preferences.defaultSaveLocation", folder);
407 case ApplicationPreferenceChange::kReopenProjectsOnLaunch:
408 result =
set(
"application.preferences.reopenProjectsOnLaunch", change.reopen_projects_on_launch());
410 case ApplicationPreferenceChange::kPackageOnClose:
411 result =
set(
"application.preferences.packageOnClose", change.package_on_close());
413 case ApplicationPreferenceChange::kDatabasePath:
416 case ApplicationPreferenceChange::kReset:
420 result = errors::INVALID_MESSAGE;
424 if (result != errors::NONE)
427 "Could not update application preferences. See the Engine log.");
430 log::engineError(
"Cannot handle application preferences: application worker is stopping.");
435 if (prefix.operationId == COMMAND_PROJECT_CLOSING_REQUEST)
437 if (prefix.destinationKind != PROCESS_KIND_ENGINE ||
439 incoming.
header.payload_type() != PAYLOAD_TYPE_NONE || incoming.
payloadSize() != 0)
441 log::engineError(
"Cannot request project closing statuses: invalid request.");
448 message::FrameOptions options{PROCESS_KIND_ELECTRON_RENDERER, COMMAND_PROJECT_CLOSING_STATUSES, message::MAIN_WINDOW_ID};
449 options.payload = statuses.SerializeAsString();
450 options.payloadType = PAYLOAD_TYPE_PROTOBUF;
451 options.messageType = MESSAGE_TYPE_PROJECT_CLOSING_STATUSES;
452 options.ttlMs = 5000;
454 ipc.sendBuilt(options);
456 std::fprintf(stderr,
"Cannot queue project closing statuses: Console worker is shutting down.\n");
460 if (prefix.operationId == COMMAND_LOG_PAGE_REQUEST)
462 auto remaining = std::make_shared<std::atomic<std::size_t>>(1);
463 const auto completed = [
this, incoming, &
ipc, remaining]
465 if (remaining->fetch_sub(1) == 1 &&
466 !_logQueue->post([
this, incoming, &
ipc] { handleLogMessage(incoming, ipc); }))
467 std::fprintf(stderr,
"Cannot queue log page request: application log worker is shutting down.\n");
470 std::lock_guard<std::mutex> lock(_projectLogsMutex);
471 std::unordered_set<ProjectLogWorker*> seen;
472 for (
const auto& route : _projectLogRoutes)
474 if (!seen.insert(route.second.get()).second)
477 if (!route.second->queue->post(completed))
479 std::fprintf(stderr,
"Cannot queue project log history barrier: worker is shutting down.\n");
487 if (prefix.operationId == COMMAND_PROMPT_RESPONSE)
489 PromptResponse response;
490 if (prefix.destinationKind != PROCESS_KIND_ENGINE ||
492 incoming.header.payload_type() != PAYLOAD_TYPE_PROTOBUF ||
493 incoming.header.message_type() != MESSAGE_TYPE_PROMPT_RESPONSE ||
494 !response.ParseFromArray(incoming.payload(),
static_cast<int>(incoming.payloadSize())))
496 log::engineError(
"Cannot handle prompt response: invalid sender or payload.");
499 std::lock_guard<std::mutex> lock(_promptMutex);
500 const auto found = _pendingPrompts.find(response.request_id());
501 if (found == _pendingPrompts.end() || found->second.completed)
506 const auto& request = found->second.request;
507 const bool validChoice = std::any_of(request.buttons().begin(), request.buttons().end(),
508 [&](
const PromptButton& button) { return button.value() == response.value(); });
509 if (response.error().empty() &&
510 ((response.dismissed() && request.has_dismissible() && !request.dismissible()) ||
511 (!response.dismissed() && !validChoice)))
516 found->second.response = std::move(response);
517 found->second.completed =
true;
520 if (prefix.operationId == COMMAND_PROJECT_PREFERENCES_REQUEST ||
521 prefix.operationId == COMMAND_PROJECT_PREFERENCE_CHANGE)
523 const bool changing = prefix.operationId == COMMAND_PROJECT_PREFERENCE_CHANGE;
524 ProjectPreferencesRequest request;
525 ProjectPreferenceChange change;
526 const bool parsed = incoming.header.payload_type() == PAYLOAD_TYPE_PROTOBUF &&
528 ? incoming.header.message_type() == MESSAGE_TYPE_PROJECT_PREFERENCE_CHANGE &&
529 change.ParseFromArray(incoming.payload(),
static_cast<int>(incoming.payloadSize())) &&
530 change.change_case() != ProjectPreferenceChange::CHANGE_NOT_SET
531 : incoming.header.message_type() == MESSAGE_TYPE_PROJECT_PREFERENCES_REQUEST &&
532 request.ParseFromArray(incoming.payload(),
static_cast<int>(incoming.payloadSize())));
533 const std::string
id = changing ? change.id() : request.id();
534 if (prefix.destinationKind != PROCESS_KIND_ENGINE || prefix.destinationId != 0 ||
536 incoming.header.has_target_project() || !parsed ||
id.empty() ||
id.find(
'\0') != std::string::npos)
542 F_err result = errors::PROJECT_NOT_FOUND;
544 std::lock_guard<std::mutex> lock(_mutex);
545 const auto found = std::find_if(_projects.begin(), _projects.end(),
546 [
this, &
id](
const auto& entry) {
547 return entry.second.id == id && isActiveProjectLocked(entry.second.project.get());
549 if (found != _projects.end())
551 result = enqueueProjectTaskLocked(
"",
false,
false,
553 const std::shared_ptr<threads::TaskQueue>&)
557 std::lock_guard<std::mutex> lock(_mutex);
558 active = isActiveProjectLocked(&project);
565 F_err changed = errors::NONE;
566 bool renamePending =
false;
567 if (changing && change.change_case() == ProjectPreferenceChange::kName)
569 renamePending = change.name() != project.name();
570 changed = project.rename(change.name());
572 else if (changing && change.change_case() == ProjectPreferenceChange::kPackageOnClose)
573 changed = project.set(
"project.preferences.packageOnClose", change.package_on_close());
574 if (changed != errors::NONE)
578 "Could not update project preferences. See the Engine log."));
583 bool package = false;
584 if (project.get(
"project.preferences.packageOnClose", package) != errors::NONE)
587 "Could not read project preferences. See the Engine log."));
591 }, found->second.project.get());
594 if (result != errors::NONE)
596 log::engineError(
"Cannot update project preferences: project is not active or unavailable.");
601 if (prefix.operationId == COMMAND_CLOSE_PROJECT ||
602 (incoming.header.has_target_project() && prefix.operationId != COMMAND_CREATE_PROJECT))
604 handleProjectMessage(incoming,
ipc);
607 if (prefix.operationId == COMMAND_CREATE_PROJECT)
609 if (prefix.senderKind != PROCESS_KIND_ELECTRON_MAIN &&
610 prefix.senderKind != PROCESS_KIND_ELECTRON_RENDERER)
615 const OpeningOrigin requestOrigin{prefix.senderKind, prefix.senderId, incoming.header.message_id(),
true};
616 ProjectCreateRequest request;
617 if (prefix.destinationKind != PROCESS_KIND_ENGINE ||
618 incoming.header.payload_type() != PAYLOAD_TYPE_PROTOBUF ||
619 incoming.header.message_type() != MESSAGE_TYPE_PROJECT_CREATE_REQUEST ||
620 !request.ParseFromArray(incoming.payload(),
static_cast<int>(incoming.payloadSize())) ||
621 !ProjectCreateChoice_IsValid(request.choice()))
627 const std::string path = request.path();
628 const std::filesystem::path target(path);
629 if (path.find(
'\0') != std::string::npos || !target.is_absolute() ||
636 const bool creating = request.choice() == PROJECT_CREATE_NEW || request.choice() == PROJECT_CREATE_IN_FOLDER ||
637 request.choice() == PROJECT_CREATE_IN_FOLDER_RESOLVE;
638 const auto choice = request.choice() == PROJECT_OPEN_ARCHIVE || request.choice() == PROJECT_SELECT_ARCHIVE
640 request.choice() == PROJECT_OPEN_FOLDER || request.choice() == PROJECT_SELECT_FOLDER ||
641 request.choice() == PROJECT_CREATE_IN_FOLDER_RESOLVE
647 request.choice() == PROJECT_SELECT_ARCHIVE || request.choice() == PROJECT_SELECT_FOLDER
649 const OpeningOrigin origin{prefix.senderKind, prefix.senderId, incoming.header.message_id(), creating};
651 watchProjectOpenings(
ipc);
652 if (!postApplication([
this, &
ipc, path, choice, mode, origin]
654 const F_err result = openProject(path, choice,
true, mode, origin);
655 if (result != errors::NONE)
659 log::engineError(
"Cannot create project: application thread is shutting down.");