23 lines
1.0 KiB
C++
23 lines
1.0 KiB
C++
#pragma once
|
|
#include "../shared/protocol.h"
|
|
#include <functional>
|
|
namespace browser_bridge {
|
|
// Launch only for an explicit add. Queries and connection checks must never start the app.
|
|
inline std::string ForwardToClient(const Message& request,const std::string& json,
|
|
const std::function<std::string(const std::string&)>& exchange,
|
|
const std::function<std::string()>& launch,
|
|
const std::function<std::string(const std::string&)>& wait_ready) {
|
|
auto response=exchange(json);
|
|
Message reply; std::string error;
|
|
if (Get(request,"action")!="add" || !Parse(response,reply,error)) return response;
|
|
const auto status=Get(reply,"status");
|
|
if (status!="client_not_running" && status!="client_not_ready") return response;
|
|
if (status=="client_not_running") {
|
|
auto failure=launch(); if (!failure.empty()) return failure;
|
|
}
|
|
auto probe=wait_ready(Serialize({{"action","ping"},{"request_id",Get(request,"request_id")}}));
|
|
if (Parse(probe,reply,error) && Get(reply,"status")=="ready") return exchange(json);
|
|
return probe;
|
|
}
|
|
}
|