NS_IMETHODIMP DirectoryServiceProvider::GetFile(const char *aProp, PRBool *aPersistent, nsIFile **aRetval) { nsCOMPtr <nsILocalFile> localFile; nsresult rv = NS_ERROR_FAILURE; *aRetval = nsnull; *aPersistent = PR_TRUE; const char *fileLocation = NULL; if (strcmp(aProp, NS_XPCOM_COMPONENT_REGISTRY_FILE) == 0) fileLocation = mCompRegLocation; else if (strcmp(aProp, NS_XPCOM_XPTI_REGISTRY_FILE) == 0) fileLocation = mXPTIDatLocation; else if (mComponentDirLocation && strcmp(aProp, NS_XPCOM_COMPONENT_DIR) == 0) fileLocation = mComponentDirLocation; else if (mCurrProcDirLocation && strcmp(aProp, NS_XPCOM_CURRENT_PROCESS_DIR) == 0) fileLocation = mCurrProcDirLocation; else return NS_ERROR_FAILURE; rv = NS_NewNativeLocalFile(nsEmbedCString(fileLocation), PR_TRUE, getter_AddRefs(localFile)); if (NS_FAILED(rv)) return rv; return localFile->QueryInterface(NS_GET_IID (nsIFile), (void **)aRetval); }
static nsresult gecko_utils_init_prefs(void) { nsresult rv; nsCOMPtr<nsIPrefService> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv)); NS_ENSURE_SUCCESS (rv, rv); nsCOMPtr<nsILocalFile> file; rv = NS_NewNativeLocalFile(nsEmbedCString(get_resource_path("default-prefs.js")), PR_TRUE, getter_AddRefs(file)); NS_ENSURE_SUCCESS (rv, rv); rv = prefService->ReadUserPrefs(file); rv |= prefService->ReadUserPrefs(nsnull); NS_ENSURE_SUCCESS (rv, rv); return rv; }
int main(int argc, char** argv) { GtkWidget *window=NULL; GtkWidget *vbox=NULL; nsCOMPtr<nsIServiceManager> servMan; nsCOMPtr<nsILocalFile> libxul; nsIWebNavigation *webNavigation; int cid=0; std::string lstStr(""); int mozSet=0; std::string xulPath(XUL_PATH); std::string xulrunPath(XUL_BIN); std::string soapServer; int maxDoc=20; if(argc<2) { usage(); return 0; } bool isCraw=true; int opt; while((opt = getopt(argc, argv, "n:i:c:l:r:t:s:b:S:CG")) != -1) { switch(opt) { case 'C': isCraw=true; break; case 'G': isCraw=false; break; case 'c': cid=atoi(optarg); break; case 'n': maxDoc=atoi(optarg); break; case 'l': lstStr=std::string(optarg); break; case 'r': mozSet=atoi(optarg); break; case 's': xulPath=std::string(optarg); break; case 'S': soapServer=std::string(optarg); break; case 'b': xulrunPath=std::string(optarg); break; default: usage(); } } if(lstStr.length()==0||lstStr.length()==0) { usage(); } nsDynamicFunctionLoad nsFuncs[] = { {"XRE_InitEmbedding", (NSFuncPtr*)&XRE_InitEmbedding}, {"XRE_TermEmbedding", (NSFuncPtr*) &XRE_TermEmbedding}, {0, 0} }; nsresult rv; //create native window gtk_init (&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); GTKEmbedGlueStartup(); GTKEmbedGlueStartupInternal(); gtk_widget_set_usize(window, 1280,1024); vbox = gtk_vbox_new(TRUE, 0); gtk_container_add(GTK_CONTAINER(window), vbox); gtk_widget_show(window); gtk_widget_show(vbox); //init standalone env rv = XPCOMGlueStartup(xulPath.c_str()); if (NS_FAILED(rv)) { printf("XPCOMGlueStartup\n"); } rv = XPCOMGlueLoadXULFunctions(nsFuncs); if (NS_FAILED(rv)) { printf("XPCOMGlueLoadXULFunctions\n"); } rv = NS_NewNativeLocalFile(nsEmbedCString(xulrunPath.c_str()), PR_FALSE, getter_AddRefs(libxul)); if (NS_FAILED(rv)) { printf("NS_NewNativeLocalFile\n"); } rv = XRE_InitEmbedding(libxul, 0, 0, 0, 0); if (NS_FAILED(rv)) { printf("XRE_InitEmbedding\n"); } rv = NS_GetServiceManager(getter_AddRefs(servMan)); if (NS_FAILED(rv)) { std::cout<<"Get manager Error:"<<rv<<std::endl; } nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); rv=registrar->AutoRegister(nsnull); if (NS_FAILED(rv)) { std::cout<<"Get manager Error:"<<rv<<std::endl; } mozilla_prefs_set_string (servMan,"general.useragent.override", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.8) Gecko/2009032600 SUSE/3.0.8-1.1 Firefox/3.0.8"); nsCOMPtr<nsIStatManager> sm=do_CreateInstance(lstStr.c_str(),&rv); if (NS_FAILED(rv)) { std::cout<<"Get stat manager Error:"<<std::hex<<rv<<std::endl; return 0; } nsCOMPtr<nsISoapInter> inter=do_CreateInstance("@nyapc.com/XPCOM/nsSoapInter;1",&rv); if (NS_FAILED(rv)) { std::cout<<"Init Soap Inter error\n"; return 0; } if(soapServer.length()>0) inter->SetAddress(nsCString(soapServer.c_str())); nsCOMPtr<nsISoapTask> task; if(isCraw) inter->GetTask(cid,maxDoc,getter_AddRefs(task)); else inter->GetWrapTask(cid,maxDoc,getter_AddRefs(task)); sm->SetInter(inter); std::list<gtkBrowser> bList; if(task!=nsnull) { sm->SetTask(task); PRInt32 len; task->GetUrlCount(&len); for(PRInt32 index=0;index<len;index++) { int Uid; nsCString aurl; task->GetUrlByIndex(index,&Uid,aurl); gtkBrowser gb(vbox,Uid); gb.SetListener(sm); gb.SetRunlevel(mozSet); gb.SetUrl(aurl); bList.push_back(gb); } for(std::list<gtkBrowser>::iterator it=bList.begin();it!=bList.end();++it) it->Start(); }else { sleep(60); exit(0); } gtk_widget_hide(vbox); gtk_widget_hide(window); /**********set listener***********/ std::cout<<"Crawler ID:"<<cid<<std::endl; gtk_main(); XRE_TermEmbedding(); XPCOMGlueShutdown(); return 0; }
/** * Initializes the COM runtime. * * This method must be called on each thread of the client application that * wants to access COM facilities. The initialization must be performed before * calling any other COM method or attempting to instantiate COM objects. * * On platforms using XPCOM, this method uses the following scheme to search for * XPCOM runtime: * * 1. If the VBOX_APP_HOME environment variable is set, the path it specifies * is used to search XPCOM libraries and components. If this method fails to * initialize XPCOM runtime using this path, it will immediately return a * failure and will NOT check for other paths as described below. * * 2. If VBOX_APP_HOME is not set, this methods tries the following paths in the * given order: * * a) Compiled-in application data directory (as returned by * RTPathAppPrivateArch()) * b) "/usr/lib/virtualbox" (Linux only) * c) "/opt/VirtualBox" (Linux only) * * The first path for which the initialization succeeds will be used. * * On MS COM platforms, the COM runtime is provided by the system and does not * need to be searched for. * * Once the COM subsystem is no longer necessary on a given thread, Shutdown() * must be called to free resources allocated for it. Note that a thread may * call Initialize() several times but for each of tese calls there must be a * corresponding Shutdown() call. * * @return S_OK on success and a COM result code in case of failure. */ HRESULT Initialize(bool fGui) { HRESULT rc = E_FAIL; #if !defined(VBOX_WITH_XPCOM) /* * We initialize COM in GUI thread in STA, to be compliant with QT and * OLE requirments (for example to allow D&D), while other threads * initialized in regular MTA. To allow fast proxyless access from * GUI thread to COM objects, we explicitly provide our COM objects * with free threaded marshaller. * !!!!! Please think twice before touching this code !!!!! */ DWORD flags = fGui ? COINIT_APARTMENTTHREADED | COINIT_SPEED_OVER_MEMORY : COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE | COINIT_SPEED_OVER_MEMORY; rc = CoInitializeEx(NULL, flags); /* the overall result must be either S_OK or S_FALSE (S_FALSE means * "already initialized using the same apartment model") */ AssertMsg(rc == S_OK || rc == S_FALSE, ("rc=%08X\n", rc)); /* To be flow compatible with the XPCOM case, we return here if this isn't * the main thread or if it isn't its first initialization call. * Note! CoInitializeEx and CoUninitialize does it's own reference * counting, so this exercise is entirely for the EventQueue init. */ bool fRc; RTTHREAD hSelf = RTThreadSelf(); if (hSelf != NIL_RTTHREAD) ASMAtomicCmpXchgHandle(&gCOMMainThread, hSelf, NIL_RTTHREAD, fRc); else fRc = false; if (fGui) Assert(RTThreadIsMain(hSelf)); if (!fRc) { if ( gCOMMainThread == hSelf && SUCCEEDED(rc)) gCOMMainInitCount++; AssertComRC(rc); return rc; } Assert(RTThreadIsMain(hSelf)); /* this is the first main thread initialization */ Assert(gCOMMainInitCount == 0); if (SUCCEEDED(rc)) gCOMMainInitCount = 1; #else /* !defined (VBOX_WITH_XPCOM) */ /* Unused here */ NOREF(fGui); if (ASMAtomicXchgBool(&gIsXPCOMInitialized, true) == true) { /* XPCOM is already initialized on the main thread, no special * initialization is necessary on additional threads. Just increase * the init counter if it's a main thread again (to correctly support * nested calls to Initialize()/Shutdown() for compatibility with * Win32). */ nsCOMPtr<nsIEventQueue> eventQ; rc = NS_GetMainEventQ(getter_AddRefs(eventQ)); if (NS_SUCCEEDED(rc)) { PRBool isOnMainThread = PR_FALSE; rc = eventQ->IsOnCurrentThread(&isOnMainThread); if (NS_SUCCEEDED(rc) && isOnMainThread) ++gXPCOMInitCount; } AssertComRC(rc); return rc; } Assert(RTThreadIsMain(RTThreadSelf())); /* this is the first initialization */ gXPCOMInitCount = 1; bool const fInitEventQueues = true; /* prepare paths for registry files */ char szCompReg[RTPATH_MAX]; char szXptiDat[RTPATH_MAX]; int vrc = GetVBoxUserHomeDirectory(szCompReg, sizeof(szCompReg)); AssertRCReturn(vrc, NS_ERROR_FAILURE); strcpy(szXptiDat, szCompReg); vrc = RTPathAppend(szCompReg, sizeof(szCompReg), "compreg.dat"); AssertRCReturn(vrc, NS_ERROR_FAILURE); vrc = RTPathAppend(szXptiDat, sizeof(szXptiDat), "xpti.dat"); AssertRCReturn(vrc, NS_ERROR_FAILURE); LogFlowFunc(("component registry : \"%s\"\n", szCompReg)); LogFlowFunc(("XPTI data file : \"%s\"\n", szXptiDat)); #if defined (XPCOM_GLUE) XPCOMGlueStartup(nsnull); #endif static const char *kAppPathsToProbe[] = { NULL, /* 0: will use VBOX_APP_HOME */ NULL, /* 1: will try RTPathAppPrivateArch() */ #ifdef RT_OS_LINUX "/usr/lib/virtualbox", "/opt/VirtualBox", #elif RT_OS_SOLARIS "/opt/VirtualBox/amd64", "/opt/VirtualBox/i386", #elif RT_OS_DARWIN "/Application/VirtualBox.app/Contents/MacOS", #endif }; /* Find out the directory where VirtualBox binaries are located */ for (size_t i = 0; i < RT_ELEMENTS(kAppPathsToProbe); ++ i) { char szAppHomeDir[RTPATH_MAX]; if (i == 0) { /* Use VBOX_APP_HOME if present */ vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_APP_HOME", szAppHomeDir, sizeof(szAppHomeDir), NULL); if (vrc == VERR_ENV_VAR_NOT_FOUND) continue; AssertRC(vrc); } else if (i == 1) { /* Use RTPathAppPrivateArch() first */ vrc = RTPathAppPrivateArch(szAppHomeDir, sizeof(szAppHomeDir)); AssertRC(vrc); } else { /* Iterate over all other paths */ szAppHomeDir[RTPATH_MAX - 1] = '\0'; strncpy(szAppHomeDir, kAppPathsToProbe[i], RTPATH_MAX - 1); vrc = VINF_SUCCESS; } if (RT_FAILURE(vrc)) { rc = NS_ERROR_FAILURE; continue; } char szCompDir[RTPATH_MAX]; vrc = RTPathAppend(strcpy(szCompDir, szAppHomeDir), sizeof(szCompDir), "components"); if (RT_FAILURE(vrc)) { rc = NS_ERROR_FAILURE; continue; } LogFlowFunc(("component directory : \"%s\"\n", szCompDir)); nsCOMPtr<DirectoryServiceProvider> dsProv; dsProv = new DirectoryServiceProvider(); if (dsProv) rc = dsProv->init(szCompReg, szXptiDat, szCompDir, szAppHomeDir); else rc = NS_ERROR_OUT_OF_MEMORY; if (NS_FAILED(rc)) break; /* Setup the application path for NS_InitXPCOM2. Note that we properly * answer the NS_XPCOM_CURRENT_PROCESS_DIR query in our directory * service provider but it seems to be activated after the directory * service is used for the first time (see the source NS_InitXPCOM2). So * use the same value here to be on the safe side. */ nsCOMPtr <nsIFile> appDir; { char *appDirCP = NULL; vrc = RTStrUtf8ToCurrentCP(&appDirCP, szAppHomeDir); if (RT_SUCCESS(vrc)) { nsCOMPtr<nsILocalFile> file; rc = NS_NewNativeLocalFile(nsEmbedCString(appDirCP), PR_FALSE, getter_AddRefs(file)); if (NS_SUCCEEDED(rc)) appDir = do_QueryInterface(file, &rc); RTStrFree(appDirCP); } else rc = NS_ERROR_FAILURE; } if (NS_FAILED(rc)) break; /* Set VBOX_XPCOM_HOME to the same app path to make XPCOM sources that * still use it instead of the directory service happy */ vrc = RTEnvSetEx(RTENV_DEFAULT, "VBOX_XPCOM_HOME", szAppHomeDir); AssertRC(vrc); /* Finally, initialize XPCOM */ { nsCOMPtr<nsIServiceManager> serviceManager; rc = NS_InitXPCOM2(getter_AddRefs(serviceManager), appDir, dsProv); if (NS_SUCCEEDED(rc)) { nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(serviceManager, &rc); if (NS_SUCCEEDED(rc)) { rc = registrar->AutoRegister(nsnull); if (NS_SUCCEEDED(rc)) { /* We succeeded, stop probing paths */ LogFlowFunc(("Succeeded.\n")); break; } } } } /* clean up before the new try */ rc = NS_ShutdownXPCOM(nsnull); if (i == 0) { /* We failed with VBOX_APP_HOME, don't probe other paths */ break; } } #endif /* !defined (VBOX_WITH_XPCOM) */ // for both COM and XPCOM, we only get here if this is the main thread; // only then initialize the autolock system (AutoLock.cpp) Assert(RTThreadIsMain(RTThreadSelf())); util::InitAutoLockSystem(); AssertComRC(rc); /* * Init the main event queue (ASSUMES it cannot fail). */ if (SUCCEEDED(rc)) EventQueue::init(); return rc; }