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.Application; | 7 import android.app.Application; |
| 8 import android.graphics.Bitmap; |
| 9 import android.graphics.Rect; |
8 import android.os.SystemClock; | 10 import android.os.SystemClock; |
9 import android.support.customtabs.CustomTabsCallback; | 11 import android.support.customtabs.CustomTabsCallback; |
10 import android.support.customtabs.CustomTabsSessionToken; | 12 import android.support.customtabs.CustomTabsSessionToken; |
11 | 13 |
12 import org.chromium.base.metrics.RecordHistogram; | 14 import org.chromium.base.metrics.RecordHistogram; |
| 15 import org.chromium.chrome.R; |
| 16 import org.chromium.chrome.browser.prerender.ExternalPrerenderHandler; |
13 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 17 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
14 import org.chromium.chrome.browser.tab.Tab; | 18 import org.chromium.chrome.browser.tab.Tab; |
15 import org.chromium.chrome.browser.tab.TabObserver; | 19 import org.chromium.chrome.browser.tab.TabObserver; |
16 import org.chromium.components.security_state.ConnectionSecurityLevel; | 20 import org.chromium.components.security_state.ConnectionSecurityLevel; |
| 21 import org.chromium.content_public.browser.ContentBitmapCallback; |
17 import org.chromium.content_public.browser.LoadUrlParams; | 22 import org.chromium.content_public.browser.LoadUrlParams; |
18 | 23 |
19 import java.util.concurrent.TimeUnit; | 24 import java.util.concurrent.TimeUnit; |
20 | 25 |
21 /** | 26 /** |
22 * A {@link TabObserver} that also handles custom tabs specific logging and mess
aging. | 27 * A {@link TabObserver} that also handles custom tabs specific logging and mess
aging. |
23 */ | 28 */ |
24 class CustomTabObserver extends EmptyTabObserver { | 29 class CustomTabObserver extends EmptyTabObserver { |
25 private final CustomTabsConnection mCustomTabsConnection; | 30 private final CustomTabsConnection mCustomTabsConnection; |
26 private final CustomTabsSessionToken mSession; | 31 private final CustomTabsSessionToken mSession; |
27 private final boolean mOpenedByChrome; | 32 private final boolean mOpenedByChrome; |
| 33 private float mScaleForNavigationInfo = 1f; |
28 | 34 |
29 private long mIntentReceivedTimestamp; | 35 private long mIntentReceivedTimestamp; |
30 private long mPageLoadStartedTimestamp; | 36 private long mPageLoadStartedTimestamp; |
31 | 37 |
32 private static final int STATE_RESET = 0; | 38 private static final int STATE_RESET = 0; |
33 private static final int STATE_WAITING_LOAD_START = 1; | 39 private static final int STATE_WAITING_LOAD_START = 1; |
34 private static final int STATE_WAITING_LOAD_FINISH = 2; | 40 private static final int STATE_WAITING_LOAD_FINISH = 2; |
35 private int mCurrentState; | 41 private int mCurrentState; |
36 | 42 |
37 public CustomTabObserver( | 43 public CustomTabObserver( |
38 Application application, CustomTabsSessionToken session, boolean ope
nedByChrome) { | 44 Application application, CustomTabsSessionToken session, boolean ope
nedByChrome) { |
39 if (openedByChrome) { | 45 if (openedByChrome) { |
40 mCustomTabsConnection = null; | 46 mCustomTabsConnection = null; |
41 } else { | 47 } else { |
42 mCustomTabsConnection = CustomTabsConnection.getInstance(application
); | 48 mCustomTabsConnection = CustomTabsConnection.getInstance(application
); |
43 } | 49 } |
44 mSession = session; | 50 mSession = session; |
| 51 if (!openedByChrome && mCustomTabsConnection.shouldSendNavigationInfoFor
Session(mSession)) { |
| 52 float desiredWidth = application.getResources().getDimensionPixelSiz
e( |
| 53 R.dimen.custom_tabs_screenshot_width); |
| 54 float desiredHeight = application.getResources().getDimensionPixelSi
ze( |
| 55 R.dimen.custom_tabs_screenshot_height); |
| 56 Rect bounds = ExternalPrerenderHandler.estimateContentSize(applicati
on, false); |
| 57 mScaleForNavigationInfo = (bounds.width() == 0 || bounds.height() ==
0) ? 1f : |
| 58 Math.min(desiredWidth / bounds.width(), desiredHeight / boun
ds.height()); |
| 59 } |
45 mOpenedByChrome = openedByChrome; | 60 mOpenedByChrome = openedByChrome; |
46 resetPageLoadTracking(); | 61 resetPageLoadTracking(); |
47 } | 62 } |
48 | 63 |
49 /** | 64 /** |
50 * Tracks the next page load, with timestamp as the origin of time. | 65 * Tracks the next page load, with timestamp as the origin of time. |
51 */ | 66 */ |
52 public void trackNextPageLoadFromTimestamp(long timestamp) { | 67 public void trackNextPageLoadFromTimestamp(long timestamp) { |
53 mIntentReceivedTimestamp = timestamp; | 68 mIntentReceivedTimestamp = timestamp; |
54 mCurrentState = STATE_WAITING_LOAD_START; | 69 mCurrentState = STATE_WAITING_LOAD_START; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // Same bounds and bucket count as "Startup.FirstCommitNavigationTim
e" | 121 // Same bounds and bucket count as "Startup.FirstCommitNavigationTim
e" |
107 RecordHistogram.recordCustomTimesHistogram( | 122 RecordHistogram.recordCustomTimesHistogram( |
108 histogramPrefix + ".IntentToFirstCommitNavigationTime", time
ToPageLoadStartedMs, | 123 histogramPrefix + ".IntentToFirstCommitNavigationTime", time
ToPageLoadStartedMs, |
109 1, TimeUnit.MINUTES.toMillis(1), TimeUnit.MILLISECONDS, 225)
; | 124 1, TimeUnit.MINUTES.toMillis(1), TimeUnit.MILLISECONDS, 225)
; |
110 // Same bounds and bucket count as PLT histograms. | 125 // Same bounds and bucket count as PLT histograms. |
111 RecordHistogram.recordCustomTimesHistogram(histogramPrefix + ".Inten
tToPageLoadedTime", | 126 RecordHistogram.recordCustomTimesHistogram(histogramPrefix + ".Inten
tToPageLoadedTime", |
112 timeToPageLoadFinishedMs, 10, TimeUnit.MINUTES.toMillis(10), | 127 timeToPageLoadFinishedMs, 10, TimeUnit.MINUTES.toMillis(10), |
113 TimeUnit.MILLISECONDS, 100); | 128 TimeUnit.MILLISECONDS, 100); |
114 } | 129 } |
115 resetPageLoadTracking(); | 130 resetPageLoadTracking(); |
| 131 captureNavigationInfo(tab); |
116 } | 132 } |
117 | 133 |
118 @Override | 134 @Override |
119 public void onDidAttachInterstitialPage(Tab tab) { | 135 public void onDidAttachInterstitialPage(Tab tab) { |
120 if (tab.getSecurityLevel() != ConnectionSecurityLevel.SECURITY_ERROR) re
turn; | 136 if (tab.getSecurityLevel() != ConnectionSecurityLevel.SECURITY_ERROR) re
turn; |
121 resetPageLoadTracking(); | 137 resetPageLoadTracking(); |
122 if (mCustomTabsConnection != null) { | 138 if (mCustomTabsConnection != null) { |
123 mCustomTabsConnection.notifyNavigationEvent( | 139 mCustomTabsConnection.notifyNavigationEvent( |
124 mSession, CustomTabsCallback.NAVIGATION_FAILED); | 140 mSession, CustomTabsCallback.NAVIGATION_FAILED); |
125 } | 141 } |
126 } | 142 } |
127 | 143 |
128 @Override | 144 @Override |
129 public void onPageLoadFailed(Tab tab, int errorCode) { | 145 public void onPageLoadFailed(Tab tab, int errorCode) { |
130 resetPageLoadTracking(); | 146 resetPageLoadTracking(); |
131 if (mCustomTabsConnection != null) { | 147 if (mCustomTabsConnection != null) { |
132 mCustomTabsConnection.notifyNavigationEvent( | 148 mCustomTabsConnection.notifyNavigationEvent( |
133 mSession, CustomTabsCallback.NAVIGATION_FAILED); | 149 mSession, CustomTabsCallback.NAVIGATION_FAILED); |
134 } | 150 } |
135 } | 151 } |
136 | 152 |
137 private void resetPageLoadTracking() { | 153 private void resetPageLoadTracking() { |
138 mCurrentState = STATE_RESET; | 154 mCurrentState = STATE_RESET; |
139 mIntentReceivedTimestamp = -1; | 155 mIntentReceivedTimestamp = -1; |
140 } | 156 } |
| 157 |
| 158 private void captureNavigationInfo(final Tab tab) { |
| 159 if (mCustomTabsConnection == null) return; |
| 160 if (!mCustomTabsConnection.shouldSendNavigationInfoForSession(mSession))
return; |
| 161 |
| 162 ContentBitmapCallback callback = new ContentBitmapCallback() { |
| 163 @Override |
| 164 public void onFinishGetBitmap(Bitmap bitmap, int response) { |
| 165 mCustomTabsConnection.sendNavigationInfo( |
| 166 mSession, tab.getUrl(), tab.getTitle(), bitmap); |
| 167 } |
| 168 }; |
| 169 tab.getWebContents().getContentBitmapAsync( |
| 170 Bitmap.Config.ARGB_8888, mScaleForNavigationInfo, new Rect(), ca
llback); |
| 171 } |
141 } | 172 } |
OLD | NEW |