Beispiel #1
0
	LRESULT CALLBACK WindowMessageHook::CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
	{
		if (nCode == HC_ACTION)
		{
			CWPRETSTRUCT * info = (CWPRETSTRUCT*) lParam;
			// info->wParam == NULL 表示焦点移到其它进程去了,我们只有在这个时候才要保护IE的焦点
			if (WM_KILLFOCUS == info->message && NULL == info->wParam)
			{
				HWND hwnd = info->hwnd;
				CString strClassName;
				GetClassName(hwnd, strClassName.GetBuffer(MAX_PATH), MAX_PATH);
				strClassName.ReleaseBuffer(); 
				if (strClassName ==  _T("Internet Explorer_Server"))
				{
					// 重新把焦点移到 plugin 窗口上,这样从别的进程窗口切换回来的时候IE才能有焦点
					CIEHostWindow* pIEHostWindow = CIEHostWindow::FromInternetExplorerServer(hwnd);
					if (pIEHostWindow)
					{
						pIEHostWindow->SetFocus();
					}
				}
			}
		}

		return CallNextHookEx(s_hhookCallWndProcRet, nCode, wParam, lParam);
	}
Beispiel #2
0
CIEHostWindow* CPlugin::CreateIEHostWindow(HWND hParent, DWORD dwId)
{
  CIEHostWindow *pIEHostWindow = NULL;
  CWnd parent;
  if (!parent.Attach(hParent))
  {
    return FALSE;
  }
  try
  {
    pIEHostWindow = CIEHostWindow::CreateNewIEHostWindow(dwId);
    if (pIEHostWindow == NULL)
    {
      throw CString(_T("Cannot Create CIEHostWindow!"));
    }
    pIEHostWindow->SetPlugin(this);
    pIEHostWindow->SetParent(&parent);
    CRect rect;
    parent.GetClientRect(rect);
    pIEHostWindow->MoveWindow(rect);
    pIEHostWindow->ShowWindow(SW_SHOW);
  }
  catch (CString strMessage)
  {
    if (pIEHostWindow) 
    {
      delete pIEHostWindow;
      pIEHostWindow = NULL;
    }
    TRACE(_T("[CPlugin::CreateIEHostWindow] Exception: %s\n"), (LPCTSTR)strMessage);
  }
  parent.Detach();
  return pIEHostWindow;
}
Beispiel #3
0
	CIEHostWindow* CPlugin::CreateIEHostWindow(HWND hParent, ULONG_PTR ulId, bool isUtils, bool* opIsNewlyCreated)
	{
		CIEHostWindow *pIEHostWindow = NULL;
		CWnd parent;
		if (!parent.Attach(hParent))
		{
			return NULL;
		}
		try
		{
			pIEHostWindow = CIEHostWindow::CreateNewIEHostWindow(&parent, ulId, isUtils, opIsNewlyCreated);
			if (pIEHostWindow == NULL)
			{
				throw CString(_T("Cannot Create CIEHostWindow!"));
			}
			if (isUtils)
			{
				// For content plugins, SetPlugin will be delayed until SPO initialization,
				// in order to avoid event ordering issues.
				pIEHostWindow->SetPlugin(this);
			}
			pIEHostWindow->SetParent(&parent);
			CRect rect;
			parent.GetClientRect(rect);
			pIEHostWindow->MoveWindow(rect);
			pIEHostWindow->ShowWindow(SW_SHOW);
		}
		catch (const CString& strMessage)
		{
			if (pIEHostWindow)
			{
				delete pIEHostWindow;
				pIEHostWindow = NULL;
			}
			UNUSED(strMessage);
			TRACE(_T("[CPlugin::CreateIEHostWindow] Exception: %s\n"), (LPCTSTR)strMessage);
		}
		parent.Detach();
		return pIEHostWindow;
	}
Beispiel #4
0
unsigned int AdBlockPlus::asyncLoader(void* ppathname)
{
	wstring* pstr = reinterpret_cast<wstring*>(ppathname);
	wstring pathname = *pstr;
	delete pstr;

	bool loaded = false;

	// then load stuff
	CFile file;
	if (file.Open(pathname.c_str(), CFile::modeRead | CFile::shareDenyWrite))
	{
		wstring content;
		bool success = Utils::File::readFile(file, content);
		file.Close();
		if (success)
		{
			WriterLock wl(s_mutex);
			clearFiltersInternal(true);

			INIParser parser;
			
			// split content into lines, process with INIParser one by one
			size_t lastPos = 0;
			wchar_t lastCh = 0;
			for (size_t i = 0; i < content.length(); i++)
			{
				wchar_t ch = content[i];
				// accept CR, LF and CRLF sequence
				if (ch == L'\r' || (ch == L'\n' && lastCh != L'\r'))
				{
					parser.process(content.substr(lastPos, i - lastPos), false);
				}
				if (ch == L'\r' || ch == L'\n')
					lastPos = i + 1;
				lastCh = ch;
			}
			if (lastPos < content.length())
				parser.process(content.substr(lastPos, content.length() - lastPos), false);
			parser.process(L"", true);

			// put everything in INIParser::filters into the matcher
			for (auto iter = parser.filters.begin(); iter != parser.filters.end(); ++iter)
			{
				ActiveFilter* filter = *iter;
				if (!filter || filter->isDisabled()) continue;

				RegExpFilter* regexpFilter = filter->toRegExpFilter();
				if (regexpFilter)
				{
					regexpMatcher.add(regexpFilter);
					continue;
				}

				ElemHideFilter* elemhideFilter = filter->toElemHideFilter();
				if (elemhideFilter)
					elemhideMatcher.add(elemhideFilter);
			}
			// generate the general filter list for elemhideMatcher to improve speed
			elemhideMatcher.generateGeneralFilters();
			loaded = true;
		}
	}

	// Notify main thread about load completion
	CIEHostWindow* pWindow = CIEHostWindow::GetAnyUtilsWindow();
	if (pWindow)
		pWindow->RunAsync([=]
		{
			filterLoadedCallback(loaded);
			pWindow->SendMessage(UserMessage::WM_USER_MESSAGE,
				loaded ? UserMessage::WPARAM_ABP_FILTER_LOADED : UserMessage::WPARAM_ABP_LOAD_FAILURE, 0);
		});
	else
		filterLoadedCallback(loaded);

	return 0;
}
Beispiel #5
0
	// The message loop of Mozilla does not handle accelertor keys.
	// IOleInplaceActivateObject requires MSG be filtered by its TranslateAccellerator() method.
	// So we install a hook to do the dirty hack.
	// Mozilla message loop is here:
	// http://mxr.mozilla.org/mozilla-central/source/widget/src/windows/nsAppShell.cpp
	// bool nsAppShell::ProcessNextNativeEvent(bool mayWait)
	// It does PeekMessage, TranslateMessage, and then pass the result directly
	// to DispatchMessage.
	// Just before PeekMessage returns, our hook procedure is called.
	LRESULT CALLBACK WindowMessageHook::GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) 
	{ 
		if (nCode >= 0 && wParam == PM_REMOVE && lParam)
		{
			MSG * pMsg = reinterpret_cast<MSG *>(lParam);
			HWND hwnd = pMsg->hwnd;

			// 只处理键盘消息
			if (pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST || hwnd == NULL)
			{
				goto Exit;
			}

			// 只处理IE窗口消息,通过检查窗口类名过滤非IE窗口
			CString strClassName;
			GetClassName(hwnd, strClassName.GetBuffer(MAX_PATH), MAX_PATH);
			strClassName.ReleaseBuffer(); 
			if (WM_KEYDOWN == pMsg->message && VK_TAB == pMsg->wParam && strClassName == _T("Internet Explorer_TridentCmboBx"))
			{
				hwnd = ::GetParent(hwnd);
				GetClassName(hwnd, strClassName.GetBuffer(MAX_PATH), MAX_PATH);
				strClassName.ReleaseBuffer(); 
			}
			if (strClassName != _T("Internet Explorer_Server"))
			{
				goto Exit;
			}

			// 获取CIEHostWindow对象
			CIEHostWindow* pIEHostWindow = CIEHostWindow::FromInternetExplorerServer(hwnd);
			if (pIEHostWindow == NULL) 
			{
				goto Exit;
			}

			if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN || pMsg->message == WM_SYSKEYUP)
			{
				BOOL bAltPressed = HIBYTE(GetKeyState(VK_MENU)) != 0;
				BOOL bCtrlPressed = HIBYTE(GetKeyState(VK_CONTROL)) != 0;
				BOOL bShiftPressed = HIBYTE(GetKeyState(VK_SHIFT))  != 0;

				// 当Alt键释放时,也向Firefox窗口转发按钮消息。否则无法通过Alt键选中主菜单。
				if (pMsg->message == WM_SYSKEYUP && pMsg->wParam == VK_MENU) 
				{
					bAltPressed = TRUE;
				}

				TRACE(_T("WindowMessageHook::GetMsgProc MSG: %x wParam: %x, lPara: %x\n"), pMsg->message, pMsg->wParam, pMsg->lParam);
				if (bCtrlPressed || bAltPressed || (pMsg->wParam >= VK_F1 && pMsg->wParam <= VK_F24))
				{
					int nKeyCode = static_cast<int>(pMsg->wParam);
					if (FilterFirefoxKey(nKeyCode, bAltPressed, bCtrlPressed, bShiftPressed))
					{
						HWND hwndMessageTarget = GetTopMozillaWindowClassWindow(pIEHostWindow->GetSafeHwnd());
						if (hwndMessageTarget)
						{
							::SetFocus(hwndMessageTarget);
							::PostMessage(hwndMessageTarget, pMsg->message, pMsg->wParam, pMsg->lParam);
							pMsg->message = WM_NULL;
							goto Exit;
						}
					}
				}
			}

			if (pIEHostWindow->m_ie.TranslateAccelerator(pMsg) == S_OK)
			{
				pMsg->message = WM_NULL;
			}
		}
Exit:
		return CallNextHookEx(s_hhookGetMessage, nCode, wParam, lParam); 
	}
	bool ScriptablePluginObject::GetProperty(NPIdentifier name, NPVariant *result)
	{
    CIEHostWindow* pMainWindow = GetIEHostWindow();
		if (pMainWindow == NULL)
			return false;

		// readonly property {string} URL
		if (name == m_URLID) 
		{
			CString URL = pMainWindow->GetURL();
			STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(URL), *result);
			return true;
		} 
		// readonly property {title} LocationURL
		else if (name == m_TitleID)
		{
			CString title = pMainWindow->GetTitle();
			STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(title), *result);
			return true;
		}
    // readonly property {string} FaviconURL
    else if (name == m_FaviconURLID)
    {
      CString url = pMainWindow->GetFaviconURL();
      STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(url), *result);
      return true;
    }
    // readonly property {boolean} CanRefresh
		else if (name == m_CanRefreshID)
		{
			BOOL canRefresh = pMainWindow->GetCanRefresh();
			BOOLEAN_TO_NPVARIANT(canRefresh, *result);
			return true;
		}
		// readonly property {boolean} CanStop
		else if (name == m_CanStopID)
		{
			BOOL canStop = pMainWindow->GetCanStop();
			BOOLEAN_TO_NPVARIANT(canStop, *result);
			return true;
		}
		// readonly property {boolean} CanBack
		else if (name == m_CanBackID)
		{
			BOOL canBack = pMainWindow->GetCanBack();
			BOOLEAN_TO_NPVARIANT(canBack, *result);
			return true;
		}
		// readonly property {boolean} CanForward
		else if (name == m_CanForwardID)
		{
			BOOL canForward = pMainWindow->GetCanForward();
			BOOLEAN_TO_NPVARIANT(canForward, *result);
			return true;
		}
		// readonly property {boolean} CanCopy
		else if (name == m_CanCopyID)
		{
			BOOL canCopy = pMainWindow->GetCanCopy();
			BOOLEAN_TO_NPVARIANT(canCopy, *result);
			return true;
		}
		// readonly property {boolean} CanCut
		else if (name == m_CanCutID)
		{
			BOOL canCut = pMainWindow->GetCanCut();
			BOOLEAN_TO_NPVARIANT(canCut, *result);
			return true;
		}
		// readonly property {boolean} CanPaste
		else if (name == m_CanPasteID)
		{
			BOOL canPaste = pMainWindow->GetCanPaste();
			BOOLEAN_TO_NPVARIANT(canPaste, *result);
			return true;
		}
		// readonly property {boolean} Progress
		else if (name == m_ProgressID)
		{
			INT32_TO_NPVARIANT(pMainWindow->GetProgress(),*result);
			return true;
		}

		VOID_TO_NPVARIANT(*result);
		return true;
	}
	bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args,
		uint32_t argCount, NPVariant *result)
	{
    CIEHostWindow* pMainWindow = GetIEHostWindow();
		if (pMainWindow == NULL)
			return false;

		// void Navigate({string} URL, {string} post, {string} headers)
		if (name == m_NavigateID) 
		{
			TRACE ("Navigate called!\n");
			if (argCount < 3)
				return false;

			NPVariant vURL = args[0];
			if (vURL.type != NPVariantType_String)
				return false;
			CString URL = NPStringToCString(vURL.value.stringValue);

			NPVariant vHeaders = args[1];
			if (vHeaders.type != NPVariantType_String)
				return false;
			CString headers = NPStringToCString(vHeaders.value.stringValue);

			NPVariant vPost = args[2];
			if (vPost.type != NPVariantType_String)
				return false;
			CString post = NPStringToCString(vPost.value.stringValue);


			pMainWindow->Navigate(URL, post, headers);

			VOID_TO_NPVARIANT(*result);

			return true;
		}
		// void Refresh()
		else if (name == m_RefreshID)
		{
			TRACE ("Refresh called!\n");
			pMainWindow->Refresh();
			return true;
		}
		// void Stop()
		else if (name == m_StopID)
		{
			TRACE ("Stop called!\n");
			pMainWindow->Stop();
			return true;
		}
		// void Back()
		else if (name == m_BackID)
		{
			TRACE ("Back called!\n");
			pMainWindow->Back();
			return true;
		}
		// void Forward()
		else if (name == m_ForwardID)
		{
			TRACE ("Forward called!\n");
			pMainWindow->Forward();
			return true;
		}
		// void Focus()
		else if (name == m_FocusID)
		{
			TRACE ("Focus called!\n");
			pMainWindow->Focus();
			return true;
		}
		// void Copy()
		else if (name == m_CopyID)
		{
			TRACE ("Copy called!\n");
			pMainWindow->Copy();
			return true;
		}
		// void Cut()
		else if (name == m_CutID)
		{
			TRACE ("Cut called!\n");
			pMainWindow->Cut();
			return true;
		}
		// void Paste()
		else if (name == m_PasteID)
		{
			TRACE ("Paste called!\n");
			pMainWindow->Paste();
			return true;
		}
		// void SelectAll()
		else if (name == m_SelectAllID)
		{
			TRACE ("SelectAll called!\n");
			pMainWindow->SelectAll();
			return true;
		}
		// void Find()
		else if (name == m_FindID)
		{
			TRACE ("Find called!\n");
			pMainWindow->Find();
			return true;
		}
		// void HandOverFocus()
		else if (name == m_HandOverFocusID)
		{
			TRACE ("HandOverFocus called!\n");
			pMainWindow->HandOverFocus();
			return true;
		}
		// void Zoom({number} level)
		else if (name == m_ZoomID)
		{
			TRACE ("Zoom called!\n");

			if (argCount < 1)
				return false;

			double level = 1;

			if (NPVARIANT_IS_DOUBLE(args[0])) 
				level = NPVARIANT_TO_DOUBLE(args[0]);
			else if ( NPVARIANT_IS_INT32(args[0]) ) 
				level = NPVARIANT_TO_INT32(args[0]);

			pMainWindow->Zoom(level);
			return true;
		}
		// void DisplaySecurityInfo()
		else if (name == m_DisplaySecurityInfoID)
		{
			TRACE ("DisplaySecurityInfo called!\n");
			pMainWindow->DisplaySecurityInfo();
			return true;
		}
		// void SaveAs()
		else if (name == m_SaveAsID)
		{
			TRACE ("SaveAs called!\n");
			pMainWindow->SaveAs();
			return true;
		}
		// void Print()
		else if (name == m_PrintID)
		{
			TRACE ("Print called!\n");
			pMainWindow->Print();
			return true;
		}
		// void PrintPreview()
		else if (name == m_PrintPreviewID)
		{
			TRACE ("PrintPreview called!\n");
			pMainWindow->PrintPreview();
			return true;
		}
		// void PrintSetup()
		else if (name == m_PrintSetupID)
		{
			TRACE ("PrintSetup called!\n");
			pMainWindow->PrintSetup();
			return true;
		}
		return false;
	}
Beispiel #8
0
unsigned int AdBlockPlus::asyncLoader(void* vpparam)
{
	AsyncLoaderParam* pparam = reinterpret_cast<AsyncLoaderParam*>(vpparam);
	wstring pathname = std::move(pparam->pathname);
	wstring additionalFilters = std::move(pparam->additionalFilters);
	unordered_map<wstring, wstring> options = std::move(pparam->options);
	delete pparam;

	bool loaded = false;

	// then load stuff
	CFile file;
	if (file.Open(pathname.c_str(), CFile::modeRead | CFile::shareDenyWrite))
	{
		wstring content;
		bool success = Utils::File::readFile(file, content);
		file.Close();
		if (success)
		{
			WriterLock wl(s_mutex);
			clearFiltersInternal(true);

			INIParser parser(options);
			loadContent(parser, content);
			loadContent(parser, additionalFilters);

			// put everything in INIParser::filters into the matcher
			for (auto iter = parser.filters.begin(); iter != parser.filters.end(); ++iter)
			{
				ActiveFilter* filter = *iter;
				if (!filter || filter->isDisabled()) continue;

				RegExpFilter* regexpFilter = filter->toRegExpFilter();
				if (regexpFilter)
				{
					regexpMatcher.add(regexpFilter);
					continue;
				}

				ElemHideFilter* elemhideFilter = filter->toElemHideFilter();
				if (elemhideFilter)
					elemhideMatcher.add(elemhideFilter);
			}
			// generate the general filter list for elemhideMatcher to improve speed
			elemhideMatcher.generateGeneralFilters();
			loaded = true;
		}
	}

	// Notify main thread about load completion
	CIEHostWindow* pWindow = CIEHostWindow::GetAnyUtilsWindow();
	if (pWindow)
	{
		pWindow->RunAsync([=]
		{
			// filterLoadedCallback() should be called on the main thread
			// to ensure atomicity;
			// Must call the callback before sending the message
			filterLoadedCallback(loaded);
			pWindow->SendMessage(UserMessage::WM_USER_MESSAGE,
				loaded ? UserMessage::WPARAM_ABP_FILTER_LOADED : UserMessage::WPARAM_ABP_LOAD_FAILURE, 0);
		});
	}
	else
	{
		// Just a fallback, may never get to here
		filterLoadedCallback(loaded);
	}

	return 0;
}