Skip to content

Commit

Permalink
Restore Linux SendBackAck behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhao271 committed Apr 15, 2022
1 parent 5ce03c2 commit 0abc7f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
35 changes: 26 additions & 9 deletions shell/browser/api/electron_api_app.cc
Expand Up @@ -1074,8 +1074,25 @@ void App::OnFirstInstanceAck(
Emit("first-instance-ack", data_to_send);
}

// This function handles the user calling
// the callback parameter sent out by the second-instance event.
// This function helps the first instance send an acknowledgement
// back to the second instance.
void App::SendBackAck(const ProcessSingleton::SendAckCallback& ack_callback,
std::vector<const uint8_t>* ack_data) {
#if BUILDFLAG(IS_WIN)
// For Windows, we use a timer to call the callback async,
// because we must exit ProcessSingleton::ProcessLaunchNotification
// before sending back the acknowledgement, otherwise
// the program will hang on the ConnectNamedPipe call.
ack_timer_.Start(FROM_HERE, base::Seconds(0),
base::BindOnce(ack_callback, ack_data));
#else
ack_callback.Run(ack_data);
#endif
}

// This function is called within the first instance
// when it prepares to send back the acknowledgement
// to the second instance.
void App::AckCallbackWrapper(
const ProcessSingleton::SendAckCallback& ack_callback,
gin::Arguments* args) {
Expand All @@ -1088,14 +1105,14 @@ void App::AckCallbackWrapper(
if (!ack_message.encoded_message.empty()) {
ack_to_send_ = std::vector<const uint8_t>(
ack_message.encoded_message.begin(), ack_message.encoded_message.end());
ack_timer_.Start(FROM_HERE, base::Seconds(0),
base::BindOnce(ack_callback, &ack_to_send_));
SendBackAck(ack_callback, &ack_to_send_);
} else {
ack_timer_.Start(FROM_HERE, base::Seconds(0),
base::BindOnce(ack_callback, nullptr));
SendBackAck(ack_callback, nullptr);
}
}

// This function is called within the first instance
// when it learns of a second instance.
void App::OnSecondInstance(
const base::CommandLine& cmd,
const base::FilePath& cwd,
Expand All @@ -1111,12 +1128,12 @@ void App::OnSecondInstance(
bool prevent_default =
Emit("second-instance", cmd.argv(), cwd, data_value, cb);
if (!prevent_default && send_ack_) {
// Call the callback ourselves, and send back nothing.
ack_timer_.Start(FROM_HERE, base::Seconds(0),
base::BindOnce(ack_callback, nullptr));
SendBackAck(ack_callback, nullptr);
}
}

// This wrapper function manages when to run
// App::OnSecondInstance above.
bool App::NotificationCallbackWrapper(
const base::RepeatingCallback<
void(const base::CommandLine& command_line,
Expand Down
2 changes: 2 additions & 0 deletions shell/browser/api/electron_api_app.h
Expand Up @@ -194,6 +194,8 @@ class App : public ElectronBrowserClient::Delegate,
std::string GetLocale();
std::string GetLocaleCountryCode();
void OnFirstInstanceAck(const base::span<const uint8_t>* first_instance_data);
void SendBackAck(const ProcessSingleton::SendAckCallback& ack_callback,
std::vector<const uint8_t>* ack_data);
void AckCallbackWrapper(const ProcessSingleton::SendAckCallback& ack_callback,
gin::Arguments* args);
void OnSecondInstance(const base::CommandLine& cmd,
Expand Down

0 comments on commit 0abc7f0

Please sign in to comment.