![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Creating Applications with Mozilla-Chapter 8. XPCOM- P6
Số trang: 11
Loại file: pdf
Dung lượng: 26.31 KB
Lượt xem: 1
Lượt tải: 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- 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ả
Nội dung trích xuất từ tài liệu:
Creating Applications with Mozilla-Chapter 8. XPCOM- P6 Chapter 8. XPCOM- P68.2.6.2. Creating an instance of an existing Mozilla componentCreating an instance of a component and accessing methods andattributes is different in C++ than it is in JavaScript. Using thensILocalFile interface lets you walk through the code to create aninstance of this component from C++:nsCOMPtrfile(do_CreateInstance(@mozilla.org/file/local;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 allocatedinstance of an nsLocalFile object.Example 8-17 accesses the public methods available from thiscomponent by using the pointer identifier file.Example 8-17. Example 8-17: Testing for nsresults fromcomponent methodsif (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; if (!leafName.IsEmpty( )) printf(leaf name is %s\n,NS_ConvertUCS2toUTF8(leafName).get( ));}Always test accessors of all XPCOM public methods, getters, andsetters. Failures can appear at any time, so be sure to use resultchecking in your implementations.8.2.7. Other Languages for XPCOMAlthough most components available from XPCOM are written inC++, the XPConnect/XPCOM pairing can also accommodate otherlanguages. Language independence is a goal of the XPCOMarchitecture. Currently, implementations for Python (PyXPCOM)and Ruby (rbXPCOM) exist, with other language bindings beingdeveloped. In this respect, the Mozilla framework dovetails withone of the main trends in application development, which is to mixdifferent languages in the development environment.8.2.7.1. PyXPCOM: the Python binding for XPCOMPython has emerged as a very popular programming language inthe last couple of years. It even does some of the application workand other heavy lifting that were the province of C++. Mozilla nowoffers a Python binding similar to the XPConnect binding forJavaScript that allows you to write application code in Python,compile it in XPCOM, and make it available like you would anyC++ component in the Mozilla application framework. As withother XPCOM programming languages, you must create animplementation file (in Python) and an interface file (in IDL), asshown in Examples 8-18 and 8-19, respectively.The terms and constructs for Python components are similar tothose of C++. In the implementation, you need to importcomponents from the XPCOM module to access the standardpublic members. The syntax is the same as that for importing anyregular Python library:from xpcom import componentsThe IDL for a Python implementation of an XPCOM componentcan 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 asscriptable with a unique UUID:[scriptable, uuid(6D9F47DE-ADC1-4a8e-8E7D-2F7B037239BF)]JavaScript accesses the component in the same way, using classesand interface members of the components interfaces to set up aninstance of the component:Components.classes[@foo.com/appSysUtils;1].getService(Components.interfaces.appISysUtils);With these foundations, and assuming that you have to have aPython distribution on your system that Mozilla can access, youare ready to go! Example 8-18 shows a complete implementationof a PyXPCOM component. This file needs to be saved with a .pyextension and put in the components directory and registered likeany other component.Example 8-18. Sample Python component implementationimport sys, osfrom xpcom import components, nsError,ServerExceptionclass appSysUtils: _com_interfaces_ =[components.interfaces.appISysUtils] _reg_clsid_ = {56F686E0-A989-4714-A5D6-D77BC850C5C0} _reg_contractid_ =@foo.com/appSysUtils;1 _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 classcorrespond to the special identifiers you must use in XPCOM tomake your code a reusable component (see Section 8.1.5, earlier inthis chapter). Table 8-3 describes these attributes.Table 8-3. Special XPCOM attributes in PythonAttribute 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 asAttribute Description nsISupports._reg_contractid_ The components 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 Pythoncomponent.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 str ...
Nội dung trích xuất từ tài liệu:
Creating Applications with Mozilla-Chapter 8. XPCOM- P6 Chapter 8. XPCOM- P68.2.6.2. Creating an instance of an existing Mozilla componentCreating an instance of a component and accessing methods andattributes is different in C++ than it is in JavaScript. Using thensILocalFile interface lets you walk through the code to create aninstance of this component from C++:nsCOMPtrfile(do_CreateInstance(@mozilla.org/file/local;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 allocatedinstance of an nsLocalFile object.Example 8-17 accesses the public methods available from thiscomponent by using the pointer identifier file.Example 8-17. Example 8-17: Testing for nsresults fromcomponent methodsif (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; if (!leafName.IsEmpty( )) printf(leaf name is %s\n,NS_ConvertUCS2toUTF8(leafName).get( ));}Always test accessors of all XPCOM public methods, getters, andsetters. Failures can appear at any time, so be sure to use resultchecking in your implementations.8.2.7. Other Languages for XPCOMAlthough most components available from XPCOM are written inC++, the XPConnect/XPCOM pairing can also accommodate otherlanguages. Language independence is a goal of the XPCOMarchitecture. Currently, implementations for Python (PyXPCOM)and Ruby (rbXPCOM) exist, with other language bindings beingdeveloped. In this respect, the Mozilla framework dovetails withone of the main trends in application development, which is to mixdifferent languages in the development environment.8.2.7.1. PyXPCOM: the Python binding for XPCOMPython has emerged as a very popular programming language inthe last couple of years. It even does some of the application workand other heavy lifting that were the province of C++. Mozilla nowoffers a Python binding similar to the XPConnect binding forJavaScript that allows you to write application code in Python,compile it in XPCOM, and make it available like you would anyC++ component in the Mozilla application framework. As withother XPCOM programming languages, you must create animplementation file (in Python) and an interface file (in IDL), asshown in Examples 8-18 and 8-19, respectively.The terms and constructs for Python components are similar tothose of C++. In the implementation, you need to importcomponents from the XPCOM module to access the standardpublic members. The syntax is the same as that for importing anyregular Python library:from xpcom import componentsThe IDL for a Python implementation of an XPCOM componentcan 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 asscriptable with a unique UUID:[scriptable, uuid(6D9F47DE-ADC1-4a8e-8E7D-2F7B037239BF)]JavaScript accesses the component in the same way, using classesand interface members of the components interfaces to set up aninstance of the component:Components.classes[@foo.com/appSysUtils;1].getService(Components.interfaces.appISysUtils);With these foundations, and assuming that you have to have aPython distribution on your system that Mozilla can access, youare ready to go! Example 8-18 shows a complete implementationof a PyXPCOM component. This file needs to be saved with a .pyextension and put in the components directory and registered likeany other component.Example 8-18. Sample Python component implementationimport sys, osfrom xpcom import components, nsError,ServerExceptionclass appSysUtils: _com_interfaces_ =[components.interfaces.appISysUtils] _reg_clsid_ = {56F686E0-A989-4714-A5D6-D77BC850C5C0} _reg_contractid_ =@foo.com/appSysUtils;1 _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 classcorrespond to the special identifiers you must use in XPCOM tomake your code a reusable component (see Section 8.1.5, earlier inthis chapter). Table 8-3 describes these attributes.Table 8-3. Special XPCOM attributes in PythonAttribute 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 asAttribute Description nsISupports._reg_contractid_ The components 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 Pythoncomponent.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 str ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 436 1 0
-
24 trang 363 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 324 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 316 0 0 -
74 trang 305 0 0
-
96 trang 301 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 294 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 288 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 271 0 0