[M120 Migration] Set IO|GPU thread type with higher priorites
[platform/framework/web/chromium-efl.git] / gin / shell_runner.cc
1 // Copyright 2014 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.
4
5 #include "gin/shell_runner.h"
6
7 #include <memory>
8
9 #include "gin/converter.h"
10 #include "gin/per_context_data.h"
11 #include "gin/public/context_holder.h"
12 #include "gin/try_catch.h"
13 #include "v8/include/v8-script.h"
14
15 using v8::Context;
16 using v8::HandleScope;
17 using v8::Isolate;
18 using v8::Object;
19 using v8::ObjectTemplate;
20 using v8::Script;
21
22 namespace gin {
23
24 ShellRunnerDelegate::ShellRunnerDelegate() = default;
25
26 ShellRunnerDelegate::~ShellRunnerDelegate() = default;
27
28 v8::Local<ObjectTemplate> ShellRunnerDelegate::GetGlobalTemplate(
29     ShellRunner* runner,
30     v8::Isolate* isolate) {
31   return v8::Local<ObjectTemplate>();
32 }
33
34 void ShellRunnerDelegate::DidCreateContext(ShellRunner* runner) {
35 }
36
37 void ShellRunnerDelegate::WillRunScript(ShellRunner* runner) {
38 }
39
40 void ShellRunnerDelegate::DidRunScript(ShellRunner* runner) {
41 }
42
43 void ShellRunnerDelegate::UnhandledException(ShellRunner* runner,
44                                                TryCatch& try_catch) {
45   CHECK(false) << try_catch.GetStackTrace();
46 }
47
48 ShellRunner::ShellRunner(ShellRunnerDelegate* delegate, Isolate* isolate)
49     : delegate_(delegate) {
50   v8::Isolate::Scope isolate_scope(isolate);
51   HandleScope handle_scope(isolate);
52   v8::Local<v8::Context> context =
53       Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this, isolate));
54
55   context_holder_ = std::make_unique<ContextHolder>(isolate);
56   context_holder_->SetContext(context);
57   PerContextData::From(context)->set_runner(this);
58
59   v8::Context::Scope scope(context);
60   delegate_->DidCreateContext(this);
61 }
62
63 ShellRunner::~ShellRunner() = default;
64
65 v8::MaybeLocal<v8::Value> ShellRunner::Run(const std::string& source,
66                                            const std::string& resource_name) {
67   v8::Isolate* isolate = GetContextHolder()->isolate();
68   TryCatch try_catch(isolate);
69   v8::ScriptOrigin origin(isolate, StringToV8(isolate, resource_name));
70   auto maybe_script = Script::Compile(GetContextHolder()->context(),
71                                       StringToV8(isolate, source), &origin);
72   v8::Local<Script> script;
73   if (!maybe_script.ToLocal(&script)) {
74     delegate_->UnhandledException(this, try_catch);
75     return v8::MaybeLocal<v8::Value>();
76   }
77
78   return Run(script);
79 }
80
81 ContextHolder* ShellRunner::GetContextHolder() {
82   return context_holder_.get();
83 }
84
85 v8::MaybeLocal<v8::Value> ShellRunner::Run(v8::Local<Script> script) {
86   TryCatch try_catch(GetContextHolder()->isolate());
87   delegate_->WillRunScript(this);
88
89   auto maybe = script->Run(GetContextHolder()->context());
90
91   delegate_->DidRunScript(this);
92   if (maybe.IsEmpty()) {
93     delegate_->UnhandledException(this, try_catch);
94   }
95   return maybe;
96 }
97
98 }  // namespace gin