Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 2 | // 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] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 8 | #include <string> |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 9 | |
10 | #include "base/memory/weak_ptr.h" | ||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 11 | #include "gin/gin_export.h" |
[email protected] | 36e37408 | 2013-12-06 01:51:17 | [diff] [blame] | 12 | #include "gin/public/context_holder.h" |
Dan Elphick | 05acd60 | 2021-08-30 15:22:07 | [diff] [blame] | 13 | #include "v8/include/v8-forward.h" |
14 | #include "v8/include/v8-isolate.h" | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 15 | |
16 | namespace gin { | ||||
17 | |||||
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 18 | // Runner is responsible for running code in a v8::Context. |
19 | class GIN_EXPORT Runner { | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 20 | public: |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 21 | Runner(); |
Daniel Hosseinian | 68c0798d | 2021-04-16 08:16:07 | [diff] [blame] | 22 | Runner(const Runner&) = delete; |
23 | Runner& operator=(const Runner&) = delete; | ||||
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 24 | virtual ~Runner(); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 25 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 26 | // 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 Nair | 7447a431 | 2022-03-15 11:44:13 | [diff] [blame] | 28 | virtual v8::MaybeLocal<v8::Value> Run(const std::string& source, |
29 | const std::string& resource_name) = 0; | ||||
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 30 | virtual ContextHolder* GetContextHolder() = 0; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 31 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 32 | v8::Local<v8::Object> global() { |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 33 | return GetContextHolder()->context()->Global(); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 34 | } |
35 | |||||
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 36 | // Useful for running script in this context asynchronously. Rather than |
37 | // holding a raw pointer to the runner, consider holding a WeakPtr. | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 38 | base::WeakPtr<Runner> GetWeakPtr() { |
39 | return weak_factory_.GetWeakPtr(); | ||||
40 | } | ||||
41 | |||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 42 | class GIN_EXPORT Scope { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 43 | public: |
44 | explicit Scope(Runner* runner); | ||||
Daniel Hosseinian | 68c0798d | 2021-04-16 08:16:07 | [diff] [blame] | 45 | Scope(const Scope&) = delete; |
46 | Scope& operator=(const Scope&) = delete; | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 47 | ~Scope(); |
48 | |||||
49 | private: | ||||
[email protected] | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 50 | v8::Isolate::Scope isolate_scope_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 51 | v8::HandleScope handle_scope_; |
52 | v8::Context::Scope scope_; | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 53 | }; |
54 | |||||
55 | private: | ||||
56 | friend class Scope; | ||||
57 | |||||
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 58 | base::WeakPtrFactory<Runner> weak_factory_{this}; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 59 | }; |
60 | |||||
61 | } // namespace gin | ||||
62 | |||||
63 | #endif // GIN_RUNNER_H_ |