Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose process.env from sandbox renderer #12166

Merged
merged 1 commit into from
Mar 14, 2018

Conversation

juturu
Copy link
Contributor

@juturu juturu commented Mar 8, 2018

In non sandbox renderer, apps can use process.env as a way to pass data from main process to renderer process.
For example:
In main process, set process.env.foo = 'bar'. This value can be retrieved in the renderer process using process.env.foo.

This PR provides this functionality in sandbox renderer process.

@juturu juturu requested review from tarruda and a team March 8, 2018 05:24
@codebytere
Copy link
Member

codebytere commented Mar 8, 2018

@juturu in your pull request, we'd appreciate if you could provide

  1. What exactly this pr does in more detail
  2. What the benefits/effects of this are and how it will affect end-users

Copy link
Contributor

@tarruda tarruda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply make process.env redirect to the browser process env? It is not like the renderer will have a different environment. If there are differences, it will probably be internal to chromium and not useful for electron apps.

If you simply copy process.env from the browser process at renderer initialization (eg process.env = remote.process.env), the implementation will be much simpler and in practice it will have the same effect.

Another option would be having process.env as an alias to remote.process.env, which would allow one to pick changes to the browser process environment (though how useful that would be I can't say for sure)

@juturu
Copy link
Contributor Author

juturu commented Mar 8, 2018

@codebytere updated the description.

if (property->IsSymbol()) {
return info.GetReturnValue().SetUndefined();
}
#ifdef __POSIX__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be #if defined(OS_POSIX).

@juturu
Copy link
Contributor Author

juturu commented Mar 8, 2018

@tarruda the reason i went with this approach was to bring parity with non sandbox renderer.

If you simply copy process.env from the browser process at renderer initialization (eg process.env = remote.process.env), the implementation will be much simpler and in practice it will have the same effect.

There will be cases where main process is changing process.env and reading from renderer will not work.

Another option would be having process.env as an alias to remote.process.env, which would allow one to pick changes to the browser process environment (though how useful that would be I can't say for sure)

I'm not in favor of using remote as it is synchronous retrieval and can cause hangs in apps.

Let me know if see any issues with this.

@MarshallOfSound
Copy link
Member

There will be cases where main process is changing process.env and reading from renderer will not work.

This is how it currently works in non-sandboxed renderers, the process.env is not linked to the main process. Changing things in the main process will not update them in the renderer.

@juturu
Copy link
Contributor Author

juturu commented Mar 9, 2018

Thanks @MarshallOfSound. If we don't have these scenarios then i can remove the setter/deleter and enumerator methods on process.env. At that point only EnvGetter method is added. Does that sound good @tarruda @MarshallOfSound ?

@tarruda
Copy link
Contributor

tarruda commented Mar 9, 2018

There will be cases where main process is changing process.env and reading from renderer will not work.

As @MarshallOfSound explained, the environment is not linked. It is inherited when the process is spawned and then becomes independent. Retrieving the browser process environment at startup via remote would have the same practical effect.

I'm not in favor of using remote as it is synchronous retrieval and can cause hangs in apps.

We already use synchronous IPC during startup to read the preload script, one more call to retrieve the environment won't have a significant impact. If optimization is a concern, you can modify the RPC that returns the preload script to return [preload, process.env] in a single call.

At that point only EnvGetter method is added. Does that sound good @tarruda @MarshallOfSound ?

I'd rather simply use remote.process.env , it will be a couple of extra javascript lines versus a bunch of extra C++

@juturu juturu force-pushed the sandbox_expose_processenv branch 2 times, most recently from 08f332e to 3092e4c Compare March 9, 2018 01:30
@juturu
Copy link
Contributor Author

juturu commented Mar 9, 2018

@tarruda @MarshallOfSound i made the change to JS. If i run through a quick-start app, i see process.env getting reflected with this change as expected. Also variables defined in main process can be retrieved in renderer process.

But, the test i added seem to not get just the variable process.env.main in the renderer process. Other properties on process.env are available. Any issue you can spot in the test?

assert.equal(test, 'foo')
done()
})
process.env.main = 'foo'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any issue you can spot in the test?

This is the issue: The tests are run in an electron renderer and you are assigning to the renderer environment. To assign to the browser process environment, use remote.process.env.main = 'foo'

@tarruda
Copy link
Contributor

tarruda commented Mar 9, 2018

Looks good, but I suggest to squash the commits and reduce cluttering of master branch

@juturu juturu force-pushed the sandbox_expose_processenv branch from cf3713a to b7684a7 Compare March 9, 2018 16:06
@juturu juturu force-pushed the sandbox_expose_processenv branch from b7684a7 to ec083b6 Compare March 9, 2018 16:18
@MarshallOfSound MarshallOfSound added the semver/minor backwards-compatible functionality label Mar 14, 2018
Copy link
Member

@MarshallOfSound MarshallOfSound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@MarshallOfSound MarshallOfSound merged commit 704af29 into master Mar 14, 2018
@MarshallOfSound MarshallOfSound deleted the sandbox_expose_processenv branch March 14, 2018 03:01
sethlu pushed a commit to sethlu/electron that referenced this pull request May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/minor backwards-compatible functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants