Home | History | Annotate | Download | only in fxjs
      1 // Copyright 2016 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #ifndef FXJS_CFXJSE_ISOLATETRACKER_H_
      8 #define FXJS_CFXJSE_ISOLATETRACKER_H_
      9 
     10 #include <map>
     11 #include <memory>
     12 #include <vector>
     13 
     14 #include "v8/include/v8.h"
     15 
     16 #include "fxjs/cfxjse_runtimedata.h"
     17 
     18 class CFXJSE_ScopeUtil_IsolateHandle {
     19  public:
     20   explicit CFXJSE_ScopeUtil_IsolateHandle(v8::Isolate* pIsolate)
     21       : m_isolate(pIsolate), m_iscope(pIsolate), m_hscope(pIsolate) {}
     22   v8::Isolate* GetIsolate() { return m_isolate; }
     23 
     24  private:
     25   CFXJSE_ScopeUtil_IsolateHandle(const CFXJSE_ScopeUtil_IsolateHandle&) =
     26       delete;
     27   void operator=(const CFXJSE_ScopeUtil_IsolateHandle&) = delete;
     28   void* operator new(size_t size) = delete;
     29   void operator delete(void*, size_t) = delete;
     30 
     31   v8::Isolate* m_isolate;
     32   v8::Isolate::Scope m_iscope;
     33   v8::HandleScope m_hscope;
     34 };
     35 
     36 class CFXJSE_ScopeUtil_IsolateHandleRootContext {
     37  public:
     38   explicit CFXJSE_ScopeUtil_IsolateHandleRootContext(v8::Isolate* pIsolate)
     39       : m_parent(pIsolate),
     40         m_cscope(v8::Local<v8::Context>::New(
     41             pIsolate,
     42             CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext)) {}
     43 
     44  private:
     45   CFXJSE_ScopeUtil_IsolateHandleRootContext(
     46       const CFXJSE_ScopeUtil_IsolateHandleRootContext&) = delete;
     47   void operator=(const CFXJSE_ScopeUtil_IsolateHandleRootContext&) = delete;
     48   void* operator new(size_t size) = delete;
     49   void operator delete(void*, size_t) = delete;
     50 
     51   CFXJSE_ScopeUtil_IsolateHandle m_parent;
     52   v8::Context::Scope m_cscope;
     53 };
     54 
     55 class CFXJSE_IsolateTracker {
     56  public:
     57   typedef void (*DisposeCallback)(v8::Isolate*, bool bOwnedIsolate);
     58 
     59   CFXJSE_IsolateTracker();
     60   ~CFXJSE_IsolateTracker();
     61 
     62   void Append(v8::Isolate* pIsolate,
     63               std::unique_ptr<v8::ArrayBuffer::Allocator> alloc);
     64   void Remove(v8::Isolate* pIsolate, DisposeCallback lpfnDisposeCallback);
     65   void RemoveAll(DisposeCallback lpfnDisposeCallback);
     66 
     67   static CFXJSE_IsolateTracker* g_pInstance;
     68 
     69  protected:
     70   std::vector<v8::Isolate*> m_OwnedIsolates;
     71   std::map<v8::Isolate*, std::unique_ptr<v8::ArrayBuffer::Allocator>>
     72       m_AllocatorMap;
     73 };
     74 
     75 #endif  // FXJS_CFXJSE_ISOLATETRACKER_H_
     76