📄 mozembed.cpp
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- *//* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org Code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 2001 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Chak Nanga <chak@netscape.com> * Conrad Carlen <ccarlen@netscape.com> * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */// File Overview....//// The typical MFC app, frame creation code + AboutDlg handling//// NS_InitEmbedding() is called in InitInstance()// // NS_TermEmbedding() is called in ExitInstance()// ExitInstance() also takes care of cleaning up of// multiple browser frame windows on app exit//// Code to handle the creation of a new browser window// Next suggested file to look at : BrowserFrm.cpp// Local Includes#include "stdafx.h"#include "MozEmbed.h"#include "BrowserFrm.h"#include "BrowserImpl.h"#include "nsIWindowWatcher.h"#include "nsIProfile.h"#include "plstr.h"#include "nsCRT.h"#include <io.h>#include <fcntl.h>#include "prthread.h"#include "nsXPCOMGlue.h"#include "Common.h"#include "nsEmbedString.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif// this is for overriding the Mozilla default PromptService component#include "nsIComponentRegistrar.h"#include "PromptService.h"int gTestMode = FALSE;int gQuitMode = FALSE;class MozEmbedCommandLine : public CCommandLineInfo{public: MozEmbedCommandLine(MozEmbedApp& app) : CCommandLineInfo(), mApp(app) { } // generic parser which bundles up flags and their parameters, to // pass to HandleFlag() or HandleNakedParameter() // if you're adding new parameters, please don't touch this // function and instead add your own handler below virtual void ParseParam(LPCTSTR szParam, BOOL bFlag, BOOL bLast) { CCommandLineInfo::ParseParam(szParam, bFlag, bLast); if (bFlag) { // advance past extra stuff like --foo while (*szParam && *szParam == '-') szParam++; // previous argument was a flag too, so process that first if (!mLastFlag.IsEmpty()) HandleFlag(mLastFlag); mLastFlag = szParam; // oops, no more arguments coming, so handle this now if (bLast) HandleFlag(mLastFlag); } else { if (!mLastFlag.IsEmpty()) HandleFlag(mLastFlag, szParam); mLastFlag.Truncate(); } } // handle flag-based parameters void HandleFlag(const nsACString& flag, const char* param=nsnull) { if (flag.Equals("console")) { DoConsole(); } else if (flag.Equals("test")) { gTestMode = TRUE; } else if (Substring(flag, 0, 4).Equals("port")) { gMessenger.SetPort(atoi(PromiseFlatCString(Substring(flag, 5, flag.Length() - 5)).get())); } // add new flag handlers here (please add a DoFoo() method below!) } void HandleNakedParameter(const char* flag) { // handle non-flag arguments here } // add your specific handlers here void DoConsole() { mApp.ShowDebugConsole(); }private: // autostring is fine, this is a stack based object anyway nsCAutoString mLastFlag; MozEmbedApp& mApp;};BEGIN_MESSAGE_MAP(MozEmbedApp, CWinApp) //{{AFX_MSG_MAP(MozEmbedApp) ON_COMMAND(ID_NEW_BROWSER, OnNewBrowser) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAPEND_MESSAGE_MAP()MozEmbedApp theApp;void SocketMsgHandler(const char *pMsg);MozEmbedApp::MozEmbedApp(){ mRefCnt = 1; // Start at one - nothing is going to addref this object mInitFailed = FALSE;}MozEmbedApp::~MozEmbedApp(){}void MozEmbedApp::ShowDebugConsole(){#ifdef _DEBUG // Show console only in debug mode if (! AllocConsole()) return; // Redirect stdout to the console int hCrtOut = _open_osfhandle( (long) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT); if (hCrtOut == -1) return; FILE *hfOut = _fdopen(hCrtOut, "w"); if (hfOut != NULL) { // Setup for unbuffered I/O so the console // output shows up right away *stdout = *hfOut; setvbuf(stdout, NULL, _IONBF, 0); } // Redirect stderr to the console int hCrtErr = _open_osfhandle( (long) GetStdHandle(STD_ERROR_HANDLE), _O_TEXT); if (hCrtErr == -1) return; FILE *hfErr = _fdopen(hCrtErr, "w"); if (hfErr != NULL) { // Setup for unbuffered I/O so the console // output shows up right away *stderr = *hfErr; setvbuf(stderr, NULL, _IONBF, 0); }#endif}BOOL MozEmbedApp::InitInstance(){ MozEmbedCommandLine cmdLine(*this); ParseCommandLine(cmdLine); Enable3dControls(); if (! gTestMode) { gMessenger.CreateServerSocket(); PRThread *socketListenThread = nsnull; if (!CreateHiddenWindow()) return FALSE; socketListenThread = PR_CreateThread(PR_USER_THREAD, PortListening, SocketMsgHandler, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (!socketListenThread) return FALSE; } else { MessageReceived("0,0,"); CBrowserFrame *pBrowserFrame = CreateNewBrowserFrame( nsIWebBrowserChrome::CHROME_ALL); m_pMainWnd = pBrowserFrame; pBrowserFrame->m_wndBrowserView.OpenURL("about:blank"); } return TRUE;}int MozEmbedApp::ExitInstance(){ if (m_pMainWnd) m_pMainWnd->DestroyWindow(); NS_ShutdownXPCOM(nsnull); XPCOMGlueShutdown(); return 1;}BOOL MozEmbedApp::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_SOCKET_MSG) { char *p = (char *)pMsg->lParam; if (p) { WBTRACE("Event from socket: %s\n", p); MessageReceived(p); } else { WBTRACE("Wrong Event\n"); } delete p; } return CWinApp::PreTranslateMessage(pMsg);}CBrowserFrame* MozEmbedApp::CreateNewBrowserFrame(PRUint32 chromeMask, HWND hParent, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy, PRBool bShowWindow){ UINT resId = IDR_MAINFRAME; // Setup a CRect with the requested window dimensions CRect winSize(x, y, cx, cy); // Use the Windows default if all are specified as -1 if (x == -1 && y == -1 && cx == -1 && cy == -1) winSize = CFrameWnd::rectDefault; // Load the window title from the string resource table CString strTitle; strTitle.LoadString(IDR_MAINFRAME); // Now, create the browser frame CBrowserFrame* pFrame = new CBrowserFrame(chromeMask); if (!pFrame->Create(NULL, strTitle, WS_OVERLAPPEDWINDOW, winSize, CWnd::FromHandle(hParent), MAKEINTRESOURCE(resId), 0L, NULL)) { delete pFrame; return NULL; } // load accelerator resource pFrame->LoadAccelTable(MAKEINTRESOURCE(IDR_MAINFRAME)); // Show the window... if (bShowWindow) { pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); } return pFrame;}CBrowserFrame* MozEmbedApp::CreateEmbeddedBrowserFrame(HWND hParent){ ASSERT(hParent != NULL); CBrowserFrame *pBrowserFrame = CreateNewBrowserFrame( nsIWebBrowserChrome::CHROME_DEFAULT, hParent, 0, 0, 0, 0); return pBrowserFrame;}void MozEmbedApp::OnNewBrowser(){ CreateNewBrowserFrame(nsIWebBrowserChrome::CHROME_ALL);}// Check the GRE directory path if Mozilla is set as the default browser.// XXX Below code is partly duplicate of WebBrowserUtil.cpp, which needs to // refactored somehow.PRBool GetGREPath(char* pathBuf, size_t pathBufSize){ HKEY hKey; const char* mozExe = "mozilla.exe"; char browserPath[_MAX_PATH+1] = "\0"; DWORD cb = sizeof(browserPath); // Get the default browser path by checking the http protocol handler. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "http\\shell\\open\\command", 0, KEY_READ, &hKey) != ERROR_SUCCESS) return PR_FALSE; if (RegQueryValueEx(hKey, "", NULL, NULL, (BYTE*)browserPath, &cb) != ERROR_SUCCESS) { RegCloseKey(hKey); return PR_FALSE; } RegCloseKey(hKey); // Check if the default browser is mozilla. char* exePtr; char* lwrBrowserPath = strlwr(strdup(browserPath)); if (!(exePtr = strstr(lwrBrowserPath, mozExe))) // The default browser is not Mozilla. return PR_FALSE; // Remove the trailing part after the first space character at the rail // of mozilla.exe. cb = strlen(browserPath); for (int i = (exePtr - lwrBrowserPath + strlen(mozExe)); i < (int)cb; i++) { if (browserPath[i] == ' ') { browserPath[i] = '\0'; break; } } // Check if xpcom.dll exists under the same parent directory as mozilla.exe. char* ptr = strrchr(browserPath, '\\'); char parentPath[_MAX_PATH+1] = "\0"; strncpy(parentPath, browserPath, ptr - browserPath); char* xpcomFilePath = strcat(parentPath, "\\xpcom.dll"); if (fopen(xpcomFilePath,"r")) { // xpcom.dll exists, Mozilla is installed with a .zip package. strncpy(pathBuf, browserPath, ptr - browserPath); pathBuf[ptr - browserPath] = '\0'; } else { // xpcom.dll doesn't exist, Mozilla is installed with an .exe package. // Check the installed GRE directory: // [Common Files]\mozilla.org\GRE\<GRE version> const char* greParentKey = "Software\\mozilla.org\\GRE\\"; DWORD loop = 0; char greVersionKey[256]; DWORD greVersionKeyLen = 256; char *lastVersionKey = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -