1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef GIN_PER_ISOLATE_DATA_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define GIN_PER_ISOLATE_DATA_H_
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <map>
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/basictypes.h"
11d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)#include "gin/gin_export.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "gin/public/wrapper_info.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "v8/include/v8.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace gin {
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// There is one instance of PerIsolateData per v8::Isolate managed by Gin. This
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// class stores all the Gin-related data that varies per isolate.
19d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)class GIN_EXPORT PerIsolateData {
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  explicit PerIsolateData(v8::Isolate* isolate);
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ~PerIsolateData();
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static PerIsolateData* From(v8::Isolate* isolate);
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Each isolate is associated with a collection of v8::ObjectTemplates and
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // v8::FunctionTemplates. Typically these template objects are created
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // lazily.
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void SetObjectTemplate(WrapperInfo* info,
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                         v8::Local<v8::ObjectTemplate> object_template);
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void SetFunctionTemplate(WrapperInfo* info,
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                           v8::Local<v8::FunctionTemplate> function_template);
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // These are low-level functions for retrieving object or function templates
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // stored in this object. Because these templates are often created lazily,
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // most clients should call higher-level functions that know how to populate
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // these templates if they haven't already been created.
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info);
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info);
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  v8::Isolate* isolate() { return isolate_; }
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  typedef std::map<
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap;
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  typedef std::map<
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap;
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // owned by the IsolateHolder, which also owns the PerIsolateData.
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  v8::Isolate* isolate_;
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ObjectTemplateMap object_templates_;
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FunctionTemplateMap function_templates_;
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PerIsolateData);
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace gin
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // GIN_PER_ISOLATE_DATA_H_
61