Danh mục

Creating Applications with Mozilla-Chapter 8. XPCOM- P1

Số trang: 15      Loại file: pdf      Dung lượng: 36.75 KB      Lượt xem: 3      Lượt tải: 0    
Thư viện của tui

Hỗ trợ phí lưu trữ khi tải xuống: 5,000 VND Tải xuống file đầy đủ (15 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu creating applications with mozilla-chapter 8. xpcom- p1, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Creating Applications with Mozilla-Chapter 8. XPCOM- P1 Chapter 8. XPCOM- P1This chapter provides a high-level introduction to XPCOM componenttechnology. XPCOM can be difficult to master, but after reading thischapter, you should have a good sense of what it is and the important part itplays as Mozillas core technology. You should be able to find and useexisting scriptable components in your own applications and create a simpleXPCOM component by using JavaScript or C++.XPCOM permits a reusable code module to be globally accessible to aMozilla-based application. You do not need to worry about includingexternal source files in your application distribution and you can distributecomponents by using XPInstall. This type of architecture makes thedevelopment of core application services flexible and entirely modular.The section Section 8.2.1 lets you create an interface from start to finish --writing the implementation for that interface, compiling it into a type library,registering it with Mozilla, and then testing the new component. Oneadvantage of using XPCOM is that you can create multiple implementationsfor a single interface; following the JavaScript component section, we willtake the same nsISimple interface and implement it in C++ as well.The section Section 8.2.5 includes some techniques and programming tasksthat are particular to C++ components, such as handling return values andgenerating header files and useful macros. The section Section 8.2.7introduces the XPCOM bindings for the Python language (pyXPCOM).First, it provides an overview of XPCOM and how it relates to othertechnologies used in Mozilla.8.1. What Is XPCOM?XPCOM is Mozillas cross-platform component object model. Although it issimilar to Microsofts COM technology, this chapter points out someimportant differences.Essentially, when you program in a component-based environment, you doone of three things: you create a new component using existing components,write a component that implements other components, and establishinterdependencies and a service network.8.1.1. What Is a Component?Youve already seen components used in this book. In some cases, you mayhave used the services of Mozilla components without knowing it -- forexample, when you created a XUL tree widget in the section Section 3.4.2 inChapter 3, and used its built-in layout and view capabilities. Some of thisfunctionality is defined in an interface called nsITreeView, which providesspecific methods and properties for a XUL tree, persisting its state, row, cell,and column properties, navigation, and other object metadata used in a treeobject. Behind the scenes, youll find an XPCOM-instantiated tree viewobject where methods and properties associated with the XUL element areaccessed via DOM > JavaScript > XPConnect > XPCOM layers.A component is a reusable or modular piece of code that implements aclearly defined interface. In Mozilla, this code can exist as a singletonservice or an object instance. A singleton service is an object instance that iscreated only once and then used by other code (usually called callers,clients, or consumers). An object instance is an object that is instantiatedonce or many times. Components are written as classes that typically havemember variables and methods. The basic purpose of a component is toimplement a clearly defined set of APIs that exist in a public interface. Theinterface exists separately so that the implementation is abstracted away, andit can be changed without affecting the interface or breaking binarycompatibility. When interfaces are deployed in a production environment,they are frozen, which means they are held in an immutable state --theoretically for as long as the application exists. While MSCOM provides acomponent-based programming model on Microsoft platforms, XPCOMprovides it on all platforms where Mozilla is available.Example 8-1 shows how simple using XPCOM components can be. In twolines, an XPConnect-wrapped nsIBookmarksService object isinstantiated, and one of its methods is called, providing easy access to thisXPCOM component from JavaScript.Example 8-1. Using an XPCOM object in script// create a bookmark service object in JSvar bmks =Components.classes[@mozilla.org/browser/bookmarks-service;1].getService(Components.interfaces.nsIBookmarksService);// call one of the objects methods:// flush the bookmarks to disk if theyve beentouched.bmks.Flush( );As you can see, the assignment of an XPCOM object to the variable bmkstakes only a single line. Once you are comfortable using XPCOM fromJavaScript, you can use any of Mozillas scriptable interfaces in yourapplication. Once an object like bmks is created, as in Example 8-1, it canbe used to call any method in the nsIBookmarksService interface, of whichFlush( ) is an example.8.1.2. XPConnect and the Component ObjectAs shown the previous example, the XPCOM object is called andinstantiated from script. For an interpreted language like JavaScript to calland instantiate it, a bridge must bind JavaScript types to XPCOM types.These type bindings are part of a technology called XPConnect.In XPConnect, XPCOM interfaces, classIDs, and progIDs are stored asglobal JavaScript objects and properties that can be manipulated directlythrough a top-level object called Components. This object accesses anycomponent that is declared scriptable in an XPCOM IDL interface.Through the Components object, you can access and use the services thatthese interfaces provide. The Component objects top-level properties andmethods include:QueryInterface A method used to match an interface with a desired implementation. The implementation can be in C, C++, JavaScript, Python, and other languages for which appropriate bindings are created. You can have multiple implementations for an ...

Tài liệu được xem nhiều: