/* * Copyright 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package androidx.webkit; import android.annotation.SuppressLint; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; import java.util.List; import java.util.Objects; /** * Holds user-agent metadata information and uses to generate user-agent client * hints. *
* This class is functionally equivalent to
* UADataValues.
*/
public final class UserAgentMetadata {
/**
* Use this value for bitness to use the platform's default bitness value, which is an empty
* string for Android WebView.
*/
public static final int BITNESS_DEFAULT = 0;
private final List
* @see Builder#setBrandVersionList
*
*/
@SuppressLint("NullableCollection")
@Nullable
public List
* @see Builder#setFullVersion
*
*/
@Nullable
public String getFullVersion() {
return mFullVersion;
}
/**
* Returns the value for the {@code sec-ch-ua-platform} client hint.
*
* @see Builder#setPlatform
*
*/
@Nullable
public String getPlatform() {
return mPlatform;
}
/**
* Returns the value for the {@code sec-ch-ua-platform-version} client hint.
*
* @see Builder#setPlatformVersion
*
* @return Platform version string.
*/
@Nullable
public String getPlatformVersion() {
return mPlatformVersion;
}
/**
* Returns the value for the {@code sec-ch-ua-arch} client hint.
*
* @see Builder#setArchitecture
*
*/
@Nullable
public String getArchitecture() {
return mArchitecture;
}
/**
* Returns the value for the {@code sec-ch-ua-model} client hint.
*
* @see Builder#setModel
*
*/
@Nullable
public String getModel() {
return mModel;
}
/**
* Returns the value for the {@code sec-ch-ua-mobile} client hint.
*
* @see Builder#setMobile
*
* @return A boolean indicates user-agent's device mobileness.
*/
public boolean isMobile() {
return mMobile;
}
/**
* Returns the value for the {@code sec-ch-ua-bitness} client hint.
*
* @see Builder#setBitness
*
* @return An integer indicates the CPU bitness, the integer value will convert to string
* when generating the user-agent client hint, and {@link UserAgentMetadata#BITNESS_DEFAULT}
* means an empty string.
*/
public int getBitness() {
return mBitness;
}
/**
* Returns the value for the {@code sec-ch-ua-wow64} client hint.
*
* @see Builder#setWow64
*
* @return A boolean to indicate whether user-agent's binary is running in 32-bit mode on
* 64-bit Windows.
*/
public boolean isWow64() {
return mWow64;
}
/**
* Two UserAgentMetadata objects are equal only if all the metadata values are equal.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UserAgentMetadata)) return false;
UserAgentMetadata that = (UserAgentMetadata) o;
return mMobile == that.mMobile && mBitness == that.mBitness && mWow64 == that.mWow64
&& Objects.equals(mBrandVersionList, that.mBrandVersionList)
&& Objects.equals(mFullVersion, that.mFullVersion)
&& Objects.equals(mPlatform, that.mPlatform) && Objects.equals(
mPlatformVersion, that.mPlatformVersion) && Objects.equals(mArchitecture,
that.mArchitecture) && Objects.equals(mModel, that.mModel);
}
@Override
public int hashCode() {
return Objects.hash(mBrandVersionList, mFullVersion, mPlatform, mPlatformVersion,
mArchitecture, mModel, mMobile, mBitness, mWow64);
}
/**
* Class that holds brand name, major version and full version. Brand name and major version
* used to generated user-agent client hint {@code sec-cu-ua}. Brand name and full version
* used to generated user-agent client hint {@code sec-ch-ua-full-version-list}.
*
* This class is functionally equivalent to
* NavigatorUABrandVersion.
*
*/
public static final class BrandVersion {
private final String mBrand;
private final String mMajorVersion;
private final String mFullVersion;
public BrandVersion(@NonNull String brand, @NonNull String majorVersion,
@NonNull String fullVersion) {
if (brand.trim().isEmpty() || majorVersion.trim().isEmpty()
|| fullVersion.trim().isEmpty()) {
throw new IllegalArgumentException("Brand name, major version and full version "
+ "should not be blank.");
}
mBrand = brand;
mMajorVersion = majorVersion;
mFullVersion = fullVersion;
}
/**
* Returns the brand of user-agent brand version tuple.
*
*/
@NonNull
public String getBrand() {
return mBrand;
}
/**
* Returns the major version of user-agent brand version tuple.
*
*/
@NonNull
public String getMajorVersion() {
return mMajorVersion;
}
/**
* Returns the full version of user-agent brand version tuple.
*
*/
@NonNull
public String getFullVersion() {
return mFullVersion;
}
@NonNull
@Override
public String toString() {
return mBrand + "," + mMajorVersion + "," + mFullVersion;
}
/**
* Two BrandVersion objects are equal only if brand name, major version and full version
* are equal.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BrandVersion)) return false;
BrandVersion that = (BrandVersion) o;
return Objects.equals(mBrand, that.mBrand) && Objects.equals(mMajorVersion,
that.mMajorVersion) && Objects.equals(mFullVersion, that.mFullVersion);
}
@Override
public int hashCode() {
return Objects.hash(mBrand, mMajorVersion, mFullVersion);
}
}
/**
* Builder used to create {@link UserAgentMetadata} objects.
*
* Examples:
*
* // Create a setting with default options.
* new UserAgentMetadata.Builder().build();
*
* // Create a setting with a brand version contains brand name: myBrand, major version: 100,
* // full version: 100.1.1.1.
* new UserAgentMetadata.Builder().setBrandVersionList(
* Collections.singletonList(new BrandVersion("myBrand", "100", "100.1.1.1"))).build();
*
* // Create a setting brand version, platform, platform version and bitness.
* new UserAgentMetadata.Builder().setBrandVersionList(
* Collections.singletonList(new BrandVersion("myBrand", "100", "100.1.1.1")))
* .setPlatform("myPlatform")
* .setPlatform("1.1.1.1")
* .setBitness(BITNESS_64)
* .build();
*
*/
public static final class Builder {
private List