blob: 0fcdecc6583c79f4fa5cd873776ddb33eaf0a38f [file] [log] [blame]
Avi Drissman468e51b62022-09-13 20:47:011// Copyright 2013 The Chromium Authors
[email protected]a22998a2013-11-10 05:00:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef GIN_RUNNER_H_
6#define GIN_RUNNER_H_
7
[email protected]97f21ca2013-11-17 17:46:078#include <string>
[email protected]855ab432013-11-18 17:09:369
10#include "base/memory/weak_ptr.h"
[email protected]48c21632013-12-12 21:32:3411#include "gin/gin_export.h"
[email protected]36e374082013-12-06 01:51:1712#include "gin/public/context_holder.h"
Dan Elphick05acd602021-08-30 15:22:0713#include "v8/include/v8-forward.h"
14#include "v8/include/v8-isolate.h"
[email protected]a22998a2013-11-10 05:00:5015
16namespace gin {
17
[email protected]1771610d2014-02-27 06:08:2418// Runner is responsible for running code in a v8::Context.
19class GIN_EXPORT Runner {
[email protected]a22998a2013-11-10 05:00:5020 public:
[email protected]1771610d2014-02-27 06:08:2421 Runner();
Daniel Hosseinian68c0798d2021-04-16 08:16:0722 Runner(const Runner&) = delete;
23 Runner& operator=(const Runner&) = delete;
[email protected]1771610d2014-02-27 06:08:2424 virtual ~Runner();
[email protected]a22998a2013-11-10 05:00:5025
[email protected]60531d52013-11-27 02:10:1526 // Before running script in this context, you'll need to enter the runner's
27 // context by creating an instance of Runner::Scope on the stack.
Abhijith Nair7447a4312022-03-15 11:44:1328 virtual v8::MaybeLocal<v8::Value> Run(const std::string& source,
29 const std::string& resource_name) = 0;
[email protected]1771610d2014-02-27 06:08:2430 virtual ContextHolder* GetContextHolder() = 0;
[email protected]a22998a2013-11-10 05:00:5031
deepak.sfaaa1b62015-04-30 07:30:4832 v8::Local<v8::Object> global() {
[email protected]1771610d2014-02-27 06:08:2433 return GetContextHolder()->context()->Global();
[email protected]a22998a2013-11-10 05:00:5034 }
35
[email protected]60531d52013-11-27 02:10:1536 // Useful for running script in this context asynchronously. Rather than
37 // holding a raw pointer to the runner, consider holding a WeakPtr.
[email protected]855ab432013-11-18 17:09:3638 base::WeakPtr<Runner> GetWeakPtr() {
39 return weak_factory_.GetWeakPtr();
40 }
41
[email protected]48c21632013-12-12 21:32:3442 class GIN_EXPORT Scope {
[email protected]a22998a2013-11-10 05:00:5043 public:
44 explicit Scope(Runner* runner);
Daniel Hosseinian68c0798d2021-04-16 08:16:0745 Scope(const Scope&) = delete;
46 Scope& operator=(const Scope&) = delete;
[email protected]a22998a2013-11-10 05:00:5047 ~Scope();
48
49 private:
[email protected]1b93c232013-11-19 19:25:1250 v8::Isolate::Scope isolate_scope_;
[email protected]a22998a2013-11-10 05:00:5051 v8::HandleScope handle_scope_;
52 v8::Context::Scope scope_;
[email protected]a22998a2013-11-10 05:00:5053 };
54
55 private:
56 friend class Scope;
57
Jeremy Roman7c5cfabd2019-08-12 15:45:2758 base::WeakPtrFactory<Runner> weak_factory_{this};
[email protected]a22998a2013-11-10 05:00:5059};
60
61} // namespace gin
62
63#endif // GIN_RUNNER_H_