1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "proxy_resolver_v8_wrapper.h"
18 #include "proxy_resolver_v8.h"
19
20 using namespace net;
21
22 // Implement C interface of ProxyResolverV8
23 extern "C" {
24
ProxyResolverV8Handle_new()25 ProxyResolverV8Handle* ProxyResolverV8Handle_new() {
26 ProxyResolverV8* proxyResolver = new ProxyResolverV8(ProxyResolverJSBindings::CreateDefault());
27 return reinterpret_cast<ProxyResolverV8Handle*>(proxyResolver);
28 }
29
ProxyResolverV8Handle_GetProxyForURL(ProxyResolverV8Handle * handle,const char16_t * spec,const char16_t * host)30 char16_t* ProxyResolverV8Handle_GetProxyForURL(ProxyResolverV8Handle* handle,
31 const char16_t* spec,
32 const char16_t* host) {
33 ProxyResolverV8* proxyResolver = reinterpret_cast<ProxyResolverV8*>(handle);
34 std::u16string specStr(spec);
35 std::u16string hostStr(host);
36 std::u16string proxies;
37 int code = proxyResolver->GetProxyForURL(specStr, hostStr, &proxies);
38 if (code != OK) {
39 return NULL;
40 }
41 auto len = proxies.length();
42 char16_t* result = (char16_t*) malloc(sizeof(char16_t) * (len + 1));
43 if (result == 0) { // Failed to allocate
44 return NULL;
45 }
46 proxies.copy(result, len);
47 result[len] = u'\0';
48 return result;
49 }
50
ProxyResolverV8Handle_SetPacScript(ProxyResolverV8Handle * handle,const char16_t * script_data)51 int ProxyResolverV8Handle_SetPacScript(ProxyResolverV8Handle* handle,
52 const char16_t* script_data) {
53 ProxyResolverV8* proxyResolver = reinterpret_cast<ProxyResolverV8*>(handle);
54 std::u16string script(script_data);
55 return proxyResolver->SetPacScript(script);
56 }
57
ProxyResolverV8Handle_delete(ProxyResolverV8Handle * handle)58 void ProxyResolverV8Handle_delete(ProxyResolverV8Handle* handle) {
59 ProxyResolverV8* proxyResolver = reinterpret_cast<ProxyResolverV8*>(handle);
60 delete proxyResolver;
61 }
62
63 } // extern "C"