1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-06 14:34:05 +02:00

Add a way to track browser process IDs

This commit is contained in:
chylex 2017-06-30 23:46:52 +02:00
parent 1712b5120e
commit 29e7ad6ce6
8 changed files with 80 additions and 6 deletions

View File

@ -1,7 +1,6 @@
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
@ -346,8 +345,20 @@ private void soundNotification_PlaybackError(object sender, PlaybackErrorEventAr
protected override void WndProc(ref Message m){
if (isLoaded){
if (m.Msg == Program.WindowRestoreMessage && WindowsUtils.CurrentProcessID == m.WParam.ToInt32()){
trayIcon_ClickRestore(trayIcon, new EventArgs());
if (m.Msg == Program.WindowRestoreMessage){
if (WindowsUtils.CurrentProcessID == m.WParam.ToInt32()){
trayIcon_ClickRestore(trayIcon, new EventArgs());
}
return;
}
else if (m.Msg == Program.SubProcessMessage){
int processId = m.WParam.ToInt32();
if (WindowsUtils.IsChildProcess(processId)){ // child process is checked in two places for safety
BrowserProcesses.Link(m.LParam.ToInt32(), processId);
}
return;
}
}

View File

@ -141,6 +141,9 @@ private void owner_FormClosed(object sender, FormClosedEventArgs e){
private void Browser_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e){
if (e.IsBrowserInitialized){
Initialized?.Invoke(this, new EventArgs());
int identifier = browser.GetBrowser().Identifier;
Disposed += (sender2, args2) => BrowserProcesses.Forget(identifier);
}
}

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Diagnostics;
using CefSharp;
namespace TweetDuck.Core.Utils{
static class BrowserProcesses{
private static readonly Dictionary<int, int> PIDs = new Dictionary<int, int>();
public static void Link(int identifier, int pid){
PIDs[identifier] = pid;
}
public static void Forget(int identifier){
PIDs.Remove(identifier);
Debug.WriteLine("rip "+identifier);
}
public static Process FindProcess(IBrowser browser){
if (PIDs.TryGetValue(browser.Identifier, out int pid) && WindowsUtils.IsChildProcess(pid)){ // child process is checked in two places for safety
return Process.GetProcessById(pid);
}
else{
return null;
}
}
}
}

View File

@ -43,6 +43,7 @@ static class Program{
private static string ConsoleLogFilePath => Path.Combine(StoragePath, "TD_Console.txt");
public static uint WindowRestoreMessage;
public static uint SubProcessMessage;
private static readonly LockManager LockManager = new LockManager(Path.Combine(StoragePath, ".lock"));
private static bool HasCleanedUp;
@ -59,6 +60,7 @@ private static void Main(){
Application.SetCompatibleTextRenderingDefault(false);
WindowRestoreMessage = NativeMethods.RegisterWindowMessage("TweetDuckRestore");
SubProcessMessage = NativeMethods.RegisterWindowMessage("TweetDuckSubProcess");
if (!WindowsUtils.CheckFolderWritePermission(StoragePath)){
MessageBox.Show(BrandName+" does not have write permissions to the storage folder: "+StoragePath, "Permission Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

View File

@ -195,6 +195,7 @@
<DependentUpon>TabSettingsNotifications.cs</DependentUpon>
</Compile>
<Compile Include="Core\Bridge\CallbackBridge.cs" />
<Compile Include="Core\Utils\BrowserProcesses.cs" />
<Compile Include="Core\Utils\CommandLineArgs.cs" />
<Compile Include="Core\Utils\CommandLineArgsParser.cs" />
<Compile Include="Core\Notification\Screenshot\FormNotificationScreenshotable.cs">

View File

@ -0,0 +1,14 @@
using System;
using System.Runtime.InteropServices;
namespace TweetDuck.Browser{
static class NativeMethods{
public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xFFFF);
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern uint RegisterWindowMessage(string messageName);
}
}

View File

@ -1,20 +1,34 @@
using System;
using System.Diagnostics;
using CefSharp;
using CefSharp.BrowserSubprocess;
namespace TweetDuck.Browser{
static class Program{
private static int Main(string[] args){
SubProcess.EnableHighDPISupport();
const string typePrefix = "--type=";
string type = Array.Find(args, arg => arg.StartsWith(typePrefix, StringComparison.OrdinalIgnoreCase)).Substring(typePrefix.Length);
if (type == "renderer"){
using(SubProcess subProcess = new SubProcess(args)){
using(RendererProcess subProcess = new RendererProcess(args)){
return subProcess.Run();
}
}
else return SubProcess.ExecuteProcess();
}
private class RendererProcess : SubProcess{
public RendererProcess(string[] args) : base(args){}
public override void OnBrowserCreated(CefBrowserWrapper wrapper){
base.OnBrowserCreated(wrapper);
using(Process me = Process.GetCurrentProcess()){
NativeMethods.PostMessage(NativeMethods.HWND_BROADCAST, NativeMethods.RegisterWindowMessage("TweetDuckSubProcess"), me.Id, wrapper.BrowserId);
}
}
}
}
}

View File

@ -24,12 +24,14 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="CefSharp, Version=57.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138" />
<Reference Include="CefSharp.BrowserSubprocess.Core">
<HintPath>..\packages\CefSharp.Common.57.0.0\CefSharp\x86\CefSharp.BrowserSubprocess.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="NativeMethods.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>