XPCOMAs a component technology implemented by Microsoft, the component development technology COM in Linux plays an important role in the Windows platform and can be seen in module reuse and cross-language communication. But today I want to introduce the com implementation in Linux ---- XPCOM, which is the basic technology used in the Mozilla Browser project. We can use C ++ to create the XPCOM component, in the C ++ client program or Mozilla browser, components are called through JavaScript scripts to reuse software modules. 1. Configure the development environment of XPCOM. First download the gecko-SDK package from Mozilla FTP. This is the development package of XPCOM. The source code of Mozilla also includes this SDK. Decompress the tgz package and you can see that more than 10 directories are generated: /SDK/gecko-SDK // SDK/gecko-SDK/XPCOM/bin/SDK/gecko-SDK/XPCOM/IDL/SDK/gecko-SDK/XPCOM/include/SDK/ gecko-SDK/NSPR ...... here are some of the basic parts. /SDK/gecko-SDK/XPCOM/bin mainly contains some files: xpidl: This is the IDL compiler used to generate C ++ header files or component-type library files based on IDL. regxpcom: this is a component registration job. If we call components in the Mozilla browser, we will not actually use this tool. Xpt-dump: The Type Library viewing program, used to view component information in the. xpt file. Libxpcomglue. A: This is the basic library file of XPCOM. It will be connected to our component library when generating components. /SDK/gecko-SDK/XPCOM/IDL, which contains the IDL data type definition file. /SDK/gecko-SDK/XPCOM/include, which contains the basic C ++ header file required to create XPCOM. /SDK/gecko-SDK also contains other directories, such as/SDK/gecko-SDK/string/include, it contains the C ++ header file of the string class in XPCOM. If we need to use these classes in our components, we only need to include the necessary header files and library files. 2. Write the IDL file. Here we first use a uuidgen (a command line program similar to Ms guidgen in Linux) to generate the uuid of the component. We will redirect the output to a text file and use it later, here is a simple example to demonstrate the component generation process. The IDL file is as follows: // filename: nsimycom. IDL // begin IDL ---------------------------------------- # include "nsisupports. IDL "[Scriptable, UUID (UID)] interface nsimycom: nsisupports {void Hello (in string in_str, [retval] out string out_str) ;}; // end IDL finished, this component is very simple. There is only one interface and only one method. This method has a string input parameter in_str and a string return value out_str. 3. Compile the IDL file and complete the C ++ implementation corresponding to the component. /SDK/gecko-SDK/XPCOM/bin/xpidl-I/SDK/gecko-SDK/XPCOM/bin/IDL-M header nsimycom. if there is no error in IDL, an nsimycom. h file, which is the c ++ header file generated by the IDL compiler corresponding to the above IDL file. Below is the nsimycom generated by the compiler. hfile content: // example # ifndef _ gen_nsimycom_h __# DEFINE _ gen_nsimycom_h _ # ifndef _ gen_nsisupports_h __# include "nsisupports. H "# endif/* For IDL files that don't want to include root IDL files. */# ifndef ns_no_vtable # define ns_no_vtable # endif/* Starting interface: nsimycom */# define ns_imycom_iid_str "5217115e-22fe-4d01-966d-9b27ffda6 498 "# define ns_imycom_iid/{0x5217115e, 0x22fe, 0x4d01,/{0x96, 0x6d, 0x9b, 0x27, 0xff, 0xda, 0x64, 0x98} class ns_no_vtable nsimycom: Public nsisupports {public: sums (ns_imycom_iid)/* void Hello (in string in_str, [retval] out string out_str ); */ns_imethod Hello (const char * in_str, char ** out_str) = 0 ;};/* use this macro when declaring classes that implement t His interface. */# define ns_decl_nsimycom/ns_imethod Hello (const char * in_str, char ** out_str);/* use this macro to declare functions that forward the behavior of this interface to another object. */# define ns_forward_nsimycom (_ to)/ns_imethod Hello (const char * in_str, char ** out_str) {return _ to hello (in_str, out_str );} /* use this macro to declare functions that forward the behavior of thi S interface to another object in a safe way. */# define ns_forward_safe_nsimycom (_ to)/ns_imethod Hello (const char * in_str, char ** out_str) {return! _? Ns_error_null_pointer: _ to-> Hello (in_str, out_str);} # If 0/* use the code below as a template for the Implementation class for this interface. * // * header file */class nsmycom: Public nsimycom {public: ns_decl_isupports ns_decl_nsimycom nsmycom (); Virtual ~ Nsmycom ();/* additional members */};/* implementation file */ns_impl_isupports1 (nsmycom, nsimycom) nsmycom: nsmycom () {/* member initializers and constructor Code */} nsmycom ::~ Nsmycom () {/* destructor Code */}/* void Hello (in string in_str, [retval] out string out_str); */ns_imethodimp nsmycom :: hello (const char * in_str, char ** out_str) {return ns_error_not_implemented;}/* end of implementation class template. */# endif/* _ gen_nsimycom_h _ * // ------------------------------------------------------- the header file of the interface is generated by IDL, the C ++ class template for this header file is also included. the next step is just as easy. Copy the code from # If 0 to # endif to the new nsmycom. H and nsmycom. in the CPP file, note that there is new code. The following two files are generated. // filename: nsmycom. h # include "nsimycom. H "# define ns_mycom_cid/{0x5217115e, 0x22fe, 0x4d01, {0x96, 0x6d, 0x9b, 0x27, 0xff, 0xda, 0x64, 0x98 }}// similar to CLSID # define ns_mycom_contractid in Windows "@ westsoft.org/mycom1_1" // similar to progid in windows; Class nsmycom: Public nsimycom {public: extends nsmycom (); Virtual ~ Nsmycom ();/* additional members */}; // filename: nsmycom. CPP # include "nsmycom. H "# include" nsmemory. H "# include <cstdio> # include <cstdlib> # include <string> ns_impl_isupports1_ci (nsmycom, nsimycom) // The macro here has been modified. nsmycom: nsmycom () {} nsmycom ::~ Nsmycom () {}/* void Hello (in string in_str, [retval] out string out_str); */ns_imethodimp nsmycom: Hello (const char * in_str, char ** out_str) {printf ("/n ---------------/N"); printf ("% s/n", in_str); STD: String str_tmp = "your input is :"; str_tmp + = in_str; * out_str = (char *) malloc (str_tmp.length () + 1); * out_str = (char *) str_tmp.c_str (); Return ns_ OK ;} 4. Complete the factory method and registration module of the component. The implementation of the component itself is the same as the above two classes. however, we can only generate a dynamic library of the above class as a component, and we still need to do one thing. implement component registration and creation functions. this is almost a fixed pattern. the code below is similar to the implementation in ms. Many macros are used: # include "nsigenericfactory. H "# include" nsmycom. H "Merge (nsmycom) Static ns_method Merge (nsicomponentmanager * acompmgr, nsifile * apath, const char * registrylocation, const char * componenttype, const nsmodulecomponentinfo * info) {return ns_ OK;} sta TIC ns_method Merge (nsicomponentmanager * acompmgr, nsifile * apath, const char * registrylocation, const nsmodulecomponentinfo * info) {return ns_ OK;} ns_decl_classinfo (nsmycom) static const nsmodulecomponentinfo components [] = {"nsmycom component", ns_mycom_cid, role, nsmycomconstructor, role/* null if you dont need one */, nsmycomunregistration Proc/* null if you dont need one */, null/* No factory destructor */, ns_ci_interface_getter_name (nsmycom), null/* no language helper */, & ns_classinfo_name (nsmycom) }}; ns_impl_nsgetmodule (nsmycommodule, components) 5. Make makefile and generate the component. After installing the component, You can compile the MAKEFILE file to compile the component we just compiled. # filename: makefile # begine ------------------------------------- CPP = g ++ cppflags + =-fno-rtti-fno-exceptions-shared gecko_s Dk_path =/SDK/gecko-sdkxpidl = $ (gecko_sdk_path) /XPCOM/bin/xpidlcppheader =-M header typelib =-M typelibregdir =/usr/local/lib/mozilla-1.6OUTDIR = $ (regdir) /items =-include mozilla-config.hGECKO_DEFINES =-dxpcom_glue gecko_shortdes =-I $ (gecko_sdk_path)/XPCOM/include/-I $ (gecko_sdk_path) /NSPR/include gecko_ldflags =-L $ (gecko_sdk_path)/XPCOM/bin- Lxpcomglue/-L $(gecko_sdk_path)/NSPR/bin-lnspr4 gecko_idl =-I $ (gecko_sdk_path)/XPCOM/IDL build: IDL nsmycom. O nsmycommodule. o $ (CPP) $ (cppflags)-O libxpmycom. so $ (gecko_defines)/$ (gecko_ldflags) nsmycom. O nsmycommodule. O chmod + x libxpmycom. so IDL: nsimycom. IDL $ (xpidl) $ (gecko_idl) $ (cppheader) nsimycom. IDL $ (xpidl) $ (gecko_idl) $ (typelib) nsimycom. IDL nsmycom. o: nsmycom. CPP $ (CPP) $ (gecko_c Onfig_include) $ (gecko_defines)/$ (gecko_shortdes)-C nsmycom. CPP-O nsmycom. O nsmycommodule. o: nsmycommodule. CPP $ (CPP) $ (gecko_config_include) $ (gecko_defines)/$ (gecko_includes)-C nsmycommodule. CPP-O nsmycommodule. O install: CP nsimycom. xpt $ (outdir)/CP libxpmycom. so $ (outdir)/clean: RM *. o rM *. so RM *. *~ RM *~ # End ------------- if everything is correct, after making, G ++ will generate the libxpmycom. So library file in the current directory. Then install the component to the Mozilla component directory: make install the Component Library and the corresponding Type Library nsimycom. xpt will be copied to/usr/local/lib/mozilla-1.6/components (to confirm your component directory, such as the mozilla1.4 directory is generally usr/local/lib/mozilla-1.4/components) directory. In this case, we can start the Mozilla Browser from the console. In a series of information output by the browser, the component will be registered successfully. 6. Test the component in HTML/JavaScript. The HTML is as follows: // -------------------------------- <HTML>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.