Skip to content

Instantly share code, notes, and snippets.

@keepsimple1
Created January 23, 2020 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keepsimple1/525d69569f0058e4c19f260f473a4b97 to your computer and use it in GitHub Desktop.
Save keepsimple1/525d69569f0058e4c19f260f473a4b97 to your computer and use it in GitHub Desktop.
Add a new API in cronet to allow setting QUIC hint after Cronet start
diff --git a/components/cronet/ios/Cronet.h b/components/cronet/ios/Cronet.h
index 05e294f238..2b4957387c 100644
--- a/components/cronet/ios/Cronet.h
+++ b/components/cronet/ios/Cronet.h
@@ -72,6 +72,10 @@ GRPC_SUPPORT_EXPORT
// host is invalid).
+ (BOOL)addQuicHint:(NSString*)host port:(int)port altPort:(int)altPort;
+// Sets QUIC hint after |start| is called.
+// It is not clear yet that if this hint will override existing hint(s) or not.
++ (void)setQuicHint:(NSString*)host port:(int)port altPort:(int)altPort;
+
// Set experimental Cronet options. Argument is a JSON string; see
// |URLRequestContextConfig| for more details. This method only has
// any effect before |start| is called.
diff --git a/components/cronet/ios/Cronet.mm b/components/cronet/ios/Cronet.mm
index e6d0fba257..d3bdb24c6c 100644
--- a/components/cronet/ios/Cronet.mm
+++ b/components/cronet/ios/Cronet.mm
@@ -372,6 +372,11 @@ new CronetHttpProtocolHandlerDelegate(
gRequestFilterBlock = nil;
}
++ (void)setQuicHint:(NSString*)host port:(int)port altPort:(int)altPort {
+ std::string quic_host = base::SysNSStringToUTF8(host);
+ gChromeNet.Get()->SetQuicHint(quic_host, port, altPort);
+}
+
+ (void)start {
cronet::EnsureInitialized();
[self startInternal];
diff --git a/components/cronet/ios/cronet_environment.h b/components/cronet/ios/cronet_environment.h
index 3717a4ff94..15b312a074 100644
--- a/components/cronet/ios/cronet_environment.h
+++ b/components/cronet/ios/cronet_environment.h
@@ -70,6 +70,8 @@ class CronetEnvironment {
void AddQuicHint(const std::string& host, int port, int alternate_port);
+ void SetQuicHint(const std::string& host, int port, int alternate_port);
+
// Setters and getters for |http2_enabled_|, |quic_enabled_|, and
// |brotli_enabled| These only have any effect
// before Start() is called.
@@ -156,6 +158,8 @@ class CronetEnvironment {
void StartNetLogOnNetworkThread(const base::FilePath&, bool log_bytes);
void StopNetLogOnNetworkThread(base::WaitableEvent* log_stopped_event);
+ void SetQuicHintInternal(const std::string& host, int port, int alternate_port);
+
std::unique_ptr<base::DictionaryValue> GetNetLogInfo() const;
// Returns the HttpNetworkSession object from the passed in
diff --git a/components/cronet/ios/cronet_environment.mm b/components/cronet/ios/cronet_environment.mm
index 3ec7cda2fb..31295446e1 100644
--- a/components/cronet/ios/cronet_environment.mm
+++ b/components/cronet/ios/cronet_environment.mm
@@ -419,6 +419,32 @@ bool IsNetLogPathValid(const base::FilePath& path) {
}
}
+void CronetEnvironment::SetQuicHint(const std::string& host, int port, int alternate_port) {
+ PostToNetworkThread(FROM_HERE,
+ base::Bind(&CronetEnvironment::SetQuicHintInternal,
+ base::Unretained(this), host, port, alternate_port));
+}
+
+void CronetEnvironment::SetQuicHintInternal(const std::string& host, int port, int alternate_port) {
+ net::HostPortPair quic_hint(host, port);
+ url::CanonHostInfo host_info;
+ std::string canon_host(net::CanonicalizeHost(quic_hint.host(), &host_info));
+ if (!host_info.IsIPAddress() &&
+ !net::IsCanonicalizedHostCompliant(canon_host)) {
+ LOG(ERROR) << "Invalid QUIC hint host: " << quic_hint.host();
+ return;
+ }
+
+ net::AlternativeService alternative_service(net::kProtoQUIC, "",
+ quic_hint.port());
+
+ url::SchemeHostPort quic_hint_server("https", quic_hint.host(),
+ quic_hint.port());
+ main_context_->http_server_properties()->SetQuicAlternativeService(
+ quic_hint_server, net::NetworkIsolationKey(), alternative_service,
+ base::Time::Max(), quic::ParsedQuicVersionVector());
+}
+
void CronetEnvironment::SetNetworkThreadPriority(double priority) {
DCHECK_LE(priority, 1.0);
DCHECK_GE(priority, 0.0);
diff --git a/components/cronet/url_request_context_config.cc b/components/cronet/url_request_context_config.cc
index 317c0b4126..f839485599 100644
--- a/components/cronet/url_request_context_config.cc
+++ b/components/cronet/url_request_context_config.cc
@@ -218,8 +218,11 @@ quic::ParsedQuicVersionVector ParseQuicVersions(
auto it = all_supported_versions.begin();
while (it != all_supported_versions.end()) {
if (quic::QuicVersionToString(*it) == version) {
+ quic::HandshakeProtocol handshake_protocol = quic::PROTOCOL_QUIC_CRYPTO;
+ if (*it == quic::QUIC_VERSION_99)
+ handshake_protocol = quic::PROTOCOL_TLS1_3;
supported_versions.push_back(
- quic::ParsedQuicVersion(quic::PROTOCOL_QUIC_CRYPTO, *it));
+ quic::ParsedQuicVersion(handshake_protocol, *it));
// Remove the supported version to deduplicate versions extracted from
// |quic_versions|.
all_supported_versions.erase(it);
@keepsimple1
Copy link
Author

please ignore the changes in components/cronet/url_request_context_config.cc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment