| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #include "stdafx.h" | 1 #include "stdafx.h" |
| 2 | 2 |
| 3 #include "../shared/AutoHandle.h" | 3 #include "../shared/AutoHandle.h" |
| 4 #include "../shared/Communication.h" | 4 #include "../shared/Communication.h" |
| 5 | 5 |
| 6 namespace | 6 namespace |
| 7 { | 7 { |
| 8 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; | 8 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; |
| 9 | 9 |
| 10 void Log(const std::string& message) | 10 void Log(const std::string& message) |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 | 33 |
| 34 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length , 0, 0, 0, 0); | 34 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length , 0, 0, 0, 0); |
| 35 if (utf8StringLength == 0) | 35 if (utf8StringLength == 0) |
| 36 throw std::runtime_error("Failed to determine the required buffer size"); | 36 throw std::runtime_error("Failed to determine the required buffer size"); |
| 37 | 37 |
| 38 std::string utf8String(utf8StringLength, '\0'); | 38 std::string utf8String(utf8StringLength, '\0'); |
| 39 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Str ingLength, 0, 0); | 39 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Str ingLength, 0, 0); |
| 40 return utf8String; | 40 return utf8String; |
| 41 } | 41 } |
| 42 | 42 |
| 43 void WriteStrings(Communication::OutputBuffer& response, | |
| 44 const std::vector<std::string> strings) | |
|
Felix Dahlke
2013/06/04 11:53:17
Shouldn't the strings be passed by reference? This
| |
| 45 { | |
| 46 int32_t count = strings.size(); | |
| 47 response << count; | |
| 48 for (int32_t i = 0; i < count; i++) | |
| 49 response << strings[i]; | |
| 50 } | |
| 51 | |
| 52 void WriteSubscriptions(Communication::OutputBuffer& response, | |
| 53 const std::vector<AdblockPlus::SubscriptionPtr> subscriptions) | |
|
Felix Dahlke
2013/06/04 11:53:17
Same as above, probably better to pass by referenc
| |
| 54 { | |
| 55 int32_t count = subscriptions.size(); | |
| 56 response << count; | |
| 57 for (int32_t i = 0; i < count; i++) | |
| 58 { | |
| 59 AdblockPlus::SubscriptionPtr subscription = subscriptions[i]; | |
| 60 response << subscription->GetProperty("url")->AsString() | |
| 61 << subscription->GetProperty("title")->AsString() | |
| 62 << subscription->GetProperty("specialization")->AsString() | |
| 63 << subscription->IsListed(); | |
| 64 } | |
| 65 } | |
| 66 | |
| 43 Communication::OutputBuffer HandleRequest(Communication::InputBuffer& request) | 67 Communication::OutputBuffer HandleRequest(Communication::InputBuffer& request) |
| 44 { | 68 { |
| 45 Communication::OutputBuffer response; | 69 Communication::OutputBuffer response; |
| 46 | 70 |
| 47 std::string procedureName; | 71 std::string procedureName; |
| 48 request >> procedureName; | 72 request >> procedureName; |
| 49 if (procedureName == "Matches") | 73 if (procedureName == "Matches") |
| 50 { | 74 { |
| 51 std::string url; | 75 std::string url; |
| 52 std::string type; | 76 std::string type; |
| 53 std::string documentUrl; | 77 std::string documentUrl; |
| 54 request >> url >> type >> documentUrl; | 78 request >> url >> type >> documentUrl; |
| 55 response << filterEngine->Matches(url, type, documentUrl); | 79 response << filterEngine->Matches(url, type, documentUrl); |
| 56 } | 80 } |
| 57 if (procedureName == "GetElementHidingSelectors") | 81 else if (procedureName == "GetElementHidingSelectors") |
| 58 { | 82 { |
| 59 std::string domain; | 83 std::string domain; |
| 60 request >> domain; | 84 request >> domain; |
| 85 WriteStrings(response, filterEngine->GetElementHidingSelectors(domain)); | |
| 86 } | |
| 87 else if (procedureName == "FetchAvailableSubscriptions") | |
| 88 { | |
| 89 WriteSubscriptions(response, filterEngine->FetchAvailableSubscriptions()); | |
| 90 } | |
| 91 else if (procedureName == "GetListedSubscriptions") | |
| 92 { | |
| 93 WriteSubscriptions(response, filterEngine->GetListedSubscriptions()); | |
| 94 } | |
| 95 else if (procedureName == "SetSubscription") | |
| 96 { | |
| 97 std::string url; | |
| 98 request >> url; | |
| 61 | 99 |
| 62 std::vector<std::string> selectors = filterEngine->GetElementHidingSelecto rs(domain); | 100 std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine->Ge tListedSubscriptions(); |
| 101 for (size_t i = 0, count = subscriptions.size(); i < count; i++) | |
| 102 subscriptions[i]->RemoveFromList(); | |
| 63 | 103 |
| 64 int32_t length = selectors.size(); | 104 filterEngine->GetSubscription(url)->AddToList(); |
| 65 response << length; | 105 } |
| 66 for (int32_t i = 0; i < length; i++) | 106 else if (procedureName == "UpdateAllSubscriptions") |
| 67 response << selectors[i]; | 107 { |
| 108 std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine->Ge tListedSubscriptions(); | |
| 109 for (size_t i = 0, count = subscriptions.size(); i < count; i++) | |
| 110 subscriptions[i]->UpdateFilters(); | |
| 111 } | |
| 112 else if (procedureName == "GetExceptionDomains") | |
| 113 { | |
| 114 std::vector<AdblockPlus::FilterPtr> filters = filterEngine->GetListedFilte rs(); | |
| 115 std::vector<std::string> domains; | |
| 116 for (size_t i = 0, count = filters.size(); i < count; i++) | |
| 117 { | |
| 118 AdblockPlus::FilterPtr filter = filters[i]; | |
| 119 if (filter->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) | |
| 120 { | |
| 121 std::string text = filter->GetProperty("text")->AsString(); | |
| 122 | |
| 123 //@@||example.com^$document | |
| 124 const char prefix[] = "@@||"; | |
| 125 const char suffix[] = "^$document"; | |
| 126 const int prefixLen = strlen(prefix); | |
| 127 const int suffixLen = strlen(suffix); | |
| 128 if (!text.compare(0, prefixLen, prefix) && | |
| 129 !text.compare(text.size() - suffixLen, suffixLen, suffix)) | |
| 130 { | |
| 131 domains.push_back(text.substr(prefixLen, text.size() - prefixLen - s uffixLen)); | |
| 132 } | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 WriteStrings(response, domains); | |
| 137 } | |
| 138 else if (procedureName == "AddFilter") | |
| 139 { | |
| 140 std::string text; | |
| 141 request >> text; | |
| 142 | |
| 143 filterEngine->GetFilter(text)->AddToList(); | |
| 68 } | 144 } |
| 69 return response; | 145 return response; |
| 70 } | 146 } |
| 71 | 147 |
| 72 DWORD WINAPI ClientThread(LPVOID param) | 148 DWORD WINAPI ClientThread(LPVOID param) |
| 73 { | 149 { |
| 74 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa ram)); | 150 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa ram)); |
| 75 | 151 |
| 76 try | 152 try |
| 77 { | 153 { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 } | 235 } |
| 160 catch (std::runtime_error e) | 236 catch (std::runtime_error e) |
| 161 { | 237 { |
| 162 LogException(e); | 238 LogException(e); |
| 163 return 1; | 239 return 1; |
| 164 } | 240 } |
| 165 } | 241 } |
| 166 | 242 |
| 167 return 0; | 243 return 0; |
| 168 } | 244 } |
| OLD | NEW |