| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_RUNNER_H_ | 5 #ifndef GIN_RUNNER_H_ |
| 6 #define GIN_RUNNER_H_ | 6 #define GIN_RUNNER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "gin/gin_export.h" | 11 #include "gin/gin_export.h" |
| 12 #include "gin/public/context_holder.h" | 12 #include "gin/public/context_holder.h" |
| 13 #include "v8/include/v8.h" |
| 13 | 14 |
| 14 namespace gin { | 15 namespace gin { |
| 15 | 16 |
| 16 class Runner; | 17 // Runner is responsible for running code in a v8::Context. |
| 17 class TryCatch; | 18 class GIN_EXPORT Runner { |
| 18 | |
| 19 // Subclass RunnerDelegate to customize the behavior of |Runner|. Typical | |
| 20 // embedders will want to subclass one of the specialized RunnerDelegates, | |
| 21 // such as ModuleRunnerDelegate. | |
| 22 class GIN_EXPORT RunnerDelegate { | |
| 23 public: | 19 public: |
| 24 RunnerDelegate(); | 20 Runner(); |
| 25 virtual ~RunnerDelegate(); | 21 virtual ~Runner(); |
| 26 | |
| 27 // Returns the template for the global object. | |
| 28 virtual v8::Handle<v8::ObjectTemplate> GetGlobalTemplate(Runner* runner); | |
| 29 virtual void DidCreateContext(Runner* runner); | |
| 30 virtual void WillRunScript(Runner* runner); | |
| 31 virtual void DidRunScript(Runner* runner); | |
| 32 virtual void UnhandledException(Runner* runner, TryCatch& try_catch); | |
| 33 }; | |
| 34 | |
| 35 // Runner lets you run code in a v8::Context. Upon construction, Runner will | |
| 36 // create a v8::Context. Upon destruction, Runner will dispose the context. | |
| 37 class GIN_EXPORT Runner : public ContextHolder { | |
| 38 public: | |
| 39 Runner(RunnerDelegate* delegate, v8::Isolate* isolate); | |
| 40 ~Runner(); | |
| 41 | 22 |
| 42 // Before running script in this context, you'll need to enter the runner's | 23 // Before running script in this context, you'll need to enter the runner's |
| 43 // context by creating an instance of Runner::Scope on the stack. | 24 // context by creating an instance of Runner::Scope on the stack. |
| 44 void Run(const std::string& source, const std::string& resource_name); | 25 virtual void Run(const std::string& source, |
| 45 void Run(v8::Handle<v8::Script> script); | 26 const std::string& resource_name) = 0; |
| 27 virtual v8::Handle<v8::Value> Call(v8::Handle<v8::Function> function, |
| 28 v8::Handle<v8::Value> receiver, |
| 29 int argc, |
| 30 v8::Handle<v8::Value> argv[]) = 0; |
| 31 virtual ContextHolder* GetContextHolder() = 0; |
| 46 | 32 |
| 47 v8::Handle<v8::Value> Call(v8::Handle<v8::Function> function, | 33 v8::Handle<v8::Object> global() { |
| 48 v8::Handle<v8::Value> receiver, | 34 return GetContextHolder()->context()->Global(); |
| 49 int argc, | |
| 50 v8::Handle<v8::Value> argv[]); | |
| 51 | |
| 52 v8::Handle<v8::Object> global() const { | |
| 53 return context()->Global(); | |
| 54 } | 35 } |
| 55 | 36 |
| 56 // Useful for running script in this context asynchronously. Rather than | 37 // Useful for running script in this context asynchronously. Rather than |
| 57 // holding a raw pointer to the runner, consider holding a WeakPtr. | 38 // holding a raw pointer to the runner, consider holding a WeakPtr. |
| 58 base::WeakPtr<Runner> GetWeakPtr() { | 39 base::WeakPtr<Runner> GetWeakPtr() { |
| 59 return weak_factory_.GetWeakPtr(); | 40 return weak_factory_.GetWeakPtr(); |
| 60 } | 41 } |
| 61 | 42 |
| 62 class GIN_EXPORT Scope { | 43 class GIN_EXPORT Scope { |
| 63 public: | 44 public: |
| 64 explicit Scope(Runner* runner); | 45 explicit Scope(Runner* runner); |
| 65 ~Scope(); | 46 ~Scope(); |
| 66 | 47 |
| 67 private: | 48 private: |
| 68 v8::Isolate::Scope isolate_scope_; | 49 v8::Isolate::Scope isolate_scope_; |
| 69 v8::HandleScope handle_scope_; | 50 v8::HandleScope handle_scope_; |
| 70 v8::Context::Scope scope_; | 51 v8::Context::Scope scope_; |
| 71 | 52 |
| 72 DISALLOW_COPY_AND_ASSIGN(Scope); | 53 DISALLOW_COPY_AND_ASSIGN(Scope); |
| 73 }; | 54 }; |
| 74 | 55 |
| 75 private: | 56 private: |
| 76 friend class Scope; | 57 friend class Scope; |
| 77 | 58 |
| 78 RunnerDelegate* delegate_; | |
| 79 | |
| 80 base::WeakPtrFactory<Runner> weak_factory_; | 59 base::WeakPtrFactory<Runner> weak_factory_; |
| 81 | 60 |
| 82 DISALLOW_COPY_AND_ASSIGN(Runner); | 61 DISALLOW_COPY_AND_ASSIGN(Runner); |
| 83 }; | 62 }; |
| 84 | 63 |
| 85 } // namespace gin | 64 } // namespace gin |
| 86 | 65 |
| 87 #endif // GIN_RUNNER_H_ | 66 #endif // GIN_RUNNER_H_ |
| OLD | NEW |