Create object of type Promise in renderer process

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Create object of type Promise in renderer process

Postby kbiskar » Tue Sep 30, 2014 7:26 pm

For various reasons, I'm trying to expose several functions that return Promise objects without leaving the renderer process. I currently can return generic objects that look like a Promise object (ie it has a '.then' and a '.catch' method), but I was wondering if I could specify the object is a specific type. I have a PromiseHandler class that takes a CefV8Value that is a function and adds a success and failure callback to it. My current prototype does mostly what I want a promise to do, but I don't want the content to know the difference between browser process Promises and renderer process psuedo-Promises. Currently I have something that looks like this:
Code: Select all
class OnSuccessHandler : public CefV8Handler
{
....
}

class PromiseHandler : public CefV8Handler
{
   virtual bool Execute(const CefString& name,
                              CefRefPtr<CefV8Value> object,
                              const CefV8ValueList& arguments,
                              CefRefPtr<CefV8Value>& retval,
                              CefString& exception) {
      m_wrappedFunction->GetFunctionHandler()->Execute(name, object, arguments, retval, exception);

      retval = CefV8Value::CreateObject(NULL);   // This object cannot be specified of a particular type
     
      CefRefPtr<CefV8Handler> onSuccessHandler = new OnSuccessHandler();
      CefRefPtr<CefV8Value> onSuccessFunction = CefV8Value::CreateFunction("OnSuccess", onSuccessHandler);
      retval->SetValue("then", onSuccessFunction, V8_PROPERTY_ATTRIBUTE_NONE);

      return true;
   }
}

I have a real Promises setup in the browser process in a more typical fashion; I Eval a number of function definitions that use Promises and have a CefMessageRouterBrowserSide::Handler impl that can execute them appropriately. But the CefMessageRouterRendererSide interfaces don't seem to have the same OnQuery methods I use to handle the registered promise functions. Is there a way to get something like that in the renderer process, or is there a way to create objects of a specific type in the renderer similar to what I already have, or is there another approach completely that I'm not seeing?
kbiskar
Techie
 
Posts: 25
Joined: Mon May 12, 2014 1:02 pm

Re: Create object of type Promise in renderer process

Postby kbiskar » Thu Oct 02, 2014 11:56 am

I think I found my in by using CefRegisterExtension during the CefRenderProcessHandler::OnWebKitInitialized() method call. I've got something like the following.

Code: Select all
void RenderProcessHandler::OnWebKitInitialized() {
   CefRefPtr<CefV8Handler> = new CustomHandler();
   CefString code = "var custom = {};"
                            "(function() {"
                            "   custom.method = function(args) {"
                            "      native function MyFunction;"
                            "      return new Promise(function(resolve, reject) {"
                            "         MyFunction(args, resolve, reject);"
                            "      }"
                            "   }"
                            "})();"
   CefRegisterExtension("Namespace", code, customHandler);
}

However, I would like to verify that a page has the appropriate security credentials before I expose my custom functions and I don't immediately see how to do that here as I don't have access to the DOM (and thus the URL and headers).
kbiskar
Techie
 
Posts: 25
Joined: Mon May 12, 2014 1:02 pm

Re: Create object of type Promise in renderer process

Postby magreenblatt » Thu Oct 02, 2014 1:04 pm

You can add native bindings to the JavaScript 'window' object in OnContextCreated. See https://code.google.com/p/chromiumembed ... ntegration.
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 43 guests