1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113

content / renderer / pepper / host_var_tracker.h [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_
#define CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_

#include <stdint.h>

#include <map>

#include "base/memory/raw_ptr.h"
#include "content/common/content_export.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/shared_impl/host_resource.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/var_tracker.h"
#include "v8/include/v8-forward.h"

namespace ppapi {
class ArrayBufferVar;
class V8ObjectVar;
}

namespace content {

class HostVarTracker : public ppapi::VarTracker {
 public:
  HostVarTracker();

  HostVarTracker(const HostVarTracker&) = delete;
  HostVarTracker& operator=(const HostVarTracker&) = delete;

  ~HostVarTracker() override;

  // Tracks all live V8ObjectVar. This is so we can map between instance +
  // V8Object and get the V8ObjectVar corresponding to it. This Add/Remove
  // function is called by the V8ObjectVar when it is created and destroyed.
  void AddV8ObjectVar(ppapi::V8ObjectVar* object_var);
  void RemoveV8ObjectVar(ppapi::V8ObjectVar* object_var);
  // Creates or retrieves a V8ObjectVar.
  PP_Var V8ObjectVarForV8Object(PP_Instance instance,
                                v8::Local<v8::Object> object);
  // Returns the number of V8ObjectVars associated with the given instance.
  // Returns 0 if the instance isn't known.
  CONTENT_EXPORT int GetLiveV8ObjectVarsForTest(PP_Instance instance);

  // VarTracker public implementation.
  PP_Var MakeResourcePPVarFromMessage(PP_Instance instance,
                                      const IPC::Message& creation_message,
                                      int pending_renderer_id,
                                      int pending_browser_id) override;
  ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) override;
  void DidDeleteInstance(PP_Instance pp_instance) override;

  int TrackSharedMemoryRegion(PP_Instance instance,
                              base::UnsafeSharedMemoryRegion region,
                              uint32_t size_in_bytes) override;
  bool StopTrackingSharedMemoryRegion(int id,
                                      PP_Instance instance,
                                      base::UnsafeSharedMemoryRegion* region,
                                      uint32_t* size_in_bytes) override;

 private:
  // VarTracker private implementation.
  ppapi::ArrayBufferVar* CreateArrayBuffer(uint32_t size_in_bytes) override;
  ppapi::ArrayBufferVar* CreateShmArrayBuffer(
      uint32_t size_in_bytes,
      base::UnsafeSharedMemoryRegion region) override;

  // Clear the reference count of the given object and remove it from
  // live_vars_.
  void ForceReleaseV8Object(ppapi::V8ObjectVar* object_var);

  // A non-unique, ordered key for a V8ObjectVar. Contains the hash of the v8
  // and the instance it is associated with.
  struct V8ObjectVarKey {
    explicit V8ObjectVarKey(ppapi::V8ObjectVar* object_var);
    V8ObjectVarKey(PP_Instance i, v8::Local<v8::Object> object);
    ~V8ObjectVarKey();

    bool operator<(const V8ObjectVarKey& other) const;

    PP_Instance instance;
    int hash;
  };
  typedef std::multimap<V8ObjectVarKey,
                        raw_ptr<ppapi::V8ObjectVar, CtnExperimental>>
      ObjectMap;

  // Returns an iterator into |object_map| which points to V8Object which
  // is associated with the given instance and object.
  ObjectMap::iterator GetForV8Object(PP_Instance instance,
                                     v8::Local<v8::Object> object);


  // A multimap of V8ObjectVarKey -> ObjectMap.
  ObjectMap object_map_;

  // Tracks all shared memory handles used for transmitting array buffers.
  struct SharedMemoryMapEntry {
    PP_Instance instance;
    base::UnsafeSharedMemoryRegion region;
    uint32_t size_in_bytes;
  };
  typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap;
  SharedMemoryMap shared_memory_map_;
  uint32_t last_shared_memory_map_id_;
};

}  // namespace content

#endif  // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_