dom/plugins/ipc/PluginScriptableObjectChild.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/ipc/PluginScriptableObjectChild.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1066 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "PluginScriptableObjectChild.h"
    1.11 +#include "PluginScriptableObjectUtils.h"
    1.12 +#include "PluginIdentifierChild.h"
    1.13 +
    1.14 +using namespace mozilla::plugins;
    1.15 +
    1.16 +// static
    1.17 +NPObject*
    1.18 +PluginScriptableObjectChild::ScriptableAllocate(NPP aInstance,
    1.19 +                                                NPClass* aClass)
    1.20 +{
    1.21 +  AssertPluginThread();
    1.22 +
    1.23 +  if (aClass != GetClass()) {
    1.24 +    NS_RUNTIMEABORT("Huh?! Wrong class!");
    1.25 +  }
    1.26 +
    1.27 +  return new ChildNPObject();
    1.28 +}
    1.29 +
    1.30 +// static
    1.31 +void
    1.32 +PluginScriptableObjectChild::ScriptableInvalidate(NPObject* aObject)
    1.33 +{
    1.34 +  AssertPluginThread();
    1.35 +
    1.36 +  if (aObject->_class != GetClass()) {
    1.37 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
    1.38 +  }
    1.39 +
    1.40 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
    1.41 +  if (object->invalidated) {
    1.42 +    // This can happen more than once, and is just fine.
    1.43 +    return;
    1.44 +  }
    1.45 +
    1.46 +  object->invalidated = true;
    1.47 +}
    1.48 +
    1.49 +// static
    1.50 +void
    1.51 +PluginScriptableObjectChild::ScriptableDeallocate(NPObject* aObject)
    1.52 +{
    1.53 +  AssertPluginThread();
    1.54 +
    1.55 +  if (aObject->_class != GetClass()) {
    1.56 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
    1.57 +  }
    1.58 +
    1.59 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
    1.60 +  PluginScriptableObjectChild* actor = object->parent;
    1.61 +  if (actor) {
    1.62 +    NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
    1.63 +    actor->DropNPObject();
    1.64 +  }
    1.65 +
    1.66 +  delete object;
    1.67 +}
    1.68 +
    1.69 +// static
    1.70 +bool
    1.71 +PluginScriptableObjectChild::ScriptableHasMethod(NPObject* aObject,
    1.72 +                                                 NPIdentifier aName)
    1.73 +{
    1.74 +  AssertPluginThread();
    1.75 +
    1.76 +  if (aObject->_class != GetClass()) {
    1.77 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
    1.78 +  }
    1.79 +
    1.80 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
    1.81 +  if (object->invalidated) {
    1.82 +    NS_WARNING("Calling method on an invalidated object!");
    1.83 +    return false;
    1.84 +  }
    1.85 +
    1.86 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
    1.87 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
    1.88 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
    1.89 +
    1.90 +  bool result;
    1.91 +  actor->CallHasMethod(static_cast<PPluginIdentifierChild*>(aName), &result);
    1.92 +
    1.93 +  return result;
    1.94 +}
    1.95 +
    1.96 +// static
    1.97 +bool
    1.98 +PluginScriptableObjectChild::ScriptableInvoke(NPObject* aObject,
    1.99 +                                              NPIdentifier aName,
   1.100 +                                              const NPVariant* aArgs,
   1.101 +                                              uint32_t aArgCount,
   1.102 +                                              NPVariant* aResult)
   1.103 +{
   1.104 +  AssertPluginThread();
   1.105 +
   1.106 +  if (aObject->_class != GetClass()) {
   1.107 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.108 +  }
   1.109 +
   1.110 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.111 +  if (object->invalidated) {
   1.112 +    NS_WARNING("Calling method on an invalidated object!");
   1.113 +    return false;
   1.114 +  }
   1.115 +
   1.116 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.117 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.118 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.119 +
   1.120 +  ProtectedVariantArray args(aArgs, aArgCount, actor->GetInstance());
   1.121 +  if (!args.IsOk()) {
   1.122 +    NS_ERROR("Failed to convert arguments!");
   1.123 +    return false;
   1.124 +  }
   1.125 +
   1.126 +  Variant remoteResult;
   1.127 +  bool success;
   1.128 +  actor->CallInvoke(static_cast<PPluginIdentifierChild*>(aName), args,
   1.129 +                    &remoteResult, &success);
   1.130 +
   1.131 +  if (!success) {
   1.132 +    return false;
   1.133 +  }
   1.134 +
   1.135 +  ConvertToVariant(remoteResult, *aResult);
   1.136 +  return true;
   1.137 +}
   1.138 +
   1.139 +// static
   1.140 +bool
   1.141 +PluginScriptableObjectChild::ScriptableInvokeDefault(NPObject* aObject,
   1.142 +                                                     const NPVariant* aArgs,
   1.143 +                                                     uint32_t aArgCount,
   1.144 +                                                     NPVariant* aResult)
   1.145 +{
   1.146 +  AssertPluginThread();
   1.147 +
   1.148 +  if (aObject->_class != GetClass()) {
   1.149 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.150 +  }
   1.151 +
   1.152 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.153 +  if (object->invalidated) {
   1.154 +    NS_WARNING("Calling method on an invalidated object!");
   1.155 +    return false;
   1.156 +  }
   1.157 +
   1.158 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.159 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.160 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.161 +
   1.162 +  ProtectedVariantArray args(aArgs, aArgCount, actor->GetInstance());
   1.163 +  if (!args.IsOk()) {
   1.164 +    NS_ERROR("Failed to convert arguments!");
   1.165 +    return false;
   1.166 +  }
   1.167 +
   1.168 +  Variant remoteResult;
   1.169 +  bool success;
   1.170 +  actor->CallInvokeDefault(args, &remoteResult, &success);
   1.171 +
   1.172 +  if (!success) {
   1.173 +    return false;
   1.174 +  }
   1.175 +
   1.176 +  ConvertToVariant(remoteResult, *aResult);
   1.177 +  return true;
   1.178 +}
   1.179 +
   1.180 +// static
   1.181 +bool
   1.182 +PluginScriptableObjectChild::ScriptableHasProperty(NPObject* aObject,
   1.183 +                                                   NPIdentifier aName)
   1.184 +{
   1.185 +  AssertPluginThread();
   1.186 +
   1.187 +  if (aObject->_class != GetClass()) {
   1.188 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.189 +  }
   1.190 +
   1.191 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.192 +  if (object->invalidated) {
   1.193 +    NS_WARNING("Calling method on an invalidated object!");
   1.194 +    return false;
   1.195 +  }
   1.196 +
   1.197 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.198 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.199 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.200 +
   1.201 +  bool result;
   1.202 +  actor->CallHasProperty(static_cast<PPluginIdentifierChild*>(aName), &result);
   1.203 +
   1.204 +  return result;
   1.205 +}
   1.206 +
   1.207 +// static
   1.208 +bool
   1.209 +PluginScriptableObjectChild::ScriptableGetProperty(NPObject* aObject,
   1.210 +                                                   NPIdentifier aName,
   1.211 +                                                   NPVariant* aResult)
   1.212 +{
   1.213 +  AssertPluginThread();
   1.214 +
   1.215 +  if (aObject->_class != GetClass()) {
   1.216 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.217 +  }
   1.218 +
   1.219 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.220 +  if (object->invalidated) {
   1.221 +    NS_WARNING("Calling method on an invalidated object!");
   1.222 +    return false;
   1.223 +  }
   1.224 +
   1.225 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.226 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.227 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.228 +
   1.229 +  Variant result;
   1.230 +  bool success;
   1.231 +  actor->CallGetParentProperty(static_cast<PPluginIdentifierChild*>(aName),
   1.232 +                               &result, &success);
   1.233 +
   1.234 +  if (!success) {
   1.235 +    return false;
   1.236 +  }
   1.237 +
   1.238 +  ConvertToVariant(result, *aResult);
   1.239 +  return true;
   1.240 +}
   1.241 +
   1.242 +// static
   1.243 +bool
   1.244 +PluginScriptableObjectChild::ScriptableSetProperty(NPObject* aObject,
   1.245 +                                                   NPIdentifier aName,
   1.246 +                                                   const NPVariant* aValue)
   1.247 +{
   1.248 +  AssertPluginThread();
   1.249 +
   1.250 +  if (aObject->_class != GetClass()) {
   1.251 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.252 +  }
   1.253 +
   1.254 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.255 +  if (object->invalidated) {
   1.256 +    NS_WARNING("Calling method on an invalidated object!");
   1.257 +    return false;
   1.258 +  }
   1.259 +
   1.260 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.261 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.262 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.263 +
   1.264 +  ProtectedVariant value(*aValue, actor->GetInstance());
   1.265 +  if (!value.IsOk()) {
   1.266 +    NS_WARNING("Failed to convert variant!");
   1.267 +    return false;
   1.268 +  }
   1.269 +
   1.270 +  bool success;
   1.271 +  actor->CallSetProperty(static_cast<PPluginIdentifierChild*>(aName), value,
   1.272 +                         &success);
   1.273 +
   1.274 +  return success;
   1.275 +}
   1.276 +
   1.277 +// static
   1.278 +bool
   1.279 +PluginScriptableObjectChild::ScriptableRemoveProperty(NPObject* aObject,
   1.280 +                                                      NPIdentifier aName)
   1.281 +{
   1.282 +  AssertPluginThread();
   1.283 +
   1.284 +  if (aObject->_class != GetClass()) {
   1.285 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.286 +  }
   1.287 +
   1.288 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.289 +  if (object->invalidated) {
   1.290 +    NS_WARNING("Calling method on an invalidated object!");
   1.291 +    return false;
   1.292 +  }
   1.293 +
   1.294 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.295 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.296 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.297 +
   1.298 +  bool success;
   1.299 +  actor->CallRemoveProperty(static_cast<PPluginIdentifierChild*>(aName),
   1.300 +                            &success);
   1.301 +
   1.302 +  return success;
   1.303 +}
   1.304 +
   1.305 +// static
   1.306 +bool
   1.307 +PluginScriptableObjectChild::ScriptableEnumerate(NPObject* aObject,
   1.308 +                                                 NPIdentifier** aIdentifiers,
   1.309 +                                                 uint32_t* aCount)
   1.310 +{
   1.311 +  AssertPluginThread();
   1.312 +
   1.313 +  if (aObject->_class != GetClass()) {
   1.314 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.315 +  }
   1.316 +
   1.317 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.318 +  if (object->invalidated) {
   1.319 +    NS_WARNING("Calling method on an invalidated object!");
   1.320 +    return false;
   1.321 +  }
   1.322 +
   1.323 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.324 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.325 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.326 +
   1.327 +  AutoInfallibleTArray<PPluginIdentifierChild*, 10> identifiers;
   1.328 +  bool success;
   1.329 +  actor->CallEnumerate(&identifiers, &success);
   1.330 +
   1.331 +  if (!success) {
   1.332 +    return false;
   1.333 +  }
   1.334 +
   1.335 +  *aCount = identifiers.Length();
   1.336 +  if (!*aCount) {
   1.337 +    *aIdentifiers = nullptr;
   1.338 +    return true;
   1.339 +  }
   1.340 +
   1.341 +  *aIdentifiers = reinterpret_cast<NPIdentifier*>(
   1.342 +      PluginModuleChild::sBrowserFuncs.memalloc(*aCount * sizeof(NPIdentifier)));
   1.343 +  if (!*aIdentifiers) {
   1.344 +    NS_ERROR("Out of memory!");
   1.345 +    return false;
   1.346 +  }
   1.347 +
   1.348 +  for (uint32_t index = 0; index < *aCount; index++) {
   1.349 +    (*aIdentifiers)[index] =
   1.350 +      static_cast<PPluginIdentifierChild*>(identifiers[index]);
   1.351 +  }
   1.352 +  return true;
   1.353 +}
   1.354 +
   1.355 +// static
   1.356 +bool
   1.357 +PluginScriptableObjectChild::ScriptableConstruct(NPObject* aObject,
   1.358 +                                                 const NPVariant* aArgs,
   1.359 +                                                 uint32_t aArgCount,
   1.360 +                                                 NPVariant* aResult)
   1.361 +{
   1.362 +  AssertPluginThread();
   1.363 +
   1.364 +  if (aObject->_class != GetClass()) {
   1.365 +    NS_RUNTIMEABORT("Don't know what kind of object this is!");
   1.366 +  }
   1.367 +
   1.368 +  ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
   1.369 +  if (object->invalidated) {
   1.370 +    NS_WARNING("Calling method on an invalidated object!");
   1.371 +    return false;
   1.372 +  }
   1.373 +
   1.374 +  ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
   1.375 +  NS_ASSERTION(actor, "This shouldn't ever be null!");
   1.376 +  NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
   1.377 +
   1.378 +  ProtectedVariantArray args(aArgs, aArgCount, actor->GetInstance());
   1.379 +  if (!args.IsOk()) {
   1.380 +    NS_ERROR("Failed to convert arguments!");
   1.381 +    return false;
   1.382 +  }
   1.383 +
   1.384 +  Variant remoteResult;
   1.385 +  bool success;
   1.386 +  actor->CallConstruct(args, &remoteResult, &success);
   1.387 +
   1.388 +  if (!success) {
   1.389 +    return false;
   1.390 +  }
   1.391 +
   1.392 +  ConvertToVariant(remoteResult, *aResult);
   1.393 +  return true;
   1.394 +}
   1.395 +
   1.396 +const NPClass PluginScriptableObjectChild::sNPClass = {
   1.397 +  NP_CLASS_STRUCT_VERSION,
   1.398 +  PluginScriptableObjectChild::ScriptableAllocate,
   1.399 +  PluginScriptableObjectChild::ScriptableDeallocate,
   1.400 +  PluginScriptableObjectChild::ScriptableInvalidate,
   1.401 +  PluginScriptableObjectChild::ScriptableHasMethod,
   1.402 +  PluginScriptableObjectChild::ScriptableInvoke,
   1.403 +  PluginScriptableObjectChild::ScriptableInvokeDefault,
   1.404 +  PluginScriptableObjectChild::ScriptableHasProperty,
   1.405 +  PluginScriptableObjectChild::ScriptableGetProperty,
   1.406 +  PluginScriptableObjectChild::ScriptableSetProperty,
   1.407 +  PluginScriptableObjectChild::ScriptableRemoveProperty,
   1.408 +  PluginScriptableObjectChild::ScriptableEnumerate,
   1.409 +  PluginScriptableObjectChild::ScriptableConstruct
   1.410 +};
   1.411 +
   1.412 +PluginScriptableObjectChild::PluginScriptableObjectChild(
   1.413 +                                                     ScriptableObjectType aType)
   1.414 +: mInstance(nullptr),
   1.415 +  mObject(nullptr),
   1.416 +  mInvalidated(false),
   1.417 +  mProtectCount(0),
   1.418 +  mType(aType)
   1.419 +{
   1.420 +  AssertPluginThread();
   1.421 +}
   1.422 +
   1.423 +PluginScriptableObjectChild::~PluginScriptableObjectChild()
   1.424 +{
   1.425 +  AssertPluginThread();
   1.426 +
   1.427 +  if (mObject) {
   1.428 +    PluginModuleChild::current()->UnregisterActorForNPObject(mObject);
   1.429 +
   1.430 +    if (mObject->_class == GetClass()) {
   1.431 +      NS_ASSERTION(mType == Proxy, "Wrong type!");
   1.432 +      static_cast<ChildNPObject*>(mObject)->parent = nullptr;
   1.433 +    }
   1.434 +    else {
   1.435 +      NS_ASSERTION(mType == LocalObject, "Wrong type!");
   1.436 +      PluginModuleChild::sBrowserFuncs.releaseobject(mObject);
   1.437 +    }
   1.438 +  }
   1.439 +}
   1.440 +
   1.441 +void
   1.442 +PluginScriptableObjectChild::InitializeProxy()
   1.443 +{
   1.444 +  AssertPluginThread();
   1.445 +  NS_ASSERTION(mType == Proxy, "Bad type!");
   1.446 +  NS_ASSERTION(!mObject, "Calling Initialize more than once!");
   1.447 +  NS_ASSERTION(!mInvalidated, "Already invalidated?!");
   1.448 +
   1.449 +  mInstance = static_cast<PluginInstanceChild*>(Manager());
   1.450 +  NS_ASSERTION(mInstance, "Null manager?!");
   1.451 +
   1.452 +  NPObject* object = CreateProxyObject();
   1.453 +  NS_ASSERTION(object, "Failed to create object!");
   1.454 +
   1.455 +  if (!PluginModuleChild::current()->RegisterActorForNPObject(object, this)) {
   1.456 +    NS_ERROR("Out of memory?");
   1.457 +  }
   1.458 +
   1.459 +  mObject = object;
   1.460 +}
   1.461 +
   1.462 +void
   1.463 +PluginScriptableObjectChild::InitializeLocal(NPObject* aObject)
   1.464 +{
   1.465 +  AssertPluginThread();
   1.466 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.467 +  NS_ASSERTION(!mObject, "Calling Initialize more than once!");
   1.468 +  NS_ASSERTION(!mInvalidated, "Already invalidated?!");
   1.469 +
   1.470 +  mInstance = static_cast<PluginInstanceChild*>(Manager());
   1.471 +  NS_ASSERTION(mInstance, "Null manager?!");
   1.472 +
   1.473 +  PluginModuleChild::sBrowserFuncs.retainobject(aObject);
   1.474 +
   1.475 +  NS_ASSERTION(!mProtectCount, "Should be zero!");
   1.476 +  mProtectCount++;
   1.477 +
   1.478 +  if (!PluginModuleChild::current()->RegisterActorForNPObject(aObject, this)) {
   1.479 +      NS_ERROR("Out of memory?");
   1.480 +  }
   1.481 +
   1.482 +  mObject = aObject;
   1.483 +}
   1.484 +
   1.485 +NPObject*
   1.486 +PluginScriptableObjectChild::CreateProxyObject()
   1.487 +{
   1.488 +  NS_ASSERTION(mInstance, "Must have an instance!");
   1.489 +  NS_ASSERTION(mType == Proxy, "Shouldn't call this for non-proxy object!");
   1.490 +
   1.491 +  NPClass* proxyClass = const_cast<NPClass*>(GetClass());
   1.492 +  NPObject* npobject =
   1.493 +    PluginModuleChild::sBrowserFuncs.createobject(mInstance->GetNPP(),
   1.494 +                                                  proxyClass);
   1.495 +  NS_ASSERTION(npobject, "Failed to create object?!");
   1.496 +  NS_ASSERTION(npobject->_class == GetClass(), "Wrong kind of object!");
   1.497 +  NS_ASSERTION(npobject->referenceCount == 1, "Some kind of live object!");
   1.498 +
   1.499 +  ChildNPObject* object = static_cast<ChildNPObject*>(npobject);
   1.500 +  NS_ASSERTION(!object->invalidated, "Bad object!");
   1.501 +  NS_ASSERTION(!object->parent, "Bad object!");
   1.502 +
   1.503 +  // We don't want to have the actor own this object but rather let the object
   1.504 +  // own this actor. Set the reference count to 0 here so that when the object
   1.505 +  // dies we will send the destructor message to the child.
   1.506 +  object->referenceCount = 0;
   1.507 +  NS_LOG_RELEASE(object, 0, "NPObject");
   1.508 +
   1.509 +  object->parent = const_cast<PluginScriptableObjectChild*>(this);
   1.510 +  return object;
   1.511 +}
   1.512 +
   1.513 +bool
   1.514 +PluginScriptableObjectChild::ResurrectProxyObject()
   1.515 +{
   1.516 +  NS_ASSERTION(mInstance, "Must have an instance already!");
   1.517 +  NS_ASSERTION(!mObject, "Should not have an object already!");
   1.518 +  NS_ASSERTION(mType == Proxy, "Shouldn't call this for non-proxy object!");
   1.519 +
   1.520 +  NPObject* object = CreateProxyObject();
   1.521 +  if (!object) {
   1.522 +    NS_WARNING("Failed to create object!");
   1.523 +    return false;
   1.524 +  }
   1.525 +
   1.526 +  InitializeProxy();
   1.527 +  NS_ASSERTION(mObject, "Initialize failed!");
   1.528 +
   1.529 +  SendProtect();
   1.530 +  return true;
   1.531 +}
   1.532 +
   1.533 +NPObject*
   1.534 +PluginScriptableObjectChild::GetObject(bool aCanResurrect)
   1.535 +{
   1.536 +  if (!mObject && aCanResurrect && !ResurrectProxyObject()) {
   1.537 +    NS_ERROR("Null object!");
   1.538 +    return nullptr;
   1.539 +  }
   1.540 +  return mObject;
   1.541 +}
   1.542 +
   1.543 +void
   1.544 +PluginScriptableObjectChild::Protect()
   1.545 +{
   1.546 +  NS_ASSERTION(mObject, "No object!");
   1.547 +  NS_ASSERTION(mProtectCount >= 0, "Negative retain count?!");
   1.548 +
   1.549 +  if (mType == LocalObject) {
   1.550 +    ++mProtectCount;
   1.551 +  }
   1.552 +}
   1.553 +
   1.554 +void
   1.555 +PluginScriptableObjectChild::Unprotect()
   1.556 +{
   1.557 +  NS_ASSERTION(mObject, "Bad state!");
   1.558 +  NS_ASSERTION(mProtectCount >= 0, "Negative retain count?!");
   1.559 +
   1.560 +  if (mType == LocalObject) {
   1.561 +    if (--mProtectCount == 0) {
   1.562 +      PluginScriptableObjectChild::Send__delete__(this);
   1.563 +    }
   1.564 +  }
   1.565 +}
   1.566 +
   1.567 +void
   1.568 +PluginScriptableObjectChild::DropNPObject()
   1.569 +{
   1.570 +  NS_ASSERTION(mObject, "Invalidated object!");
   1.571 +  NS_ASSERTION(mObject->_class == GetClass(), "Wrong type of object!");
   1.572 +  NS_ASSERTION(mType == Proxy, "Shouldn't call this for non-proxy object!");
   1.573 +
   1.574 +  // We think we're about to be deleted, but we could be racing with the other
   1.575 +  // process.
   1.576 +  PluginModuleChild::current()->UnregisterActorForNPObject(mObject);
   1.577 +  mObject = nullptr;
   1.578 +
   1.579 +  SendUnprotect();
   1.580 +}
   1.581 +
   1.582 +void
   1.583 +PluginScriptableObjectChild::NPObjectDestroyed()
   1.584 +{
   1.585 +  NS_ASSERTION(LocalObject == mType,
   1.586 +               "ScriptableDeallocate should have handled this for proxies");
   1.587 +  mInvalidated = true;
   1.588 +  mObject = nullptr;
   1.589 +}
   1.590 +
   1.591 +bool
   1.592 +PluginScriptableObjectChild::AnswerInvalidate()
   1.593 +{
   1.594 +  AssertPluginThread();
   1.595 +
   1.596 +  if (mInvalidated) {
   1.597 +    return true;
   1.598 +  }
   1.599 +
   1.600 +  mInvalidated = true;
   1.601 +
   1.602 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.603 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.604 +
   1.605 +  if (mObject->_class && mObject->_class->invalidate) {
   1.606 +    mObject->_class->invalidate(mObject);
   1.607 +  }
   1.608 +
   1.609 +  Unprotect();
   1.610 +
   1.611 +  return true;
   1.612 +}
   1.613 +
   1.614 +bool
   1.615 +PluginScriptableObjectChild::AnswerHasMethod(PPluginIdentifierChild* aId,
   1.616 +                                             bool* aHasMethod)
   1.617 +{
   1.618 +  AssertPluginThread();
   1.619 +
   1.620 +  if (mInvalidated) {
   1.621 +    NS_WARNING("Calling AnswerHasMethod with an invalidated object!");
   1.622 +    *aHasMethod = false;
   1.623 +    return true;
   1.624 +  }
   1.625 +
   1.626 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.627 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.628 +
   1.629 +  if (!(mObject->_class && mObject->_class->hasMethod)) {
   1.630 +    *aHasMethod = false;
   1.631 +    return true;
   1.632 +  }
   1.633 +
   1.634 +  PluginIdentifierChild::StackIdentifier id(aId);
   1.635 +  *aHasMethod = mObject->_class->hasMethod(mObject, id->ToNPIdentifier());
   1.636 +  return true;
   1.637 +}
   1.638 +
   1.639 +bool
   1.640 +PluginScriptableObjectChild::AnswerInvoke(PPluginIdentifierChild* aId,
   1.641 +                                          const InfallibleTArray<Variant>& aArgs,
   1.642 +                                          Variant* aResult,
   1.643 +                                          bool* aSuccess)
   1.644 +{
   1.645 +  AssertPluginThread();
   1.646 +
   1.647 +  if (mInvalidated) {
   1.648 +    NS_WARNING("Calling AnswerInvoke with an invalidated object!");
   1.649 +    *aResult = void_t();
   1.650 +    *aSuccess = false;
   1.651 +    return true;
   1.652 +  }
   1.653 +
   1.654 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.655 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.656 +
   1.657 +  if (!(mObject->_class && mObject->_class->invoke)) {
   1.658 +    *aResult = void_t();
   1.659 +    *aSuccess = false;
   1.660 +    return true;
   1.661 +  }
   1.662 +
   1.663 +  AutoFallibleTArray<NPVariant, 10> convertedArgs;
   1.664 +  uint32_t argCount = aArgs.Length();
   1.665 +
   1.666 +  if (!convertedArgs.SetLength(argCount)) {
   1.667 +    *aResult = void_t();
   1.668 +    *aSuccess = false;
   1.669 +    return true;
   1.670 +  }
   1.671 +
   1.672 +  for (uint32_t index = 0; index < argCount; index++) {
   1.673 +    ConvertToVariant(aArgs[index], convertedArgs[index]);
   1.674 +  }
   1.675 +
   1.676 +  NPVariant result;
   1.677 +  VOID_TO_NPVARIANT(result);
   1.678 +  PluginIdentifierChild::StackIdentifier id(aId);
   1.679 +  bool success = mObject->_class->invoke(mObject, id->ToNPIdentifier(),
   1.680 +                                         convertedArgs.Elements(), argCount,
   1.681 +                                         &result);
   1.682 +
   1.683 +  for (uint32_t index = 0; index < argCount; index++) {
   1.684 +    PluginModuleChild::sBrowserFuncs.releasevariantvalue(&convertedArgs[index]);
   1.685 +  }
   1.686 +
   1.687 +  if (!success) {
   1.688 +    *aResult = void_t();
   1.689 +    *aSuccess = false;
   1.690 +    return true;
   1.691 +  }
   1.692 +
   1.693 +  Variant convertedResult;
   1.694 +  success = ConvertToRemoteVariant(result, convertedResult, GetInstance(),
   1.695 +                                   false);
   1.696 +
   1.697 +  DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
   1.698 +
   1.699 +  if (!success) {
   1.700 +    *aResult = void_t();
   1.701 +    *aSuccess = false;
   1.702 +    return true;
   1.703 +  }
   1.704 +
   1.705 +  *aSuccess = true;
   1.706 +  *aResult = convertedResult;
   1.707 +  return true;
   1.708 +}
   1.709 +
   1.710 +bool
   1.711 +PluginScriptableObjectChild::AnswerInvokeDefault(const InfallibleTArray<Variant>& aArgs,
   1.712 +                                                 Variant* aResult,
   1.713 +                                                 bool* aSuccess)
   1.714 +{
   1.715 +  AssertPluginThread();
   1.716 +
   1.717 +  if (mInvalidated) {
   1.718 +    NS_WARNING("Calling AnswerInvokeDefault with an invalidated object!");
   1.719 +    *aResult = void_t();
   1.720 +    *aSuccess = false;
   1.721 +    return true;
   1.722 +  }
   1.723 +
   1.724 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.725 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.726 +
   1.727 +  if (!(mObject->_class && mObject->_class->invokeDefault)) {
   1.728 +    *aResult = void_t();
   1.729 +    *aSuccess = false;
   1.730 +    return true;
   1.731 +  }
   1.732 +
   1.733 +  AutoFallibleTArray<NPVariant, 10> convertedArgs;
   1.734 +  uint32_t argCount = aArgs.Length();
   1.735 +
   1.736 +  if (!convertedArgs.SetLength(argCount)) {
   1.737 +    *aResult = void_t();
   1.738 +    *aSuccess = false;
   1.739 +    return true;
   1.740 +  }
   1.741 +
   1.742 +  for (uint32_t index = 0; index < argCount; index++) {
   1.743 +    ConvertToVariant(aArgs[index], convertedArgs[index]);
   1.744 +  }
   1.745 +
   1.746 +  NPVariant result;
   1.747 +  VOID_TO_NPVARIANT(result);
   1.748 +  bool success = mObject->_class->invokeDefault(mObject,
   1.749 +                                                convertedArgs.Elements(),
   1.750 +                                                argCount, &result);
   1.751 +
   1.752 +  for (uint32_t index = 0; index < argCount; index++) {
   1.753 +    PluginModuleChild::sBrowserFuncs.releasevariantvalue(&convertedArgs[index]);
   1.754 +  }
   1.755 +
   1.756 +  if (!success) {
   1.757 +    *aResult = void_t();
   1.758 +    *aSuccess = false;
   1.759 +    return true;
   1.760 +  }
   1.761 +
   1.762 +  Variant convertedResult;
   1.763 +  success = ConvertToRemoteVariant(result, convertedResult, GetInstance(),
   1.764 +                                   false);
   1.765 +
   1.766 +  DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
   1.767 +
   1.768 +  if (!success) {
   1.769 +    *aResult = void_t();
   1.770 +    *aSuccess = false;
   1.771 +    return true;
   1.772 +  }
   1.773 +
   1.774 +  *aResult = convertedResult;
   1.775 +  *aSuccess = true;
   1.776 +  return true;
   1.777 +}
   1.778 +
   1.779 +bool
   1.780 +PluginScriptableObjectChild::AnswerHasProperty(PPluginIdentifierChild* aId,
   1.781 +                                               bool* aHasProperty)
   1.782 +{
   1.783 +  AssertPluginThread();
   1.784 +
   1.785 +  if (mInvalidated) {
   1.786 +    NS_WARNING("Calling AnswerHasProperty with an invalidated object!");
   1.787 +    *aHasProperty = false;
   1.788 +    return true;
   1.789 +  }
   1.790 +
   1.791 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.792 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.793 +
   1.794 +  if (!(mObject->_class && mObject->_class->hasProperty)) {
   1.795 +    *aHasProperty = false;
   1.796 +    return true;
   1.797 +  }
   1.798 +
   1.799 +  PluginIdentifierChild::StackIdentifier id(aId);
   1.800 +  *aHasProperty = mObject->_class->hasProperty(mObject, id->ToNPIdentifier());
   1.801 +  return true;
   1.802 +}
   1.803 +
   1.804 +bool
   1.805 +PluginScriptableObjectChild::AnswerGetChildProperty(PPluginIdentifierChild* aId,
   1.806 +                                                    bool* aHasProperty,
   1.807 +                                                    bool* aHasMethod,
   1.808 +                                                    Variant* aResult,
   1.809 +                                                    bool* aSuccess)
   1.810 +{
   1.811 +  AssertPluginThread();
   1.812 +
   1.813 +  *aHasProperty = *aHasMethod = *aSuccess = false;
   1.814 +  *aResult = void_t();
   1.815 +
   1.816 +  if (mInvalidated) {
   1.817 +    NS_WARNING("Calling AnswerGetProperty with an invalidated object!");
   1.818 +    return true;
   1.819 +  }
   1.820 +
   1.821 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.822 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.823 +
   1.824 +  if (!(mObject->_class && mObject->_class->hasProperty &&
   1.825 +        mObject->_class->hasMethod && mObject->_class->getProperty)) {
   1.826 +    return true;
   1.827 +  }
   1.828 +
   1.829 +  PluginIdentifierChild::StackIdentifier stackID(aId);
   1.830 +  NPIdentifier id = stackID->ToNPIdentifier();
   1.831 +
   1.832 +  *aHasProperty = mObject->_class->hasProperty(mObject, id);
   1.833 +  *aHasMethod = mObject->_class->hasMethod(mObject, id);
   1.834 +
   1.835 +  if (*aHasProperty) {
   1.836 +    NPVariant result;
   1.837 +    VOID_TO_NPVARIANT(result);
   1.838 +
   1.839 +    if (!mObject->_class->getProperty(mObject, id, &result)) {
   1.840 +      return true;
   1.841 +    }
   1.842 +
   1.843 +    Variant converted;
   1.844 +    if ((*aSuccess = ConvertToRemoteVariant(result, converted, GetInstance(),
   1.845 +                                            false))) {
   1.846 +      DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
   1.847 +      *aResult = converted;
   1.848 +    }
   1.849 +  }
   1.850 +
   1.851 +  return true;
   1.852 +}
   1.853 +
   1.854 +bool
   1.855 +PluginScriptableObjectChild::AnswerSetProperty(PPluginIdentifierChild* aId,
   1.856 +                                               const Variant& aValue,
   1.857 +                                               bool* aSuccess)
   1.858 +{
   1.859 +  AssertPluginThread();
   1.860 +
   1.861 +  if (mInvalidated) {
   1.862 +    NS_WARNING("Calling AnswerSetProperty with an invalidated object!");
   1.863 +    *aSuccess = false;
   1.864 +    return true;
   1.865 +  }
   1.866 +
   1.867 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.868 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.869 +
   1.870 +  if (!(mObject->_class && mObject->_class->hasProperty &&
   1.871 +        mObject->_class->setProperty)) {
   1.872 +    *aSuccess = false;
   1.873 +    return true;
   1.874 +  }
   1.875 +
   1.876 +  PluginIdentifierChild::StackIdentifier stackID(aId);
   1.877 +  NPIdentifier id = stackID->ToNPIdentifier();
   1.878 +
   1.879 +  if (!mObject->_class->hasProperty(mObject, id)) {
   1.880 +    *aSuccess = false;
   1.881 +    return true;
   1.882 +  }
   1.883 +
   1.884 +  NPVariant converted;
   1.885 +  ConvertToVariant(aValue, converted);
   1.886 +
   1.887 +  if ((*aSuccess = mObject->_class->setProperty(mObject, id, &converted))) {
   1.888 +    PluginModuleChild::sBrowserFuncs.releasevariantvalue(&converted);
   1.889 +  }
   1.890 +  return true;
   1.891 +}
   1.892 +
   1.893 +bool
   1.894 +PluginScriptableObjectChild::AnswerRemoveProperty(PPluginIdentifierChild* aId,
   1.895 +                                                  bool* aSuccess)
   1.896 +{
   1.897 +  AssertPluginThread();
   1.898 +
   1.899 +  if (mInvalidated) {
   1.900 +    NS_WARNING("Calling AnswerRemoveProperty with an invalidated object!");
   1.901 +    *aSuccess = false;
   1.902 +    return true;
   1.903 +  }
   1.904 +
   1.905 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.906 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.907 +
   1.908 +  if (!(mObject->_class && mObject->_class->hasProperty &&
   1.909 +        mObject->_class->removeProperty)) {
   1.910 +    *aSuccess = false;
   1.911 +    return true;
   1.912 +  }
   1.913 +
   1.914 +  PluginIdentifierChild::StackIdentifier stackID(aId);
   1.915 +  NPIdentifier id = stackID->ToNPIdentifier();
   1.916 +  *aSuccess = mObject->_class->hasProperty(mObject, id) ?
   1.917 +              mObject->_class->removeProperty(mObject, id) :
   1.918 +              true;
   1.919 +
   1.920 +  return true;
   1.921 +}
   1.922 +
   1.923 +bool
   1.924 +PluginScriptableObjectChild::AnswerEnumerate(InfallibleTArray<PPluginIdentifierChild*>* aProperties,
   1.925 +                                             bool* aSuccess)
   1.926 +{
   1.927 +  AssertPluginThread();
   1.928 +
   1.929 +  if (mInvalidated) {
   1.930 +    NS_WARNING("Calling AnswerEnumerate with an invalidated object!");
   1.931 +    *aSuccess = false;
   1.932 +    return true;
   1.933 +  }
   1.934 +
   1.935 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.936 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.937 +
   1.938 +  if (!(mObject->_class && mObject->_class->enumerate)) {
   1.939 +    *aSuccess = false;
   1.940 +    return true;
   1.941 +  }
   1.942 +
   1.943 +  NPIdentifier* ids;
   1.944 +  uint32_t idCount;
   1.945 +  if (!mObject->_class->enumerate(mObject, &ids, &idCount)) {
   1.946 +    *aSuccess = false;
   1.947 +    return true;
   1.948 +  }
   1.949 +
   1.950 +  aProperties->SetCapacity(idCount);
   1.951 +
   1.952 +  for (uint32_t index = 0; index < idCount; index++) {
   1.953 +    PluginIdentifierChild* id = static_cast<PluginIdentifierChild*>(ids[index]);
   1.954 +    aProperties->AppendElement(id);
   1.955 +  }
   1.956 +
   1.957 +  PluginModuleChild::sBrowserFuncs.memfree(ids);
   1.958 +  *aSuccess = true;
   1.959 +  return true;
   1.960 +}
   1.961 +
   1.962 +bool
   1.963 +PluginScriptableObjectChild::AnswerConstruct(const InfallibleTArray<Variant>& aArgs,
   1.964 +                                             Variant* aResult,
   1.965 +                                             bool* aSuccess)
   1.966 +{
   1.967 +  AssertPluginThread();
   1.968 +
   1.969 +  if (mInvalidated) {
   1.970 +    NS_WARNING("Calling AnswerConstruct with an invalidated object!");
   1.971 +    *aResult = void_t();
   1.972 +    *aSuccess = false;
   1.973 +    return true;
   1.974 +  }
   1.975 +
   1.976 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
   1.977 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
   1.978 +
   1.979 +  if (!(mObject->_class && mObject->_class->construct)) {
   1.980 +    *aResult = void_t();
   1.981 +    *aSuccess = false;
   1.982 +    return true;
   1.983 +  }
   1.984 +
   1.985 +  AutoFallibleTArray<NPVariant, 10> convertedArgs;
   1.986 +  uint32_t argCount = aArgs.Length();
   1.987 +
   1.988 +  if (!convertedArgs.SetLength(argCount)) {
   1.989 +    *aResult = void_t();
   1.990 +    *aSuccess = false;
   1.991 +    return true;
   1.992 +  }
   1.993 +
   1.994 +  for (uint32_t index = 0; index < argCount; index++) {
   1.995 +    ConvertToVariant(aArgs[index], convertedArgs[index]);
   1.996 +  }
   1.997 +
   1.998 +  NPVariant result;
   1.999 +  VOID_TO_NPVARIANT(result);
  1.1000 +  bool success = mObject->_class->construct(mObject, convertedArgs.Elements(),
  1.1001 +                                            argCount, &result);
  1.1002 +
  1.1003 +  for (uint32_t index = 0; index < argCount; index++) {
  1.1004 +    PluginModuleChild::sBrowserFuncs.releasevariantvalue(&convertedArgs[index]);
  1.1005 +  }
  1.1006 +
  1.1007 +  if (!success) {
  1.1008 +    *aResult = void_t();
  1.1009 +    *aSuccess = false;
  1.1010 +    return true;
  1.1011 +  }
  1.1012 +
  1.1013 +  Variant convertedResult;
  1.1014 +  success = ConvertToRemoteVariant(result, convertedResult, GetInstance(),
  1.1015 +                                   false);
  1.1016 +
  1.1017 +  DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
  1.1018 +
  1.1019 +  if (!success) {
  1.1020 +    *aResult = void_t();
  1.1021 +    *aSuccess = false;
  1.1022 +    return true;
  1.1023 +  }
  1.1024 +
  1.1025 +  *aResult = convertedResult;
  1.1026 +  *aSuccess = true;
  1.1027 +  return true;
  1.1028 +}
  1.1029 +
  1.1030 +bool
  1.1031 +PluginScriptableObjectChild::RecvProtect()
  1.1032 +{
  1.1033 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
  1.1034 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
  1.1035 +
  1.1036 +  Protect();
  1.1037 +  return true;
  1.1038 +}
  1.1039 +
  1.1040 +bool
  1.1041 +PluginScriptableObjectChild::RecvUnprotect()
  1.1042 +{
  1.1043 +  NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
  1.1044 +  NS_ASSERTION(mType == LocalObject, "Bad type!");
  1.1045 +
  1.1046 +  Unprotect();
  1.1047 +  return true;
  1.1048 +}
  1.1049 +
  1.1050 +bool
  1.1051 +PluginScriptableObjectChild::Evaluate(NPString* aScript,
  1.1052 +                                      NPVariant* aResult)
  1.1053 +{
  1.1054 +  nsDependentCString script("");
  1.1055 +  if (aScript->UTF8Characters && aScript->UTF8Length) {
  1.1056 +    script.Rebind(aScript->UTF8Characters, aScript->UTF8Length);
  1.1057 +  }
  1.1058 +
  1.1059 +  bool success;
  1.1060 +  Variant result;
  1.1061 +  CallNPN_Evaluate(script, &result, &success);
  1.1062 +
  1.1063 +  if (!success) {
  1.1064 +    return false;
  1.1065 +  }
  1.1066 +
  1.1067 +  ConvertToVariant(result, *aResult);
  1.1068 +  return true;
  1.1069 +}

mercurial