wspr/nacl: Set namespace roots received via config.js

Change-Id: Ie251cf1fb8b4f544fbda06768839c5e7d935cef0
diff --git a/services/wsprd/app/app.go b/services/wsprd/app/app.go
index 3d13afb..1ac1363 100644
--- a/services/wsprd/app/app.go
+++ b/services/wsprd/app/app.go
@@ -146,12 +146,14 @@
 // NewController creates a new Controller.  writerCreator will be used to create a new flow for rpcs to
 // javascript server. veyronProxyEP is an endpoint for the veyron proxy to serve through.  It can't be empty.
 // opts are any options that should be passed to the rt.New().
-func NewController(writerCreator func(id int64) lib.ClientWriter,
-	listenSpec *ipc.ListenSpec, opts ...veyron2.ROpt) (*Controller, error) {
+func NewController(writerCreator func(id int64) lib.ClientWriter, listenSpec *ipc.ListenSpec, namespaceRoots []string, opts ...veyron2.ROpt) (*Controller, error) {
 	r, err := rt.New(opts...)
 	if err != nil {
 		return nil, err
 	}
+	if namespaceRoots != nil {
+		r.Namespace().SetRoots(namespaceRoots...)
+	}
 	client, err := r.NewClient()
 	if err != nil {
 		return nil, err
diff --git a/services/wsprd/app/app_test.go b/services/wsprd/app/app_test.go
index ebf95d8..afa0eb8 100644
--- a/services/wsprd/app/app_test.go
+++ b/services/wsprd/app/app_test.go
@@ -195,7 +195,7 @@
 	defer s.Stop()
 	spec := profiles.LocalListenSpec
 	spec.Proxy = "mockVeyronProxyEP"
-	controller, err := NewController(nil, &spec, options.RuntimePrincipal{newBlessedPrincipal(r)})
+	controller, err := NewController(nil, &spec, nil, options.RuntimePrincipal{newBlessedPrincipal(r)})
 
 	if err != nil {
 		t.Fatalf("Failed to create controller: %v", err)
@@ -231,7 +231,7 @@
 
 	spec := profiles.LocalListenSpec
 	spec.Proxy = "mockVeyronProxyEP"
-	controller, err := NewController(nil, &spec, options.RuntimePrincipal{newBlessedPrincipal(r)})
+	controller, err := NewController(nil, &spec, nil, options.RuntimePrincipal{newBlessedPrincipal(r)})
 
 	if err != nil {
 		t.Errorf("unable to create controller: %v", err)
@@ -355,7 +355,7 @@
 	}
 	spec := profiles.LocalListenSpec
 	spec.Proxy = "/" + proxyEndpoint
-	controller, err := NewController(writerCreator, &spec, options.RuntimePrincipal{testPrincipal})
+	controller, err := NewController(writerCreator, &spec, nil, options.RuntimePrincipal{testPrincipal})
 
 	if err != nil {
 		return nil, err
diff --git a/services/wsprd/wspr.go b/services/wsprd/wspr.go
index b8c76e3..8b84612 100644
--- a/services/wsprd/wspr.go
+++ b/services/wsprd/wspr.go
@@ -18,7 +18,7 @@
 
 	rt.Init()
 
-	proxy := wspr.NewWSPR(*port, roaming.ListenSpec, *identd)
+	proxy := wspr.NewWSPR(*port, roaming.ListenSpec, *identd, nil)
 	defer proxy.Shutdown()
 
 	proxy.Listen()
diff --git a/services/wsprd/wspr/pipe.go b/services/wsprd/wspr/pipe.go
index 7678609..2333317 100644
--- a/services/wsprd/wspr/pipe.go
+++ b/services/wsprd/wspr/pipe.go
@@ -127,7 +127,7 @@
 		// TODO(bjornick): Send an error to the client when all of the principal stuff is set up.
 	}
 
-	pipe.controller, err = app.NewController(creator, &wspr.listenSpec, options.RuntimePrincipal{p})
+	pipe.controller, err = app.NewController(creator, &wspr.listenSpec, wspr.namespaceRoots, options.RuntimePrincipal{p})
 	if err != nil {
 		wspr.rt.Logger().Errorf("Could not create controller: %v", err)
 		http.Error(w, fmt.Sprintf("Failed to create controller: %v", err), http.StatusInternalServerError)
diff --git a/services/wsprd/wspr/wspr.go b/services/wsprd/wspr/wspr.go
index e46ad83..a1c48db 100644
--- a/services/wsprd/wspr/wspr.go
+++ b/services/wsprd/wspr/wspr.go
@@ -79,6 +79,7 @@
 	logger           vlog.Logger
 	listenSpec       ipc.ListenSpec
 	identdEP         string
+	namespaceRoots   []string
 	principalManager *principal.PrincipalManager
 	blesser          blesserService
 	pipes            map[*http.Request]*pipe
@@ -158,7 +159,7 @@
 }
 
 // Creates a new WebSocket Proxy object.
-func NewWSPR(httpPort int, listenSpec ipc.ListenSpec, identdEP string, opts ...veyron2.ROpt) *WSPR {
+func NewWSPR(httpPort int, listenSpec ipc.ListenSpec, identdEP string, namespaceRoots []string, opts ...veyron2.ROpt) *WSPR {
 	if listenSpec.Proxy == "" {
 		vlog.Fatalf("a veyron proxy must be set")
 	}
@@ -170,14 +171,18 @@
 	if err != nil {
 		vlog.Fatalf("rt.New failed: %s", err)
 	}
+	if namespaceRoots != nil {
+		newrt.Namespace().SetRoots(namespaceRoots...)
+	}
 
 	wspr := &WSPR{
-		httpPort:   httpPort,
-		listenSpec: listenSpec,
-		identdEP:   identdEP,
-		rt:         newrt,
-		logger:     newrt.Logger(),
-		pipes:      map[*http.Request]*pipe{},
+		httpPort:       httpPort,
+		listenSpec:     listenSpec,
+		identdEP:       identdEP,
+		namespaceRoots: namespaceRoots,
+		rt:             newrt,
+		logger:         newrt.Logger(),
+		pipes:          map[*http.Request]*pipe{},
 	}
 
 	// TODO(nlacasse, bjornick) use a serializer that can actually persist.
diff --git a/services/wsprd/wspr/wspr_test.go b/services/wsprd/wspr/wspr_test.go
index 458bc64..dbd30ef 100644
--- a/services/wsprd/wspr/wspr_test.go
+++ b/services/wsprd/wspr/wspr_test.go
@@ -47,7 +47,7 @@
 func setup(t *testing.T) (*WSPR, func()) {
 	spec := profiles.LocalListenSpec
 	spec.Proxy = "/mock/proxy"
-	wspr := NewWSPR(0, spec, "/mock/identd")
+	wspr := NewWSPR(0, spec, "/mock/identd", nil)
 	wspr.blesser = newMockBlesserService(wspr.rt.Principal())
 	return wspr, func() {
 		wspr.Shutdown()
diff --git a/services/wsprd/wspr_nacl/main_nacl.go b/services/wsprd/wspr_nacl/main_nacl.go
index b3c7ef8..5c1f491 100644
--- a/services/wsprd/wspr_nacl/main_nacl.go
+++ b/services/wsprd/wspr_nacl/main_nacl.go
@@ -5,7 +5,6 @@
 	"crypto/ecdsa"
 	"fmt"
 	"runtime/ppapi"
-	"syscall"
 
 	"veyron.io/veyron/veyron2/ipc"
 	"veyron.io/veyron/veyron2/options"
@@ -13,7 +12,6 @@
 	"veyron.io/veyron/veyron2/security"
 	"veyron.io/wspr/veyron/services/wsprd/wspr"
 
-	"veyron.io/veyron/veyron/lib/flags"
 	_ "veyron.io/veyron/veyron/profiles"
 	vsecurity "veyron.io/veyron/veyron/security"
 )
@@ -96,8 +94,7 @@
 	if err := vsecurity.InitDefaultBlessings(principal, defaultBlessingName); err != nil {
 		panic(err.Error())
 	}
-
-	rt.Init(options.RuntimePrincipal{principal})
+	runtime := rt.Init(options.RuntimePrincipal{principal})
 
 	veyronProxy, err := message.LookupStringValuedKey("proxyName")
 	if err != nil {
@@ -111,8 +108,7 @@
 	if err != nil {
 		panic(err.Error())
 	}
-	syscall.Setenv("MOUNTTABLE_ROOT", mounttable)
-	syscall.Setenv(flags.NamespaceRootPrefix, mounttable)
+	runtime.Namespace().SetRoots(mounttable)
 
 	identd, err := message.LookupStringValuedKey("identityd")
 	if err != nil {
@@ -133,7 +129,7 @@
 	}
 
 	fmt.Printf("Starting WSPR with config: proxy=%q mounttable=%q identityd=%q port=%d", veyronProxy, mounttable, identd, wsprHttpPort)
-	proxy := wspr.NewWSPR(wsprHttpPort, listenSpec, identd, options.RuntimePrincipal{principal})
+	proxy := wspr.NewWSPR(wsprHttpPort, listenSpec, identd, []string{mounttable}, options.RuntimePrincipal{principal})
 
 	proxy.Listen()
 	go func() {