-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
324 lines (290 loc) · 12.4 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import React, { useState, useEffect } from 'react';
import { Activity, XCircle, RefreshCw, HardDrive, Cpu } from 'lucide-react';
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';
import { Alert, AlertTitle, AlertDescription } from './componenets/ui/alert';
const ProcessManager = () => {
const [processes, setProcesses] = useState({});
const [selectedProcess, setSelectedProcess] = useState(null);
const [cpuHistory, setCpuHistory] = useState([]);
const [memoryHistory, setMemoryHistory] = useState([]);
const [error, setError] = useState(null);
const [filter, setFilter] = useState('');
const [groupBy, setGroupBy] = useState('none');
const [sortBy, setSortBy] = useState('cpu');
const [sortDirection, setSortDirection] = useState('desc');
useEffect(() => {
const updateProcesses = async () => {
try {
const info = await chrome.processes.getProcessInfo([], true);
setProcesses(info);
} catch (err) {
setError(err.message);
}
};
// Initial load
updateProcesses();
// Set up listeners for process updates
chrome.processes.onUpdatedWithMemory.addListener((updatedProcesses) => {
setProcesses(updatedProcesses);
if (selectedProcess) {
const process = updatedProcesses[selectedProcess];
if (process) {
// Update history for charts
const timestamp = new Date().toLocaleTimeString();
setCpuHistory(prev => [...prev.slice(-20), {
time: timestamp,
cpu: process.cpu || 0
}]);
setMemoryHistory(prev => [...prev.slice(-20), {
time: timestamp,
memory: process.privateMemory ? process.privateMemory / (1024 * 1024) : 0
}]);
}
}
});
chrome.processes.onExited.addListener((processId) => {
if (selectedProcess === processId) {
setSelectedProcess(null);
}
});
return () => {
chrome.processes.onUpdatedWithMemory.removeListener();
chrome.processes.onExited.removeListener();
};
}, [selectedProcess]);
const terminateProcess = async (processId) => {
try {
const success = await chrome.processes.terminate(processId);
if (!success) {
setError('Failed to terminate process');
}
} catch (err) {
setError(err.message);
}
};
const formatMemory = (bytes) => {
if (!bytes) return '0 MB';
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};
const formatCacheSize = (process) => {
if (!process.imageCache && !process.cssCache && !process.scriptCache) {
return 'N/A';
}
const total = (process.imageCache?.liveSize || 0) +
(process.cssCache?.liveSize || 0) +
(process.scriptCache?.liveSize || 0);
return formatMemory(total);
};
const getProcessTypeIcon = (type) => {
switch (type) {
case 'browser': return <Activity className="w-5 h-5" />;
case 'renderer': return <Cpu className="w-5 h-5" />;
case 'gpu': return <HardDrive className="w-5 h-5" />;
default: return <Activity className="w-5 h-5" />;
}
};
const groupProcesses = (processes) => {
if (groupBy === 'none') return processes;
return Object.entries(processes).reduce((groups, [id, process]) => {
const key = groupBy === 'type' ? process.type : 'other';
if (!groups[key]) groups[key] = {};
groups[key][id] = process;
return groups;
}, {});
};
const sortProcesses = (processes) => {
return Object.entries(processes).sort(([, a], [, b]) => {
const aValue = a[sortBy] || 0;
const bValue = b[sortBy] || 0;
return sortDirection === 'desc' ? bValue - aValue : aValue - bValue;
});
};
const filterProcesses = (processes) => {
if (!filter) return processes;
return Object.entries(processes).reduce((filtered, [id, process]) => {
if (
process.type.toLowerCase().includes(filter.toLowerCase()) ||
id.toString().includes(filter)
) {
filtered[id] = process;
}
return filtered;
}, {});
};
const getProcessName = (process) => {
// Special handling for utility processes
if (process.type === 'utility') {
if (process.title?.includes('Network')) return 'Utility: Network Service';
if (process.title?.includes('Storage')) return 'Utility: Storage Service';
if (process.title?.includes('Device')) return 'Utility: On-Device Model Service';
return `Utility: ${process.title || 'Unknown'}`;
}
// Special handling for extensions and tabs
if (process.type === 'renderer') {
if (process.title?.includes('Extension:')) {
return process.title;
}
if (process.title) {
return `Tab: ${process.title}`;
}
return 'Tab';
}
// Handle other process types
switch (process.type) {
case 'browser':
return 'Browser';
case 'gpu':
return 'GPU Process';
case 'extension':
return `Extension: ${process.title || 'Unknown'}`;
case 'plugin':
return `Plugin: ${process.title || 'Unknown'}`;
case 'spare_renderer':
return 'Spare renderer';
default:
return process.title || process.type || 'Unknown Process';
}
};
// Add this debug function
const debugProcess = (process) => {
console.log('Process:', {
id: process.id,
type: process.type,
title: process.title,
profile: process.profile,
tasks: process.tasks
});
};
// Add window resize handling
useEffect(() => {
const handleResize = () => {
// Force a re-render on window resize
setProcesses(prev => ({...prev}));
};
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
// Update the main container styles
return (
<div className="min-h-screen p-6">
<div className="max-w-[1400px] mx-auto bg-white rounded-lg shadow-lg">
{error && (
<Alert variant="destructive" className="mb-6">
<AlertTitle>Error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<div className="flex flex-1 min-h-[600px]">
{/* Left side - Process List */}
<div className={`${selectedProcess ? 'w-2/3' : 'w-full'} transition-all duration-200`}>
<div className="bg-white shadow-lg rounded-lg overflow-hidden">
<div className="p-4 border-b border-gray-200">
<h2 className="text-lg font-semibold text-gray-900">Chrome Processes</h2>
</div>
<div className="overflow-auto max-h-[calc(100vh-180px)]">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50 sticky top-0">
<tr>
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Task</th>
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Memory footprint ▼</th>
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">CPU</th>
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Network</th>
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Process ID</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{Object.entries(processes).map(([id, process]) => (
<tr
key={id}
className={`hover:bg-gray-50 cursor-pointer ${
selectedProcess === parseInt(id) ? 'bg-blue-50' : ''
}`}
onClick={() => setSelectedProcess(parseInt(id))}
>
<td className="px-6 py-4 whitespace-nowrap">
<div className="flex items-center">
{getProcessTypeIcon(process.type)}
<span className="ml-3 text-sm text-gray-900">{getProcessName(process)}</span>
</div>
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{formatMemory(process.privateMemory)}
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{process.cpu ? `${process.cpu.toFixed(1)}` : '0.0'}
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{process.network ? process.network.toFixed(1) : '0'}
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{process.osProcessId}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
{/* Right side - Process Details */}
{selectedProcess !== null && processes[selectedProcess] && (
<div className="w-1/3 transition-all duration-200">
<div className="bg-white shadow-lg rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-900 mb-6">Process Details</h3>
<div className="space-y-6">
{/* Basic Info */}
<div className="bg-gray-50 rounded-lg p-4">
<h4 className="font-medium text-gray-700 mb-3">Process Information</h4>
<div className="space-y-3">
<div>
<span className="text-sm text-gray-500">Type:</span>
<div className="flex items-center mt-1">
{getProcessTypeIcon(processes[selectedProcess].type)}
<span className="ml-2 text-sm text-gray-900">{processes[selectedProcess].type}</span>
</div>
</div>
<div>
<span className="text-sm text-gray-500">Process ID:</span>
<p className="mt-1 text-sm text-gray-900">{processes[selectedProcess].osProcessId}</p>
</div>
</div>
</div>
{/* Memory Metrics */}
<div className="bg-gray-50 rounded-lg p-4">
<h4 className="font-medium text-gray-700 mb-3">Memory Usage</h4>
<div className="space-y-3">
<div>
<span className="text-sm text-gray-500">Private Memory:</span>
<p className="mt-1 text-sm text-gray-900">{formatMemory(processes[selectedProcess].privateMemory)}</p>
</div>
{/* Add other memory metrics here */}
</div>
</div>
{/* Performance Metrics */}
<div className="bg-gray-50 rounded-lg p-4">
<h4 className="font-medium text-gray-700 mb-3">Performance</h4>
<div className="space-y-3">
<div>
<span className="text-sm text-gray-500">CPU Usage:</span>
<p className="mt-1 text-sm text-gray-900">
{processes[selectedProcess].cpu ? `${processes[selectedProcess].cpu.toFixed(1)}%` : 'N/A'}
</p>
</div>
<div>
<span className="text-sm text-gray-500">Network Activity:</span>
<p className="mt-1 text-sm text-gray-900">
{processes[selectedProcess].network ? `${processes[selectedProcess].network.toFixed(1)} KB/s` : 'N/A'}
</p>
</div>
</div>
</div>
</div>
</div>
</div>
)}
</div>
</div>
</div>
);
};
export default ProcessManager;