Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeader.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.chrome.browser.omnibox.geo;
6
7 import android.Manifest;
8 import android.content.Context;
9 import android.content.pm.PackageManager;
10 import android.location.Location;
11 import android.net.Uri;
12 import android.os.Process;
13 import android.util.Base64;
14
15 import org.chromium.base.metrics.RecordHistogram;
16 import org.chromium.chrome.browser.UrlUtilities;
17 import org.chromium.chrome.browser.preferences.website.ContentSetting;
18 import org.chromium.chrome.browser.preferences.website.GeolocationInfo;
19
20 import java.util.Locale;
21
22 /**
23 * Provides methods for building the X-Geo HTTP header, which provides device lo cation to a server
24 * when making an HTTP request.
25 *
26 * X-Geo header spec: https://goto.google.com/xgeospec.
27 */
28 public class GeolocationHeader {
29
30 // Values for the histogram Geolocation.HeaderSentOrNot. Values 1, 5, 6, and 7 are defined in
31 // histograms.xml and should not be used in other ways.
32 public static final int UMA_LOCATION_DISABLED_FOR_GOOGLE_DOMAIN = 0;
33 public static final int UMA_LOCATION_NOT_AVAILABLE = 2;
34 public static final int UMA_LOCATION_STALE = 3;
35 public static final int UMA_HEADER_SENT = 4;
36 public static final int UMA_LOCATION_DISABLED_FOR_CHROME_APP = 5;
37 public static final int UMA_MAX = 8;
38
39 /** The maximum age in milliseconds of a location that we'll send in an X-Ge o header. */
40 private static final int MAX_LOCATION_AGE = 24 * 60 * 60 * 1000; // 24 hour s
41
42 /** The maximum age in milliseconds of a location before we'll request a ref resh. */
43 private static final int REFRESH_LOCATION_AGE = 5 * 60 * 1000; // 5 minutes
44
45 private static final String HTTPS_SCHEME = "https";
46
47 /**
48 * Requests a location refresh so that a valid location will be available fo r constructing
49 * an X-Geo header in the near future (i.e. within 5 minutes).
50 *
51 * @param context The Context used to get the device location.
52 */
53 public static void primeLocationForGeoHeader(Context context) {
54 if (!hasGeolocationPermission(context)) return;
55
56 GeolocationTracker.refreshLastKnownLocation(context, REFRESH_LOCATION_AG E);
57 }
58
59 private static boolean hasGeolocationPermission(Context context) {
60 return context.checkPermission(
61 Manifest.permission.ACCESS_COARSE_LOCATION, Process.myPid(), Pro cess.myUid())
62 == PackageManager.PERMISSION_GRANTED;
63 }
64
65 /**
66 * Returns an X-Geo HTTP header string if:
67 * 1. The current mode is not incognito.
68 * 2. The url is a google search URL (e.g. www.google.co.uk/search?q=cars), and
69 * 3. The user has not disabled sharing location with this url, and
70 * 4. There is a valid and recent location available.
71 *
72 * Returns null otherwise.
73 *
74 * @param context The Context used to get the device location.
75 * @param url The URL of the request with which this header will be sent.
76 * @param isIncognito Whether the request will happen in an incognito tab.
77 * @return The X-Geo header string or null.
78 */
79 public static String getGeoHeader(Context context, String url, boolean isInc ognito) {
80 // Only send X-Geo in normal mode.
81 if (isIncognito) return null;
82
83 // Only send X-Geo header to Google domains.
84 if (!UrlUtilities.nativeIsGoogleSearchUrl(url)) return null;
85
86 Uri uri = Uri.parse(url);
87 if (!HTTPS_SCHEME.equals(uri.getScheme())) return null;
88
89 if (!hasGeolocationPermission(context)) {
90 recordHistogram(UMA_LOCATION_DISABLED_FOR_CHROME_APP);
91 return null;
92 }
93
94 // Only send X-Geo header if the user hasn't disabled geolocation for ur l.
95 if (isLocationDisabledForUrl(uri)) {
96 recordHistogram(UMA_LOCATION_DISABLED_FOR_GOOGLE_DOMAIN);
97 return null;
98 }
99
100 // Only send X-Geo header if there's a fresh location available.
101 Location location = GeolocationTracker.getLastKnownLocation(context);
102 if (location == null) {
103 recordHistogram(UMA_LOCATION_NOT_AVAILABLE);
104 return null;
105 }
106 if (GeolocationTracker.getLocationAge(location) > MAX_LOCATION_AGE) {
107 recordHistogram(UMA_LOCATION_STALE);
108 return null;
109 }
110
111 recordHistogram(UMA_HEADER_SENT);
112
113 // Timestamp in microseconds since the UNIX epoch.
114 long timestamp = location.getTime() * 1000;
115 // Latitude times 1e7.
116 int latitude = (int) (location.getLatitude() * 10000000);
117 // Longitude times 1e7.
118 int longitude = (int) (location.getLongitude() * 10000000);
119 // Radius of 68% accuracy in mm.
120 int radius = (int) (location.getAccuracy() * 1000);
121
122 // Encode location using ascii protobuf format followed by base64 encodi ng.
123 // https://goto.google.com/partner_location_proto
124 String locationAscii = String.format(Locale.US,
125 "role:1 producer:12 timestamp:%d latlng{latitude_e7:%d longitude _e7:%d} radius:%d",
126 timestamp, latitude, longitude, radius);
127 String locationBase64 = new String(Base64.encode(locationAscii.getBytes( ), Base64.NO_WRAP));
128
129 return "X-Geo: a " + locationBase64;
130 }
131
132 /** Records a data point for the Geolocation.HeaderSentOrNot histogram. */
133 private static void recordHistogram(int result) {
134 RecordHistogram.recordEnumeratedHistogram("Geolocation.HeaderSentOrNot", result, UMA_MAX);
135 }
136
137 /**
138 * Returns true if the user has disabled sharing their location with url (e. g. via the
139 * geolocation infobar). If the user has not chosen a preference for url and url uses the https
140 * scheme, this considers the user's preference for url with the http scheme instead.
141 */
142 private static boolean isLocationDisabledForUrl(Uri uri) {
143 GeolocationInfo locationSettings = new GeolocationInfo(uri.toString(), n ull);
144 ContentSetting locationPermission = locationSettings.getContentSetting() ;
145
146 // If no preference has been chosen and the scheme is https, fall back t o the preference for
147 // this same host over http with no explicit port number.
148 if (locationPermission == null || locationPermission == ContentSetting.A SK) {
149 String scheme = uri.getScheme();
150 if (scheme != null && scheme.toLowerCase(Locale.US).equals("https")
151 && uri.getAuthority() != null && uri.getUserInfo() == null) {
152 String urlWithHttp = "http://" + uri.getHost();
153 locationSettings = new GeolocationInfo(urlWithHttp, null);
154 locationPermission = locationSettings.getContentSetting();
155 }
156 }
157
158 return locationPermission == ContentSetting.BLOCK;
159 }
160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698