Creating Applications with Mozilla-Chapter 8. XPCOM- P6

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:11

0
41
lượt xem
4
download

Creating Applications with Mozilla-Chapter 8. XPCOM- P6

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'creating applications with mozilla-chapter 8. xpcom- p6', 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ả

Chủ đề:
Lưu

Nội dung Text: Creating Applications with Mozilla-Chapter 8. XPCOM- P6

  1. Chapter 8. XPCOM- P6 8.2.6.2. Creating an instance of an existing Mozilla component Creating an instance of a component and accessing methods and attributes is different in C++ than it is in JavaScript. Using the nsILocalFile interface lets you walk through the code to create an instance of this component from C++: nsCOMPtr file(do_CreateInstance("@mozilla.org/file/lo cal;1")); You can also instantiate the object as follows: nsresult rv; nsCOMPtr file = do_CreateInstance("@mozilla.org/file/local;1 ", &rv); if (NS_FAILED(rv)) return rv; Both techniques assign an nsCOMPtr to a newly allocated instance of an nsLocalFile object.
  2. Example 8-17 accesses the public methods available from this component by using the pointer identifier file. Example 8-17. Example 8-17: Testing for nsresults from component methods if (file) { nsresult rv; rv = file- >InitWithPath(NS_LITERAL_STRING("/tmp")); if (NS_FAILED(rv)) return rv; PRBool exists; rv = file->Exists(&exists); if (NS_FAILED(rv)) return rv; if (exists) print("yep it exists!\n"); nsAutoString leafName; rv = file->GetLeafName(leafName); if (NS_FAILED(rv)) return rv;
  3. if (!leafName.IsEmpty( )) printf("leaf name is %s\n", NS_ConvertUCS2toUTF8(leafName).get( )); } Always test accessors of all XPCOM public methods, getters, and setters. Failures can appear at any time, so be sure to use result checking in your implementations. 8.2.7. Other Languages for XPCOM Although most components available from XPCOM are written in C++, the XPConnect/XPCOM pairing can also accommodate other languages. Language independence is a goal of the XPCOM architecture. Currently, implementations for Python (PyXPCOM) and Ruby (rbXPCOM) exist, with other language bindings being developed. In this respect, the Mozilla framework dovetails with one of the main trends in application development, which is to mix different languages in the development environment. 8.2.7.1. PyXPCOM: the Python binding for XPCOM Python has emerged as a very popular programming language in the last couple of years. It even does some of the application work and other heavy lifting that were the province of C++. Mozilla now offers a Python "binding" similar to the XPConnect binding for JavaScript that allows you to write application code in Python,
  4. compile it in XPCOM, and make it available like you would any C++ component in the Mozilla application framework. As with other XPCOM programming languages, you must create an implementation file (in Python) and an interface file (in IDL), as shown in Examples 8-18 and 8-19, respectively. The terms and constructs for Python components are similar to those of C++. In the implementation, you need to import components from the XPCOM module to access the standard public members. The syntax is the same as that for importing any regular Python library: from xpcom import components The IDL for a Python implementation of an XPCOM component can be identical to one for a JavaScript- or C++-based component (which is the point of XPCOM, after all). As in any component, your IDL needs to include nsISupports.idl and declare itself as scriptable with a unique UUID: [scriptable, uuid(6D9F47DE-ADC1-4a8e-8E7D- 2F7B037239BF)] JavaScript accesses the component in the same way, using classes and interface members of the component's interfaces to set up an instance of the component:
  5. Components.classes["@foo.com/appSysUtils;1"] . getService(Components.interfaces.appISysUtil s); With these foundations, and assuming that you have to have a Python distribution on your system that Mozilla can access, you are ready to go! Example 8-18 shows a complete implementation of a PyXPCOM component. This file needs to be saved with a .py extension and put in the components directory and registered like any other component. Example 8-18. Sample Python component implementation import sys, os from xpcom import components, nsError, ServerException class appSysUtils: _com_interfaces_ = [components.interfaces.appISysUtils] _reg_clsid_ = "{56F686E0-A989-4714-A5D6- D77BC850C5C0}" _reg_contractid_ = "@foo.com/appSysUtils;1"
  6. _reg_desc_ = "System Utilities Service" def _ _init_ _(self): self.F_OK = os.F_OK self.R_OK = os.R_OK self.W_OK = os.W_OK self.X_OK = os.X_OK # ... def Access(self, filename, mode): return os.access(filename, mode) The special attributes defined in the appSysUtils class correspond to the special identifiers you must use in XPCOM to make your code a reusable component (see Section 8.1.5, earlier in this chapter). Table 8-3 describes these attributes. Table 8-3. Special XPCOM attributes in Python Attribute Description The interface IDs supported by this component. This attribute is required. It _com_interfaces_ can be a single IID or a list, but you do not have to list base interfaces such as
  7. Attribute Description nsISupports. _reg_contractid_ The component's contract ID. Required. The Class ID (CLSID) or progID of the _reg_clsid_ component in the form: @domain/component;version.Required. A description of the component. _reg_desc_ Optional. Example 8-19 is the IDL file you also need to create a Python component. Example 8-19. IDL for the Python component #include "nsISupports.idl" // some useful system utilities [scriptable, uuid(6D9F47DE-ADC1-4a8e-8E7D- 2F7B037239BF)] interface appSysUtils : nsISupports { boolean IsFile(in string filename); boolean IsDir(in string dirname); void Stat(in string filename,
  8. out PRUint32 st_mode, out PRUint32 st_ino, out PRUint32 st_dev, out PRUint32 st_nlink, out PRUint32 st_uid, out PRUint32 st_gid, out PRUint32 st_size, out PRUint32 st_atime, out PRUint32 st_mtime, out PRUint32 st_ctime); boolean Access(in string filename, in PRUint32 mode); readonly attribute PRUint32 F_OK; readonly attribute PRUint32 R_OK; readonly attribute PRUint32 W_OK; readonly attribute PRUint32 X_OK; }; Finally, Example 8-20 shows how this component might be used in script -- for example, in a function you define for an event handler in the XUL interface.
  9. Example 8-20. Using the Python component in script var appSysUtils = Components.classes["@foo.com/appSysUtils;1"] .getService(Components interfaces.appISysUtils); // Read-write status var write = appSysUtils.Access(url, appSysUtils.W_OK); var read = appSysUtils.Access(url, appSysUtils.R_OK); var rwcheck = document.getElementById('rwCheckbox'); if (read) { if (write && read) ro = false; else ro = true; rwcheck.setAttribute('checked', ro); } The component is a small system utility that checks the read/write permissions status of a file on the local filesystem. The JavaScript
  10. uses it to display a visual notifier of the status in the UI. In this case, the DOM's rwcheck node refers to a checkbox. It's easy to imagine this component being extended to do other things, such as getting information about a file (the Stat stub is in the IDL). The source code, samples, and documentation for PyXPCOM are located in the Mozilla tree at mozilla/extensions/python. 8.2.8. XPCOM as an Open Cross-Platform Solution XPCOM can be an entire book in itself. This chapter has merely touched upon the role it plays in Mozilla application development. Understanding the basics of this framework is vital to understanding the very foundation of Mozilla's componentized architecture. Although other component-based systems exist on various platforms -- MSCOM for Microsoft or a CORBA system for GNOME, for example -- if you want to write truly cross-platform component-based applications, then XPCOM is the best tool for the job. It can be deployed on any platform Mozilla is ported to, and can be scripted by using JavaScript or Python. Above all, XPCOM is entirely open source, so there are no costs associated with it, no proprietary secrets in how it's put together, and you have various software licenses to choose from. Although XPCOM has become a solid framework, its developers are still
  11. making improvements and uncovering and fixing bugs. However, XPCOM offers tremendous flexibility as a software development framework and the Mozilla community is an excellent technical support resource for all technologies covered in this book.
Đồng bộ tài khoản