示例#1
0
/**
 * Calls a method on a Java object and returns a two-element JS array, with
 * the first element being a boolean flag indicating an exception was thrown,
 * and the second element is the actual return value or exception.
 */
JSBool JavaObject::invokeJava(JSContext* ctx, SessionData* data,
    const gwt::Value& javaThis, int dispId, int numArgs, const jsval* jsargs,
    jsval* rval) {
  HostChannel* channel = data->getHostChannel();
  SessionHandler* handler = data->getSessionHandler();
  scoped_array<gwt::Value> args(new gwt::Value[numArgs]);
  for (int i = 0; i < numArgs; ++i) {
    data->makeValueFromJsval(args[i], ctx, jsargs[i]);
  }

  bool isException = false;
  gwt::Value returnValue;
  if (!InvokeMessage::send(*channel, javaThis, dispId, numArgs, args.get())) {
    Debug::log(Debug::Debugging) << "JavaObject::call failed to send invoke message" << Debug::flush;
  } else {
    Debug::log(Debug::Spam) << " return from invoke" << Debug::flush;
    scoped_ptr<ReturnMessage> retMsg(channel->reactToMessagesWhileWaitingForReturn(handler));
    if (!retMsg.get()) {
      Debug::log(Debug::Debugging) << "JavaObject::call failed to get return value" << Debug::flush;
    } else {
      isException = retMsg->isException();
      returnValue = retMsg->getReturnValue();
    }
  }
  // Since we can set exceptions normally, we always return false to the
  // wrapper function and set the exception ourselves if one occurs.
  // TODO: cleanup exception case
  jsval retvalArray[] = {JSVAL_FALSE, JSVAL_VOID};
  JSObject* retval = JS_NewArrayObject(ctx, 2, retvalArray);
  *rval = OBJECT_TO_JSVAL(retval);
  jsval retJsVal;
  Debug::log(Debug::Spam) << "  result is " << returnValue << Debug::flush;
  data->makeJsvalFromValue(retJsVal, ctx, returnValue);
  if (isException) {
    JS::MutableHandleValue argHandle = JS::MutableHandleValue::fromMarkedLocation(&retJsVal);
    JS_SetPendingException(ctx, argHandle);
    return false;
  }
#if GECKO_VERSION >= 26000
        JS::Rooted<JS::Value> tmp(ctx);
        tmp = retJsVal;
#else
        jsval tmp = retJsVal;
#endif
  if (!JS_SetElement(ctx, retval, 1, &tmp)) {
    Debug::log(Debug::Error) << "Error setting return value element in array"
        << Debug::flush;
    return false;
  }
  return true;
}
// ---------------------------------------------------------------------------
// RPeninputServerImpl::HandleCommand
// Ask ui layout handle command
// ---------------------------------------------------------------------------
//
TInt RPeninputServerImpl::HandleCommand(TInt aCmd,const TDesC8& aBuf, TInt &aResult)
    {
    //if(iSingletonServer != this)
      //  return iSingletonServer->HandleCommand(aCmd,aBuf, aResult);
    
    if(!IsForegroundSession()) // not handle non-foregound session command
        return EFalse;
    
    TIpcArgs arg;    
    TPckgC<TInt> cmdMsg(aCmd);    
    arg.Set(KMsgSlot0,&cmdMsg);   
    
    arg.Set(KMsgSlot1,&aBuf);
    
    TPckg<TInt> retMsg(aResult);
    arg.Set(KMsgSlot2,&retMsg);
    
    return SendReceive(EPeninputRequestHandleClientCommand,arg);
    }
// ---------------------------------------------------------------------------
// RPeninputServerImpl::SetForeground
// Set current session to be foreground application session
// ---------------------------------------------------------------------------
//
TBool RPeninputServerImpl::SetForeground(TBool aMustConnectFlag)
    {
 //   if(iIsForegroundSession)
 //       return ETrue;
    
    TInt curAppId;
    TRAP_IGNORE( curAppId = GetAppUidByWndGroupIdL(CCoeEnv::Static()->WsSession(),
                        CCoeEnv::Static()->RootWin().Identifier()).iUid);
    TIpcArgs arg;        
    TPckgC<TInt> idData(curAppId);
    arg.Set(KMsgSlot0,&idData); 

    TPckgC<TBool> flagMsg(aMustConnectFlag);    
    arg.Set(KMsgSlot1,&flagMsg); 
    
    TPckg<TInt> retMsg(iIsForegroundSession);
    arg.Set(KMsgSlot2,&retMsg);
    
    SendReceive(EPeninputRequestSetForeground,arg);
    return iIsForegroundSession;
    }