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
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192

content / renderer / pepper / host_var_tracker.cc [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.

#include "content/renderer/pepper/host_var_tracker.h"

#include <tuple>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/not_fatal_until.h"
#include "base/notreached.h"
#include "content/renderer/pepper/host_array_buffer_var.h"
#include "content/renderer/pepper/host_globals.h"
#include "content/renderer/pepper/host_resource_var.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "content/renderer/pepper/v8object_var.h"
#include "ppapi/c/pp_var.h"

using ppapi::ArrayBufferVar;
using ppapi::V8ObjectVar;

namespace content {

HostVarTracker::V8ObjectVarKey::V8ObjectVarKey(V8ObjectVar* object_var)
    : instance(object_var->instance()->pp_instance()) {
  v8::Local<v8::Object> object = object_var->GetHandle();
  hash = object.IsEmpty() ? 0 : object->GetIdentityHash();
}

HostVarTracker::V8ObjectVarKey::V8ObjectVarKey(PP_Instance instance,
                                               v8::Local<v8::Object> object)
    : instance(instance),
      hash(object.IsEmpty() ? 0 : object->GetIdentityHash()) {}

HostVarTracker::V8ObjectVarKey::~V8ObjectVarKey() {}

bool HostVarTracker::V8ObjectVarKey::operator<(
    const V8ObjectVarKey& other) const {
  return std::tie(instance, hash) < std::tie(other.instance, other.hash);
}

HostVarTracker::HostVarTracker()
    : VarTracker(SINGLE_THREADED), last_shared_memory_map_id_(0) {}

HostVarTracker::~HostVarTracker() {}

ArrayBufferVar* HostVarTracker::CreateArrayBuffer(uint32_t size_in_bytes) {
  return new HostArrayBufferVar(size_in_bytes);
}

ArrayBufferVar* HostVarTracker::CreateShmArrayBuffer(
    uint32_t size_in_bytes,
    base::UnsafeSharedMemoryRegion region) {
  return new HostArrayBufferVar(size_in_bytes, region);
}

void HostVarTracker::AddV8ObjectVar(V8ObjectVar* object_var) {
  CheckThreadingPreconditions();
  v8::HandleScope handle_scope(object_var->instance()->GetIsolate());
  DCHECK(GetForV8Object(object_var->instance()->pp_instance(),
                        object_var->GetHandle()) == object_map_.end());
  object_map_.insert(std::make_pair(V8ObjectVarKey(object_var), object_var));
}

void HostVarTracker::RemoveV8ObjectVar(V8ObjectVar* object_var) {
  CheckThreadingPreconditions();
  v8::HandleScope handle_scope(object_var->instance()->GetIsolate());
  auto it = GetForV8Object(object_var->instance()->pp_instance(),
                           object_var->GetHandle());
  CHECK(it != object_map_.end(), base::NotFatalUntil::M130);
  object_map_.erase(it);
}

PP_Var HostVarTracker::V8ObjectVarForV8Object(PP_Instance instance,
                                              v8::Local<v8::Object> object) {
  CheckThreadingPreconditions();
  ObjectMap::const_iterator it = GetForV8Object(instance, object);
  if (it == object_map_.end())
    return (new V8ObjectVar(instance, object))->GetPPVar();
  return it->second->GetPPVar();
}

int HostVarTracker::GetLiveV8ObjectVarsForTest(PP_Instance instance) {
  CheckThreadingPreconditions();
  int count = 0;
  // Use a key with an empty handle to find the v8 object var in the map with
  // the given instance and the lowest hash.
  V8ObjectVarKey key(instance, v8::Local<v8::Object>());
  ObjectMap::const_iterator it = object_map_.lower_bound(key);
  while (it != object_map_.end() && it->first.instance == instance) {
    ++count;
    ++it;
  }
  return count;
}

PP_Var HostVarTracker::MakeResourcePPVarFromMessage(
    PP_Instance instance,
    const IPC::Message& creation_message,
    int pending_renderer_id,
    int pending_browser_id) {
  // On the host side, the creation message is ignored when creating a resource.
  // Therefore, a call to this function indicates a null resource. Return the
  // resource 0.
  return MakeResourcePPVar(0);
}

ppapi::ResourceVar* HostVarTracker::MakeResourceVar(PP_Resource pp_resource) {
  return new HostResourceVar(pp_resource);
}

void HostVarTracker::DidDeleteInstance(PP_Instance pp_instance) {
  CheckThreadingPreconditions();

  PepperPluginInstanceImpl* instance =
      HostGlobals::Get()->GetInstance(pp_instance);
  v8::HandleScope handle_scope(instance->GetIsolate());
  // Force delete all var references. ForceReleaseV8Object() will cause
  // this object, and potentially others it references, to be removed from
  // |live_vars_|.

  // Use a key with an empty handle to find the v8 object var in the map with
  // the given instance and the lowest hash.
  V8ObjectVarKey key(pp_instance, v8::Local<v8::Object>());
  auto it = object_map_.lower_bound(key);
  while (it != object_map_.end() && it->first.instance == pp_instance) {
    ForceReleaseV8Object(it->second);
    object_map_.erase(it++);
  }
}

void HostVarTracker::ForceReleaseV8Object(ppapi::V8ObjectVar* object_var) {
  object_var->InstanceDeleted();
  auto iter = live_vars_.find(object_var->GetExistingVarID());
  if (iter == live_vars_.end()) {
    NOTREACHED_IN_MIGRATION();
    return;
  }
  iter->second.ref_count = 0;
  DCHECK(iter->second.track_with_no_reference_count == 0);
  DeleteObjectInfoIfNecessary(iter);
}

HostVarTracker::ObjectMap::iterator HostVarTracker::GetForV8Object(
    PP_Instance instance,
    v8::Local<v8::Object> object) {
  std::pair<ObjectMap::iterator, ObjectMap::iterator> range =
      object_map_.equal_range(V8ObjectVarKey(instance, object));

  for (auto it = range.first; it != range.second; ++it) {
    if (object == it->second->GetHandle())
      return it;
  }
  return object_map_.end();
}

int HostVarTracker::TrackSharedMemoryRegion(
    PP_Instance instance,
    base::UnsafeSharedMemoryRegion region,
    uint32_t size_in_bytes) {
  SharedMemoryMapEntry entry;
  entry.instance = instance;
  entry.region = std::move(region);
  entry.size_in_bytes = size_in_bytes;

  // Find a free id for our map.
  while (base::Contains(shared_memory_map_, last_shared_memory_map_id_)) {
    ++last_shared_memory_map_id_;
  }
  shared_memory_map_[last_shared_memory_map_id_] = std::move(entry);
  return last_shared_memory_map_id_;
}

bool HostVarTracker::StopTrackingSharedMemoryRegion(
    int id,
    PP_Instance instance,
    base::UnsafeSharedMemoryRegion* region,
    uint32_t* size_in_bytes) {
  auto it = shared_memory_map_.find(id);
  if (it == shared_memory_map_.end())
    return false;
  if (it->second.instance != instance)
    return false;

  *region = std::move(it->second.region);
  *size_in_bytes = it->second.size_in_bytes;
  shared_memory_map_.erase(it);
  return true;
}

}  // namespace content