| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 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 |
package cual |
| 18 |
|
| 19 |
/* |
| 20 |
#cgo pkg-config: lomiri-app-launch-0 |
| 21 |
#include <glib.h> |
| 22 |
|
| 23 |
gboolean add_observer (gpointer); |
| 24 |
gboolean remove_observer (gpointer); |
| 25 |
char* launch(gchar* app_id, gchar* f1, gchar* f2, gpointer p); |
| 26 |
gboolean stop(gchar* app_id, gchar* iid); |
| 27 |
*/ |
| 28 |
import "C" |
| 29 |
import ( |
| 30 |
"errors" |
| 31 |
"unsafe" |
| 32 |
|
| 33 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 34 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 35 |
) |
| 36 |
|
| 37 |
func gstring(s string) *C.gchar { |
| 38 |
return (*C.gchar)(C.CString(s)) |
| 39 |
} |
| 40 |
|
| 41 |
type helperState struct { |
| 42 |
log logger.Logger |
| 43 |
done func(string) |
| 44 |
} |
| 45 |
|
| 46 |
//export helperDone |
| 47 |
func helperDone(gp unsafe.Pointer, ciid *C.char) { |
| 48 |
hs := (*helperState)(gp) |
| 49 |
iid := C.GoString(ciid) |
| 50 |
hs.done(iid) |
| 51 |
} |
| 52 |
|
| 53 |
var ( |
| 54 |
ErrCantObserve = errors.New("can't add observer") |
| 55 |
ErrCantUnobserve = errors.New("can't remove observer") |
| 56 |
ErrCantLaunch = errors.New("can't launch helper") |
| 57 |
ErrCantStop = errors.New("can't stop helper") |
| 58 |
) |
| 59 |
|
| 60 |
func New(log logger.Logger) *helperState { |
| 61 |
return &helperState{log: log} |
| 62 |
} |
| 63 |
|
| 64 |
func (hs *helperState) InstallObserver(done func(string)) error { |
| 65 |
hs.done = done |
| 66 |
if C.add_observer(C.gpointer(hs)) != C.TRUE { |
| 67 |
return ErrCantObserve |
| 68 |
} |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
func (hs *helperState) RemoveObserver() error { |
| 73 |
if C.remove_observer(C.gpointer(hs)) != C.TRUE { |
| 74 |
return ErrCantUnobserve |
| 75 |
} |
| 76 |
return nil |
| 77 |
} |
| 78 |
|
| 79 |
func (hs *helperState) HelperAppID(app *click.AppId) string { |
| 80 |
return app.String() |
| 81 |
} |
| 82 |
|
| 83 |
func (hs *helperState) Launch(appId, f1, f2 string) (string, error) { |
| 84 |
// launch(...) takes over ownership of things passed in |
| 85 |
iid := C.GoString(C.launch(gstring(appId), gstring(f1), gstring(f2), C.gpointer(hs))) |
| 86 |
if iid == "" { |
| 87 |
return "", ErrCantLaunch |
| 88 |
} |
| 89 |
return iid, nil |
| 90 |
} |
| 91 |
|
| 92 |
func (hs *helperState) Stop(appId, instanceId string) error { |
| 93 |
if C.stop(gstring(appId), gstring(instanceId)) != C.TRUE { |
| 94 |
return ErrCantStop |
| 95 |
} |
| 96 |
return nil |
| 97 |
} |