1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GIN_SHELL_RUNNER_H_
6 #define GIN_SHELL_RUNNER_H_
10 #include "base/memory/raw_ptr.h"
11 #include "gin/runner.h"
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 {
24 ShellRunnerDelegate();
25 virtual ~ShellRunnerDelegate();
27 // Returns the template for the global object.
28 virtual v8::Local<v8::ObjectTemplate> GetGlobalTemplate(
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);
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 {
42 ShellRunner(ShellRunnerDelegate* delegate, v8::Isolate* isolate);
43 ShellRunner(const ShellRunner&) = delete;
44 ShellRunner& operator=(const ShellRunner&) = delete;
45 ~ShellRunner() override;
47 // Before running script in this context, you'll need to enter the runner's
48 // context by creating an instance of Runner::Scope on the stack.
51 v8::MaybeLocal<v8::Value> Run(const std::string& source,
52 const std::string& resource_name) override;
53 ContextHolder* GetContextHolder() override;
58 v8::MaybeLocal<v8::Value> Run(v8::Local<v8::Script> script);
60 raw_ptr<ShellRunnerDelegate> delegate_;
62 std::unique_ptr<ContextHolder> context_holder_;
67 #endif // GIN_SHELL_RUNNER_H_