1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "base/memory/weak_ptr.h"
11 #include "gin/gin_export.h"
12 #include "gin/public/context_holder.h"
13 #include "v8/include/v8-forward.h"
14 #include "v8/include/v8-isolate.h"
18 // Runner is responsible for running code in a v8::Context.
19 class GIN_EXPORT Runner {
22 Runner(const Runner&) = delete;
23 Runner& operator=(const Runner&) = delete;
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.
28 virtual v8::MaybeLocal<v8::Value> Run(const std::string& source,
29 const std::string& resource_name) = 0;
30 virtual ContextHolder* GetContextHolder() = 0;
32 v8::Local<v8::Object> global() {
33 return GetContextHolder()->context()->Global();
36 // Useful for running script in this context asynchronously. Rather than
37 // holding a raw pointer to the runner, consider holding a WeakPtr.
38 base::WeakPtr<Runner> GetWeakPtr() {
39 return weak_factory_.GetWeakPtr();
42 class GIN_EXPORT Scope {
44 explicit Scope(Runner* runner);
45 Scope(const Scope&) = delete;
46 Scope& operator=(const Scope&) = delete;
50 v8::Isolate::Scope isolate_scope_;
51 v8::HandleScope handle_scope_;
52 v8::Context::Scope scope_;
58 base::WeakPtrFactory<Runner> weak_factory_{this};
63 #endif // GIN_RUNNER_H_