[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 1 | // Copyright 2013 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 | |||||
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 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 12 | #include "gin/gin_export.h" |
[email protected] | 36e37408 | 2013-12-06 01:51:17 | [diff] [blame] | 13 | #include "gin/public/context_holder.h" |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 14 | #include "v8/include/v8.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(); |
22 | virtual ~Runner(); | ||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 23 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 24 | // Before running script in this context, you'll need to enter the runner's |
25 | // context by creating an instance of Runner::Scope on the stack. | ||||
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 26 | virtual void Run(const std::string& source, |
27 | const std::string& resource_name) = 0; | ||||
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 28 | virtual v8::Local<v8::Value> Call(v8::Local<v8::Function> function, |
29 | v8::Local<v8::Value> receiver, | ||||
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 30 | int argc, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 31 | v8::Local<v8::Value> argv[]) = 0; |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 32 | virtual ContextHolder* GetContextHolder() = 0; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 33 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 34 | v8::Local<v8::Object> global() { |
[email protected] | 1771610d | 2014-02-27 06:08:24 | [diff] [blame] | 35 | return GetContextHolder()->context()->Global(); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 36 | } |
37 | |||||
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 38 | // Useful for running script in this context asynchronously. Rather than |
39 | // holding a raw pointer to the runner, consider holding a WeakPtr. | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 40 | base::WeakPtr<Runner> GetWeakPtr() { |
41 | return weak_factory_.GetWeakPtr(); | ||||
42 | } | ||||
43 | |||||
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 44 | class GIN_EXPORT Scope { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 45 | public: |
46 | explicit Scope(Runner* runner); | ||||
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_; | ||||
53 | |||||
54 | DISALLOW_COPY_AND_ASSIGN(Scope); | ||||
55 | }; | ||||
56 | |||||
57 | private: | ||||
58 | friend class Scope; | ||||
59 | |||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 60 | base::WeakPtrFactory<Runner> weak_factory_; |
61 | |||||
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 62 | DISALLOW_COPY_AND_ASSIGN(Runner); |
63 | }; | ||||
64 | |||||
65 | } // namespace gin | ||||
66 | |||||
67 | #endif // GIN_RUNNER_H_ |