OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef ADBLOCK_PLUS_UTILS_H | 18 #ifndef ADBLOCK_PLUS_UTILS_H |
19 #define ADBLOCK_PLUS_UTILS_H | 19 #define ADBLOCK_PLUS_UTILS_H |
20 | 20 |
21 #include <algorithm> | 21 #include <algorithm> |
22 #include <cctype> | 22 #include <cctype> |
23 #include <functional> | 23 #include <functional> |
24 #include <string> | 24 #include <string> |
25 #include <vector> | 25 #include <vector> |
26 #include <v8.h> | 26 #include <v8.h> |
27 | 27 |
28 #include "AdblockPlus/JsValue.h" | 28 #include "AdblockPlus/JsValue.h" |
29 | 29 |
| 30 #include "JsError.h" |
| 31 |
30 namespace AdblockPlus | 32 namespace AdblockPlus |
31 { | 33 { |
32 namespace Utils | 34 namespace Utils |
33 { | 35 { |
| 36 void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch); |
| 37 |
| 38 /* |
| 39 * Check for exception and then that a MaybeLocal<> isn't empty, |
| 40 * and throw a JsError if it is, otherwise return the Local<> |
| 41 * Call using the macro %CHECKED_MAYBE to get the location. |
| 42 */ |
| 43 template<class T> |
| 44 v8::Local<T> CheckedToLocal(v8::Isolate* isolate, |
| 45 v8::MaybeLocal<T>& value, const v8::TryCatch& tryCatch, |
| 46 const char* filename, int line) |
| 47 { |
| 48 CheckTryCatch(isolate, tryCatch); |
| 49 if (value.IsEmpty()) |
| 50 throw AdblockPlus::JsError("Empty value at ", filename, line); |
| 51 return value.ToLocalChecked(); |
| 52 } |
| 53 |
| 54 #define CHECKED_TO_LOCAL(isolate, value, tryCatch) \ |
| 55 AdblockPlus::Utils::CheckedToLocal(isolate, value, tryCatch, __FILE__, __LIN
E__) |
| 56 |
34 std::string FromV8String(v8::Isolate* isolate, const v8::Local<v8::Value>& v
alue); | 57 std::string FromV8String(v8::Isolate* isolate, const v8::Local<v8::Value>& v
alue); |
35 StringBuffer StringBufferFromV8String(v8::Isolate* isolate, const v8::Local<
v8::Value>& value); | 58 StringBuffer StringBufferFromV8String(v8::Isolate* isolate, const v8::Local<
v8::Value>& value); |
36 v8::Local<v8::String> ToV8String(v8::Isolate* isolate, const std::string& st
r); | 59 v8::Local<v8::String> ToV8String(v8::Isolate* isolate, const std::string& st
r); |
37 v8::Local<v8::String> StringBufferToV8String(v8::Isolate* isolate, const Str
ingBuffer& bytes); | 60 v8::Local<v8::String> StringBufferToV8String(v8::Isolate* isolate, const Str
ingBuffer& bytes); |
38 void ThrowExceptionInJS(v8::Isolate* isolate, const std::string& str); | 61 void ThrowExceptionInJS(v8::Isolate* isolate, const std::string& str); |
39 | 62 |
40 // Code for templated function has to be in a header file, can't be in .cpp | 63 // Code for templated function has to be in a header file, can't be in .cpp |
41 template<class T> | 64 template<class T> |
42 T TrimString(const T& text) | 65 T TrimString(const T& text) |
43 { | 66 { |
44 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-tri
m-stdstring | 67 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-tri
m-stdstring |
45 T trimmed(text); | 68 T trimmed(text); |
46 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end()
, std::not1(std::ptr_fun<int, int>(std::isspace)))); | 69 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end()
, std::not1(std::ptr_fun<int, int>(std::isspace)))); |
47 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std
::ptr_fun<int, int>(std::isspace))).base(), trimmed.end()); | 70 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std
::ptr_fun<int, int>(std::isspace))).base(), trimmed.end()); |
48 return trimmed; | 71 return trimmed; |
49 } | 72 } |
50 #ifdef _WIN32 | 73 #ifdef _WIN32 |
51 std::wstring ToUtf16String(const std::string& str); | 74 std::wstring ToUtf16String(const std::string& str); |
52 std::string ToUtf8String(const std::wstring& str); | 75 std::string ToUtf8String(const std::wstring& str); |
53 std::wstring CanonizeUrl(const std::wstring& url); | 76 std::wstring CanonizeUrl(const std::wstring& url); |
54 #endif | 77 #endif |
55 } | 78 } |
56 } | 79 } |
57 | 80 |
58 #endif | 81 #endif |
OLD | NEW |