2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-07-28 08:00:15 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
|
2014-07-28 08:00:15 +00:00
|
|
|
|
2018-10-15 15:26:47 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2014-09-01 08:41:26 +00:00
|
|
|
#include "gin/public/isolate_holder.h"
|
2020-04-24 19:57:41 +00:00
|
|
|
#include "uv.h" // NOLINT(build/include_directory)
|
2021-09-01 19:55:07 +00:00
|
|
|
#include "v8/include/v8-locker.h"
|
2014-07-28 08:00:15 +00:00
|
|
|
|
2017-02-28 00:56:09 +00:00
|
|
|
namespace node {
|
|
|
|
class Environment;
|
2018-01-06 15:58:24 +00:00
|
|
|
class MultiIsolatePlatform;
|
2018-04-18 01:44:10 +00:00
|
|
|
} // namespace node
|
2017-02-28 00:56:09 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2014-07-28 08:00:15 +00:00
|
|
|
|
2018-10-15 15:26:47 +00:00
|
|
|
class MicrotasksRunner;
|
2017-02-28 00:56:09 +00:00
|
|
|
// Manage the V8 isolate and context automatically.
|
2014-07-28 08:00:15 +00:00
|
|
|
class JavascriptEnvironment {
|
|
|
|
public:
|
2018-10-05 18:40:17 +00:00
|
|
|
explicit JavascriptEnvironment(uv_loop_t* event_loop);
|
2018-04-17 23:37:22 +00:00
|
|
|
~JavascriptEnvironment();
|
2014-07-28 08:00:15 +00:00
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
JavascriptEnvironment(const JavascriptEnvironment&) = delete;
|
|
|
|
JavascriptEnvironment& operator=(const JavascriptEnvironment&) = delete;
|
|
|
|
|
2018-10-15 15:26:47 +00:00
|
|
|
void OnMessageLoopCreated();
|
2016-06-24 05:45:31 +00:00
|
|
|
void OnMessageLoopDestroying();
|
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
node::MultiIsolatePlatform* platform() const { return platform_; }
|
2014-07-28 08:00:15 +00:00
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
|
|
|
v8::Local<v8::Context> context() const {
|
|
|
|
return v8::Local<v8::Context>::New(isolate_, context_);
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:38:43 +00:00
|
|
|
static v8::Isolate* GetIsolate();
|
|
|
|
|
2014-07-28 08:00:15 +00:00
|
|
|
private:
|
2018-10-05 18:40:17 +00:00
|
|
|
v8::Isolate* Initialize(uv_loop_t* event_loop);
|
2017-12-08 00:23:17 +00:00
|
|
|
// Leaked on exit.
|
2018-01-06 15:58:24 +00:00
|
|
|
node::MultiIsolatePlatform* platform_;
|
2017-12-08 00:23:17 +00:00
|
|
|
|
2014-07-28 08:00:15 +00:00
|
|
|
v8::Isolate* isolate_;
|
2018-10-05 18:40:17 +00:00
|
|
|
gin::IsolateHolder isolate_holder_;
|
2014-07-28 08:00:15 +00:00
|
|
|
v8::Locker locker_;
|
2018-10-05 18:40:17 +00:00
|
|
|
v8::Global<v8::Context> context_;
|
2014-07-28 08:00:15 +00:00
|
|
|
|
2018-10-15 15:26:47 +00:00
|
|
|
std::unique_ptr<MicrotasksRunner> microtasks_runner_;
|
2014-07-28 08:00:15 +00:00
|
|
|
};
|
|
|
|
|
2017-02-28 00:56:09 +00:00
|
|
|
// Manage the Node Environment automatically.
|
|
|
|
class NodeEnvironment {
|
|
|
|
public:
|
2017-03-02 08:26:15 +00:00
|
|
|
explicit NodeEnvironment(node::Environment* env);
|
2017-02-28 00:56:09 +00:00
|
|
|
~NodeEnvironment();
|
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
NodeEnvironment(const NodeEnvironment&) = delete;
|
|
|
|
NodeEnvironment& operator=(const NodeEnvironment&) = delete;
|
|
|
|
|
2020-06-17 17:08:10 +00:00
|
|
|
node::Environment* env() { return env_; }
|
|
|
|
|
2017-02-28 00:56:09 +00:00
|
|
|
private:
|
|
|
|
node::Environment* env_;
|
|
|
|
};
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2014-07-28 08:00:15 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
|