Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / gin / runner.h
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.
4
5 #ifndef GIN_RUNNER_H_
6 #define GIN_RUNNER_H_
7
8 #include <string>
9
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"
15
16 namespace gin {
17
18 // Runner is responsible for running code in a v8::Context.
19 class GIN_EXPORT Runner {
20  public:
21   Runner();
22   Runner(const Runner&) = delete;
23   Runner& operator=(const Runner&) = delete;
24   virtual ~Runner();
25
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;
31
32   v8::Local<v8::Object> global() {
33     return GetContextHolder()->context()->Global();
34   }
35
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();
40   }
41
42   class GIN_EXPORT Scope {
43    public:
44     explicit Scope(Runner* runner);
45     Scope(const Scope&) = delete;
46     Scope& operator=(const Scope&) = delete;
47     ~Scope();
48
49    private:
50     v8::Isolate::Scope isolate_scope_;
51     v8::HandleScope handle_scope_;
52     v8::Context::Scope scope_;
53   };
54
55  private:
56   friend class Scope;
57
58   base::WeakPtrFactory<Runner> weak_factory_{this};
59 };
60
61 }  // namespace gin
62
63 #endif  // GIN_RUNNER_H_