<commit_before>// +build android

package security

import (
	"runtime"

	"veyron.io/jni/runtimes/google/util"
	inaming "veyron.io/veyron/veyron/runtimes/google/naming"
	"veyron.io/veyron/veyron2/naming"
	"veyron.io/veyron/veyron2/security"
)

// #cgo LDFLAGS: -ljniwrapper
// #include "jni_wrapper.h"
import "C"

// newJavaContext constructs a new context in java based on the passed go context.
func newJavaContext(env interface{}, context security.Context) C.jobject {
	util.GoRef(&context) // Un-refed when the Java Context object is finalized.
	return C.jobject(util.NewObjectOrCatch(env, jContextImplClass, []util.Sign{util.LongSign}, &context))
}

func newContext(env *C.JNIEnv, jContext C.jobject) *context {
	// We cannot cache Java environments as they are only valid in the current
	// thread.  We can, however, cache the Java VM and obtain an environment
	// from it in whatever thread happens to be running at the time.
	var jVM *C.JavaVM
	if status := C.GetJavaVM(env, &jVM); status != 0 {
		panic("couldn't get Java VM from the (Java) environment")
	}
	// Reference Java context; it will be de-referenced when the go context
	// created below is garbage-collected (through the finalizer callback we
	// setup just below).
	jContext = C.NewGlobalRef(env, jContext)
	c := &context{
		jVM:      jVM,
		jContext: jContext,
	}
	runtime.SetFinalizer(c, func(c *context) {
		envPtr, freeFunc := util.GetEnv(c.jVM)
		env := (*C.JNIEnv)(envPtr)
		defer freeFunc()
		C.DeleteGlobalRef(env, c.jContext)
	})
	return c
}

// context is the go interface to the java implementation of security.Context
type context struct {
	jVM      *C.JavaVM
	jContext C.jobject
}

func (c *context) Method() string {
	return c.callStringMethod("method")
}

func (c *context) Name() string {
	return c.callStringMethod("name")
}

func (c *context) Suffix() string {
	return c.callStringMethod("suffix")
}

func (c *context) Label() security.Label {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	labelSign := util.ClassSign("io.veyron.veyron.veyron2.security.Label")
	jLabel := C.jobject(util.CallObjectMethodOrCatch(env, c.jContext, "label", nil, labelSign))
	return security.Label(util.JIntField(env, jLabel, "value"))
}

func (c *context) Discharges() map[string]security.Discharge {
	// TODO(spetrovic): implement this method.
	return nil
}

func (c *context) LocalID() security.PublicID {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	publicIDSign := util.ClassSign("io.veyron.veyron.veyron2.security.PublicID")
	jID := C.jobject(util.CallObjectMethodOrCatch(env, c.jContext, "localID", nil, publicIDSign))
	idPtr := util.CallLongMethodOrCatch(env, jID, "getNativePtr", nil)
	return (*(*security.PublicID)(util.Ptr(idPtr)))
}

func (c *context) RemoteID() security.PublicID {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	publicIDSign := util.ClassSign("io.veyron.veyron.veyron2.security.PublicID")
	jID := C.jobject(util.CallObjectMethodOrCatch(env, c.jContext, "remoteID", nil, publicIDSign))
	idPtr := util.CallLongMethodOrCatch(env, jID, "getNativePtr", nil)
	return (*(*security.PublicID)(util.Ptr(idPtr)))
}

func (c *context) LocalEndpoint() naming.Endpoint {
	epStr := c.callStringMethod("localEndpoint")
	ep, err := inaming.NewEndpoint(epStr)
	if err != nil {
		panic("Couldn't parse endpoint string: " + epStr)
	}
	return ep
}

func (c *context) LocalPrincipal() security.Principal {
	// TODO(spetrovic): Implement this.
	return nil
}

func (c *context) RemoteBlessings() security.Blessings {
	// TODO(spetrovic): Implement this.
	return nil
}

func (c *context) RemoteEndpoint() naming.Endpoint {
	epStr := c.callStringMethod("remoteEndpoint")
	ep, err := inaming.NewEndpoint(epStr)
	if err != nil {
		panic("Couldn't parse endpoint string: " + epStr)
	}
	return ep
}

func (c *context) callStringMethod(methodName string) string {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	return util.CallStringMethodOrCatch(env, c.jContext, methodName, nil)
}<commit_msg>TBR: runtimes/google/security: Update corresponding to: https://veyron-review.googlesource.com/#/c/4920/<commit_after>// +build android

package security

import (
	"runtime"

	"veyron.io/jni/runtimes/google/util"
	inaming "veyron.io/veyron/veyron/runtimes/google/naming"
	"veyron.io/veyron/veyron2/naming"
	"veyron.io/veyron/veyron2/security"
)

// #cgo LDFLAGS: -ljniwrapper
// #include "jni_wrapper.h"
import "C"

// newJavaContext constructs a new context in java based on the passed go context.
func newJavaContext(env interface{}, context security.Context) C.jobject {
	util.GoRef(&context) // Un-refed when the Java Context object is finalized.
	return C.jobject(util.NewObjectOrCatch(env, jContextImplClass, []util.Sign{util.LongSign}, &context))
}

func newContext(env *C.JNIEnv, jContext C.jobject) *context {
	// We cannot cache Java environments as they are only valid in the current
	// thread.  We can, however, cache the Java VM and obtain an environment
	// from it in whatever thread happens to be running at the time.
	var jVM *C.JavaVM
	if status := C.GetJavaVM(env, &jVM); status != 0 {
		panic("couldn't get Java VM from the (Java) environment")
	}
	// Reference Java context; it will be de-referenced when the go context
	// created below is garbage-collected (through the finalizer callback we
	// setup just below).
	jContext = C.NewGlobalRef(env, jContext)
	c := &context{
		jVM:      jVM,
		jContext: jContext,
	}
	runtime.SetFinalizer(c, func(c *context) {
		envPtr, freeFunc := util.GetEnv(c.jVM)
		env := (*C.JNIEnv)(envPtr)
		defer freeFunc()
		C.DeleteGlobalRef(env, c.jContext)
	})
	return c
}

// context is the go interface to the java implementation of security.Context
type context struct {
	jVM      *C.JavaVM
	jContext C.jobject
}

func (c *context) Method() string {
	return c.callStringMethod("method")
}

func (c *context) Name() string {
	return c.callStringMethod("name")
}

func (c *context) Suffix() string {
	return c.callStringMethod("suffix")
}

func (c *context) Label() security.Label {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	labelSign := util.ClassSign("io.veyron.veyron.veyron2.security.Label")
	jLabel := C.jobject(util.CallObjectMethodOrCatch(env, c.jContext, "label", nil, labelSign))
	return security.Label(util.JIntField(env, jLabel, "value"))
}

func (c *context) Discharges() map[string]security.Discharge {
	// TODO(spetrovic): implement this method.
	return nil
}

func (c *context) LocalID() security.PublicID {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	publicIDSign := util.ClassSign("io.veyron.veyron.veyron2.security.PublicID")
	jID := C.jobject(util.CallObjectMethodOrCatch(env, c.jContext, "localID", nil, publicIDSign))
	idPtr := util.CallLongMethodOrCatch(env, jID, "getNativePtr", nil)
	return (*(*security.PublicID)(util.Ptr(idPtr)))
}

func (c *context) RemoteID() security.PublicID {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	publicIDSign := util.ClassSign("io.veyron.veyron.veyron2.security.PublicID")
	jID := C.jobject(util.CallObjectMethodOrCatch(env, c.jContext, "remoteID", nil, publicIDSign))
	idPtr := util.CallLongMethodOrCatch(env, jID, "getNativePtr", nil)
	return (*(*security.PublicID)(util.Ptr(idPtr)))
}

func (c *context) LocalEndpoint() naming.Endpoint {
	epStr := c.callStringMethod("localEndpoint")
	ep, err := inaming.NewEndpoint(epStr)
	if err != nil {
		panic("Couldn't parse endpoint string: " + epStr)
	}
	return ep
}

func (c *context) LocalPrincipal() security.Principal {
	// TODO(spetrovic): Implement this.
	return nil
}

func (c *context) LocalBlessings() security.Blessings {
	// TODO(spetrovic): Implement this.
	return nil
}

func (c *context) RemoteBlessings() security.Blessings {
	// TODO(spetrovic): Implement this.
	return nil
}

func (c *context) RemoteEndpoint() naming.Endpoint {
	epStr := c.callStringMethod("remoteEndpoint")
	ep, err := inaming.NewEndpoint(epStr)
	if err != nil {
		panic("Couldn't parse endpoint string: " + epStr)
	}
	return ep
}

func (c *context) callStringMethod(methodName string) string {
	envPtr, freeFunc := util.GetEnv(c.jVM)
	env := (*C.JNIEnv)(envPtr)
	defer freeFunc()
	return util.CallStringMethodOrCatch(env, c.jContext, methodName, nil)
}
{'fraction_non_alphanumeric': 0.13989821280624926, 'fraction_numerical': 0.001893715232571902, 'mean_word_length': 4.783709787816564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6326749', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2974, 'n_words': 745}