[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | df55d259 | 2014-02-27 19:49:39 | [diff] [blame] | 5 | #ifndef GIN_SHELL_RUNNER_H_ |
| 6 | #define GIN_SHELL_RUNNER_H_ |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 7 | |
dcheng | 18ec0b54 | 2016-04-26 19:28:53 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 11 | #include "gin/runner.h" |
| 12 | |
| 13 | namespace gin { |
| 14 | |
| 15 | class ContextHolder; |
| 16 | class ShellRunner; |
| 17 | class TryCatch; |
| 18 | |
| 19 | // Subclass ShellRunnerDelegate to customize the behavior of ShellRunner. |
| 20 | // Typical embedders will want to subclass one of the specialized |
| 21 | // ShellRunnerDelegates, such as ModuleRunnerDelegate. |
| 22 | class GIN_EXPORT ShellRunnerDelegate { |
| 23 | public: |
| 24 | ShellRunnerDelegate(); |
| 25 | virtual ~ShellRunnerDelegate(); |
| 26 | |
| 27 | // Returns the template for the global object. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 28 | virtual v8::Local<v8::ObjectTemplate> GetGlobalTemplate( |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 29 | ShellRunner* runner, |
| 30 | v8::Isolate* isolate); |
| 31 | virtual void DidCreateContext(ShellRunner* runner); |
| 32 | virtual void WillRunScript(ShellRunner* runner); |
| 33 | virtual void DidRunScript(ShellRunner* runner); |
| 34 | virtual void UnhandledException(ShellRunner* runner, TryCatch& try_catch); |
| 35 | }; |
| 36 | |
| 37 | // ShellRunner executes the script/functions directly in a v8::Context. |
| 38 | // ShellRunner owns a ContextHolder and v8::Context, both of which are destroyed |
| 39 | // when the ShellRunner is deleted. |
| 40 | class GIN_EXPORT ShellRunner : public Runner { |
| 41 | public: |
| 42 | ShellRunner(ShellRunnerDelegate* delegate, v8::Isolate* isolate); |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 43 | ~ShellRunner() override; |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 44 | |
| 45 | // Before running script in this context, you'll need to enter the runner's |
| 46 | // context by creating an instance of Runner::Scope on the stack. |
| 47 | |
| 48 | // Runner overrides: |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 49 | void Run(const std::string& source, |
| 50 | const std::string& resource_name) override; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 51 | v8::Local<v8::Value> Call(v8::Local<v8::Function> function, |
| 52 | v8::Local<v8::Value> receiver, |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 53 | int argc, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 54 | v8::Local<v8::Value> argv[]) override; |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 55 | ContextHolder* GetContextHolder() override; |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | friend class Scope; |
| 59 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 60 | void Run(v8::Local<v8::Script> script); |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 61 | |
| 62 | ShellRunnerDelegate* delegate_; |
| 63 | |
dcheng | 18ec0b54 | 2016-04-26 19:28:53 | [diff] [blame] | 64 | std::unique_ptr<ContextHolder> context_holder_; |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(ShellRunner); |
| 67 | }; |
| 68 | |
| 69 | } // namespace gin |
| 70 | |
[email protected] | df55d259 | 2014-02-27 19:49:39 | [diff] [blame] | 71 | #endif // GIN_SHELL_RUNNER_H_ |