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

Side by Side Diff: Shared/AdblockPlusClient.cpp

Issue 9998007: Initial libadblockplus integration (Closed)
Patch Set: Created April 1, 2013, 9:55 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
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 "PluginDictionary.h" 7 #include "PluginDictionary.h"
8 #include "PluginHttpRequest.h" 8 #include "PluginHttpRequest.h"
9 #include "PluginMutex.h" 9 #include "PluginMutex.h"
10 #include "PluginClass.h" 10 #include "PluginClass.h"
11 11
12 #include "AdblockPlusClient.h" 12 #include "AdblockPlusClient.h"
13 13
14 14
15 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 15 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
16 16
17 17
18 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 18 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
19 { 19 {
20 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 20 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
21 fileReader = std::auto_ptr<LibFileReader>(new LibFileReader());
22 errorCallback = std::auto_ptr<CerrErrorCallback>(new CerrErrorCallback());
23 jsEngine = std::auto_ptr<AdblockPlus::JsEngine>(new AdblockPlus::JsEngine(file Reader.get(), errorCallback.get()));
24 filterEngine = std::auto_ptr<AdblockPlus::FilterEngine>(new AdblockPlus::Filte rEngine(*jsEngine));
21 } 25 }
22 CAdblockPlusClient::~CAdblockPlusClient() 26 CAdblockPlusClient::~CAdblockPlusClient()
23 { 27 {
24 s_instance = NULL; 28 s_instance = NULL;
25 } 29 }
26 30
27 31
28 CAdblockPlusClient* CAdblockPlusClient::GetInstance() 32 CAdblockPlusClient* CAdblockPlusClient::GetInstance()
29 { 33 {
30 CAdblockPlusClient* instance = NULL; 34 CAdblockPlusClient* instance = NULL;
31 35
32 s_criticalSectionLocal.Lock(); 36 s_criticalSectionLocal.Lock();
33 { 37 {
34 if (!s_instance) 38 if (!s_instance)
35 { 39 {
36 CAdblockPlusClient* client = new CAdblockPlusClient(); 40 CAdblockPlusClient* client = new CAdblockPlusClient();
37 41
38 s_instance = client; 42 s_instance = client;
39 } 43 }
40 44
41 instance = s_instance; 45 instance = s_instance;
42 } 46 }
43 s_criticalSectionLocal.Unlock(); 47 s_criticalSectionLocal.Unlock();
44 48
45 return instance; 49 return instance;
46 } 50 }
47 51
52 bool CAdblockPlusClient::LoadFilters()
53 {
54 return m_filter->LoadHideFilters(filterEngine->GetElementHidingRules());
55 }
56
57 AdblockPlus::FilterEngine* CAdblockPlusClient::GetFilterEngine()
58 {
59 return filterEngine.get();
60 }
48 61
49 bool CAdblockPlusClient::ShouldBlock(CString src, int contentType, const CString & domain, bool addDebug) 62 bool CAdblockPlusClient::ShouldBlock(CString src, int contentType, const CString & domain, bool addDebug)
50 { 63 {
51 bool isBlocked = false; 64 bool isBlocked = false;
52 65
53 bool isCached = false; 66 bool isCached = false;
54 67
55 CPluginSettings* settings = CPluginSettings::GetInstance(); 68 CPluginSettings* settings = CPluginSettings::GetInstance();
56 69
57 m_criticalSectionCache.Lock(); 70 m_criticalSectionCache.Lock();
(...skipping 25 matching lines...) Expand all
83 m_cacheBlockedSources[src] = isBlocked; 96 m_cacheBlockedSources[src] = isBlocked;
84 } 97 }
85 m_criticalSectionCache.Unlock(); 98 m_criticalSectionCache.Unlock();
86 } 99 }
87 } 100 }
88 101
89 102
90 return isBlocked; 103 return isBlocked;
91 } 104 }
92 105
93 void CAdblockPlusClient::RequestFilterDownload(const CString& filter, const CStr ing& filterPath)
94 {
95 DEBUG_GENERAL(L"*** Requesting filter download:" + filter)
96
97 m_criticalSectionFilter.Lock();
98 {
99 m_filterDownloads.insert(std::make_pair(filter, filterPath));
100 }
101 m_criticalSectionFilter.Unlock();
102 }
103
104
105 bool CAdblockPlusClient::DownloadFirstMissingFilter()
106 {
107 bool isDownloaded = false;
108
109 CString filterFilename;
110 CString filterDownloadPath;
111
112 m_criticalSectionFilter.Lock();
113 {
114 TFilterFileList::iterator it = m_filterDownloads.begin();
115 if (it != m_filterDownloads.end())
116 {
117 filterFilename = it->first;
118 filterDownloadPath = it->second;
119
120 m_filterDownloads.erase(it);
121 }
122 }
123 m_criticalSectionFilter.Unlock();
124
125 if (!filterFilename.IsEmpty() && m_filter->DownloadFilterFile(filterDownloadPa th, filterFilename))
126 {
127 isDownloaded = true;
128
129 CPluginSettings* settings = CPluginSettings::GetInstance();
130
131 settings->IncrementTabVersion(SETTING_TAB_FILTER_VERSION);
132 }
133
134 return isDownloaded;
135 }
136
137
138 //in this method we read the filter that are in the persistent storage
139 //then we read them and use these to create a new filterclass
140
141 void CAdblockPlusClient::ReadFilters()
142 {
143 CPluginSettings* settings = CPluginSettings::GetInstance();
144
145 // Check existence of filter file
146 // if (settings->IsMainProcess())
147 // {
148 // CPluginFilter::CreateFilters();
149 // }
150
151 TFilterFileList filterFileNames;
152
153 TFilterUrlList filters = settings->GetFilterUrlList();
154 std::map<CString, CString> filterFileNameList = settings->GetFilterFileNamesLi st();
155
156 // Remember first entry in the map, is the filename, second is the version of the filter
157 for (TFilterUrlList::iterator it = filters.begin(); it != filters.end(); ++it)
158 {
159 DEBUG_FILTER(L"Filter::ReadFilters - adding url:" + it->first)
160
161 CString filename = "";
162 if (filterFileNameList.find(it->first) != filterFileNameList.end() )
163 {
164 filename = filterFileNameList.find(it->first)->second;
165 }
166 else
167 {
168 filename = it->first.Right(it->first.GetLength() - it->first.ReverseFind(' /') - 1);
169 }
170 filterFileNames.insert(std::make_pair(filename, it->first));
171 }
172
173 // Create our filter class which can be used from now on
174 std::auto_ptr<CPluginFilter> filter = std::auto_ptr<CPluginFilter>(new CPlugin Filter(filterFileNames, CPluginSettings::GetDataPath()));
175
176 m_criticalSectionFilter.Lock();
177 {
178 m_filter = filter;
179 }
180 m_criticalSectionFilter.Unlock();
181
182 ClearWhiteListCache();
183 }
184
185
186 bool CAdblockPlusClient::IsElementHidden(const CString& tag, IHTMLElement* pEl, const CString& domain, const CString& indent) 106 bool CAdblockPlusClient::IsElementHidden(const CString& tag, IHTMLElement* pEl, const CString& domain, const CString& indent)
187 { 107 {
188 bool isHidden; 108 bool isHidden;
189 m_criticalSectionFilter.Lock(); 109 m_criticalSectionFilter.Lock();
190 { 110 {
191 isHidden = m_filter.get() && m_filter->IsElementHidden(tag, pEl, domain, ind ent); 111 isHidden = m_filter.get() && m_filter->IsElementHidden(tag, pEl, domain, ind ent);
192 } 112 }
193 m_criticalSectionFilter.Unlock(); 113 m_criticalSectionFilter.Unlock();
194 return isHidden; 114 return isHidden;
195 } 115 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 BYTE version[50]; 147 BYTE version[50];
228 cbData = 50; 148 cbData = 50;
229 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 149 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
230 if (status != 0) 150 if (status != 0)
231 { 151 {
232 return 0; 152 return 0;
233 } 153 }
234 RegCloseKey(hKey); 154 RegCloseKey(hKey);
235 return (int)(version[0] - 48); 155 return (int)(version[0] - 48);
236 } 156 }
OLDNEW

Powered by Google App Engine
This is Rietveld