shell_runner.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef GIN_SHELL_RUNNER_H_
  5. #define GIN_SHELL_RUNNER_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "gin/runner.h"
  9. namespace gin {
  10. class ContextHolder;
  11. class ShellRunner;
  12. class TryCatch;
  13. // Subclass ShellRunnerDelegate to customize the behavior of ShellRunner.
  14. // Typical embedders will want to subclass one of the specialized
  15. // ShellRunnerDelegates, such as ModuleRunnerDelegate.
  16. class GIN_EXPORT ShellRunnerDelegate {
  17. public:
  18. ShellRunnerDelegate();
  19. virtual ~ShellRunnerDelegate();
  20. // Returns the template for the global object.
  21. virtual v8::Local<v8::ObjectTemplate> GetGlobalTemplate(
  22. ShellRunner* runner,
  23. v8::Isolate* isolate);
  24. virtual void DidCreateContext(ShellRunner* runner);
  25. virtual void WillRunScript(ShellRunner* runner);
  26. virtual void DidRunScript(ShellRunner* runner);
  27. virtual void UnhandledException(ShellRunner* runner, TryCatch& try_catch);
  28. };
  29. // ShellRunner executes the script/functions directly in a v8::Context.
  30. // ShellRunner owns a ContextHolder and v8::Context, both of which are destroyed
  31. // when the ShellRunner is deleted.
  32. class GIN_EXPORT ShellRunner : public Runner {
  33. public:
  34. ShellRunner(ShellRunnerDelegate* delegate, v8::Isolate* isolate);
  35. ShellRunner(const ShellRunner&) = delete;
  36. ShellRunner& operator=(const ShellRunner&) = delete;
  37. ~ShellRunner() override;
  38. // Before running script in this context, you'll need to enter the runner's
  39. // context by creating an instance of Runner::Scope on the stack.
  40. // Runner overrides:
  41. v8::MaybeLocal<v8::Value> Run(const std::string& source,
  42. const std::string& resource_name) override;
  43. ContextHolder* GetContextHolder() override;
  44. private:
  45. friend class Scope;
  46. v8::MaybeLocal<v8::Value> Run(v8::Local<v8::Script> script);
  47. raw_ptr<ShellRunnerDelegate> delegate_;
  48. std::unique_ptr<ContextHolder> context_holder_;
  49. };
  50. } // namespace gin
  51. #endif // GIN_SHELL_RUNNER_H_