コード例 #1
0
nsresult TranslateChoices(
    JS::HandleValue aChoices,
    const nsTArray<PermissionRequest>& aPermissionRequests,
    nsTArray<PermissionChoice>& aTranslatedChoices) {
  if (aChoices.isNullOrUndefined()) {
    // No choice is specified.
  } else if (aChoices.isObject()) {
    // Iterate through all permission types.
    for (uint32_t i = 0; i < aPermissionRequests.Length(); ++i) {
      nsCString type = aPermissionRequests[i].type();

      JS::Rooted<JSObject*> obj(RootingCx(), &aChoices.toObject());
      // People really shouldn't be passing WindowProxy or Location
      // objects for the choices here.
      obj = js::CheckedUnwrapStatic(obj);
      if (!obj) {
        return NS_ERROR_FAILURE;
      }

      AutoJSAPI jsapi;
      jsapi.Init();

      JSContext* cx = jsapi.cx();
      JSAutoRealm ar(cx, obj);

      JS::Rooted<JS::Value> val(cx);

      if (!JS_GetProperty(cx, obj, type.BeginReading(), &val) ||
          !val.isString()) {
        // no setting for the permission type, clear exception and skip it
        jsapi.ClearException();
      } else {
        nsAutoJSString choice;
        if (!choice.init(cx, val)) {
          jsapi.ClearException();
          return NS_ERROR_FAILURE;
        }
        aTranslatedChoices.AppendElement(PermissionChoice(type, choice));
      }
    }
  } else {
    MOZ_ASSERT(false, "SelectedChoices should be undefined or an JS object");
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}
コード例 #2
0
void
JavaScriptFilterService::doIdle(void)
{
    ODL_OBJENTER(); //####
    if (isActive())
    {
        ODL_LOG("(isActive())"); //####
        if (_goAhead.check())
        {
            ODL_LOG("(_goAhead.check())"); //####
            if (_scriptThreadFunc.isNullOrUndefined())
            {
                ODL_LOG("(_scriptThreadFunc.isNullOrUndefined())"); //####
                // We have a request from an input handler.
                if (_inHandlers.size() > _mostRecentSlot)
                {
                    ODL_LOG("(getInletCount() > _mostRecentSlot)"); //####
                    JS::HandleValue                handlerFunc = _inletHandlers[_mostRecentSlot];
                    JavaScriptFilterInputHandler * aHandler = _inHandlers.at(_mostRecentSlot);

                    if (aHandler && (! handlerFunc.isNullOrUndefined()))
                    {
                        ODL_LOG("(aHandler && (! handlerFunc.isNullOrUndefined()))"); //####
                        JS::RootedValue     argValue(_context);
                        JS::Value           slotNumberValue;
                        JS::AutoValueVector funcArgs(_context);
                        JS::RootedValue     funcResult(_context);

                        slotNumberValue.setInt32(static_cast<int32_t>(_mostRecentSlot));
                        createValueFromBottle(_context, aHandler->getReceivedData(), &argValue);
                        funcArgs.append(slotNumberValue);
                        funcArgs.append(argValue);
                        JS_BeginRequest(_context);
                        if (JS_CallFunctionValue(_context, _global, handlerFunc, funcArgs,
                                                 &funcResult))
                        {
                            // We don't care about the function result, as it's supposed to just
                            // write to the outlet stream(s).
                        }
                        else
                        {
                            ODL_LOG("! (JS_CallFunctionValue(_context, _global, " //####
                                    "handlerFunc, funcArgs, &funcResult))"); //####
                            JS::RootedValue exc(_context);

                            if (JS_GetPendingException(_context, &exc))
                            {
                                JS_ClearPendingException(_context);
                                std::stringstream buff;
                                YarpString        message("Exception occurred while executing "
                                                          "handler function for inlet ");

                                buff << _mostRecentSlot;
                                message += buff.str();
                                message += ".";
                                MpM_FAIL_(message.c_str());
                            }
                        }
                        JS_EndRequest(_context);
                    }
                }
                _staller.post();
            }
            else
            {
                ODL_LOG("! (_scriptThreadFunc.isNullOrUndefined())"); //####
                try
                {
                    JS::AutoValueVector funcArgs(_context);
                    JS::RootedValue     funcResult(_context);

                    JS_BeginRequest(_context);
                    if (JS_CallFunctionValue(_context, _global, _scriptThreadFunc, funcArgs,
                                             &funcResult))
                    {
                        ODL_LOG("(JS_CallFunctionValue(_context, _global, " //####
                                "_scriptThreadFunc, funcArgs, &funcResult))"); //####
                        // We don't care about the function result, as it's supposed to just perform
                        // an iteration of the thread.
                    }
                    else
                    {
                        ODL_LOG("! (JS_CallFunctionValue(_context, _global, " //####
                                "_scriptThreadFunc, funcArgs, &funcResult))"); //####
                        JS::RootedValue exc(_context);

                        if (JS_GetPendingException(_context, &exc))
                        {
                            ODL_LOG("(JS_GetPendingException(_context, &exc))"); //####
                            JS_ClearPendingException(_context);
                            MpM_FAIL_("Exception occurred while executing the scriptThread "
                                      "function.");
                        }
                    }
                    JS_EndRequest(_context);
                }
                catch (...)
                {
                    ODL_LOG("Exception caught"); //####
                    throw;
                }
            }
        }
    }
    ODL_OBJEXIT(); //####
} // JavaScriptFilterService::doIdle