📜 ⬆️ ⬇️

Bada Web-based application: C ++ and Javascript interactions

Hi, habradrug!

Briefly about the main thing: as you know, it is enough to call the EvaluateJavascriptN method from C ++ code to transfer data or trigger an event. But from Javascript, calling the C ++ method is not yet possible. To do this, we have to use very much iron scrap. But if it is more convenient for you to develop an application on html / css / Javascript , but at the same time you need access to native code and local files, then you are definitely under the cat. There we will look at this crowbar in more detail and prepare for ourselves a Project Template for a quick start.

First, I will ask for an apology for my gaps in the knowledge of the Russian language, but I will make every effort so that the text would be without errors.

Little lyrics


Last week received an order to develop a variety of small applications for Bada. I myself work in web areas, but the chef remembering my old knowledge in C ++ and my experience in .NET asked for help from my colleagues, they say, you remember / figure it out, but you need to help - otherwise we will not have time. And so I received the first two tasks:

The beginning of the way


Roll up your sleeves quickly downloaded Bada SDK . And creating a new project interested me template "Web-Based Application" . It created a web control in which the local html file was loaded, and a search on the Internet suggested that the bada has the -webkit engine and these facts made me very happy. But my joy was hasty: the audio does not play and you cannot receive a local file via ajax because of security. It was here that it occurred to me that we need to link Javascript and C ++ in some way, so in the first case, at the touch of a button, we send the corresponding file path to the native code and play the sound from there. Well, in the second, we will send again the path to the file and get its content, ala XmlHttpRequest .
')

And here is the scrap


After reviewing the web control closer, it turns out that only ILoadingListener is at our disposal . A glimpse in the code mentions a more reassuring IWebUiListener . But the developers have not yet reached the hands of his execution, so back to ILoadingListener . Fortunately, it works as it should, that is, if we add an iframe to the body or call window.frames.myFrame.location.reload () we will receive loading events in the code. I should note that we can not even specify the src attribute in the iframe . Well, now everything is very simple, change hash , do an iframe reload and get the OnLoadingRequested url of the page and react accordingly. However, when I saw that the web control has a GetTitle method, I stopped by changing the page title and getting it in OnLoadingStarted . Thus, we have no problems with URL encoding.

Decorate crowbar - get the framework


There was a desire to create a ready-made base from which to start writing such applications. Here are the main examples / tasks of the code:

Javascript:


bada.sendData({key: 'Value' ,secondKey: 'secondValue' });

This function will pass in C ++ "key = Value & secondKey = secondValue" and there it will be parsed in WebCppProxy :: Data :: StringDictionary .
More examples do not require an explanation:
bada.getFile( '/Res/help.html' , function (responseText){ $( '#helpDialog' ).html(responseText)});<br>bada.playAudio( '/Res/sound.mp3' );


For better testing, a console was created, which will be available if you set the debug = 'true' attribute in the body tag

C ++


Here the leader is the WebCppProxy :: WebProxyForm class - this is the same web control, but in which the interconnection with the JavaScript is implemented. The counselor obtains a class inherited from WebCppProxy :: ICommandListener from the current application and calls certain methods during execution.
virtual void OnCommand(Osp::Base:: String command) = 0;<br> virtual void OnCommand(WebCppProxy::Data::StringDictionary* dictionary) = 0;<br> virtual void OnReady() = 0; // ondomready <br>


Reading a file and playing mp3 happens behind the scenes. There are sketches of code for working with the database, but after the lack of audio, I was pleasantly surprised that Bada implemented localStorage .

Here, in fact, very briefly described the purpose and implementation, and the prepared project can be downloaded / viewed here: Assembla

The code is provided for reference only, and not because of copyright, but because of safety / stability / productivity considerations, because it is still very damp.

I hope I helped someone in writing applications for Bada. Although using html5 / css / javascript you can easily port them to other platforms and vice versa.

Source: https://habr.com/ru/post/112856/


All Articles