C++ Binding: How to bind a c++ object to JS Options

928 views
Skip to first unread message

ExtensionDev

unread,
Feb 29, 2012, 1:04:10 PM2/29/12
to Chromium-extensions
(Re-post from v8-users discussion group).

Hello,

I am a newbie Chrome extension developer. I am currently working on a
new extension for which I need to know on how to bind a c++ object to
JS.

I am trying to implement the following:
C++ Code:
Class Foo {
public:
Foo();
~Foo();
Baz GetBazOj();
private:
Baz baz_;
};

Class Baz {
public:
Baz();
~Baz();
int MethodBaz();
private:
int value_;
};

In Javascript, I want to create the instance of c++ class and access
their member functions like this:

var fooObj1 = new Foo();
var fooObj2 = new Foo();
var baz1 = fooObj1.GetBazObj();
var baz2 = fooObj2.GetBazObj();
var baz1Value = baz1.MethodBaz();
var baz2Value = baz2.MethodBaz();

In Chromium extensions internals source code, I found some reference
to NPAPI based CPPBoundClass. I thought NPAPI functions is for Plugin.
I am not sure on how to use this class to do the require c++
bindings.
Can someone provide some examples and some information regarding
this?

I really appreciate your help.

Thanks.

Mike West

unread,
Mar 1, 2012, 7:10:46 AM3/1/12
to Chromium-extensions, ExtensionDev
If you're trying to bind C++ code to a JavaScript interface, you're
really going to have to either recompile Chrome, or rely on bridging
JavaScript to a Native Client application or NPAPI plugin. There's no
mechanism to just throw C++ code into the extension system to achieve
the result you're looking for.

To step back a moment, what's the goal you're trying to reach? There
might be a simpler mechanism available to you.

-Mike

Nathan Wray

unread,
Mar 1, 2012, 7:22:20 AM3/1/12
to chromium-...@chromium.org
If you're trying to access a C++ object to Javascript, you're looking for NPAPI. 

"
NPAPI is a really big hammer that should only be used when no other approach will work."

http://code.google.com/chrome/extensions/npapi.html

Take a look at the Firebreath project which makes plugin development much easier. 

http://www.firebreath.org/display/documentation/FireBreath+Home




--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


ext dev

unread,
Mar 1, 2012, 1:06:37 PM3/1/12
to Mike West, Chromium-extensions
Mike,

In extension JS, I want to do the following.

chrome.browserAction.onClicked.addListener(function(tab) {
       experimental.custom.getFooObject(function(FooObj) {
            var sum = FooObj.val1 + FooObj.val2;
            FooObj.SetSum(sum);
            var storedSumVal = FooObj.GetSum();
        });
});

In C++, I will be having a Foo Class.

Class Foo {
  public:
     void SetSum(int sum);
      int  GetSum();
      int val1;
      int val2;
      int sum;
};


I am trying to write a new extension API  (Let's say experimental.custom.getFooObject).  From my understanding, when an extension JS calls this API,  this call goes from the extension renderer process to the browser process. In the browser process, I want to do a DB access and return the results(val1 and val2) to Extension Renderer. In the extension renderer, I want to construct a C++ object using the results received from the browser process and return the newly constructed object to extension JS. From the extension JS, I should be able to access the member functions and data members of that C++ object.

I can make a custom Chromium build. In your previous reply, you said NPAPI Plugin. With NPAPi plugins, I cannot add utility classes to my JS namespace. I need to instantiate the new plugin element(<object>/<element>) and call the methods on that plugin element. Is it right? If so, I don't want to create new elements to the html file.

Can you explain in detail about bridging JavaScript to a Native Client application?

Thanks.

Mohamed Mansour

unread,
Mar 1, 2012, 1:47:25 PM3/1/12
to ext dev, Mike West, Chromium-extensions
Hello,

Usually in NPAPI, you create one element (embed) in your background page, and that is all. Then you can use properties and methods from your C++.

If you don't want to do that and want to create your own extension api (extend the current extension model with your own API methods), then you have to compile your own version of Chromium. You can follow these design documents  http://dev.chromium.org/developers/design-documents/extensions  and bind your own V8 extensions so it could be exposed.


Kind regards,
Mohamed Mansour



ext dev

unread,
Mar 1, 2012, 7:10:17 PM3/1/12
to Mohamed Mansour, Mike West, Chromium-extensions
Mohamed,

Thanks for your reply.  Using the CppBoundClass declared in base/webkit/glue/cpp_bound_class.h,  I wrote the code to bind the C++ class to a JS object. I successfully bound "Foo" c++ class object with "window.Foo" in JS. I can access the C++ instance data members and member functions in JS as follows:

window.Foo.val1
window.Foo.setVal1(29);
var value = window.Foo.GetVal1();

But, I did not find any way to return a object of a different class from the "Foo" instance.

Eg:
var baz = window.Foo.getBazObj();
baz.getObjValue();
baz.calculateSum();

Is there any way to implement this?

Thanks.
Reply all
Reply to author
Forward
0 new messages