4d351bd27790d04d61ffc783fdc4a43959078b22
[iec.git] / src / type3_AndroidCloud / anbox-master / android / service / platform_api_stub.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
18 #include "android/service/platform_api_stub.h"
19 #include "anbox/rpc/channel.h"
20
21 #include "anbox_rpc.pb.h"
22 #include "anbox_bridge.pb.h"
23
24 #include <fstream>
25
26 #include <sys/stat.h>
27
28 #define LOG_TAG "Anboxd"
29 #include <cutils/log.h>
30
31 namespace {
32 constexpr const char *first_boot_marker_path{"/data/.anbox_initialized"};
33 }
34
35 namespace anbox {
36 PlatformApiStub::PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel) :
37     rpc_channel_(rpc_channel) {
38 }
39
40 void PlatformApiStub::boot_finished() {
41     protobuf::bridge::EventSequence seq;
42     auto event = seq.mutable_boot_finished();
43
44     struct stat st;
45     if (stat(first_boot_marker_path, &st) != 0) {
46       event->set_first_boot_done(true);
47       std::ofstream marker(first_boot_marker_path);
48     }
49
50     rpc_channel_->send_event(seq);
51 }
52
53 void PlatformApiStub::update_window_state(const WindowStateUpdate &state) {
54     protobuf::bridge::EventSequence seq;
55     auto event = seq.mutable_window_state_update();
56
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);
67     };
68
69     for (const auto &window : state.updated_windows) {
70         auto w = event->add_windows();
71         convert_window(window, w);
72     }
73
74     for (const auto &window : state.removed_windows) {
75         auto w = event->add_removed_windows();
76         convert_window(window, w);
77     }
78
79     rpc_channel_->send_event(seq);
80 }
81
82 void PlatformApiStub::update_application_list(const ApplicationListUpdate &update) {
83     protobuf::bridge::EventSequence seq;
84     auto event = seq.mutable_application_list_update();
85
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);
90
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();
99             *c = category;
100         }
101
102         if (a.icon.size() > 0)
103           app->set_icon(a.icon.data(), a.icon.size());
104     }
105
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);
110     }
111
112     rpc_channel_->send_event(seq);
113 }
114
115 void PlatformApiStub::on_clipboard_data_set(Request<protobuf::rpc::Void> *request) {
116     request->wh.result_received();
117 }
118
119 void PlatformApiStub::set_clipboard_data(const ClipboardData &data) {
120     auto c = std::make_shared<Request<protobuf::rpc::Void>>();
121
122     protobuf::bridge::ClipboardData message;
123     message.set_text(data.text);
124
125     {
126       std::lock_guard<decltype(mutex_)> lock(mutex_);
127       c->wh.expect_result();
128     }
129
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()));
133
134     c->wh.wait_for_all();
135
136     if (c->response->has_error()) throw std::runtime_error(c->response->error());
137 }
138
139 void PlatformApiStub::on_clipboard_data_get(Request<protobuf::bridge::ClipboardData> *request) {
140     request->wh.result_received();
141 }
142
143 PlatformApiStub::ClipboardData PlatformApiStub::get_clipboard_data() {
144     auto c = std::make_shared<Request<protobuf::bridge::ClipboardData>>();
145
146     protobuf::rpc::Void message;
147
148     received_clipboard_data_ = ClipboardData{};
149
150     {
151       std::lock_guard<decltype(mutex_)> lock(mutex_);
152       c->wh.expect_result();
153     }
154
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()));
158
159     c->wh.wait_for_all();
160
161     if (c->response->has_error()) throw std::runtime_error(c->response->error());
162
163     return ClipboardData{c->response->text()};
164 }
165 } // namespace anbox