1

In this link from MDN, it explains how to write a unit test for developing an addon for firefox. However, there are several segments that I don't understand and didn't find any useful result after searching google.

The first one, the following is a paragraph quoted from above link:

In a web page, you can perform Base64 encoding and decoding using the btoa() and atob() functions. Unfortunately these functions are attached to the window object: since this object is not available in your main add-on code, atob() and btoa() aren't available either. So we'll create a base64 module to expose these functions from the platform (see Creating Reusable Modules).

What does "the platform" in the above paragraph mean? the "Services.jsm"?

Also in the following code:

const { atob, btoa } = require("resource://gre/modules/Services.jsm");

this makes atob and btoa as one of the Services that is available for other class? or make (constant variables)atob and btoa both reference to the Services.jsm?

The Second one: what are these two lines of code do?

exports.atob = a => atob(a);
exports.btoa = b => btoa(b);

I understand the part

exports.atob

which enables atob function to be available from other classes outside the "base64.js". but what does the following mean?

= a => atob(a);

I didn't find that javascript has "=>" operator!

From my understaning, the reason we create a base64.js is because atob and btoa can only be called if we have the window object. Therefore we can use Services.jsm to get a reference of the window object?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ganor
  • 13
  • 3

1 Answers1

1

For "platform" in the paragraph you quoted they mean the set of functions which is not strictly Firefox browser code but rather implements basic, share functionalities. This usually lives in toolkit/modules in the mozilla-central repository. Services.jsm lives there as well so yes, that's part of the platform. Moreover, atob and btoa are both imported from Services.jsm.

The arrow => in exports.atob = a => atob(a); defines what's called an arrow function: it's a new, shorter syntax to define functions in JavaScript. This SO answer has many useful informations about it.

From my understanding, the reason we create a base64.js is because atob and btoa can only be called if we have the window object. Therefore we can use Services.jsm to get a reference of the window object?

That's almost correct: you need to export the function from the underlying platform as you don't have a window object there. If you had a window object, you would have just done window.atob or something like that. That call would have still called the same function you imported from Services.jsm.

So you're not using Services.jsm to get a reference to the window object, but rather directly importing the needed functions so that you don't need to have a window object.

Community
  • 1
  • 1
Dexter
  • 2,482
  • 27
  • 40
  • Thank you so much for your detailed explanation!
    Can I say that Services.jsm is the bridge to export all the functions that bond to the https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/btoa windowbase64?
    – ganor Jul 25 '16 at 16:22
  • Grrrr...I can only edit comments for 5mins, sorry for this following comment ! so can I say Services.jsm can be used to export all the functions that are not available without window related objects? or it is a case by case situation? I cannot believe that the '=>' was answered before on the forum! I did search "=>" and nothing showed up...really appreciate! – ganor Jul 25 '16 at 16:34
  • No problem! Services.jsm can be used to export all the function defined there, which may or may not be _all_ the functions. – Dexter Jul 26 '16 at 06:30
  • I think where I stuck right now is after looking into Services.jsm's source code, I didn't find btoa() being defined inside Services.jsm.https://dxr.mozilla.org/mozilla-central/source/toolkit/modules/Services.jsm besides, after `code`const { atob, btoa } = require("resource://gre/modules/Services.jsm"); or btoa is defined in one of the module inside of Service.jsm? – ganor Jul 26 '16 at 12:53
  • Please ignore my previous comment, I screwed up that one, sorry! I think where I stuck right now is after looking into Services.jsm's source code, I didn't find btoa() being defined inside Services.jsm. [link](https://dxr.mozilla.org/mozilla-central/source/toolkit/modules/Services.jsm),or btoa is defined in one of the module inside of Service.jsm? besides, after 'const { atob, btoa } = require("resource://gre/modules/Services.jsm");' atob contain the reference to the platform/Services.jsm or the real atob function? THX – ganor Jul 26 '16 at 13:03