summaryrefslogtreecommitdiff
path: root/NativeLauncher/src/waiter/waiter.cc
blob: 6447d4aeb283dd5f5ddaa151b66eef7537d8b355 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

#include <poll.h>

#ifndef NO_TIZEN
#include <launchpad.h>
#include <aul.h>
#endif

#include <memory>
#include <vector>
#include <map>
#include <poll.h>

#include <iostream>

#include "waiter.h"
#include "log.h"

namespace dotnet {
namespace runtime {

struct FdHandler
{
  pollfd *info;
  Receiver receiver;
};

static volatile bool Waiting_;
static std::vector<pollfd> Fdlist_;
static std::map<int, FdHandler> Handlers_;
static Waiter::AppInfo AppInfo_;

void Waiter::OnPrepared()
{
  if (!context.Prepare())
  {
    _DBG("Fail to Prepare...");
  }
}

void Waiter::OnRequested(const AppInfo& info)
{
  // do some job on user id is still system

  if (!context.Request())
  {
    _DBG("Fail to Request...");
  }
}

void Waiter::OnExecuted(const char *path, const char *app_root, int argc, char *argv[])
{
  if (!context.Execute(path, app_root, argc, argv))
  {
    _DBG("Fail to Execute...");
  }
}

void Waiter::OnWaiting()
{
  // Start the loop
  Waiting_ = true;

  _DBG("start polling...");
  while (Waiting_)
  {
    if (poll(Fdlist_.data(), Fdlist_.size(), -1) < 0)
      continue;

    for (auto &p : Fdlist_)
    {
      if ( (p.revents | POLLIN) != 0 )
        Handlers_[p.fd].receiver(p.fd);
    }
  }
  _DBG("end polling...");
}

void Waiter::Stop()
{
  // Stop the loop

  Waiting_ = false;
}


void Waiter::RegisterFd(int fd, Receiver receiver)
{
  // register fd should be caught in event loop

  _DBG("Register fd %d", fd);

  pollfd info;
  info.fd = fd;
  info.events = POLLIN;
  info.revents = 0;

  FdHandler handler;
  Fdlist_.push_back(info);
  handler.info = &Fdlist_.back(); 
  handler.receiver = receiver;

  Handlers_[fd] = handler;
}

void Waiter::DeregisterFd(int fd)
{
  // deregister fd should be caught in event loop
  
  pollfd *info = Handlers_[fd].info;
  Fdlist_.erase(Fdlist_.begin() - (info - &Fdlist_.front()));
  Handlers_.erase(fd);
}

int Waiter::WaitToLaunching(int argc, char *argv[])
{
#ifndef NO_TIZEN
  auto on_create = [](bundle *extra, int type, void *user_data)
  {
    _DBG("on_create..."); // XXX
    Waiter* waiter = static_cast<Waiter*>(user_data);
    waiter->OnPrepared();
  };

  auto on_launch = [](int argc, char **argv, const char *app_path,
      const char *appid, const char *pkgid,
      const char *pkg_type, void *user_data) -> int
  {
    _DBG("on_launch..."); // XXX
    Waiter* waiter = static_cast<Waiter*>(user_data);

    _DBG ("app path : %s", app_path);
    _DBG ("app id : %s", appid);
    _DBG ("pkg id : %s", pkgid);
    _DBG ("pkg type : %s", pkg_type);

    AppInfo info = {
      AppPath : app_path,
      AppId : appid,
      PkgId : pkgid,
      PkgType : pkg_type
    };

    waiter->OnRequested(info);
    return 0;
  };

  auto on_terminate = [](int argc, char **argv, void *user_data) -> int
  {
    _DBG("on_terminate..."); // XXX
    
    string app_root(aul_get_app_root_path());
    Waiter* waiter = static_cast<Waiter*>(user_data);
    waiter->OnExecuted(argv[0], app_root.c_str(), argc, argv);
    return 0;
  };

  auto on_start_loop = [](void *user_data)
  {
    _DBG("on_start_loop..."); // XXX
    Waiter* waiter = static_cast<Waiter*>(user_data);
    waiter->OnWaiting();
  };

  auto on_quit_loop = [](void *user_data)
  {
    _DBG("on_quit_loop..."); // XXX
    Waiter* waiter = static_cast<Waiter*>(user_data);
    waiter->Stop();
  };

  auto on_add_fd = [](void *user_data, int fd, loader_receiver_cb receiver)
  {
    _DBG("on_add_fd..."); // XXX
    Waiter* waiter = static_cast<Waiter*>(user_data);
    waiter->RegisterFd(fd, receiver);
  };

  auto on_remove_fd = [](void *user_data, int fd)
  {
    _DBG("on_remove_fd..."); // XXX
    Waiter* waiter = static_cast<Waiter*>(user_data);
    waiter->DeregisterFd(fd);
  };

  _DBG("launcher wait..."); // XXX
  loader_lifecycle_callback_s callbacks = {
    .create = on_create,
    .launch = on_launch,
    .terminate = on_terminate
  };

	loader_adapter_s adapter = {
		.loop_begin = on_start_loop,
		.loop_quit = on_quit_loop,
		.add_fd = on_add_fd,
		.remove_fd = on_remove_fd
	};

  return launchpad_loader_main(argc, argv, &callbacks, &adapter, this);
#else
  if (argc < 2)
  {
    _DBG("not enough args : %d", argc);
    return -1;
  }
  _DBG("argv[1] = %s", argv[1]);
  std::string app_path(argv[1]);
  std::string app_root;
  auto pos = app_path.find_last_of('/');
  if (pos != std::string::npos)
    app_root = app_path.substr(0, pos);
  else
    app_root = ".";

  this->OnPrepared();
  AppInfo info = {
    AppPath : argv[1],
    AppId : "",
    PkgId : "",
    PkgType : ""
  };
  this->OnRequested(info);
  this->OnExecuted(app_path.c_str(), app_root.c_str(), argc, argv);
#endif
}

void Waiter::SetContext(WaiterContext ctx)
{
  context = ctx;
}

WaiterContext::WaiterContext()
{
  Step = Status::Started;
}

bool WaiterContext::Prepare()
{
  if (Step == Status::Started && Prepared != nullptr && Prepared(Data) == 0)
  {
    Step = Status::Prepared;
    return true;
  }
  return false;
}

bool WaiterContext::Request()
{
  if (Step == Status::Prepared && Requested != nullptr && Requested(Data) == 0)
  {
    Step = Status::Requested;
    return true;
  }
  return false;
}

bool WaiterContext::Execute(const char *path, const char *app_root, int argc, char *argv[])
{
  if (Step == Status::Requested && Executed != nullptr &&
      Executed(path, app_root, argc, argv, Data) == 0)
  {
    Step = Status::Executed;
    return true;
  }
  return false;
}

}  // namespace runtime
}  // namespace dotnet

using dotnet::runtime::Waiter;
using dotnet::runtime::WaiterContext;

static Waiter waiter;

void register_launching_callback(prepared_callback prepared,
    requested_callback requested, executed_callback executed, void *data)
{
  WaiterContext context;
  context.Prepared = prepared;
  context.Requested = requested;
  context.Executed = executed;
  context.Data = data;

  waiter.SetContext(context);
}

void wait_for_launching(int argc, char *argv[])
{
  _DBG("wait_for_launching...");

  waiter.WaitToLaunching(argc, argv);
}