File: platform_api_stub.cpp

package info (click to toggle)
anbox 0.0~git20190124-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 22,000 kB
  • sloc: cpp: 76,190; ansic: 7,434; sh: 1,350; xml: 818; java: 780; python: 390; makefile: 35; lisp: 7
file content (165 lines) | stat: -rw-r--r-- 5,312 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "android/service/platform_api_stub.h"
#include "anbox/rpc/channel.h"

#include "anbox_rpc.pb.h"
#include "anbox_bridge.pb.h"

#include <fstream>

#include <sys/stat.h>

#define LOG_TAG "Anboxd"
#include <cutils/log.h>

namespace {
constexpr const char *first_boot_marker_path{"/data/.anbox_initialized"};
}

namespace anbox {
PlatformApiStub::PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel) :
    rpc_channel_(rpc_channel) {
}

void PlatformApiStub::boot_finished() {
    protobuf::bridge::EventSequence seq;
    auto event = seq.mutable_boot_finished();

    struct stat st;
    if (stat(first_boot_marker_path, &st) != 0) {
      event->set_first_boot_done(true);
      std::ofstream marker(first_boot_marker_path);
    }

    rpc_channel_->send_event(seq);
}

void PlatformApiStub::update_window_state(const WindowStateUpdate &state) {
    protobuf::bridge::EventSequence seq;
    auto event = seq.mutable_window_state_update();

    auto convert_window = [](WindowStateUpdate::Window in, anbox::protobuf::bridge::WindowStateUpdateEvent_WindowState *out) {
        out->set_display_id(in.display_id);
        out->set_has_surface(in.has_surface);
        out->set_package_name(in.package_name);
        out->set_frame_left(in.frame.left);
        out->set_frame_top(in.frame.top);
        out->set_frame_right(in.frame.right);
        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);
    }

    for (const auto &window : state.removed_windows) {
        auto w = event->add_removed_windows();
        convert_window(window, w);
    }

    rpc_channel_->send_event(seq);
}

void PlatformApiStub::update_application_list(const ApplicationListUpdate &update) {
    protobuf::bridge::EventSequence seq;
    auto event = seq.mutable_application_list_update();

    for (const auto &a : update.applications) {
        auto app = event->add_applications();
        app->set_name(a.name);
        app->set_package(a.package);

        auto launch_intent = app->mutable_launch_intent();
        launch_intent->set_action(a.launch_intent.action);
        launch_intent->set_uri(a.launch_intent.uri);
        launch_intent->set_type(a.launch_intent.type);
        launch_intent->set_package(a.launch_intent.package);
        launch_intent->set_component(a.launch_intent.component);
        for (const auto &category : a.launch_intent.categories) {
            auto c = launch_intent->add_categories();
            *c = category;
        }

        if (a.icon.size() > 0)
          app->set_icon(a.icon.data(), a.icon.size());
    }

    for (const auto &package : update.removed_applications) {
      auto app = event->add_removed_applications();
      app->set_name("unknown");
      app->set_package(package);
    }

    rpc_channel_->send_event(seq);
}

void PlatformApiStub::on_clipboard_data_set(Request<protobuf::rpc::Void> *request) {
    request->wh.result_received();
}

void PlatformApiStub::set_clipboard_data(const ClipboardData &data) {
    auto c = std::make_shared<Request<protobuf::rpc::Void>>();

    protobuf::bridge::ClipboardData message;
    message.set_text(data.text);

    {
      std::lock_guard<decltype(mutex_)> lock(mutex_);
      c->wh.expect_result();
    }

    rpc_channel_->call_method("set_clipboard_data", &message, c->response.get(),
                              google::protobuf::NewCallback(
                                  this, &PlatformApiStub::on_clipboard_data_set, c.get()));

    c->wh.wait_for_all();

    if (c->response->has_error()) throw std::runtime_error(c->response->error());
}

void PlatformApiStub::on_clipboard_data_get(Request<protobuf::bridge::ClipboardData> *request) {
    request->wh.result_received();
}

PlatformApiStub::ClipboardData PlatformApiStub::get_clipboard_data() {
    auto c = std::make_shared<Request<protobuf::bridge::ClipboardData>>();

    protobuf::rpc::Void message;

    received_clipboard_data_ = ClipboardData{};

    {
      std::lock_guard<decltype(mutex_)> lock(mutex_);
      c->wh.expect_result();
    }

    rpc_channel_->call_method("get_clipboard_data", &message, c->response.get(),
                              google::protobuf::NewCallback(
                                  this, &PlatformApiStub::on_clipboard_data_get, c.get()));

    c->wh.wait_for_all();

    if (c->response->has_error()) throw std::runtime_error(c->response->error());

    return ClipboardData{c->response->text()};
}
} // namespace anbox