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

Side by Side Diff: src/plugin/AdblockPlusClient.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 | « src/plugin/AdblockPlusClient.h ('k') | src/shared/Communication.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "PluginStdAfx.h" 1 #include "PluginStdAfx.h"
2 2
3 #include "PluginSettings.h" 3 #include "PluginSettings.h"
4 #include "PluginSystem.h" 4 #include "PluginSystem.h"
5 #include "PluginFilter.h" 5 #include "PluginFilter.h"
6 #include "PluginClientFactory.h" 6 #include "PluginClientFactory.h"
7 #include "PluginMutex.h" 7 #include "PluginMutex.h"
8 #include "PluginClass.h" 8 #include "PluginClass.h"
9 9
10 #include "AdblockPlusClient.h" 10 #include "AdblockPlusClient.h"
(...skipping 20 matching lines...) Expand all
31 0, 0, 0, 0, 0, 0, &startupInfo, &processInformatio n)) 31 0, 0, 0, 0, 0, 0, &startupInfo, &processInformatio n))
32 { 32 {
33 DWORD error = GetLastError(); 33 DWORD error = GetLastError();
34 throw std::runtime_error("Failed to start Adblock Plus Engine"); 34 throw std::runtime_error("Failed to start Adblock Plus Engine");
35 } 35 }
36 36
37 CloseHandle(processInformation.hProcess); 37 CloseHandle(processInformation.hProcess);
38 CloseHandle(processInformation.hThread); 38 CloseHandle(processInformation.hThread);
39 } 39 }
40 40
41 std::auto_ptr<Communication::Pipe> OpenAdblockPlusEnginePipe() 41 Communication::Pipe* OpenEnginePipe()
42 { 42 {
43 try 43 try
44 { 44 {
45 return std::auto_ptr<Communication::Pipe>(new Communication::Pipe(Communic ation::pipeName, Communication::Pipe::MODE_CONNECT)); 45 return new Communication::Pipe(Communication::pipeName, Communication::Pip e::MODE_CONNECT);
46 } 46 }
47 catch (Communication::PipeConnectionError e) 47 catch (Communication::PipeConnectionError e)
48 { 48 {
49 SpawnAdblockPlusEngine(); 49 SpawnAdblockPlusEngine();
50 50
51 const int step = 100; 51 const int step = 100;
52 for (int timeout = 10000; timeout > 0; timeout -= step) 52 for (int timeout = 10000; timeout > 0; timeout -= step)
53 { 53 {
54 Sleep(step); 54 Sleep(step);
55 try 55 try
56 { 56 {
57 return std::auto_ptr<Communication::Pipe>(new Communication::Pipe(Comm unication::pipeName, Communication::Pipe::MODE_CONNECT)); 57 return new Communication::Pipe(Communication::pipeName, Communication: :Pipe::MODE_CONNECT);
58 } 58 }
59 catch (Communication::PipeConnectionError e) 59 catch (Communication::PipeConnectionError e)
60 { 60 {
61 } 61 }
62 } 62 }
63 throw std::runtime_error("Unable to open Adblock Plus Engine pipe"); 63 throw std::runtime_error("Unable to open Adblock Plus Engine pipe");
64 } 64 }
65 } 65 }
66 66
67 std::vector<std::wstring> ReadStrings(Communication::InputBuffer& message) 67 std::vector<std::wstring> ReadStrings(Communication::InputBuffer& message)
(...skipping 27 matching lines...) Expand all
95 message >> title; 95 message >> title;
96 description.title = ToUtf16String(title); 96 description.title = ToUtf16String(title);
97 std::string specialization; 97 std::string specialization;
98 message >> specialization; 98 message >> specialization;
99 description.specialization = ToUtf16String(specialization); 99 description.specialization = ToUtf16String(specialization);
100 message >> description.listed; 100 message >> description.listed;
101 result.push_back(description); 101 result.push_back(description);
102 } 102 }
103 return result; 103 return result;
104 } 104 }
105
106 bool CallEngine(Communication::OutputBuffer& message, Communication::InputBuff er& inputBuffer = Communication::InputBuffer())
107 {
108 try
109 {
110 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe();
111 pipe->WriteMessage(message);
112 inputBuffer = pipe->ReadMessage();
113 }
114 catch (const std::exception& e)
115 {
116 DEBUG_GENERAL(e.what());
117 return false;
118 }
119 return true;
120 }
121
122 bool CallEngine(Communication::ProcType proc, Communication::InputBuffer& inpu tBuffer = Communication::InputBuffer())
123 {
124 Communication::OutputBuffer message;
125 message << proc;
126 return CallEngine(message, inputBuffer);
127 }
128 } 105 }
129 106
130 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 107 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
131 108
132 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 109 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
133 { 110 {
134 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 111 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
135 } 112 }
136 113
114 bool CAdblockPlusClient::CallEngine(Communication::OutputBuffer& message, Commun ication::InputBuffer& inputBuffer)
115 {
116 try
117 {
118 if (!enginePipe)
119 enginePipe.reset(OpenEnginePipe());
Oleksandr 2013/08/08 06:26:31 Might it make sense to have some sort of a thread
Felix Dahlke 2013/08/08 08:32:51 Do we use a single CAdblockPlusClient instance fro
120 enginePipe->WriteMessage(message);
121 inputBuffer = enginePipe->ReadMessage();
122 }
123 catch (const std::exception& e)
124 {
125 DEBUG_GENERAL(e.what());
126 return false;
127 }
128 return true;
129 }
130
131 bool CAdblockPlusClient::CallEngine(Communication::ProcType proc, Communication: :InputBuffer& inputBuffer)
132 {
133 Communication::OutputBuffer message;
134 message << proc;
135 return CallEngine(message, inputBuffer);
136 }
137
137 CAdblockPlusClient::~CAdblockPlusClient() 138 CAdblockPlusClient::~CAdblockPlusClient()
138 { 139 {
139 s_instance = NULL; 140 s_instance = NULL;
140 } 141 }
141 142
142 143
143 CAdblockPlusClient* CAdblockPlusClient::GetInstance() 144 CAdblockPlusClient* CAdblockPlusClient::GetInstance()
144 { 145 {
145 CAdblockPlusClient* instance = NULL; 146 CAdblockPlusClient* instance = NULL;
146 147
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 426
426 std::wstring CAdblockPlusClient::GetDocumentationLink() 427 std::wstring CAdblockPlusClient::GetDocumentationLink()
427 { 428 {
428 Communication::InputBuffer response; 429 Communication::InputBuffer response;
429 if (!CallEngine(Communication::PROC_GET_DOCUMENTATION_LINK, response)) 430 if (!CallEngine(Communication::PROC_GET_DOCUMENTATION_LINK, response))
430 return L""; 431 return L"";
431 std::wstring docLink; 432 std::wstring docLink;
432 response >> docLink; 433 response >> docLink;
433 return docLink; 434 return docLink;
434 } 435 }
OLDNEW
« no previous file with comments | « src/plugin/AdblockPlusClient.h ('k') | src/shared/Communication.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld