|
| 1 | +// Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#import <Foundation/Foundation.h> |
| 6 | + |
| 7 | +#include "bidirectional_stream_c.h" |
| 8 | +#include "cronet.idl_c.h" |
| 9 | +#include "cronet_c.h" |
| 10 | +#include "cronet_export.h" |
| 11 | + |
| 12 | +// Type of HTTP cache; public interface to private implementation defined in |
| 13 | +// URLRequestContextConfig class. |
| 14 | +typedef NS_ENUM(NSInteger, CRNHttpCacheType) { |
| 15 | + // Disabled HTTP cache. Some data may still be temporarily stored in memory. |
| 16 | + CRNHttpCacheTypeDisabled, |
| 17 | + // Enable on-disk HTTP cache, including HTTP data. |
| 18 | + CRNHttpCacheTypeDisk, |
| 19 | + // Enable in-memory cache, including HTTP data. |
| 20 | + CRNHttpCacheTypeMemory, |
| 21 | +}; |
| 22 | + |
| 23 | +/// Cronet error domain name. |
| 24 | +FOUNDATION_EXPORT GRPC_SUPPORT_EXPORT NSString* const CRNCronetErrorDomain; |
| 25 | + |
| 26 | +/// Enum of Cronet NSError codes. |
| 27 | +NS_ENUM(NSInteger){ |
| 28 | + CRNErrorInvalidArgument = 1001, CRNErrorUnsupportedConfig = 1002, |
| 29 | +}; |
| 30 | + |
| 31 | +/// The corresponding value is a String object that contains the name of |
| 32 | +/// an invalid argument inside the NSError userInfo dictionary. |
| 33 | +FOUNDATION_EXPORT GRPC_SUPPORT_EXPORT NSString* const CRNInvalidArgumentKey; |
| 34 | + |
| 35 | +// A block, that takes a request, and returns YES if the request should |
| 36 | +// be handled. |
| 37 | +typedef BOOL (^RequestFilterBlock)(NSURLRequest* request); |
| 38 | + |
| 39 | +// Interface for installing Cronet. |
| 40 | +// TODO(gcasto): Should this macro be separate from the one defined in |
| 41 | +// bidirectional_stream_c.h? |
| 42 | +GRPC_SUPPORT_EXPORT |
| 43 | +@interface Cronet : NSObject |
| 44 | + |
| 45 | +// Sets the HTTP Accept-Language header. This method only has any effect before |
| 46 | +// |start| is called. |
| 47 | ++ (void)setAcceptLanguages:(NSString*)acceptLanguages; |
| 48 | + |
| 49 | +// Sets whether HTTP/2 should be supported by CronetEngine. This method only has |
| 50 | +// any effect before |start| is called. |
| 51 | ++ (void)setHttp2Enabled:(BOOL)http2Enabled; |
| 52 | + |
| 53 | +// Sets whether QUIC should be supported by CronetEngine. This method only has |
| 54 | +// any effect before |start| is called. |
| 55 | ++ (void)setQuicEnabled:(BOOL)quicEnabled; |
| 56 | + |
| 57 | +// Sets whether Brotli should be supported by CronetEngine. This method only has |
| 58 | +// any effect before |start| is called. |
| 59 | ++ (void)setBrotliEnabled:(BOOL)brotliEnabled; |
| 60 | + |
| 61 | +// Sets whether Metrics should be collected by CronetEngine. This method only |
| 62 | +// has any effect before |start| is called. |
| 63 | ++ (void)setMetricsEnabled:(BOOL)metricsEnabled; |
| 64 | + |
| 65 | +// Set HTTP Cache type to be used by CronetEngine. This method only has any |
| 66 | +// effect before |start| is called. See HttpCacheType enum for available |
| 67 | +// options. |
| 68 | ++ (void)setHttpCacheType:(CRNHttpCacheType)httpCacheType; |
| 69 | + |
| 70 | +// Adds hint that host supports QUIC on altPort. This method only has any effect |
| 71 | +// before |start| is called. Returns NO if it fails to add hint (because the |
| 72 | +// host is invalid). |
| 73 | ++ (BOOL)addQuicHint:(NSString*)host port:(int)port altPort:(int)altPort; |
| 74 | + |
| 75 | +// Set experimental Cronet options. Argument is a JSON string; see |
| 76 | +// |URLRequestContextConfig| for more details. This method only has |
| 77 | +// any effect before |start| is called. |
| 78 | ++ (void)setExperimentalOptions:(NSString*)experimentalOptions; |
| 79 | + |
| 80 | +// Sets the User-Agent request header string to be sent with all requests. |
| 81 | +// If |partial| is set to YES, then actual user agent value is based on device |
| 82 | +// model, OS version, and |userAgent| argument. For example "Foo/3.0.0.0" is |
| 83 | +// sent as "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) |
| 84 | +// AppleWebKit/601.1 (KHTML, like Gecko) Foo/3.0.0.0 Mobile/15G31 |
| 85 | +// Safari/601.1.46". |
| 86 | +// If |partial| is set to NO, then |userAgent| value is complete value sent to |
| 87 | +// the remote. For Example: "Foo/3.0.0.0" is sent as "Foo/3.0.0.0". |
| 88 | +// |
| 89 | +// This method only has any effect before |start| is called. |
| 90 | ++ (void)setUserAgent:(NSString*)userAgent partial:(BOOL)partial; |
| 91 | + |
| 92 | +// Sets SSLKEYLogFileName to export SSL key for Wireshark decryption of packet |
| 93 | +// captures. This method only has any effect before |start| is called. |
| 94 | ++ (void)setSslKeyLogFileName:(NSString*)sslKeyLogFileName; |
| 95 | + |
| 96 | +/// Pins a set of public keys for a given host. This method only has any effect |
| 97 | +/// before |start| is called. By pinning a set of public keys, |pinHashes|, |
| 98 | +/// communication with |host| is required to authenticate with a certificate |
| 99 | +/// with a public key from the set of pinned ones. |
| 100 | +/// An app can pin the public key of the root certificate, any of the |
| 101 | +/// intermediate certificates or the end-entry certificate. Authentication will |
| 102 | +/// fail and secure communication will not be established if none of the public |
| 103 | +/// keys is present in the host's certificate chain, even if the host attempts |
| 104 | +/// to authenticate with a certificate allowed by the device's trusted store of |
| 105 | +/// certificates. |
| 106 | +/// |
| 107 | +/// Calling this method multiple times with the same host name overrides the |
| 108 | +/// previously set pins for the host. |
| 109 | +/// |
| 110 | +/// More information about the public key pinning can be found in |
| 111 | +/// [RFC 7469](https://tools.ietf.org/html/rfc7469). |
| 112 | +/// |
| 113 | +/// @param host name of the host to which the public keys should be pinned. |
| 114 | +/// A host that consists only of digits and the dot character |
| 115 | +/// is treated as invalid. |
| 116 | +/// @param pinHashes a set of pins. Each pin is the SHA-256 cryptographic |
| 117 | +/// hash of the DER-encoded ASN.1 representation of the |
| 118 | +/// Subject Public Key Info (SPKI) of the host's X.509 |
| 119 | +/// certificate. Although, the method does not mandate the |
| 120 | +/// presence of the backup pin that can be used if the control |
| 121 | +/// of the primary private key has been lost, it is highly |
| 122 | +/// recommended to supply one. |
| 123 | +/// @param includeSubdomains indicates whether the pinning policy should be |
| 124 | +/// applied to subdomains of |host|. |
| 125 | +/// @param expirationDate specifies the expiration date for the pins. |
| 126 | +/// @param outError on return, if the pin cannot be added, a pointer to an |
| 127 | +/// error object that encapsulates the reason for the error. |
| 128 | +/// @return returns |YES| if the pins were added successfully; |NO|, otherwise. |
| 129 | ++ (BOOL)addPublicKeyPinsForHost:(NSString*)host |
| 130 | + pinHashes:(NSSet<NSData*>*)pinHashes |
| 131 | + includeSubdomains:(BOOL)includeSubdomains |
| 132 | + expirationDate:(NSDate*)expirationDate |
| 133 | + error:(NSError**)outError; |
| 134 | + |
| 135 | +// Sets the block used to determine whether or not Cronet should handle the |
| 136 | +// request. If the block is not set, Cronet will handle all requests. Cronet |
| 137 | +// retains strong reference to the block, which can be released by calling this |
| 138 | +// method with nil block. |
| 139 | ++ (void)setRequestFilterBlock:(RequestFilterBlock)block; |
| 140 | + |
| 141 | +// Starts CronetEngine. It is recommended to call this method on the application |
| 142 | +// main thread. If the method is called on any thread other than the main one, |
| 143 | +// the method will internally try to execute synchronously using the main GCD |
| 144 | +// queue. Please make sure that the main thread is not blocked by a job |
| 145 | +// that calls this method; otherwise, a deadlock can occur. |
| 146 | ++ (void)start; |
| 147 | + |
| 148 | +// Registers Cronet as HttpProtocol Handler. Once registered, Cronet intercepts |
| 149 | +// and handles all requests made through NSURLConnection and shared |
| 150 | +// NSURLSession. |
| 151 | +// This method must be called after |start|. |
| 152 | ++ (void)registerHttpProtocolHandler; |
| 153 | + |
| 154 | +// Unregister Cronet as HttpProtocol Handler. This means that Cronet will stop |
| 155 | +// intercepting requests, however, it won't tear down the Cronet environment. |
| 156 | +// This method must be called after |start|. |
| 157 | ++ (void)unregisterHttpProtocolHandler; |
| 158 | + |
| 159 | +// Installs Cronet into NSURLSessionConfiguration so that all |
| 160 | +// NSURLSessions created with this configuration will use the Cronet stack. |
| 161 | +// Note that all Cronet settings are global and are shared between |
| 162 | +// all NSURLSessions & NSURLConnections that use the Cronet stack. |
| 163 | +// This method must be called after |start|. |
| 164 | ++ (void)installIntoSessionConfiguration:(NSURLSessionConfiguration*)config; |
| 165 | + |
| 166 | +// Returns the absolute path that startNetLogToFile:fileName will actually |
| 167 | +// write to. |
| 168 | ++ (NSString*)getNetLogPathForFile:(NSString*)fileName; |
| 169 | + |
| 170 | +// Starts net-internals logging to a file named |fileName|. Where fileName is |
| 171 | +// relative to the application documents directory. |fileName| must not be |
| 172 | +// empty. Log level is determined by |logBytes| - if YES then LOG_ALL otherwise |
| 173 | +// LOG_ALL_BUT_BYTES. If the file exists it is truncated before starting. If |
| 174 | +// actively logging the call is ignored. |
| 175 | ++ (BOOL)startNetLogToFile:(NSString*)fileName logBytes:(BOOL)logBytes; |
| 176 | + |
| 177 | +// Stop net-internals logging and flush file to disk. If a logging session is |
| 178 | +// not in progress this call is ignored. |
| 179 | ++ (void)stopNetLog; |
| 180 | + |
| 181 | +// Returns the full user-agent that will be used unless it is overridden on the |
| 182 | +// NSURLRequest used. |
| 183 | ++ (NSString*)getUserAgent; |
| 184 | + |
| 185 | +// Sets priority of the network thread. The |priority| should be a |
| 186 | +// floating point number between 0.0 to 1.0, where 1.0 is highest priority. |
| 187 | +// This method can be called multiple times before or after |start| method. |
| 188 | ++ (void)setNetworkThreadPriority:(double)priority; |
| 189 | + |
| 190 | +// Get a pointer to global instance of cronet_engine for GRPC C API. |
| 191 | ++ (stream_engine*)getGlobalEngine; |
| 192 | + |
| 193 | +// Returns differences in metrics collected by Cronet since the last call to |
| 194 | +// getGlobalMetricsDeltas, serialized as a [protobuf] |
| 195 | +// (https://developers.google.com/protocol-buffers). |
| 196 | +// |
| 197 | +// Cronet starts collecting these metrics after the first call to |
| 198 | +// getGlobalMetricsDeltras, so the first call returns no |
| 199 | +// useful data as no metrics have yet been collected. |
| 200 | ++ (NSData*)getGlobalMetricsDeltas; |
| 201 | + |
| 202 | +// Sets Host Resolver Rules for testing. |
| 203 | +// This method must be called after |start| has been called. |
| 204 | ++ (void)setHostResolverRulesForTesting:(NSString*)hostResolverRulesForTesting; |
| 205 | + |
| 206 | +// Enables TestCertVerifier which accepts all certificates for testing. |
| 207 | +// This method only has any effect before |start| is called. |
| 208 | ++ (void)enableTestCertVerifierForTesting; |
| 209 | + |
| 210 | +@end |
0 commit comments