Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.customtabs; | 5 package org.chromium.chrome.browser.customtabs; |
| 6 | 6 |
| 7 import android.app.ActivityManager; | 7 import android.app.ActivityManager; |
| 8 import android.app.Application; | 8 import android.app.Application; |
| 9 import android.app.PendingIntent; | 9 import android.app.PendingIntent; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 import android.support.customtabs.CustomTabsSessionToken; | 26 import android.support.customtabs.CustomTabsSessionToken; |
| 27 import android.text.TextUtils; | 27 import android.text.TextUtils; |
| 28 import android.util.Pair; | 28 import android.util.Pair; |
| 29 import android.widget.RemoteViews; | 29 import android.widget.RemoteViews; |
| 30 | 30 |
| 31 import org.chromium.base.CommandLine; | 31 import org.chromium.base.CommandLine; |
| 32 import org.chromium.base.Log; | 32 import org.chromium.base.Log; |
| 33 import org.chromium.base.SysUtils; | 33 import org.chromium.base.SysUtils; |
| 34 import org.chromium.base.ThreadUtils; | 34 import org.chromium.base.ThreadUtils; |
| 35 import org.chromium.base.TimeUtils; | 35 import org.chromium.base.TimeUtils; |
| 36 import org.chromium.base.TraceEvent; | |
| 36 import org.chromium.base.VisibleForTesting; | 37 import org.chromium.base.VisibleForTesting; |
| 37 import org.chromium.base.annotations.SuppressFBWarnings; | 38 import org.chromium.base.annotations.SuppressFBWarnings; |
| 38 import org.chromium.base.library_loader.ProcessInitException; | 39 import org.chromium.base.library_loader.ProcessInitException; |
| 39 import org.chromium.base.metrics.RecordHistogram; | 40 import org.chromium.base.metrics.RecordHistogram; |
| 40 import org.chromium.chrome.R; | 41 import org.chromium.chrome.R; |
| 41 import org.chromium.chrome.browser.ChromeApplication; | 42 import org.chromium.chrome.browser.ChromeApplication; |
| 42 import org.chromium.chrome.browser.ChromeFeatureList; | 43 import org.chromium.chrome.browser.ChromeFeatureList; |
| 43 import org.chromium.chrome.browser.IntentHandler; | 44 import org.chromium.chrome.browser.IntentHandler; |
| 44 import org.chromium.chrome.browser.WarmupManager; | 45 import org.chromium.chrome.browser.WarmupManager; |
| 45 import org.chromium.chrome.browser.device.DeviceClassManager; | 46 import org.chromium.chrome.browser.device.DeviceClassManager; |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 */ | 239 */ |
| 239 private boolean warmupInternal(final boolean mayCreateSpareWebContents) { | 240 private boolean warmupInternal(final boolean mayCreateSpareWebContents) { |
| 240 // Here and in mayLaunchUrl(), don't do expensive work for background ap plications. | 241 // Here and in mayLaunchUrl(), don't do expensive work for background ap plications. |
| 241 if (!isCallerForegroundOrSelf()) return false; | 242 if (!isCallerForegroundOrSelf()) return false; |
| 242 mClientManager.recordUidHasCalledWarmup(Binder.getCallingUid()); | 243 mClientManager.recordUidHasCalledWarmup(Binder.getCallingUid()); |
| 243 final boolean initialized = !mWarmupHasBeenCalled.compareAndSet(false, t rue); | 244 final boolean initialized = !mWarmupHasBeenCalled.compareAndSet(false, t rue); |
| 244 final int uid = Binder.getCallingUid(); | 245 final int uid = Binder.getCallingUid(); |
| 245 // The call is non-blocking and this must execute on the UI thread, post a task. | 246 // The call is non-blocking and this must execute on the UI thread, post a task. |
| 246 ThreadUtils.postOnUiThread(new Runnable() { | 247 ThreadUtils.postOnUiThread(new Runnable() { |
| 247 @Override | 248 @Override |
| 248 public void run() { | 249 public void run() { |
|
pasko
2016/11/21 17:33:02
this easier to read at least to me:
try {
Trace
| |
| 249 if (!initialized) initializeBrowser(mApplication); | 250 TraceEvent.begin("CustomTabsConnection.warmupInternal()"); |
| 250 if (mayCreateSpareWebContents && mSpeculation == null | 251 try { |
| 251 && !SysUtils.isLowEndDevice()) { | 252 if (!initialized) initializeBrowser(mApplication); |
| 252 WarmupManager.getInstance().createSpareWebContents(); | 253 if (mayCreateSpareWebContents && mSpeculation == null |
| 253 // The throttling database uses shared preferences, that can cause a StrictMode | 254 && !SysUtils.isLowEndDevice()) { |
| 254 // violation on the first access. Make sure that this access is not in | 255 WarmupManager.getInstance().createSpareWebContents(); |
| 255 // mayLauchUrl. | 256 } |
| 256 RequestThrottler.getForUid(mApplication, uid); | 257 if (!initialized) { |
| 258 // The throttling database uses shared preferences, that can cause a | |
| 259 // StrictMode violation on the first access. Make sure t hat this access is | |
| 260 // not in mayLauchUrl. | |
| 261 RequestThrottler.getForUid(mApplication, uid); | |
| 257 | 262 |
| 258 Profile profile = Profile.getLastUsedProfile(); | 263 Profile profile = Profile.getLastUsedProfile(); |
| 259 new ResourcePrefetchPredictor(profile).startInitialization() ; | 264 new ResourcePrefetchPredictor(profile).startInitializati on(); |
| 265 } | |
| 266 mWarmupHasBeenFinished.set(true); | |
| 267 } finally { | |
| 268 TraceEvent.end("CustomTabsConnection.warmupInternal()"); | |
| 260 } | 269 } |
| 261 mWarmupHasBeenFinished.set(true); | |
| 262 } | 270 } |
| 263 }); | 271 }); |
| 264 return true; | 272 return true; |
| 265 } | 273 } |
| 266 | 274 |
| 267 /** @return the URL converted to string, or null if it's invalid. */ | 275 /** @return the URL converted to string, or null if it's invalid. */ |
| 268 private static String checkAndConvertUri(Uri uri) { | 276 private static String checkAndConvertUri(Uri uri) { |
| 269 if (uri == null) return null; | 277 if (uri == null) return null; |
| 270 // Don't do anything for unknown schemes. Not having a scheme is allowed , as we allow | 278 // Don't do anything for unknown schemes. Not having a scheme is allowed , as we allow |
| 271 // "www.example.com". | 279 // "www.example.com". |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 if (url != null) { | 348 if (url != null) { |
| 341 warmupManager.maybePreconnectUrlAndSubResources(profile, url); | 349 warmupManager.maybePreconnectUrlAndSubResources(profile, url); |
| 342 atLeastOneUrl = true; | 350 atLeastOneUrl = true; |
| 343 } | 351 } |
| 344 } | 352 } |
| 345 return atLeastOneUrl; | 353 return atLeastOneUrl; |
| 346 } | 354 } |
| 347 | 355 |
| 348 public boolean mayLaunchUrl(CustomTabsSessionToken session, Uri url, Bundle extras, | 356 public boolean mayLaunchUrl(CustomTabsSessionToken session, Uri url, Bundle extras, |
| 349 List<Bundle> otherLikelyBundles) { | 357 List<Bundle> otherLikelyBundles) { |
| 350 boolean success = mayLaunchUrlInternal(session, url, extras, otherLikely Bundles); | 358 TraceEvent.begin("CustomTabsConnection.mayLaunchUrl()"); |
| 351 logCall("mayLaunchUrl()", success); | 359 try { |
| 352 return success; | 360 boolean success = mayLaunchUrlInternal(session, url, extras, otherLi kelyBundles); |
| 361 logCall("mayLaunchUrl()", success); | |
| 362 return success; | |
| 363 } finally { | |
| 364 TraceEvent.end("CustomTabsConnection.mayLaunchUrl()"); | |
| 365 } | |
| 353 } | 366 } |
| 354 | 367 |
| 355 private boolean mayLaunchUrlInternal(final CustomTabsSessionToken session, U ri url, | 368 private boolean mayLaunchUrlInternal(final CustomTabsSessionToken session, U ri url, |
| 356 final Bundle extras, final List<Bundle> otherLikelyBundles) { | 369 final Bundle extras, final List<Bundle> otherLikelyBundles) { |
| 357 final boolean lowConfidence = | 370 final boolean lowConfidence = |
| 358 (url == null || TextUtils.isEmpty(url.toString())) && otherLikel yBundles != null; | 371 (url == null || TextUtils.isEmpty(url.toString())) && otherLikel yBundles != null; |
| 359 final String urlString = checkAndConvertUri(url); | 372 final String urlString = checkAndConvertUri(url); |
| 360 if (url != null && urlString == null && !lowConfidence) return false; | 373 if (url != null && urlString == null && !lowConfidence) return false; |
| 361 | 374 |
| 362 // Things below need the browser process to be initialized. | 375 // Things below need the browser process to be initialized. |
| 363 | 376 |
| 364 // Forbids warmup() from creating a spare renderer, as prerendering woul dn't reuse | 377 // Forbids warmup() from creating a spare renderer, as prerendering woul dn't reuse |
| 365 // it. Checking whether prerendering is enabled requires the native libr ary to be loaded, | 378 // it. Checking whether prerendering is enabled requires the native libr ary to be loaded, |
| 366 // which is not necessarily the case yet. | 379 // which is not necessarily the case yet. |
| 367 if (!warmupInternal(false)) return false; // Also does the foreground ch eck. | 380 if (!warmupInternal(false)) return false; // Also does the foreground ch eck. |
| 368 | 381 |
| 369 final int uid = Binder.getCallingUid(); | 382 final int uid = Binder.getCallingUid(); |
| 370 // TODO(lizeb): Also throttle low-confidence mode. | 383 // TODO(lizeb): Also throttle low-confidence mode. |
| 371 if (!lowConfidence | 384 if (!lowConfidence |
| 372 && !mClientManager.updateStatsAndReturnWhetherAllowed(session, u id, urlString)) { | 385 && !mClientManager.updateStatsAndReturnWhetherAllowed(session, u id, urlString)) { |
| 373 return false; | 386 return false; |
| 374 } | 387 } |
| 375 ThreadUtils.postOnUiThread(new Runnable() { | 388 ThreadUtils.postOnUiThread(new Runnable() { |
| 376 @Override | 389 @Override |
| 377 public void run() { | 390 public void run() { |
| 378 if (lowConfidence) { | 391 TraceEvent.begin("CustomTabsConnection.mayLaunchUrlInternal()"); |
| 379 lowConfidenceMayLaunchUrl(otherLikelyBundles); | 392 try { |
| 380 } else { | 393 if (lowConfidence) { |
| 381 highConfidenceMayLaunchUrl(session, uid, urlString, extras, otherLikelyBundles); | 394 lowConfidenceMayLaunchUrl(otherLikelyBundles); |
| 395 } else { | |
| 396 highConfidenceMayLaunchUrl( | |
| 397 session, uid, urlString, extras, otherLikelyBund les); | |
| 398 } | |
| 399 } finally { | |
| 400 TraceEvent.end("CustomTabsConnection.mayLaunchUrlInternal()" ); | |
| 382 } | 401 } |
| 383 } | 402 } |
| 384 }); | 403 }); |
| 385 return true; | 404 return true; |
| 386 } | 405 } |
| 387 | 406 |
| 388 public Bundle extraCommand(String commandName, Bundle args) { | 407 public Bundle extraCommand(String commandName, Bundle args) { |
| 389 return null; | 408 return null; |
| 390 } | 409 } |
| 391 | 410 |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 917 @VisibleForTesting | 936 @VisibleForTesting |
| 918 void resetThrottling(Context context, int uid) { | 937 void resetThrottling(Context context, int uid) { |
| 919 mClientManager.resetThrottling(uid); | 938 mClientManager.resetThrottling(uid); |
| 920 } | 939 } |
| 921 | 940 |
| 922 @VisibleForTesting | 941 @VisibleForTesting |
| 923 void ban(Context context, int uid) { | 942 void ban(Context context, int uid) { |
| 924 mClientManager.ban(uid); | 943 mClientManager.ban(uid); |
| 925 } | 944 } |
| 926 } | 945 } |
| OLD | NEW |