Fix for Geolocation webTCT failures
[platform/framework/web/chromium-efl.git] / url / url_idna_icu_alternatives_android.cc
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <string.h>
6
7 #include <string>
8 #include <string_view>
9
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_string.h"
12 #include "url/url_canon_internal.h"
13 #include "url/url_jni_headers/IDNStringUtil_jni.h"
14
15 using base::android::ScopedJavaLocalRef;
16
17 namespace url {
18
19 // This uses the JDK's conversion function, which uses IDNA 2003, unlike the
20 // ICU implementation.
21 bool IDNToASCII(std::u16string_view src, CanonOutputW* output) {
22   DCHECK_EQ(0u, output->length());  // Output buffer is assumed empty.
23
24   JNIEnv* env = base::android::AttachCurrentThread();
25   base::android::ScopedJavaLocalRef<jstring> java_src =
26       base::android::ConvertUTF16ToJavaString(env, src);
27   ScopedJavaLocalRef<jstring> java_result =
28       android::Java_IDNStringUtil_idnToASCII(env, java_src);
29   // NULL indicates failure.
30   if (java_result.is_null())
31     return false;
32
33   std::u16string utf16_result =
34       base::android::ConvertJavaStringToUTF16(java_result);
35   output->Append(utf16_result.data(), utf16_result.size());
36   return true;
37 }
38
39 }  // namespace url