Return to Video

Introduction to XPCOM and using your first XPCOM component

  • 0:00 - 0:03
    In this video, I'm going to show you what XPCOM is, and how to use it.
  • 0:03 - 0:08
    XPCOM is a technology created by Mozilla, for creating cross platform components.
  • 0:08 - 0:11
    It stands for Cross Platform Component Object Model.
  • 0:11 - 0:16
    Now what an XPCOM object is, is it's basically just a set of functionality that accomplishes something.
  • 0:16 - 0:19
    And what's special about them is these 3 things.
  • 0:19 - 0:29
    1. It can be used across different products, so some examples are Thunderbird, Firefox, SeaMonkey, or any other third party product that wants to support it.
  • 0:29 - 0:32
    Secondly they can be used across different platforms.
  • 0:32 - 0:36
    So typically each XPCOM object is supported on Boot2Gecko [Firefox OS], Linux, OSX, Windows, etc.
  • 0:36 - 0:41
    And thirdly, it can be used across different programming languages.
  • 0:41 - 0:45
    Most commonly C++ and JavaScript, but you can also use Python,
  • 0:45 - 0:49
    I believe .NET with Mono and there's probably a bunch of other languages that are supported as well.
  • 0:49 - 0:53
    So I'm just going to show you quickly, an example of an XPCOM component.
  • 0:53 - 0:55
    And that's the file picker control.
  • 0:55 - 0:58
    So if I just hit Control + S on my keyboard.
  • 0:58 - 1:00
    You'll see that it comes up right here.
  • 1:00 - 1:04
    And this whole file picker is wrapped around an XPCOM component.
  • 1:04 - 1:07
    And the browser uses that XPCOM component to tie into the functionality.
  • 1:07 - 1:09
    And no matter which operating system you're on.
  • 1:09 - 1:11
    The code is the same for handling showing that file picker.
  • 1:11 - 1:19
    So what I'm going to do in this video, is that I'm going to use the scratchpad and I'm going to interact with that filepicker XPCOM component.
  • 1:19 - 1:21
    So I'm just going to hit Alt to get the menu.
  • 1:21 - 1:24
    Go to Tools, Web Developer, Scratchpad.
  • 1:24 - 1:28
    And if you haven't seen the Scratchpad video, I recommend watching that first.
  • 1:28 - 1:31
    So that you can get this Environment menu up here.
  • 1:31 - 1:35
    So next I'm going to go to the environment menu, and I'm going to change this to Browser.
  • 1:35 - 1:38
    And it's just saying that this scratchpad executes in the Browser context.
  • 1:38 - 1:44
    And that's because otherwise, it's executing in the web page context, which doesn't have access to XPCOM components.
  • 1:44 - 1:48
    So the first thing I want to do is show you a list of XPCOM components that are available to you.
  • 1:48 - 1:50
    So I'm going to go to Components.classes
  • 1:50 - 1:55
    And I'm going to select that and I'm going to go to Inspect.
  • 1:55 - 2:01
    And you can see on the right here, here's a list of all the different XPCOM components.
  • 2:01 - 2:04
    And you can see there is a ton of them.
  • 2:04 - 2:07
    And it has this little Filter properties right here.
  • 2:07 - 2:10
    So I'm just going to search for filepicker and see if anything comes up.
  • 2:10 - 2:15
    And it does, you can expand it, and see its properties right here.
  • 2:15 - 2:21
    There's also something called interfaces. I'm going to go to Components.interfaces just to show you.
  • 2:21 - 2:23
    I'm going to Inspect that.
  • 2:23 - 2:26
    And this shows you all the different interfaces.
  • 2:26 - 2:30
    Now each and every XPCOM component will implement one or more of these interfaces.
  • 2:30 - 2:37
    And I say it implements at least one because, every single XPCOM component implements something called: nsISupports.
  • 2:37 - 2:45
    Now nsISupports is like IUnknown if you happen to know Win32 COM.
  • 2:45 - 2:47
    But if you're not familiar with that, that's fine.
  • 2:47 - 2:54
    Basically, all you need to know is this has 3 methods. One is AddRef, one is Release, and one is QueryInterface.
  • 2:54 - 3:00
    And QueryInterface is how you can use an XPCOM object to see if it supports any of these different interfaces.
  • 3:00 - 3:03
    So let's go ahead and create a file picker XPCOM object.
  • 3:03 - 3:06
    So I'm just going to clear this out, and I'm going to create the file picker.
  • 3:06 - 3:14
    Components.classes here I just put the ID that we previously obtained.
  • 3:14 - 3:16
    @mozilla.org/filepicker;1
  • 3:16 - 3:21
    dot createInstance is the method you call to actually create an XPCOM object.
  • 3:21 - 3:28
    Components.interfaces.nsIFilePicker
  • 3:28 - 3:30
    So you have to pass in the interface right there as well.
  • 3:30 - 3:31
    And I can look that up right here.
  • 3:31 - 3:34
    nsIFilePicker, and you can see it right there.
  • 3:34 - 3:38
    Now another way you can get the interface that corresponds to an object.
  • 3:38 - 3:42
    Is obviously using QueryInterface, which I described before from nsISupports.
  • 3:42 - 3:47
    Or you can just copy this right here, and paste it in Google, with quotes.
  • 3:47 - 3:55
    and usually you'll find an MDN documentation describing how to use that object, and also what the interface methods are.
  • 3:55 - 3:58
    So here's the description of what it is right here.
  • 3:58 - 4:03
    And then it shows which interface it is right here, and then it shows the different methods that are available to you.
  • 4:03 - 4:05
    And the different attributes.
  • 4:05 - 4:12
    So I'm going to go back to the scratchpad, and I'm just going to Inspect that picker control.
  • 4:12 - 4:19
    And you can see here all the different methods that are available for us to use.
  • 4:19 - 4:23
    So next you would normally read the documentation to see how to use the XPCOM object.
  • 4:23 - 4:24
    But I happen to already know.
  • 4:24 - 4:27
    So there are 2 important methods. One is called init and one is called show.
  • 4:27 - 4:29
    And I'll just show you them up here.
  • 4:29 - 4:33
    init function, and actually the next one is called open.
  • 4:33 - 4:36
    And you can see the open right there.
  • 4:36 - 4:47
    So I'm just going to go picker.init(window, "Select a file", and you pass in the mode, whether it's open or save here.
  • 4:47 - 4:52
    Components.interfaces.nsIFilePicker.modeOpen
  • 4:52 - 5:04
    And then you go picker.open, and this takes a callback function which gets passed a result.
  • 5:04 - 5:17
    And inside I'm not going to put any error checking, but I'm just going to say file chosen is picker.file.path.
  • 5:17 - 5:20
    And again I didn't need to know any of this.
  • 5:20 - 5:23
    I could have just looked at the documentation and then figured it out.
  • 5:23 - 5:27
    So I'm just going to hit Run now and see what happens.
  • 5:27 - 5:32
    And you see that the first thing that happens is it's asking me to select a file.
  • 5:32 - 5:34
    It has the title up there.
  • 5:34 - 5:37
    And let's say I select this extra include dot diff file.
  • 5:37 - 5:38
    And hit Open.
  • 5:38 - 5:41
    And now you can see that it shows that alert that we created.
  • 5:41 - 5:43
    File chosen: and then it shows you the file that was chosen.
  • 5:43 - 5:45
    So now I hit OK.
  • 5:45 - 5:51
    So in this video I showed you what XPCOM is, and how to use one particular component.
Title:
Introduction to XPCOM and using your first XPCOM component
Description:

Tutorial showing you what XPCOM is and how to use it.
http://codefirefox.com

Help us caption & translate this video!

http://amara.org/v/DRhu/

more » « less
Video Language:
English
Duration:
05:51

English subtitles

Revisions