Cross-Application Data Interaction in Firefox OS

Source: Internet
Author: User

Due to security restrictions, cross-origin invocation of WEB applications is troublesome. Traditional methods include server proxy and jsonp interfaces. However, in firefox OS, such a large discount is not required. The cross-application interaction mechanism has been provided in firefox OS. Although it is not very convenient at present, it can already implement basic cross-application interaction apps. This document uses an actual DEMO to explain the cross-application invocation of B2G.

Overview

This article will develop two applications: Application 1: Caller and Application 2: worker er

Caller has a Show Images button. Click this button to start the consumer er. The consumer er provides some Image Browsing functions. After selecting an image in the consumer er, click the Select button, transfers the image to Caller and displays the selected image in Caller.

The overall effect is as follows:

 

Caller Application

Receiver Application

Details

Cycler

In Firefox OS, to allow an application to be called by other applications, you must declare the application in manifest. webapp. Let's take a look at the manifest. webapp file of the explorer.

 
 
  1. {  
  2.      "name": "Receiver",  
  3.      "launch_path": "/index.html",  
  4.      "developer": {  
  5.          "name": "chyblog",  
  6.          "url": "http://www.chyblog.com"  
  7.      },  
  8.      "icons": {  
  9.          "120": "/style/icons/Receiver.png"  
  10.      },  
  11.      "permissions": [  
  12.          "background",  
  13.          "backgroundservice",  
  14.          "attention",  
  15.          "contacts",  
  16.          "settings",  
  17.          "desktop-notification"  
  18.      ],  
  19.      "activities": {  
  20.          "pick": {  
  21.              "filters": {  
  22.                  "type": ["image/png", "image/gif"]  
  23.              },  
  24.              "returnValue": true,  
  25.              "disposition": "inline"  
  26.          }  
  27.      }  
  28.  }  

The activities attribute declares a web activity named pick. The filters attribute tells the system which types of request responses are applied by the Cycler, and the response name can be pick, A response whose type is image/png or image/gif.

ReturnValue is a Boolean attribute. If it is set to true, the web activity must return data to the application that calls it. If it is set to false, no data is returned to the caller.

The disposition attribute has two optional values: window and inline. If it is a window, it will be opened by starting a new application. If it is an inline, the application interface is displayed on the screen.

In the javaser application, you also need to use js to write the processing code for response calls.

 
 
  1. navigator.mozSetMessageHandler('activity', webActivityHandler);  
  2.  
  3. var activityRequest;  
  4.  
  5. var webActivityHandler = function(request) {  
  6.     activityRequest = request;  
  7.  
  8.     document.getElementById('button').disabled = '';  
  9.     document.getElementById('cancelButton').disabled = '';  
  10.  };  

The preceding code registers an Event Response handle and stores the requester information in the global variable activityRequest.

When you click the Select button, the image information is sent to Caller. The Code is as follows:

 
 
  1. function select() {  
  2.     if (!activityRequest) {  
  3.         alert('There are no any pending activity request.');  
  4.         return;  
  5.     }  
  6.  
  7.     // Return the request  
  8.     activityRequest.postResult({  
  9.         type : 'image/png',  
  10.         text : getBase64Image(selectedImg)  
  11.     });  
  12.     activityRequest = null;  
  13.  
  14.     // close app, currently useless,  
  15.     // see https://bugzilla.mozilla.org/show_bug.cgi?id=789392 
  16.     window.close();  
  17. };  

Because the data interaction between the two applications can only be transmitted using text information, the image in the Caller is sent to the Caller in base64 text encoding mode. In the Caller, set the text to backgroundImage, the code for obtaining the base64 encoding of the image is as follows:

 
 
  1. function getBase64Image(imgSrc) {  
  2.     var img = new Image();  
  3.     img.src = imgSrc;  
  4.     var canvas = document.createElement("canvas");  
  5.     canvas.width = img.width;  
  6.     canvas.height = img.height;  
  7.     var ctx = canvas.getContext("2d");  
  8.     ctx.drawImage(img, 0, 0);  
  9.     var dataURL = canvas.toDataURL("image/png", 0.2);  
  10.     return dataURL;  
 

Caller

In the Call application, it mainly triggers the response of the caller Call and receives the processing of the returned data.

Call first creates an activity request. You must inform system of the Request Name and type. And register the processing logic for successful request return and request failure. How to call a request is completely controlled by system. The code for creating a request is as follows:

 
 
  1. var a = new MozActivity({ name : 'pick', data : { type : 'image/png' } }); a.onsuccess = function() { reopenApp(); document.getElementById('showImg').style.backgroundImage = "url(" + this.result.text + ")"; document.getElementById('result').textContent = 'well done'; }; a.onerror = function() { reopenApp(); document.getElementById('showImg').style.backgroundImage = ""; document.getElementById('result').textContent = '(canceled)'; };

It should be emphasized that Caller and aggreger are two independent applications, and the interaction between them is the responsibility of the system app. What we need to do is declare the web activity and trigger the request and Response Request.

Source code download: http://chyblog-chyblog.stor.sinaapp.com/wp-content/uploads/2012/10/webActivity.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.