📄 common.cpp
字号:
nsMemory::Free(currProfileName); NS_ENSURE_SUCCESS(rv, rv); if (NS_FAILED(rv)) { nsMemory::Free(currProfileName); return rv; } rv = currProfileDir->AppendNative(nsEmbedCString("prefs.js")); NS_ENSURE_SUCCESS(rv, rv); PRBool exists; rv = currProfileDir->Exists(&exists); if (NS_SUCCEEDED(rv) && exists) { CopyPrefs(currProfileDir, privateProfilePrefs); } } // get prefs nsCOMPtr<nsIPrefService> pref; rv = GetService("@mozilla.org/preferences-service;1", NS_GET_IID(nsIPrefService), getter_AddRefs(pref)); NS_ENSURE_SUCCESS(rv, rv); // regenerate the nsIFile object here, because ReadUserPrefs needs to get the file size rv = privateProfileDir->Clone(getter_AddRefs(privateProfilePrefs)); NS_ENSURE_SUCCESS(rv, rv); rv = privateProfilePrefs->AppendNative(copiedPrefs); NS_ENSURE_SUCCESS(rv, rv); // activate our copied prefs.js pref->ReadUserPrefs(privateProfilePrefs); return NS_OK;}void ReportError(const char* msg){ SendSocketMessage(-1, CEVENT_INIT_FAILED, msg);}// copy ASCII characters from |str| into |result|. use a temporary buffer.PRBoolConvertAsciiToUtf16(const char *str, nsEmbedString &result){ int len = strlen(str); PRUnichar *buf = (PRUnichar *) malloc(len * sizeof(PRUnichar)); if (!buf) return PR_FALSE; for (int i=0; i<len; ++i) buf[i] = PRUnichar(str[i]); result.Assign(buf, len); free(buf); return PR_TRUE;}// helper function for converting UTF-8 chars to UTF-16 chars.PRBoolConvertUtf8ToUtf16(const nsEmbedCString &aSource, nsEmbedString &aDest){#ifdef USING_GECKO_SDK_1_7 // Note: Below function was frozen for Mozilla 1.7, but not available for 1.4. // See bug 239123 for details: // http://bugzilla.mozilla.org/show_bug.cgi?id=239123 NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_UTF8, aDest); #else nsEmbedCString::const_iterator source_start, source_end; CalculateUTF8Length calculator; copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), calculator); PRUint32 count = calculator.Length(); aDest.SetLength(count); nsEmbedString::iterator dest; aDest.BeginWriting(dest); dest.advance(0); ConvertUTF8toUTF16 converter(dest.get()); copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), converter); if (converter.Length() != count) { aDest.SetLength(0); return PR_FALSE; }#endif return PR_TRUE;}// helper function for converting UTF-16 chars to UTF-8 chars.PRBoolConvertUtf16ToUtf8(const nsEmbedString &aSource, nsEmbedCString &aDest){#ifdef USING_GECKO_SDK_1_7 // Note: Below function was frozen for Mozilla 1.7, but not available for 1.4. // See bug 239123 for details: // http://bugzilla.mozilla.org/show_bug.cgi?id=239123 NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_UTF8, aDest); #else nsEmbedString::const_iterator source_start, source_end; CalculateUTF8Size calculator; copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), calculator); PRUint32 count = calculator.Size(); aDest.SetLength(count); nsEmbedCString::iterator dest; aDest.BeginWriting(dest); dest.advance(0); ConvertUTF16toUTF8 converter(dest.get()); copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), converter); if (converter.Size() != count) { aDest.SetLength(0); return PR_FALSE; }#endif return PR_TRUE;}// helper function for getting xpcom servicesnsresultGetService(const char *aContractID, const nsIID &aIID, void **aResult){ nsCOMPtr<nsIServiceManager> svcMgr; nsresult rv = NS_GetServiceManager(getter_AddRefs(svcMgr)); if (NS_FAILED(rv)) return rv; return svcMgr->GetServiceByContractID(aContractID, aIID, aResult);}nsresultCreateInstance(const char *aContractID, const nsIID &aIID, void **aResult){ nsCOMPtr<nsIComponentManager> compMgr; nsresult rv = NS_GetComponentManager(getter_AddRefs(compMgr)); if (NS_FAILED(rv)) return rv; return compMgr->CreateInstanceByContractID(aContractID, nsnull, aIID, aResult);}// helper function for getting the HTML page content.char* GetContent(nsIWebNavigation *aWebNav){ // JavaScript to return the content of the currently loaded URL // in *Mozilla*, which is different from the JavaScript for IE. char* MOZ_GETCONTENT_SCRIPT = "(new XMLSerializer()).serializeToString(document);"; return ExecuteScript(aWebNav, MOZ_GETCONTENT_SCRIPT);}// helper function for seting the HTML page content.nsresult SetContent(nsIWebNavigation *aWebNav, const char *htmlContent) { nsCOMPtr<nsIDOMDocument> domDoc; aWebNav->GetDocument(getter_AddRefs(domDoc)); nsCOMPtr<nsIDOMHTMLDocument> domHtmlDoc(do_QueryInterface(domDoc)); domHtmlDoc->Open(); // The htmlContent must be encoded with "UTF-8" charset from the Java side. nsEmbedString unicodeContent; ConvertUtf8ToUtf16(nsEmbedCString(htmlContent), unicodeContent); domHtmlDoc->Write(unicodeContent); domHtmlDoc->Close(); return NS_OK;}// helper function for executing javascript stringchar* ExecuteScript(nsIWebNavigation *aWebNav, const char *jscript){ // Use JavaScript command // eval("<the user input JavaScript string>"); // to evaluate the JavaScript contained within the brackets, in some cases // it may return a value. // // To execute this "eval" command and retrieve its returned value, // nsIWebNavigation::LoadURI() is used to load below complete URI: // javascript:var retValue = eval("<the user input JavaScript string>"); \ // var heads = document.getElementsByTagName('head');"); \ // heads[0].setAttribute(<JDIC_BROWSER_INTERMEDIATE_PROP>, retValue); \ // ;void(0); const int MAX_JSCRIPTURI_LENGTH = 8192; char jscriptURI[MAX_JSCRIPTURI_LENGTH]; memset(jscriptURI, '\0', MAX_JSCRIPTURI_LENGTH); strcat(jscriptURI, "javascript:"); // Tune the given jscript to assign the returned value to a predefine // DOM property of the currently loaded webapge: // JDIC_BROWSER_INTERMEDIATE_PROP strcat(jscriptURI, TuneJavaScript(jscript)); strcat(jscriptURI, ";void(0);"); nsEmbedString unicodeURI; ConvertAsciiToUtf16(jscriptURI, unicodeURI); aWebNav->LoadURI(unicodeURI.get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull); // Retrieve the returned value of eval command. nsCOMPtr<nsIDOMDocument> doc; aWebNav->GetDocument(getter_AddRefs(doc)); nsIDOMNodeList* elements = nsnull; nsCOMPtr<nsIDOMNode> node; nsEmbedString unicodeTag; char *elementTag = "head"; ConvertAsciiToUtf16(elementTag, unicodeTag); nsresult rv = doc->GetElementsByTagName(unicodeTag, &elements); if (NS_FAILED(rv)) { return NULL; } rv = elements->Item(0, getter_AddRefs(node)); nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(node, &rv)); if (NS_FAILED(rv)) { return NULL; } nsEmbedString attrValue; nsEmbedString unicodeProp; char *attrProp = JDIC_BROWSER_INTERMEDIATE_PROP; ConvertAsciiToUtf16(attrProp, unicodeProp); rv = elt->GetAttribute(unicodeProp, attrValue); // Remove the attribute created by JDIC Browser. elt->RemoveAttribute(unicodeProp); if (attrValue.Length() ==0) return NULL; nsEmbedCString utf8AttrValue; ConvertUtf16ToUtf8(attrValue, utf8AttrValue); // Return the result encoded with "UTF-8" charset, which must be decoded // with the same charset in the Java side. char* resultStr = strdup(utf8AttrValue.get()); if (resultStr != NULL && strncmp(resultStr, "undefined", strlen(resultStr))) return resultStr; else return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -