Skip to content

Commit

Permalink
[cronet] Make getAcceptLanguages index a static table instead of the …
Browse files Browse the repository at this point in the history
…application bundle.

First steps to try and decrease initialization time by putting acceptLanguages into a static
table.

BUG=655689
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.android:android_cronet_tester

Review-Url: https://codereview.chromium.org/2665703002
Cr-Commit-Position: refs/heads/master@{#454660}
  • Loading branch information
lilyhoughton authored and Commit bot committed Mar 3, 2017
1 parent 1021ed2 commit fe175d2
Show file tree
Hide file tree
Showing 63 changed files with 150 additions and 199 deletions.
78 changes: 12 additions & 66 deletions components/cronet/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ process_version("cronet_version_header") {
source_set("cronet_sources") {
deps = [
":cronet_version_header",
":generate_accept_languages",
"//base:base",
"//components/grpc_support",
"//components/metrics:metrics",
Expand Down Expand Up @@ -70,77 +71,11 @@ tweak_info_plist("tweak_cronet_plist") {
info_plist = "Info.plist"
}

bundle_data("cronet_framework_resources") {
# This bundle contains "Accept-Languages" header values for known locales.
# TODO(mef): These strings should be auto-generated from chrome's .xtb
# files, not hardcoded.
sources = [
"Resources/Localization/am.lproj",
"Resources/Localization/ar.lproj",
"Resources/Localization/bg.lproj",
"Resources/Localization/bn.lproj",
"Resources/Localization/ca.lproj",
"Resources/Localization/cs.lproj",
"Resources/Localization/da.lproj",
"Resources/Localization/de.lproj",
"Resources/Localization/el.lproj",
"Resources/Localization/en-GB.lproj",
"Resources/Localization/en.lproj",
"Resources/Localization/es-419.lproj",
"Resources/Localization/es.lproj",
"Resources/Localization/fa.lproj",
"Resources/Localization/fi.lproj",
"Resources/Localization/fil.lproj",
"Resources/Localization/fr.lproj",
"Resources/Localization/gu.lproj",
"Resources/Localization/he.lproj",
"Resources/Localization/hi.lproj",
"Resources/Localization/hr.lproj",
"Resources/Localization/hu.lproj",
"Resources/Localization/id.lproj",
"Resources/Localization/it.lproj",
"Resources/Localization/ja.lproj",
"Resources/Localization/kn.lproj",
"Resources/Localization/ko.lproj",
"Resources/Localization/lt.lproj",
"Resources/Localization/lv.lproj",
"Resources/Localization/ml.lproj",
"Resources/Localization/mr.lproj",
"Resources/Localization/ms.lproj",
"Resources/Localization/nb.lproj",
"Resources/Localization/nl.lproj",
"Resources/Localization/pl.lproj",
"Resources/Localization/pt-BR.lproj",
"Resources/Localization/pt-PT.lproj",
"Resources/Localization/pt.lproj",
"Resources/Localization/ro.lproj",
"Resources/Localization/ru.lproj",
"Resources/Localization/sk.lproj",
"Resources/Localization/sl.lproj",
"Resources/Localization/sr.lproj",
"Resources/Localization/sv.lproj",
"Resources/Localization/sw.lproj",
"Resources/Localization/ta.lproj",
"Resources/Localization/te.lproj",
"Resources/Localization/th.lproj",
"Resources/Localization/tr.lproj",
"Resources/Localization/uk.lproj",
"Resources/Localization/vi.lproj",
"Resources/Localization/zh-Hans.lproj",
"Resources/Localization/zh-Hant.lproj",
"Resources/Localization/zh.lproj",
]
outputs = [
"{{bundle_resources_dir}}/cronet_resources.bundle/{{source_file_part}}",
]
}

ios_framework_bundle("cronet_framework") {
output_name = "Cronet"
info_plist_target = ":tweak_cronet_plist"

deps = [
":cronet_framework_resources",
":cronet_sources",
"//base",
"//net:net",
Expand Down Expand Up @@ -189,6 +124,17 @@ test("cronet_unittests") {
]
}

action("generate_accept_languages") {
script = "//components/cronet/tools/generate_accept_languages.py"
args = [
rebase_path("$target_gen_dir"),
rebase_path("//"),
]
outputs = [
"$target_gen_dir/accept_languages_table.h",
]
}

if (additional_toolchains == [] || current_toolchain == default_toolchain) {
_package_dir = "$root_out_dir/cronet"

Expand Down
4 changes: 4 additions & 0 deletions components/cronet/ios/Cronet.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ typedef BOOL (^RequestFilterBlock)(NSURLRequest* request);
GRPC_SUPPORT_EXPORT
@interface Cronet : NSObject

// Sets the HTTP Accept-Language header. This method only has any effect before
// |start| is called.
+ (void)setAcceptLanguages:(NSString*)acceptLanguages;

// Sets whether HTTP/2 should be supported by CronetEngine. This method only has
// any effect before |start| is called.
+ (void)setHttp2Enabled:(BOOL)http2Enabled;
Expand Down
44 changes: 30 additions & 14 deletions components/cronet/ios/Cronet.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/memory/scoped_vector.h"
#include "base/strings/sys_string_conversions.h"
#include "base/synchronization/lock.h"
#include "components/cronet/ios/accept_languages_table.h"
#include "components/cronet/ios/cronet_environment.h"
#include "components/cronet/url_request_context_config.h"
#include "ios/net/crn_http_protocol_handler.h"
Expand Down Expand Up @@ -43,6 +44,7 @@
std::unique_ptr<CronetHttpProtocolHandlerDelegate> gHttpProtocolHandlerDelegate;
NSURLCache* gPreservedSharedURLCache = nil;
BOOL gEnableTestCertVerifierForTesting = FALSE;
NSString* gAcceptLanguages = nil;

// CertVerifier, which allows any certificates for testing.
class TestCertVerifier : public net::CertVerifier {
Expand Down Expand Up @@ -116,20 +118,34 @@ + (void)configureCronetEnvironmentForTesting:
}
}

+ (NSString*)getAcceptLanguagesFromPreferredLanguages:
(NSArray<NSString*>*)languages {
NSMutableArray* acceptLanguages = [NSMutableArray new];
for (NSString* lang_region in languages) {
NSString* lang = [lang_region componentsSeparatedByString:@"-"][0];
NSString* localeAcceptLangs = acceptLangs[lang_region] ?: acceptLangs[lang];
if (localeAcceptLangs)
[acceptLanguages
addObjectsFromArray:[localeAcceptLangs
componentsSeparatedByString:@","]];
}

NSString* acceptLanguageString =
[[[NSOrderedSet orderedSetWithArray:acceptLanguages] array]
componentsJoinedByString:@","];

return [acceptLanguageString length] != 0 ? acceptLanguageString
: @"en-US,en";
}

+ (NSString*)getAcceptLanguages {
// Use the framework bundle to search for resources.
NSBundle* frameworkBundle = [NSBundle bundleForClass:self];
NSString* bundlePath =
[frameworkBundle pathForResource:@"cronet_resources" ofType:@"bundle"];
NSBundle* bundle = [NSBundle bundleWithPath:bundlePath];
NSString* acceptLanguages = NSLocalizedStringWithDefaultValue(
@"IDS_ACCEPT_LANGUAGES", @"Localizable", bundle, @"en-US,en",
@"These values are copied from Chrome's .xtb files, so the same "
"values are used in the |Accept-Language| header. Key name matches "
"Chrome's.");
if (acceptLanguages == Nil)
acceptLanguages = @"";
return acceptLanguages;
return [self
getAcceptLanguagesFromPreferredLanguages:[NSLocale preferredLanguages]];
}

+ (void)setAcceptLanguages:(NSString*)acceptLanguages {
[self checkNotStarted];
gAcceptLanguages = acceptLanguages;
}

+ (void)checkNotStarted {
Expand Down Expand Up @@ -193,7 +209,7 @@ + (void)startInternal {
gChromeNet.Get().reset(
new cronet::CronetEnvironment(user_agent, gUserAgentPartial));
gChromeNet.Get()->set_accept_language(
base::SysNSStringToUTF8([self getAcceptLanguages]));
base::SysNSStringToUTF8(gAcceptLanguages ?: [self getAcceptLanguages]));

gChromeNet.Get()->set_http2_enabled(gHttp2Enabled);
gChromeNet.Get()->set_quic_enabled(gQuicEnabled);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit fe175d2

Please sign in to comment.