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