Example #1
0
bool BrowserClient::OnQuery(CefRefPtr<CefBrowser> browser,
                               CefRefPtr<CefFrame> frame,
                               int64 query_id,
                               const CefString& request,
                               bool persistent,
                               CefRefPtr<Callback> callback)
{
  // Parse JSON args
  std::string requestStr = request;
  OE_DEBUG << LC << "Query received: " << requestStr << std::endl;

  JsonArguments args(request);
  std::string command = args["command"];


  // Pass to callbacks to handle
  bool handled = false;
  if (!command.empty())
  {
    for( ExecuteCallbacks::iterator i = _callbacks.begin(); i != _callbacks.end(); )
    {
        if ( i->valid() )
        {
            osg::ref_ptr<ExecuteCallback::ReturnVal> ret = i->get()->execute(query_id, command, args, persistent ? callback : 0L);
            if (ret.valid())
            {
                handled = true;

                if (ret->errorCode == 0)
                {
                    callback->Success(ret->value);
                }
                else
                {
                    callback->Failure(ret->errorCode, ret->value);
                }

                break;
            }

            ++i;
        }
        else
        {
            i = _callbacks.erase( i );
        }
    }
  }


  // If unhandled call Failure callback
  if (!handled)
  {
      std::string result = "No handler found for query.";
      callback->Failure(404, result);
  }

  return true;
}
void BrowserClient::OnQueryCanceled(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int64 query_id)
{
    for( ExecuteCallbacks::iterator i = _callbacks.begin(); i != _callbacks.end(); )
    {
        if ( i->valid() )
        {
            if (i->get()->cancel(query_id))
                break;

            ++i;
        }
        else
        {
            i = _callbacks.erase( i );
        }
    }
}