1 // Copyright 2019 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 "url/origin.h"
6
7 #include <cstdint>
8
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/memory/ptr_util.h"
13 #include "url/android/gurl_android.h"
14 #include "url/url_jni_headers/Origin_jni.h"
15
16 namespace url {
17
CreateJavaObject() const18 base::android::ScopedJavaLocalRef<jobject> Origin::CreateJavaObject() const {
19 JNIEnv* env = base::android::AttachCurrentThread();
20 const base::UnguessableToken* token = Origin::GetNonceForSerialization();
21 return Java_Origin_Constructor(
22 env, base::android::ConvertUTF8ToJavaString(env, tuple_.scheme()),
23 base::android::ConvertUTF8ToJavaString(env, tuple_.host()), tuple_.port(),
24 opaque(), token ? token->GetHighForSerialization() : 0,
25 token ? token->GetLowForSerialization() : 0);
26 }
27
28 // static
FromJavaObject(const base::android::JavaRef<jobject> & java_origin)29 Origin Origin::FromJavaObject(
30 const base::android::JavaRef<jobject>& java_origin) {
31 JNIEnv* env = base::android::AttachCurrentThread();
32 std::unique_ptr<Origin> origin = base::WrapUnique<Origin>(
33 reinterpret_cast<Origin*>(Java_Origin_toNativeOrigin(env, java_origin)));
34 return std::move(*origin);
35 }
36
37 // static
CreateNative(JNIEnv * env,const base::android::JavaRef<jstring> & java_scheme,const base::android::JavaRef<jstring> & java_host,uint16_t port,bool is_opaque,uint64_t token_high_bits,uint64_t token_low_bits)38 jlong Origin::CreateNative(JNIEnv* env,
39 const base::android::JavaRef<jstring>& java_scheme,
40 const base::android::JavaRef<jstring>& java_host,
41 uint16_t port,
42 bool is_opaque,
43 uint64_t token_high_bits,
44 uint64_t token_low_bits) {
45 const std::string& scheme = ConvertJavaStringToUTF8(env, java_scheme);
46 const std::string& host = ConvertJavaStringToUTF8(env, java_host);
47
48 absl::optional<base::UnguessableToken> nonce_token =
49 base::UnguessableToken::Deserialize(token_high_bits, token_low_bits);
50 bool has_nonce = nonce_token.has_value();
51 CHECK(has_nonce == is_opaque);
52 Origin::Nonce nonce;
53 if (has_nonce) {
54 nonce = Origin::Nonce(nonce_token.value());
55 }
56 Origin origin = is_opaque
57 ? Origin::CreateOpaqueFromNormalizedPrecursorTuple(
58 scheme, host, port, nonce)
59 : Origin::CreateFromNormalizedTuple(scheme, host, port);
60 return reinterpret_cast<intptr_t>(new Origin(origin));
61 }
62
JNI_Origin_CreateOpaque(JNIEnv * env)63 static base::android::ScopedJavaLocalRef<jobject> JNI_Origin_CreateOpaque(
64 JNIEnv* env) {
65 return Origin().CreateJavaObject();
66 }
67
JNI_Origin_CreateFromGURL(JNIEnv * env,const base::android::JavaParamRef<jobject> & j_gurl)68 static base::android::ScopedJavaLocalRef<jobject> JNI_Origin_CreateFromGURL(
69 JNIEnv* env,
70 const base::android::JavaParamRef<jobject>& j_gurl) {
71 return Origin::Create(*GURLAndroid::ToNativeGURL(env, j_gurl))
72 .CreateJavaObject();
73 }
74
JNI_Origin_CreateNative(JNIEnv * env,const base::android::JavaParamRef<jstring> & java_scheme,const base::android::JavaParamRef<jstring> & java_host,jshort port,jboolean is_opaque,jlong token_high_bits,jlong token_low_bits)75 static jlong JNI_Origin_CreateNative(
76 JNIEnv* env,
77 const base::android::JavaParamRef<jstring>& java_scheme,
78 const base::android::JavaParamRef<jstring>& java_host,
79 jshort port,
80 jboolean is_opaque,
81 jlong token_high_bits,
82 jlong token_low_bits) {
83 return Origin::CreateNative(env, java_scheme, java_host, port, is_opaque,
84 token_high_bits, token_low_bits);
85 }
86
87 } // namespace url
88