1 /* 2 * Copyright 2017 The gRPC Authors 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 io.grpc.cronet; 18 19 import io.grpc.CallOptions; 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.Collections; 23 24 /** Call options for use with the Cronet transport. */ 25 public final class CronetCallOptions { CronetCallOptions()26 private CronetCallOptions() {} 27 28 /** 29 * Used for attaching annotation objects to Cronet streams. When the stream finishes, the user can 30 * get Cronet metrics from {@link org.chromium.net.RequestFinishedInfo.Listener} with the same 31 * annotation object. 32 * 33 * <p>The Object must not be null. 34 * 35 * @deprecated Use {@link CronetCallOptions#withAnnotation} instead. 36 */ 37 @Deprecated 38 public static final CallOptions.Key<Object> CRONET_ANNOTATION_KEY = 39 CallOptions.Key.create("cronet-annotation"); 40 41 /** 42 * Returns a copy of {@code callOptions} with {@code annotation} included as one of the Cronet 43 * annotation objects. When an RPC is made using a {@link CallOptions} instance returned by this 44 * method, the annotation objects will be attached to the underlying Cronet bidirectional stream. 45 * When the stream finishes, the user can retrieve the annotation objects via {@link 46 * org.chromium.net.RequestFinishedInfo.Listener}. 47 * 48 * @param annotation the object to attach to the Cronet stream 49 */ withAnnotation(CallOptions callOptions, Object annotation)50 public static CallOptions withAnnotation(CallOptions callOptions, Object annotation) { 51 Collection<Object> existingAnnotations = callOptions.getOption(CRONET_ANNOTATIONS_KEY); 52 ArrayList<Object> newAnnotations; 53 if (existingAnnotations == null) { 54 newAnnotations = new ArrayList<>(); 55 } else { 56 newAnnotations = new ArrayList<>(existingAnnotations); 57 } 58 newAnnotations.add(annotation); 59 return callOptions.withOption( 60 CronetCallOptions.CRONET_ANNOTATIONS_KEY, Collections.unmodifiableList(newAnnotations)); 61 } 62 63 static final CallOptions.Key<Collection<Object>> CRONET_ANNOTATIONS_KEY = 64 CallOptions.Key.create("cronet-annotations"); 65 } 66