blob: 861df4454f7ce2686284e8ee2cff5420d4b4e19c [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
""" - Enforce Cronet API requirements."""
import argparse
import os
import re
import shutil
import sys
import tempfile
REPOSITORY_ROOT = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir))
sys.path.insert(0, os.path.join(REPOSITORY_ROOT, 'build/android/gyp'))
from util import build_utils # pylint: disable=wrong-import-position
sys.path.insert(0, os.path.join(REPOSITORY_ROOT, 'components'))
from import update_api # pylint: disable=wrong-import-position
# These regular expressions catch the beginning of lines that declare classes
# and methods. The first group returned by a match is the class or method name.
from import CLASS_RE # pylint: disable=wrong-import-position
METHOD_RE = re.compile(r'.* ([^ ]*)\(.*\);')
# Allowed exceptions. Adding anything to this list is dangerous and should be
# avoided if possible. For now these exceptions are for APIs that existed in
# the first version of Cronet and will be supported forever.
# TODO(pauljensen): Remove these.
' ->'
' org/chromium/net/ExperimentalCronetEngine/getVersionString:'
'mpl/read -> org/chromium/net/UploadDataSink/onReadSucceeded:(Z)V',
'mpl/rewind -> org/chromium/net/UploadDataSink/onRewindError:'
' ->'
' org/chromium/net/UrlRequest/cancel:()V',
' ->'
' org/chromium/net/UrlResponseInfo/getHttpStatusText:()Ljava/lang/String;',
' ->'
' org/chromium/net/UrlResponseInfo/getHttpStatusCode:()I',
' ->'
' org/chromium/net/UrlResponseInfo/getHttpStatusCode:()I',
' ->'
' org/chromium/net/UrlResponseInfo/getHttpStatusCode:()I',
' ->'
' org/chromium/net/UrlRequest/read:(Ljava/nio/ByteBuffer;)V',
'k/onRedirectReceived -> org/chromium/net/UrlRequest/followRedirect:()V',
'k/onRedirectReceived -> org/chromium/net/UrlRequest/cancel:()V',
'l/read -> org/chromium/net/UploadDataSink/onReadSucceeded:(Z)V',
'l/rewind -> org/chromium/net/UploadDataSink/onRewindError:'
'pl/read -> org/chromium/net/UploadDataSink/onReadSucceeded:(Z)V',
'pl/rewind -> org/chromium/net/UploadDataSink/onRewindSucceeded:()V',
'connection.CronetHttpURLStreamHandler -> org/chromium/net/ExperimentalCron'
'connection.CronetHttpURLStreamHandler -> org/chromium/net/ExperimentalCron'
# getMessage() is an java.lang.Exception member, and so cannot be removed.
' -> '
# Filename of file containing the interface API version number.
INTERFACE_API_VERSION_FILENAME = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', 'android', 'interface_api_version.txt'))
# Filename of file containing the implementation API version number.
IMPLEMENTATION_API_VERSION_FILENAME = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', 'android',
JAR_PATH = os.path.join(build_utils.JAVA_HOME, 'bin', 'jar')
JAVAP_PATH = os.path.join(build_utils.JAVA_HOME, 'bin', 'javap')
def find_api_calls(dump, api_classes, bad_calls):
# Given a dump of an implementation class, find calls through API classes.
# |dump| is the output of "javap -c" on the implementation class files.
# |api_classes| is the list of classes comprising the API.
# |bad_calls| is the list of calls through API classes. This list is built up
# by this function.
for line in dump:
if CLASS_RE.match(line):
caller_class = CLASS_RE.match(line).group(1)
if METHOD_RE.match(line):
caller_method = METHOD_RE.match(line).group(1)
if line[8:16] == ': invoke':
callee = line.split(' // ')[1].split('Method ')[1].split('\n')[0]
callee_class = callee.split('.')[0]
assert callee_class
if callee_class in api_classes:
callee_method = callee.split('.')[1]
assert callee_method
# Ignore constructor calls for now as every implementation class
# that extends an API class will call them.
# TODO(pauljensen): Look into enforcing restricting constructor calls.
if callee_method.startswith('"<init>"'):
# Ignore VersionSafe calls
if 'VersionSafeCallbacks' in caller_class:
bad_call = '%s/%s -> %s/%s' % (caller_class, caller_method,
callee_class, callee_method)
if bad_call in ALLOWED_EXCEPTIONS:
bad_calls += [bad_call]
def check_api_calls(opts):
# Returns True if no calls through API classes in implementation.
temp_dir = tempfile.mkdtemp()
# Extract API class files from jar
jar_cmd = [os.path.relpath(JAR_PATH, temp_dir), 'xf',
build_utils.CheckOutput(jar_cmd, cwd=temp_dir)
shutil.rmtree(os.path.join(temp_dir, 'META-INF'), ignore_errors=True)
# Collect names of API classes
api_classes = []
for dirpath, _, filenames in os.walk(temp_dir):
if not filenames:
package = os.path.relpath(dirpath, temp_dir)
for filename in filenames:
if filename.endswith('.class'):
classname = filename[:-len('.class')]
api_classes += [os.path.normpath(os.path.join(package, classname))]
temp_dir = tempfile.mkdtemp()
# Extract impl class files from jars
for impl_jar in opts.impl_jar:
jar_cmd = [os.path.relpath(JAR_PATH, temp_dir), 'xf',
build_utils.CheckOutput(jar_cmd, cwd=temp_dir)
shutil.rmtree(os.path.join(temp_dir, 'META-INF'), ignore_errors=True)
# Process classes
bad_api_calls = []
for dirpath, _, filenames in os.walk(temp_dir):
if not filenames:
# Dump classes
dump_file = os.path.join(temp_dir, 'dump.txt')
javap_cmd = '%s -c %s > %s' % (
' '.join(os.path.join(dirpath, f) for f in filenames).replace('$',
if os.system(javap_cmd):
print('ERROR: javap failed on ' + ' '.join(filenames))
return False
# Process class dump
with open(dump_file, 'r') as dump:
find_api_calls(dump, api_classes, bad_api_calls)
if bad_api_calls:
print('ERROR: Found the following calls from implementation classes '
print(' API classes. These could fail if older API is used that')
print(' does not contain newer methods. Please call through a')
print(' wrapper class from VersionSafeCallbacks.')
return not bad_api_calls
def check_api_version(opts):
if not update_api.check_up_to_date(opts.api_jar):
print('ERROR: API file out of date. Please run this command:')
print(' components/cronet/tools/ --api_jar %s' % (
return False
interface_api_version = None
implementation_api_version = None
as interface_api_version_file:
interface_api_version = int(
as implementation_api_version_file:
implementation_api_version = int(
if interface_api_version > implementation_api_version:
print('ERROR: Interface API version cannot be higher than the current '
'implementation API version.')
return False
if implementation_api_version not in \
(interface_api_version + 1, interface_api_version):
print('ERROR: Implementation API version can be preemptively bumped up '
'at most once. Land the interface part of the API which is already '
'being released before adding a new one.')
return False
return True
def main(args):
parser = argparse.ArgumentParser(
description='Enforce Cronet API requirements.')
help='Path to API jar (i.e. cronet_api.jar)',
help='Path to implementation jar '
'(i.e. cronet_impl_native_java.jar)',
parser.add_argument('--stamp', help='Path to touch on success.')
opts = parser.parse_args(args)
ret = True
ret = check_api_calls(opts) and ret
ret = check_api_version(opts) and ret
if ret and opts.stamp:
return ret
if __name__ == '__main__':
sys.exit(0 if main(sys.argv[1:]) else -1)