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
3 // found in the LICENSE file.
4 
5 #ifndef GIN_PUBLIC_CONTEXT_HOLDER_H_
6 #define GIN_PUBLIC_CONTEXT_HOLDER_H_
7 
8 #include <list>
9 #include <memory>
10 
11 #include "base/macros.h"
12 #include "gin/gin_export.h"
13 #include "v8/include/v8.h"
14 
15 namespace gin {
16 
17 // Gin embedder that store embedder data in v8::Contexts must do so in a
18 // single field with the index kPerContextDataStartIndex + GinEmbedder-enum.
19 // The field at kDebugIdIndex is treated specially by V8 and is reserved for
20 // a V8 debugger implementation (not used by gin).
21 enum ContextEmbedderDataFields {
22   kDebugIdIndex = v8::Context::kDebugIdIndex,
23   kPerContextDataStartIndex,
24 };
25 
26 class PerContextData;
27 
28 // ContextHolder is a generic class for holding a v8::Context.
29 class GIN_EXPORT ContextHolder {
30  public:
31   explicit ContextHolder(v8::Isolate* isolate);
32   ~ContextHolder();
33 
isolate()34   v8::Isolate* isolate() const { return isolate_; }
35 
context()36   v8::Local<v8::Context> context() const {
37     return v8::Local<v8::Context>::New(isolate_, context_);
38   }
39 
40   void SetContext(v8::Local<v8::Context> context);
41 
42  private:
43   v8::Isolate* isolate_;
44   v8::UniquePersistent<v8::Context> context_;
45   std::unique_ptr<PerContextData> data_;
46 
47   DISALLOW_COPY_AND_ASSIGN(ContextHolder);
48 };
49 
50 }  // namespace gin
51 
52 #endif  // GIN_PUBLIC_CONTEXT_HOLDER_H_
53