| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 package org.chromium.net; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.pm.PackageInfo; | |
| 9 import android.content.pm.PackageManager; | |
| 10 import android.content.pm.PackageManager.NameNotFoundException; | |
| 11 import android.os.Build; | |
| 12 | |
| 13 import java.util.Locale; | |
| 14 | |
| 15 /** | |
| 16 * Constructs a User-Agent string. | |
| 17 */ | |
| 18 final class UserAgent { | |
| 19 private static final Object sLock = new Object(); | |
| 20 | |
| 21 private static final int VERSION_CODE_UNINITIALIZED = 0; | |
| 22 private static int sVersionCode = VERSION_CODE_UNINITIALIZED; | |
| 23 | |
| 24 private UserAgent() { | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Constructs a User-Agent string including application name and version, | |
| 29 * system build version, model and Id, and Cronet version. | |
| 30 * @param context the context to fetch the application name and version | |
| 31 * from. | |
| 32 * @return User-Agent string. | |
| 33 */ | |
| 34 public static String from(Context context) { | |
| 35 StringBuilder builder = new StringBuilder(); | |
| 36 | |
| 37 // Our package name and version. | |
| 38 builder.append(context.getPackageName()); | |
| 39 builder.append('/'); | |
| 40 builder.append(versionFromContext(context)); | |
| 41 | |
| 42 // The platform version. | |
| 43 builder.append(" (Linux; U; Android "); | |
| 44 builder.append(Build.VERSION.RELEASE); | |
| 45 builder.append("; "); | |
| 46 builder.append(Locale.getDefault().toString()); | |
| 47 | |
| 48 String model = Build.MODEL; | |
| 49 if (model.length() > 0) { | |
| 50 builder.append("; "); | |
| 51 builder.append(model); | |
| 52 } | |
| 53 | |
| 54 String id = Build.ID; | |
| 55 if (id.length() > 0) { | |
| 56 builder.append("; Build/"); | |
| 57 builder.append(id); | |
| 58 } | |
| 59 | |
| 60 builder.append(";"); | |
| 61 appendCronetVersion(builder); | |
| 62 | |
| 63 builder.append(')'); | |
| 64 | |
| 65 return builder.toString(); | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Constructs default QUIC User Agent Id string including application name | |
| 70 * and Cronet version. | |
| 71 * @param context the context to fetch the application name from. | |
| 72 * @return User-Agent string. | |
| 73 */ | |
| 74 static String getQuicUserAgentIdFrom(Context context) { | |
| 75 StringBuilder builder = new StringBuilder(); | |
| 76 | |
| 77 // Application name and cronet version. | |
| 78 builder.append(context.getPackageName()); | |
| 79 appendCronetVersion(builder); | |
| 80 | |
| 81 return builder.toString(); | |
| 82 } | |
| 83 | |
| 84 private static int versionFromContext(Context context) { | |
| 85 synchronized (sLock) { | |
| 86 if (sVersionCode == VERSION_CODE_UNINITIALIZED) { | |
| 87 PackageManager packageManager = context.getPackageManager(); | |
| 88 String packageName = context.getPackageName(); | |
| 89 try { | |
| 90 PackageInfo packageInfo = packageManager.getPackageInfo( | |
| 91 packageName, 0); | |
| 92 sVersionCode = packageInfo.versionCode; | |
| 93 } catch (NameNotFoundException e) { | |
| 94 throw new IllegalStateException( | |
| 95 "Cannot determine package version"); | |
| 96 } | |
| 97 } | |
| 98 return sVersionCode; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 private static void appendCronetVersion(StringBuilder builder) { | |
| 103 builder.append(" Cronet/"); | |
| 104 // TODO(pauljensen): This is the API version not the implementation | |
| 105 // version. The implementation version may be more appropriate for the | |
| 106 // UserAgent but is not available until after the CronetEngine is | |
| 107 // instantiated. Down the road, if the implementation is loaded via | |
| 108 // other means, this should be replaced with the implementation version. | |
| 109 builder.append(ApiVersion.CRONET_VERSION); | |
| 110 } | |
| 111 } | |
| OLD | NEW |