2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "android/service/platform_api_stub.h"
19 #include "anbox/rpc/channel.h"
21 #include "anbox_rpc.pb.h"
22 #include "anbox_bridge.pb.h"
28 #define LOG_TAG "Anboxd"
29 #include <cutils/log.h>
32 constexpr const char *first_boot_marker_path{"/data/.anbox_initialized"};
36 PlatformApiStub::PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel) :
37 rpc_channel_(rpc_channel) {
40 void PlatformApiStub::boot_finished() {
41 protobuf::bridge::EventSequence seq;
42 auto event = seq.mutable_boot_finished();
45 if (stat(first_boot_marker_path, &st) != 0) {
46 event->set_first_boot_done(true);
47 std::ofstream marker(first_boot_marker_path);
50 rpc_channel_->send_event(seq);
53 void PlatformApiStub::update_window_state(const WindowStateUpdate &state) {
54 protobuf::bridge::EventSequence seq;
55 auto event = seq.mutable_window_state_update();
57 auto convert_window = [](WindowStateUpdate::Window in, anbox::protobuf::bridge::WindowStateUpdateEvent_WindowState *out) {
58 out->set_display_id(in.display_id);
59 out->set_has_surface(in.has_surface);
60 out->set_package_name(in.package_name);
61 out->set_frame_left(in.frame.left);
62 out->set_frame_top(in.frame.top);
63 out->set_frame_right(in.frame.right);
64 out->set_frame_bottom(in.frame.bottom);
65 out->set_task_id(in.task_id);
66 out->set_stack_id(in.stack_id);
69 for (const auto &window : state.updated_windows) {
70 auto w = event->add_windows();
71 convert_window(window, w);
74 for (const auto &window : state.removed_windows) {
75 auto w = event->add_removed_windows();
76 convert_window(window, w);
79 rpc_channel_->send_event(seq);
82 void PlatformApiStub::update_application_list(const ApplicationListUpdate &update) {
83 protobuf::bridge::EventSequence seq;
84 auto event = seq.mutable_application_list_update();
86 for (const auto &a : update.applications) {
87 auto app = event->add_applications();
88 app->set_name(a.name);
89 app->set_package(a.package);
91 auto launch_intent = app->mutable_launch_intent();
92 launch_intent->set_action(a.launch_intent.action);
93 launch_intent->set_uri(a.launch_intent.uri);
94 launch_intent->set_type(a.launch_intent.type);
95 launch_intent->set_package(a.launch_intent.package);
96 launch_intent->set_component(a.launch_intent.component);
97 for (const auto &category : a.launch_intent.categories) {
98 auto c = launch_intent->add_categories();
102 if (a.icon.size() > 0)
103 app->set_icon(a.icon.data(), a.icon.size());
106 for (const auto &package : update.removed_applications) {
107 auto app = event->add_removed_applications();
108 app->set_name("unknown");
109 app->set_package(package);
112 rpc_channel_->send_event(seq);
115 void PlatformApiStub::on_clipboard_data_set(Request<protobuf::rpc::Void> *request) {
116 request->wh.result_received();
119 void PlatformApiStub::set_clipboard_data(const ClipboardData &data) {
120 auto c = std::make_shared<Request<protobuf::rpc::Void>>();
122 protobuf::bridge::ClipboardData message;
123 message.set_text(data.text);
126 std::lock_guard<decltype(mutex_)> lock(mutex_);
127 c->wh.expect_result();
130 rpc_channel_->call_method("set_clipboard_data", &message, c->response.get(),
131 google::protobuf::NewCallback(
132 this, &PlatformApiStub::on_clipboard_data_set, c.get()));
134 c->wh.wait_for_all();
136 if (c->response->has_error()) throw std::runtime_error(c->response->error());
139 void PlatformApiStub::on_clipboard_data_get(Request<protobuf::bridge::ClipboardData> *request) {
140 request->wh.result_received();
143 PlatformApiStub::ClipboardData PlatformApiStub::get_clipboard_data() {
144 auto c = std::make_shared<Request<protobuf::bridge::ClipboardData>>();
146 protobuf::rpc::Void message;
148 received_clipboard_data_ = ClipboardData{};
151 std::lock_guard<decltype(mutex_)> lock(mutex_);
152 c->wh.expect_result();
155 rpc_channel_->call_method("get_clipboard_data", &message, c->response.get(),
156 google::protobuf::NewCallback(
157 this, &PlatformApiStub::on_clipboard_data_get, c.get()));
159 c->wh.wait_for_all();
161 if (c->response->has_error()) throw std::runtime_error(c->response->error());
163 return ClipboardData{c->response->text()};