Firefox add-on in C++ - firefox

Firefox provides XPCOM API interface for writing add-on classes in C++ and allow third-party web applications to access them. I'm wondering - is there any other way of achieving these benefits (i.e. write an add-on in C++ and provide a JavaScript interface, so any JavaScript app can use this interface and eventually C++ class functionality)?

Yes, it is possible to write XPCOM components in C++ (or Javascript for that matter) and expose them to websites. You would register them in the JavaScript-global-property, JavaScript-global-constructor, etc. category or categories.
This stuff is generally not documented very well, if at all. If you'd like to know more about it, then read the code (e.g. on mxr).
But doing so is strongly discouraged for a lot of reasons:
It is very hard to ensure such things are actually secure and make it work reliable.
It is hard to control what website may actually use the new API. JavaScript-global-property and friends will be available to all websites!
Your add-on would be introducing new names into the global javascript namespace, which might clash with web site code. (Because of this, many new WebAPIs aren't introduced directly into the global namespace, but are made a property of navigator).
Due to these reasons, the addons.mozilla.org site will only accept add-ons using this mechanism as an exception to the rule and only when the author demonstrates that the use is required and without real alternatives.
MDN has a couple of articles regarding webpage-to-extension communication and vice versa you could use instead, e.g. "Interaction between privileged and non-privileged pages".
Also, it is discouraged to ship binary XPCOM components within extensions:
You'd need to re-compile your binaries against each Gecko version (every six weeks), because binary components are version tagged since Firefox 4.
Binary APIs/ABIs between (point) releases are not as stable as you would hope. Actually APIs/ABIs where inadvertently broken late in the game quite often, causing lots of unnecessary crashes for a lot of users, e.g. bug 828296.
The platform developers officially said binary components in extensions are a hassle.
Instead, you should try to use Javascript where possible, and/or create a normal binary library (.so, .dll, .dylib) with a C-API/ABI and use js-ctypes. You could implement your XPCOM components in javascript, which would then call into your library with js-ctypes.
AFAIK most add-ons switched to js-ctypes by now.

Related

XPCOM using Web Workers from a Firefox Extension

My Firefox extension is parsing big chunks of data. I would usually use WebWorkers to do this, however in XPCOM I seems that this is not an option. The ChromeWorker, https://developer.mozilla.org/en-US/docs/Web/API/ChromeWorker, seems to be obsolete and "discouraged in new projects".
Are there any options to use workers in a Firefox extension?
Short version: no, you cannot access XPCOM from a different thread. But that doesn't mean that you cannot use chrome workers.
Long version: Firefox used to allow accessing XPCOM from other threads, e.g. via ChromeWorker. This led to all kinds of issues like weird crashes or just plain inconsistent behavior. In the end Mozilla decided that supporting multithreaded XPCOM access was too complicated and error-prone, as was documenting its limitations and stopping people from shooting themselves in the feet.
With current Firefox versions, accessing XPCOM from ChromeWorker is no longer possible. ChromeWorker itself however isn't deprecated however even though the MDN comment can be easily misread as a general deprecation statement. The idea is that you would use ChromeWorker in combination with js-ctypes which will allow you to use native libraries (the ones provided by the operating system, libraries included in Firefox like NSS and libraries distributed with your extension) on a different thread.
Depending on what you are trying to achieve, this might work for you. E.g., if you need XPCOM for file access then you don't even need to use js-ctypes directly - OS.File API will do that for you. However, XPCOM access is limited to the main thread.

What runtime is used by most Windows Viruses?

Most applications created with Microsoft developer tools need some kind of runtime to be installed first.
However most viruses never need any kind of runtime to work. Also they also seem to use undocumented core/kernel APIs without have lib files etc.
So what runtime/application do most virus /virus writers use ?
If the runtime is statically linked in (as opposed to dynamically), then an EXE will be self-contained and you won't need a runtime DLL. However, really, you don't even need a runtime library at all if your code can do everything without calling standard library functions.
As for Windows APIs, in many cases you don't strictly need an import library either -- particularly if you load addresses dynamically via GetProcAddress. Some development tools will even let you link directly against the DLLs (and will generate method stubs or whatever for you). MS tries to ensure that names for documented API calls stay the same between versions. Undocumented functions, not so much...but then, compatibility typically isn't the foremost of concerns anyway when you're deliberately writing malicious software.

Is it possible to hook API calls on Mac OS?

On Windows there a few libraries that allow you to intercept calls to DLLs:
http://www.codeproject.com/kb/system/hooksys.aspx
Is it possible to do this on Mac OS? If so, how is it done?
The answer depends on whether you want to do this in your own application or systemwide. In your own application, it's pretty easy; the dynamic linker provides features such as DYLD_INSERT_LIBRARIES. If you're doing this for debugging/instrumentation purposes, also check out DTrace.
You can replace Objective-C method implementations with method swizzling, e.g. JRSwizzle or Apple's method_exchangeImplementations (10.5+).
If you want to modify library behavior systemwide, you're going to need to load into other processes' address spaces.
Two loading mechanisms originally designed for other purposes (input managers and scripting additions) are commonly abused for this purpose, but I wouldn't really recommend them.
mach_inject/mach_override are an open-source set of libraries for loading code and replacing function implementations, respectively; however, you're responsible for writing your own application which uses the libraries. (Also, take a look at this answer; you need special permissions to inject code into other processes.)
Please keep in mind that application patching/code injection for non-debugging purposes is strongly discouraged by Apple and some Mac users (and developers) are extremely critical of the practice. Much of this criticism is poorly informed, but there have been a number of legitimately poorly written "plug-ins" (particularly those which patch Safari) that have been implicated in application crashes and problems. Code defensively.
(Disclaimer: I am the author of a (free) APE module and an application which uses mach_inject.)

What technology is involved in making Mozilla Firefox?

Programming Languages, Open source libraries and standards adopted to make Firefox works.
It's a large, long lived project, so it's got far too many to list. Especially when you consider ancillary technologies - for example, the Elkhound parser combined with their JavaScript engine creates Dehydra, used to perform static analysis and transform source code, used to bring the old XPCOM stuff up to more recent standards and update dependencies on JavaScript calls.
At the broadest level, the runtime consists of mostly C++ components, configured by XUL interface description language and scripted with JavaScript. IIRC, some of the JS engine code from Adobe is C rather than C++, as are some of the lower level networking libraries. Over recent years, some UI functions have moved from C++ into JS. Then there are the build support and debugging code, which can be Python, perl, make scripts, and so on.
Its all in here :-)
https://developer.mozilla.org/En

Windows API for VISTA, 7 & Beyond

Is there any fundamental differences in the WinAPI/Win32? Is there any additional knowledge required to take advantage of new OS features?
Are there any pitfalls which someone who's coded Win32 apps in the past might fall in?
I'm not talking about Silverlight, that's a whole different ball of wax. (I don't have the VS that supports that at work yet.)
Edit:
Drew has a pretty good answer so far, but what's critical for a programmer to know?
i.e. What should be in an appendix to Charles Petzold's book? (Theoretically)
There are of course lots of new APIs that you should be aware of to make sure you have the tools you need. Beyond that, there are some changes to note.
Philosophical changes
Large parts of the old win32 APIs focused on C-style APIs where handles were passed around. Nowadays, many of the new APIs being developed are COM-based, so boning up on COM and ATL would be worthwhile.
You might also want to take note of the new API style if you're writing your own libraries, which is a bit more consistent and avoids things like hungarian notation.
Replacements
Generally, don't assume that the methods you knew about 10 years ago are still state-of-the-art; they all still exist, so you won't necessarily be told you're doing it wrong. Check MSDN to see if it refers you to something better, and use the latest SDK so that you'll get deprecation warnings for some functions. Especially, make sure string functions you're using are secure.
Specifically, one 'replacement' API is Direct 2d, which is a DirectX-style API for UIs. If you're writing graphics code for Windows 7, you should consider Direct2d over GDI, which has a programming model that is compatible with, but very different than, GDI's. Direct 2d may be ported back to Vista.
Also, instead of using win32-style menuing, consider using the Ribbon, which will be available for Vista as well as Win7.
If you're using the common controls library, make sure to use v6, not the default of v5.
Finally, make sure you're not unnecessarily calling things that require admin privileges, as that will prompt UAC.
All I can think of for now.
There are new API's for each.
There is additional knowledge, though it may not be required, you should be familiar with 64-bit and multi-threaded application development to name a few. The higher level constructs such as Direct2D, .NET etc., etc., are what require the adjustment in knowledge, not necessarily the lower level APIs.
You have the choice: traditional C/C++ or use the newer .Net framework languages (C# / VB.net / Python.net and more). For the latter, knowing the framework is more important than the implementation. You're isolated (in general) from pointers, threading, buffers and memory management and apart from a few differences in syntax once you know the framework it's portable between languages (ie, you could easily pick up VB.net programming if you're a C# guy as most of what your apps will do is call parts of the framework). You can create a class in C#, use it in a VB.net program and reference the same class out of a Powershell cmdlet for example.
The old-style C interfaces are still for Win32 there but unless you've got a specific need to use them (legacy code, Direct X, device drivers for example) I'd look at the newer stuff. As for things like WPF, there isn't even a direct route in via unmanaged code - you have to jump through all sorts of ugly interop hoops.
Nothing special. The old stuff pretty much works as it did.
There are some new APIs, but nothing earth shattering (and following the old Win32 conventions).
So everything you know from Vista is still true for Win7.
Now, there are some new guidelines regarding user experience (touch screen, libraries (user experience stuff, not programmer stuff)), but the API style is the same.
Integrity levels are also a good thing to learn about. Depending on the nature of your application, if it attempts to do anything involving other processes that are running on the OS, it is important to know about this. This technology prevents processes at a lower integrity level from interacting with processes that are running at a higher integrity level. This includes messaging, hooks, DLL injection, opening handles, and many other techniques.

Resources