blob: a898b035e55cca261eedacf2fec7b9c479fe7895 [file] [log] [blame]
[email protected]a22998a2013-11-10 05:00:501// 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]97f21ca2013-11-17 17:46:078#include <string>
[email protected]855ab432013-11-18 17:09:369
avi90e658dd2015-12-21 07:16:1910#include "base/macros.h"
[email protected]855ab432013-11-18 17:09:3611#include "base/memory/weak_ptr.h"
[email protected]48c21632013-12-12 21:32:3412#include "gin/gin_export.h"
[email protected]36e374082013-12-06 01:51:1713#include "gin/public/context_holder.h"
[email protected]1771610d2014-02-27 06:08:2414#include "v8/include/v8.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();
22 virtual ~Runner();
[email protected]a22998a2013-11-10 05:00:5023
[email protected]60531d52013-11-27 02:10:1524 // 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]1771610d2014-02-27 06:08:2426 virtual void Run(const std::string& source,
27 const std::string& resource_name) = 0;
deepak.sfaaa1b62015-04-30 07:30:4828 virtual v8::Local<v8::Value> Call(v8::Local<v8::Function> function,
29 v8::Local<v8::Value> receiver,
[email protected]1771610d2014-02-27 06:08:2430 int argc,
deepak.sfaaa1b62015-04-30 07:30:4831 v8::Local<v8::Value> argv[]) = 0;
[email protected]1771610d2014-02-27 06:08:2432 virtual ContextHolder* GetContextHolder() = 0;
[email protected]a22998a2013-11-10 05:00:5033
deepak.sfaaa1b62015-04-30 07:30:4834 v8::Local<v8::Object> global() {
[email protected]1771610d2014-02-27 06:08:2435 return GetContextHolder()->context()->Global();
[email protected]a22998a2013-11-10 05:00:5036 }
37
[email protected]60531d52013-11-27 02:10:1538 // Useful for running script in this context asynchronously. Rather than
39 // holding a raw pointer to the runner, consider holding a WeakPtr.
[email protected]855ab432013-11-18 17:09:3640 base::WeakPtr<Runner> GetWeakPtr() {
41 return weak_factory_.GetWeakPtr();
42 }
43
[email protected]48c21632013-12-12 21:32:3444 class GIN_EXPORT Scope {
[email protected]a22998a2013-11-10 05:00:5045 public:
46 explicit Scope(Runner* runner);
47 ~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_;
53
54 DISALLOW_COPY_AND_ASSIGN(Scope);
55 };
56
57 private:
58 friend class Scope;
59
[email protected]855ab432013-11-18 17:09:3660 base::WeakPtrFactory<Runner> weak_factory_;
61
[email protected]a22998a2013-11-10 05:00:5062 DISALLOW_COPY_AND_ASSIGN(Runner);
63};
64
65} // namespace gin
66
67#endif // GIN_RUNNER_H_