1 /* 2 * Copyright (C) 2015 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 package com.android.statementservice.retriever; 18 19 import android.annotation.NonNull; 20 import android.net.Network; 21 22 import com.android.statementservice.network.retriever.StatementRetriever; 23 24 import kotlin.coroutines.Continuation; 25 26 /** 27 * An immutable value type representing a statement, consisting of a source, target, and relation. 28 * This reflects an assertion that the relation holds for the source, target pair. For example, if a 29 * web site has the following in its assetlinks.json file: 30 * 31 * <pre> 32 * { 33 * "relation": ["delegate_permission/common.handle_all_urls"], 34 * "target" : {"namespace": "android_app", "package_name": "com.example.app", 35 * "sha256_cert_fingerprints": ["00:11:22:33"] } 36 * } 37 * </pre> 38 * 39 * Then invoking {@link StatementRetriever#retrieve(AbstractAsset, Network, Continuation)} will 40 * return a {@link Statement} with {@link #getSource} equal to the input parameter, 41 * {@link #getRelation} equal to 42 * 43 * <pre>Relation.create("delegate_permission", "common.get_login_creds");</pre> 44 * 45 * and with {@link #getTarget} equal to 46 * 47 * <pre>AbstractAsset.create("{\"namespace\" : \"android_app\"," 48 * + "\"package_name\": \"com.example.app\"}" 49 * + "\"sha256_cert_fingerprints\": \"[\"00:11:22:33\"]\"}"); 50 * </pre> 51 */ 52 public final class Statement { 53 54 private final AbstractAsset mTarget; 55 private final Relation mRelation; 56 private final AbstractAsset mSource; 57 Statement(AbstractAsset source, AbstractAsset target, Relation relation)58 private Statement(AbstractAsset source, AbstractAsset target, Relation relation) { 59 mSource = source; 60 mTarget = target; 61 mRelation = relation; 62 } 63 64 /** 65 * Returns the source asset of the statement. 66 */ 67 @NonNull getSource()68 public AbstractAsset getSource() { 69 return mSource; 70 } 71 72 /** 73 * Returns the target asset of the statement. 74 */ 75 @NonNull getTarget()76 public AbstractAsset getTarget() { 77 return mTarget; 78 } 79 80 /** 81 * Returns the relation of the statement. 82 */ 83 @NonNull getRelation()84 public Relation getRelation() { 85 return mRelation; 86 } 87 88 /** 89 * Creates a new Statement object for the specified target asset and relation. For example: 90 * <pre> 91 * Asset asset = Asset.Factory.create( 92 * "{\"namespace\" : \"web\",\"site\": \"https://www.test.com\"}"); 93 * Relation relation = Relation.create("delegate_permission", "common.get_login_creds"); 94 * Statement statement = Statement.create(asset, relation); 95 * </pre> 96 */ create(@onNull AbstractAsset source, @NonNull AbstractAsset target, @NonNull Relation relation)97 public static Statement create(@NonNull AbstractAsset source, @NonNull AbstractAsset target, 98 @NonNull Relation relation) { 99 return new Statement(source, target, relation); 100 } 101 102 @Override equals(Object o)103 public boolean equals(Object o) { 104 if (this == o) { 105 return true; 106 } 107 if (o == null || getClass() != o.getClass()) { 108 return false; 109 } 110 111 Statement statement = (Statement) o; 112 113 if (!mRelation.equals(statement.mRelation)) { 114 return false; 115 } 116 if (!mTarget.equals(statement.mTarget)) { 117 return false; 118 } 119 if (!mSource.equals(statement.mSource)) { 120 return false; 121 } 122 123 return true; 124 } 125 126 @Override hashCode()127 public int hashCode() { 128 int result = mTarget.hashCode(); 129 result = 31 * result + mRelation.hashCode(); 130 result = 31 * result + mSource.hashCode(); 131 return result; 132 } 133 134 @Override toString()135 public String toString() { 136 StringBuilder statement = new StringBuilder(); 137 statement.append("Statement: "); 138 statement.append(mSource); 139 statement.append(", "); 140 statement.append(mTarget); 141 statement.append(", "); 142 statement.append(mRelation); 143 return statement.toString(); 144 } 145 } 146