HMODULE ChildProcessManager::GetChildProcessMainModule() const { HMODULE ret(NULL); if (IsChildProcessCreated()) { HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, m_piProcInfo->dwProcessId ); if (hProcess != INVALID_HANDLE_VALUE) { char lpBaseName[MAX_PATH]; CProcessModuleIterator pmi(m_piProcInfo->dwProcessId); for (HMODULE hModule = pmi.First(); hModule; hModule = pmi.Next()) { if (GetModuleBaseName(hProcess, hModule, lpBaseName, MAX_PATH)) { std::string moduleName(lpBaseName); if (moduleName.find(".exe") != std::string::npos) { ret = hModule; break; } } } CloseHandle(hProcess); } } return ret; }
nsresult nsSeamonkeyProfileMigrator::CopyPasswords(PRBool aReplace) { nsresult rv; nsXPIDLCString signonsFileName; GetSignonFileName(aReplace, getter_Copies(signonsFileName)); if (signonsFileName.IsEmpty()) return NS_ERROR_FILE_NOT_FOUND; nsAutoString fileName; fileName.AssignWithConversion(signonsFileName); if (aReplace) rv = CopyFile(fileName, fileName); else { nsCOMPtr<nsIFile> seamonkeyPasswordsFile; mSourceProfile->Clone(getter_AddRefs(seamonkeyPasswordsFile)); seamonkeyPasswordsFile->Append(fileName); nsCOMPtr<nsIPasswordManagerInternal> pmi(do_GetService("@mozilla.org/passwordmanager;1")); rv = pmi->ReadPasswords(seamonkeyPasswordsFile); } return rv; }
std::string LLEventPumps::registerNew(const LLEventPump& pump, const std::string& name, bool tweak) { std::pair<PumpMap::iterator, bool> inserted = mPumpMap.insert(PumpMap::value_type(name, const_cast<LLEventPump*>(&pump))); // If the insert worked, then the name is unique; return that. if (inserted.second) return name; // Here the new entry was NOT inserted, and therefore name isn't unique. // Unless we're permitted to tweak it, that's Bad. if (! tweak) { throw LLEventPump::DupPumpName(std::string("Duplicate LLEventPump name '") + name + "'"); } // The passed name isn't unique, but we're permitted to tweak it. Find the // first decimal-integer suffix not already taken. The insert() attempt // above will have set inserted.first to the iterator of the existing // entry by that name. Starting there, walk forward until we reach an // entry that doesn't start with 'name'. For each entry consisting of name // + integer suffix, capture the integer suffix in a set. Use a set // because we're going to encounter string suffixes in the order: name1, // name10, name11, name2, ... Walking those possibilities in that order // isn't convenient to detect the first available "hole." std::set<int> suffixes; PumpMap::iterator pmi(inserted.first), pmend(mPumpMap.end()); // We already know inserted.first references the existing entry with // 'name' as the key; skip that one and start with the next. while (++pmi != pmend) { if (pmi->first.substr(0, name.length()) != name) { // Found the first entry beyond the entries starting with 'name': // stop looping. break; } // Here we're looking at an entry that starts with 'name'. Is the rest // of it an integer? // Dubious (?) assumption: in the local character set, decimal digits // are in increasing order such that '9' is the last of them. This // test deals with 'name' values such as 'a', where there might be a // very large number of entries starting with 'a' whose suffixes // aren't integers. A secondary assumption is that digit characters // precede most common name characters (true in ASCII, false in // EBCDIC). The test below is correct either way, but it's worth more // if the assumption holds. if (pmi->first[name.length()] > '9') break; // It should be cheaper to detect that we're not looking at a digit // character -- and therefore the suffix can't possibly be an integer // -- than to attempt the lexical_cast and catch the exception. if (! std::isdigit(pmi->first[name.length()])) continue; // Okay, the first character of the suffix is a digit, it's worth at // least attempting to convert to int. try { suffixes.insert(boost::lexical_cast<int>(pmi->first.substr(name.length()))); } catch (const boost::bad_lexical_cast&) { // If the rest of pmi->first isn't an int, just ignore it. } } // Here we've accumulated in 'suffixes' all existing int suffixes of the // entries starting with 'name'. Find the first unused one. int suffix = 1; for ( ; suffixes.find(suffix) != suffixes.end(); ++suffix) ; // Here 'suffix' is not in 'suffixes'. Construct a new name based on that // suffix, insert it and return it. std::ostringstream out; out << name << suffix; return registerNew(pump, out.str(), tweak); }