Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/engine/Main.cpp

Issue 11430025: Shut down the engine when the last tab is closed (Closed)
Patch Set: Created Aug. 7, 2013, 2:57 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/plugin/AdblockPlusClient.h » ('j') | src/plugin/AdblockPlusClient.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <functional> 2 #include <functional>
3 #include <vector> 3 #include <vector>
4 #include <Windows.h> 4 #include <Windows.h>
5 5
6 #include "../shared/AutoHandle.h" 6 #include "../shared/AutoHandle.h"
7 #include "../shared/Communication.h" 7 #include "../shared/Communication.h"
8 #include "../shared/Dictionary.h" 8 #include "../shared/Dictionary.h"
9 #include "../shared/Utils.h" 9 #include "../shared/Utils.h"
10 #include "../shared/Version.h" 10 #include "../shared/Version.h"
11 #include "../shared/CriticalSection.h" 11 #include "../shared/CriticalSection.h"
12 #include "Debug.h" 12 #include "Debug.h"
13 #include "Updater.h" 13 #include "Updater.h"
14 14
15 namespace 15 namespace
16 { 16 {
17 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; 17 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine;
18 int activeConnections = 0;
19 CriticalSection activeConnectionsLock;
18 20
19 void WriteStrings(Communication::OutputBuffer& response, 21 void WriteStrings(Communication::OutputBuffer& response,
20 const std::vector<std::string>& strings) 22 const std::vector<std::string>& strings)
21 { 23 {
22 int32_t count = strings.size(); 24 int32_t count = strings.size();
23 response << count; 25 response << count;
24 for (int32_t i = 0; i < count; i++) 26 for (int32_t i = 0; i < count; i++)
25 response << strings[i]; 27 response << strings[i];
26 } 28 }
27 29
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 } 267 }
266 268
267 } 269 }
268 return response; 270 return response;
269 } 271 }
270 272
271 DWORD WINAPI ClientThread(LPVOID param) 273 DWORD WINAPI ClientThread(LPVOID param)
272 { 274 {
273 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa ram)); 275 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa ram));
274 276
275 try 277 std::stringstream stream;
278 stream << GetCurrentThreadId();
279 std::string threadId = stream.str();
280 std::string threadString = "(Thread ID: " + threadId + ")";
281 Debug("Client connected " + threadString);
282
276 { 283 {
277 Communication::InputBuffer message = pipe->ReadMessage(); 284 CriticalSection::Lock lock(activeConnectionsLock);
278 Communication::OutputBuffer response = HandleRequest(message); 285 activeConnections++;
279 pipe->WriteMessage(response);
280 }
281 catch (const std::exception& e)
282 {
283 DebugException(e);
284 } 286 }
285 287
286 // TODO: Keep the pipe open until the client disconnects 288 for (;;)
289 {
290 try
291 {
292 Communication::InputBuffer message = pipe->ReadMessage();
293 Communication::OutputBuffer response = HandleRequest(message);
294 pipe->WriteMessage(response);
295 }
296 catch (const Communication::PipeDisconnectedError&)
297 {
298 break;
299 }
300 catch (const std::exception& e)
301 {
302 DebugException(e);
303 break;
304 }
305 }
306
307 Debug("Client disconnected " + threadString);
308
309 {
310 CriticalSection::Lock lock(activeConnectionsLock);
311 activeConnections--;
312 if (activeConnections < 1)
313 {
314 Debug("No connections left, shutting down the engine");
315 activeConnections = 0;
316 exit(0);
317 }
318 }
287 319
288 return 0; 320 return 0;
289 } 321 }
290 322
291 void OnUpdateAvailable(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValue List& params) 323 void OnUpdateAvailable(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValue List& params)
292 { 324 {
293 updateAvailable = true; 325 updateAvailable = true;
294 if (params.size() < 1) 326 if (params.size() < 1)
295 { 327 {
296 Debug("updateAvailable event missing URL"); 328 Debug("updateAvailable event missing URL");
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 Dictionary::Create(locale); 383 Dictionary::Create(locale);
352 filterEngine = CreateFilterEngine(locale); 384 filterEngine = CreateFilterEngine(locale);
353 385
354 for (;;) 386 for (;;)
355 { 387 {
356 try 388 try
357 { 389 {
358 Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeNam e, 390 Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeNam e,
359 Communication::Pipe::MODE_CREATE); 391 Communication::Pipe::MODE_CREATE);
360 392
361 // TODO: Count established connections, kill the engine when none are left
362 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pip e), 0, 0)); 393 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pip e), 0, 0));
363 if (!thread) 394 if (!thread)
364 { 395 {
365 delete pipe; 396 delete pipe;
366 DebugLastError("CreateThread failed"); 397 DebugLastError("CreateThread failed");
367 return 1; 398 return 1;
368 } 399 }
369 } 400 }
370 catch (std::runtime_error e) 401 catch (std::runtime_error e)
371 { 402 {
372 DebugException(e); 403 DebugException(e);
373 return 1; 404 return 1;
374 } 405 }
375 } 406 }
376 407
377 return 0; 408 return 0;
378 } 409 }
OLDNEW
« no previous file with comments | « no previous file | src/plugin/AdblockPlusClient.h » ('j') | src/plugin/AdblockPlusClient.cpp » ('J')

Powered by Google App Engine
This is Rietveld