提交 a40fe516 编写于 作者: S Simon Fels

Use events instead of method invokes for window state updates and others

上级 53c2fac5
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#define LOG_TAG "Anbox" #define LOG_TAG "Anboxd"
#include <cutils/log.h> #include <cutils/log.h>
namespace { namespace {
......
...@@ -21,59 +21,44 @@ ...@@ -21,59 +21,44 @@
#include "anbox_rpc.pb.h" #include "anbox_rpc.pb.h"
#include "anbox_bridge.pb.h" #include "anbox_bridge.pb.h"
#define LOG_TAG "Anbox"
#include <cutils/log.h>
namespace anbox { namespace anbox {
PlatformApiStub::PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel) : PlatformApiStub::PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel) :
rpc_channel_(rpc_channel) { rpc_channel_(rpc_channel) {
} }
void PlatformApiStub::boot_finished() { void PlatformApiStub::boot_finished() {
auto c = std::make_shared<Request<protobuf::rpc::Void>>(); protobuf::bridge::EventSequence seq;
auto event = seq.mutable_boot_finished();
ALOGI("Boot finished"); (void) event;
rpc_channel_->send_event(seq);
{
std::lock_guard<decltype(mutex_)> lock(mutex_);
boot_finished_wait_handle_.expect_result();
}
protobuf::rpc::Void message;
rpc_channel_->call_method(
"boot_finished",
&message, c->response.get(),
google::protobuf::NewCallback(this, &PlatformApiStub::handle_boot_finished_response, c.get()));
boot_finished_wait_handle_.wait_for_all();
ALOGI("Boot finished sent successfully!");
} }
void PlatformApiStub::handle_boot_finished_response(Request<protobuf::rpc::Void>*) { void PlatformApiStub::update_window_state(const WindowStateUpdate &state) {
boot_finished_wait_handle_.result_received(); protobuf::bridge::EventSequence seq;
} auto event = seq.mutable_window_state_update();
void PlatformApiStub::update_window_state(const anbox::protobuf::bridge::WindowStateUpdate &window_state) { auto convert_window = [](WindowStateUpdate::Window in, anbox::protobuf::bridge::WindowStateUpdateEvent_WindowState *out) {
auto c = std::make_shared<Request<protobuf::rpc::Void>>(); out->set_display_id(in.display_id);
out->set_has_surface(in.has_surface);
ALOGI("Updating window state"); out->set_package_name(in.package_name);
out->set_frame_left(in.frame.left);
{ out->set_frame_top(in.frame.top);
std::lock_guard<decltype(mutex_)> lock(mutex_); out->set_frame_right(in.frame.right);
update_window_state_wait_handle_.expect_result(); out->set_frame_bottom(in.frame.bottom);
out->set_task_id(in.task_id);
out->set_stack_id(in.stack_id);
};
for (const auto &window : state.updated_windows) {
auto w = event->add_windows();
convert_window(window, w);
} }
rpc_channel_->call_method( for (const auto &window : state.removed_windows) {
"update_window_state", auto w = event->add_removed_windows();
&window_state, c->response.get(), convert_window(window, w);
google::protobuf::NewCallback(this, &PlatformApiStub::handle_update_window_state_response, c.get())); }
update_window_state_wait_handle_.wait_for_all();
}
void PlatformApiStub::handle_update_window_state_response(Request<protobuf::rpc::Void> *request) { rpc_channel_->send_event(seq);
update_window_state_wait_handle_.result_received();
} }
} // namespace anbox } // namespace anbox
...@@ -21,16 +21,14 @@ ...@@ -21,16 +21,14 @@
#include "anbox/common/wait_handle.h" #include "anbox/common/wait_handle.h"
#include <memory> #include <memory>
#include <vector>
#include <string>
namespace anbox { namespace anbox {
namespace protobuf { namespace protobuf {
namespace rpc { namespace rpc {
class Void; class Void;
class WindowStateUpdate;
} // namespace rpc } // namespace rpc
namespace bridge {
class WindowStateUpdate;
} // namespace bridge
} // namespace protobuf } // namespace protobuf
namespace rpc { namespace rpc {
class Channel; class Channel;
...@@ -40,7 +38,27 @@ public: ...@@ -40,7 +38,27 @@ public:
PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel); PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel);
void boot_finished(); void boot_finished();
void update_window_state(const anbox::protobuf::bridge::WindowStateUpdate &window_state);
struct WindowStateUpdate {
struct Window {
int display_id;
bool has_surface;
std::string package_name;
struct Frame {
int left;
int top;
int right;
int bottom;
};
Frame frame;
int task_id;
int stack_id;
};
std::vector<Window> updated_windows;
std::vector<Window> removed_windows;
};
void update_window_state(const WindowStateUpdate &state);
private: private:
template<typename Response> template<typename Response>
...@@ -50,12 +68,7 @@ private: ...@@ -50,12 +68,7 @@ private:
bool success; bool success;
}; };
void handle_boot_finished_response(Request<protobuf::rpc::Void> *request);
void handle_update_window_state_response(Request<protobuf::rpc::Void> *request);
mutable std::mutex mutex_; mutable std::mutex mutex_;
common::WaitHandle boot_finished_wait_handle_;
common::WaitHandle update_window_state_wait_handle_;
std::shared_ptr<rpc::Channel> rpc_channel_; std::shared_ptr<rpc::Channel> rpc_channel_;
}; };
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
*/ */
#include "android/service/platform_service.h" #include "android/service/platform_service.h"
#include "android/service/platform_api_stub.h"
#include "anbox/rpc/channel.h" #include "anbox/rpc/channel.h"
#include "anbox_rpc.pb.h" #include "anbox_rpc.pb.h"
...@@ -39,8 +38,8 @@ status_t PlatformService::boot_finished() { ...@@ -39,8 +38,8 @@ status_t PlatformService::boot_finished() {
return OK; return OK;
} }
void PlatformService::unpack_window_state(anbox::protobuf::bridge::WindowStateUpdate_WindowState *window, const Parcel &data) { anbox::PlatformApiStub::WindowStateUpdate::Window PlatformService::unpack_window_state(const Parcel &data) {
window->set_has_surface(data.readByte() != 0); bool has_surface = data.readByte() != 0;
String8 package_name(data.readString16()); String8 package_name(data.readString16());
...@@ -51,38 +50,45 @@ void PlatformService::unpack_window_state(anbox::protobuf::bridge::WindowStateUp ...@@ -51,38 +50,45 @@ void PlatformService::unpack_window_state(anbox::protobuf::bridge::WindowStateUp
auto task_id = data.readInt32(); auto task_id = data.readInt32();
auto stack_id = data.readInt32(); auto stack_id = data.readInt32();
window->set_package_name(package_name.string()); ALOGI(" Window: package=%s frame={%d,%d,%d,%d} task=%d stack=%d",
window->set_frame_left(frame_left); package_name.string(), frame_left, frame_top, frame_right, frame_bottom,
window->set_frame_top(frame_top); task_id, stack_id);
window->set_frame_right(frame_right);
window->set_frame_bottom(frame_bottom); return anbox::PlatformApiStub::WindowStateUpdate::Window{
window->set_task_id(task_id); -1, // Display id will be added by the caller
window->set_stack_id(stack_id); has_surface,
package_name.string(),
{frame_left, frame_top, frame_right, frame_bottom},
task_id,
stack_id,
};
} }
status_t PlatformService::update_window_state(const Parcel &data) { status_t PlatformService::update_window_state(const Parcel &data) {
anbox::protobuf::bridge::WindowStateUpdate window_state; anbox::PlatformApiStub::WindowStateUpdate state;
ALOGI("Udated windows:");
const auto num_displays = data.readInt32(); const auto num_displays = data.readInt32();
for (auto n = 0; n < num_displays; n++) { for (auto n = 0; n < num_displays; n++) {
const auto display_id = data.readInt32(); const auto display_id = data.readInt32();
const auto num_windows = data.readInt32(); const auto num_windows = data.readInt32();
ALOGI(" Display: id=%d", display_id);
for (auto m = 0; m < num_windows; m++) { for (auto m = 0; m < num_windows; m++) {
auto window = window_state.add_windows(); auto window = unpack_window_state(data);
window->set_display_id(display_id); window.display_id = display_id;
unpack_window_state(window, data); state.updated_windows.push_back(window);
} }
} }
ALOGI("Removed windows:");
const auto num_removed_windows = data.readInt32(); const auto num_removed_windows = data.readInt32();
for (auto n = 0; n < num_removed_windows; n++) { for (auto n = 0; n < num_removed_windows; n++) {
auto window = window_state.add_removed_windows(); auto window = unpack_window_state(data);
window->set_display_id(0); state.removed_windows.push_back(window);
unpack_window_state(window, data);
} }
platform_api_stub_->update_window_state(window_state); platform_api_stub_->update_window_state(state);
return OK; return OK;
} }
......
...@@ -19,20 +19,12 @@ ...@@ -19,20 +19,12 @@
#define ANBOX_ANDROID_PLATFORM_SERVICE_H_ #define ANBOX_ANDROID_PLATFORM_SERVICE_H_
#include "android/service/platform_service_interface.h" #include "android/service/platform_service_interface.h"
#include "android/service/platform_api_stub.h"
#include <binder/Parcel.h> #include <binder/Parcel.h>
#include <memory> #include <memory>
namespace anbox {
class PlatformApiStub;
namespace protobuf {
namespace bridge {
class WindowStateUpdate_WindowState;
} // namespace bridge
} // namespace protobuf
} // namespace anbox
namespace android { namespace android {
class PlatformService : public BnPlatformService { class PlatformService : public BnPlatformService {
public: public:
...@@ -44,7 +36,7 @@ public: ...@@ -44,7 +36,7 @@ public:
status_t update_window_state(const Parcel &data) override; status_t update_window_state(const Parcel &data) override;
private: private:
void unpack_window_state(anbox::protobuf::bridge::WindowStateUpdate_WindowState *window, const Parcel &data); anbox::PlatformApiStub::WindowStateUpdate::Window unpack_window_state(const Parcel &data);
std::shared_ptr<anbox::PlatformApiStub> platform_api_stub_; std::shared_ptr<anbox::PlatformApiStub> platform_api_stub_;
}; };
} // namespace android } // namespace android
......
...@@ -33,28 +33,15 @@ PlatformApiSkeleton::PlatformApiSkeleton(const std::shared_ptr<rpc::PendingCallC ...@@ -33,28 +33,15 @@ PlatformApiSkeleton::PlatformApiSkeleton(const std::shared_ptr<rpc::PendingCallC
PlatformApiSkeleton::~PlatformApiSkeleton() { PlatformApiSkeleton::~PlatformApiSkeleton() {
} }
void PlatformApiSkeleton::boot_finished(anbox::protobuf::rpc::Void const *request, void PlatformApiSkeleton::handle_boot_finished_event(const anbox::protobuf::bridge::BootFinishedEvent &event) {
anbox::protobuf::rpc::Void *response, (void) event;
google::protobuf::Closure *done) {
(void) request;
(void) response;
if (on_boot_finished_action_) if (boot_finished_handler_)
on_boot_finished_action_(); boot_finished_handler_();
done->Run();
} }
void PlatformApiSkeleton::update_window_state(anbox::protobuf::bridge::WindowStateUpdate const *request, void PlatformApiSkeleton::handle_window_state_update_event(const anbox::protobuf::bridge::WindowStateUpdateEvent &event) {
anbox::protobuf::rpc::Void *response, auto convert_window_state = [](const ::anbox::protobuf::bridge::WindowStateUpdateEvent_WindowState &window) {
google::protobuf::Closure *done) {
(void) response;
auto convert_window_state = [](const ::anbox::protobuf::bridge::WindowStateUpdate_WindowState &window) {
DEBUG("Window: display=%d has_surface=%d frame={%d,%d,%d,%d} package=%s task=%d stack=-1",
window.display_id(), window.has_surface(),
window.frame_left(), window.frame_top(), window.frame_right(), window.frame_bottom(),
window.package_name(), window.task_id());
return wm::WindowState( return wm::WindowState(
wm::Display::Id(window.display_id()), wm::Display::Id(window.display_id()),
window.has_surface(), window.has_surface(),
...@@ -65,24 +52,22 @@ void PlatformApiSkeleton::update_window_state(anbox::protobuf::bridge::WindowSta ...@@ -65,24 +52,22 @@ void PlatformApiSkeleton::update_window_state(anbox::protobuf::bridge::WindowSta
}; };
wm::WindowState::List updated; wm::WindowState::List updated;
for (int n = 0; n < request->windows_size(); n++) { for (int n = 0; n < event.windows_size(); n++) {
const auto window = request->windows(n); const auto window = event.windows(n);
updated.push_back(convert_window_state(window)); updated.push_back(convert_window_state(window));
} }
wm::WindowState::List removed; wm::WindowState::List removed;
for (int n = 0; n < request->removed_windows_size(); n++) { for (int n = 0; n < event.removed_windows_size(); n++) {
const auto window = request->removed_windows(n); const auto window = event.removed_windows(n);
removed.push_back(convert_window_state(window)); removed.push_back(convert_window_state(window));
} }
window_manager_->apply_window_state_update(updated, removed); window_manager_->apply_window_state_update(updated, removed);
done->Run();
} }
void PlatformApiSkeleton::on_boot_finished(const std::function<void()> &action) { void PlatformApiSkeleton::register_boot_finished_handler(const std::function<void()> &action) {
on_boot_finished_action_ = action; boot_finished_handler_ = action;
} }
} // namespace bridge } // namespace bridge
} // namespace anbox } // namespace anbox
...@@ -32,8 +32,8 @@ namespace rpc { ...@@ -32,8 +32,8 @@ namespace rpc {
class Void; class Void;
} // namespace rpc } // namespace rpc
namespace bridge { namespace bridge {
class Notification; class BootFinishedEvent;
class WindowStateUpdate; class WindowStateUpdateEvent;
} // namespace bridge } // namespace bridge
} // namespace protobuf } // namespace protobuf
namespace rpc { namespace rpc {
...@@ -49,20 +49,15 @@ public: ...@@ -49,20 +49,15 @@ public:
const std::shared_ptr<wm::Manager> &window_manager); const std::shared_ptr<wm::Manager> &window_manager);
virtual ~PlatformApiSkeleton(); virtual ~PlatformApiSkeleton();
void boot_finished(anbox::protobuf::rpc::Void const *request, void handle_boot_finished_event(const anbox::protobuf::bridge::BootFinishedEvent &event);
anbox::protobuf::rpc::Void *response, void handle_window_state_update_event(const anbox::protobuf::bridge::WindowStateUpdateEvent &event);
google::protobuf::Closure *done);
void update_window_state(anbox::protobuf::bridge::WindowStateUpdate const *request, void register_boot_finished_handler(const std::function<void()> &action);
anbox::protobuf::rpc::Void *response,
google::protobuf::Closure *done);
void on_boot_finished(const std::function<void()> &action);
private: private:
std::shared_ptr<rpc::PendingCallCache> pending_calls_; std::shared_ptr<rpc::PendingCallCache> pending_calls_;
std::shared_ptr<wm::Manager> window_manager_; std::shared_ptr<wm::Manager> window_manager_;
std::function<void()> on_boot_finished_action_; std::function<void()> boot_finished_handler_;
}; };
} // namespace bridge } // namespace bridge
} // namespace anbox } // namespace anbox
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "anbox/bridge/platform_message_processor.h" #include "anbox/bridge/platform_message_processor.h"
#include "anbox/bridge/platform_api_skeleton.h" #include "anbox/bridge/platform_api_skeleton.h"
#include "anbox/rpc/template_message_processor.h" #include "anbox/rpc/template_message_processor.h"
#include "anbox/logger.h"
#include "anbox_bridge.pb.h" #include "anbox_bridge.pb.h"
...@@ -34,13 +35,20 @@ PlatformMessageProcessor::~PlatformMessageProcessor() { ...@@ -34,13 +35,20 @@ PlatformMessageProcessor::~PlatformMessageProcessor() {
} }
void PlatformMessageProcessor::dispatch(rpc::Invocation const& invocation) { void PlatformMessageProcessor::dispatch(rpc::Invocation const& invocation) {
if (invocation.method_name() == "boot_finished")
invoke(this, server_.get(), &PlatformApiSkeleton::boot_finished, invocation);
else if (invocation.method_name() == "update_window_state")
invoke(this, server_.get(), &PlatformApiSkeleton::update_window_state, invocation);
} }
void PlatformMessageProcessor::process_event_sequence(const std::string&) { void PlatformMessageProcessor::process_event_sequence(const std::string &raw_events) {
anbox::protobuf::bridge::EventSequence seq;
if (!seq.ParseFromString(raw_events)) {
WARNING("Failed to parse events from raw string");
return;
}
if (seq.has_window_state_update())
server_->handle_window_state_update_event(seq.window_state_update());
if (seq.has_boot_finished())
server_->handle_boot_finished_event(seq.boot_finished());
} }
} // namespace anbox } // namespace anbox
} // namespace network } // namespace network
...@@ -130,7 +130,8 @@ anbox::cmds::Run::Run(const BusFactory& bus_factory) ...@@ -130,7 +130,8 @@ anbox::cmds::Run::Run(const BusFactory& bus_factory)
android_api_stub->set_rpc_channel(rpc_channel); android_api_stub->set_rpc_channel(rpc_channel);
auto server = std::make_shared<bridge::PlatformApiSkeleton>(pending_calls, window_manager); auto server = std::make_shared<bridge::PlatformApiSkeleton>(pending_calls, window_manager);
server->on_boot_finished([&]() { server->register_boot_finished_handler([&]() {
DEBUG("Android successfully booted");
dispatcher->dispatch([&]() { dispatcher->dispatch([&]() {
// FIXME make this configurable or once we have a bridge let the host // FIXME make this configurable or once we have a bridge let the host
// act as a DNS proxy. // act as a DNS proxy.
......
...@@ -2,6 +2,11 @@ option optimize_for = LITE_RUNTIME; ...@@ -2,6 +2,11 @@ option optimize_for = LITE_RUNTIME;
package anbox.protobuf.bridge; package anbox.protobuf.bridge;
message StructuredError {
optional uint32 domain = 1;
optional uint32 code = 2;
}
message Notification { message Notification {
required string package_name = 1; required string package_name = 1;
required string category = 2; required string category = 2;
...@@ -27,7 +32,11 @@ message SetDnsServers { ...@@ -27,7 +32,11 @@ message SetDnsServers {
repeated Server servers = 2; repeated Server servers = 2;
} }
message WindowStateUpdate {
message BootFinishedEvent {
}
message WindowStateUpdateEvent {
message WindowState { message WindowState {
required int32 display_id = 1; required int32 display_id = 1;
required bool has_surface = 2; required bool has_surface = 2;
...@@ -42,3 +51,11 @@ message WindowStateUpdate { ...@@ -42,3 +51,11 @@ message WindowStateUpdate {
repeated WindowState windows = 1; repeated WindowState windows = 1;
repeated WindowState removed_windows = 2; repeated WindowState removed_windows = 2;
} }
message EventSequence {
optional BootFinishedEvent boot_finished = 1;
optional WindowStateUpdateEvent window_state_update = 2;
optional string error = 127;
optional StructuredError structured_error = 128;
}
...@@ -12,6 +12,7 @@ message Invocation { ...@@ -12,6 +12,7 @@ message Invocation {
message Result { message Result {
optional uint32 id = 1; optional uint32 id = 1;
optional bytes response = 2; optional bytes response = 2;
repeated bytes events = 3;
} }
message StructuredError { message StructuredError {
......
...@@ -42,7 +42,17 @@ void Channel::call_method(std::string const& method_name, ...@@ -42,7 +42,17 @@ void Channel::call_method(std::string const& method_name,
google::protobuf::Closure *complete) { google::protobuf::Closure *complete) {
auto const &invocation = invocation_for(method_name, parameters); auto const &invocation = invocation_for(method_name, parameters);
pending_calls_->save_completion_details(invocation, response, complete); pending_calls_->save_completion_details(invocation, response, complete);
send_message(invocation); send_message(MessageType::invocation, invocation);
}
void Channel::send_event(google::protobuf::MessageLite const& event) {
VariableLengthArray<2048> buffer{static_cast<size_t>(event.ByteSize())};
event.SerializeWithCachedSizesToArray(buffer.data());
anbox::protobuf::rpc::Result response;
response.add_events(buffer.data(), buffer.size());
send_message(MessageType::response, response);
} }
protobuf::rpc::Invocation Channel::invocation_for(std::string const& method_name, protobuf::rpc::Invocation Channel::invocation_for(std::string const& method_name,
...@@ -62,17 +72,17 @@ protobuf::rpc::Invocation Channel::invocation_for(std::string const& method_name ...@@ -62,17 +72,17 @@ protobuf::rpc::Invocation Channel::invocation_for(std::string const& method_name
return invoke; return invoke;
} }
void Channel::send_message(anbox::protobuf::rpc::Invocation const& invocation) { void Channel::send_message(const std::uint8_t &type, google::protobuf::MessageLite const& message) {
const size_t size = invocation.ByteSize(); const size_t size = message.ByteSize();
const unsigned char header_bytes[header_size] = { const unsigned char header_bytes[header_size] = {
static_cast<unsigned char>((size >> 8) & 0xff), static_cast<unsigned char>((size >> 8) & 0xff),
static_cast<unsigned char>((size >> 0) & 0xff), static_cast<unsigned char>((size >> 0) & 0xff),
MessageType::invocation, type,
}; };
std::vector<std::uint8_t> send_buffer(sizeof(header_bytes) + size); std::vector<std::uint8_t> send_buffer(sizeof(header_bytes) + size);
std::copy(header_bytes, header_bytes + sizeof(header_bytes), send_buffer.begin()); std::copy(header_bytes, header_bytes + sizeof(header_bytes), send_buffer.begin());
invocation.SerializeToArray(send_buffer.data() + sizeof(header_bytes), size); message.SerializeToArray(send_buffer.data() + sizeof(header_bytes), size);
try { try {
std::lock_guard<std::mutex> lock(write_mutex_); std::lock_guard<std::mutex> lock(write_mutex_);
......
...@@ -52,11 +52,13 @@ public: ...@@ -52,11 +52,13 @@ public:
google::protobuf::MessageLite *response, google::protobuf::MessageLite *response,
google::protobuf::Closure *complete); google::protobuf::Closure *complete);
void send_event(google::protobuf::MessageLite const& event);
private: private:
protobuf::rpc::Invocation invocation_for( protobuf::rpc::Invocation invocation_for(
std::string const& method_name, std::string const& method_name,
google::protobuf::MessageLite const* request); google::protobuf::MessageLite const* request);
void send_message(anbox::protobuf::rpc::Invocation const& invocation); void send_message(const std::uint8_t &type, google::protobuf::MessageLite const& message);
int next_id(); int next_id();
void notify_disconnected(); void notify_disconnected();
......
...@@ -78,7 +78,11 @@ bool MessageProcessor::process_data(const std::vector<std::uint8_t> &data) { ...@@ -78,7 +78,11 @@ bool MessageProcessor::process_data(const std::vector<std::uint8_t> &data) {
buffer_.erase(buffer_.begin(), buffer_.begin() + message_size); buffer_.erase(buffer_.begin(), buffer_.begin() + message_size);
pending_calls_->complete_response(*result); if (result->has_id())
pending_calls_->complete_response(*result);
for (int n = 0; n < result->events_size(); n++)
process_event_sequence(result->events(n));
} }
return true; return true;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册