Skip to content

Commit 3f2a541

Browse files
Patrick NolandPatrick Noland
authored andcommitted
[EoC] Fix crash caused by invalid ukm_source_id
Bootstrap the id by always calling ReportEvent(FETCH_REQUESTED) before fetching. Report FETCH_DELAYED when we set the baseline timestamp. Ensure that events generated by the tab and triggering delayed fetch are not checked against page being loaded Bug: 832712 Change-Id: Ida168013b0920a6428093af943f06762dfbe0600 Reviewed-on: https://chromium-review.googlesource.com/1012440 Commit-Queue: Patrick Noland <[email protected]> Reviewed-by: Theresa <[email protected]> Reviewed-by: Filip Gorski <[email protected]> Cr-Original-Commit-Position: refs/heads/master@{#551169}(cherry picked from commit ff3479f) Reviewed-on: https://chromium-review.googlesource.com/1016863 Reviewed-by: Patrick Noland <[email protected]> Cr-Commit-Position: refs/branch-heads/3396@{#84} Cr-Branched-From: 9ef2aa8-refs/heads/master@{#550428}
1 parent 582efd0 commit 3f2a541

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

chrome/android/java/src/org/chromium/chrome/browser/contextual_suggestions/ContextualSuggestionsMediator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.chromium.components.feature_engagement.EventConstants;
2424
import org.chromium.components.feature_engagement.FeatureConstants;
2525
import org.chromium.components.feature_engagement.Tracker;
26+
import org.chromium.content_public.browser.WebContents;
2627
import org.chromium.ui.widget.ViewRectProvider;
2728

2829
import java.util.Collections;
@@ -158,6 +159,7 @@ public void onSettingsStateChanged(boolean enabled) {}
158159

159160
@Override
160161
public void requestSuggestions(String url) {
162+
reportEvent(ContextualSuggestionsEvent.FETCH_REQUESTED);
161163
mCurrentRequestUrl = url;
162164
mSuggestionsSource.fetchSuggestions(url, (suggestionsResult) -> {
163165
if (mSuggestionsSource == null) return;
@@ -182,6 +184,13 @@ public void clearState() {
182184
clearSuggestions();
183185
}
184186

187+
@Override
188+
public void reportFetchDelayed(WebContents webContents) {
189+
if (mTabModelSelector.getCurrentTab().getWebContents() == webContents) {
190+
reportEvent(ContextualSuggestionsEvent.FETCH_DELAYED);
191+
}
192+
}
193+
185194
/**
186195
* Called when suggestions are cleared either due to the user explicitly dismissing
187196
* suggestions via the close button or due to the FetchHelper signaling state should

chrome/android/java/src/org/chromium/chrome/browser/contextual_suggestions/FetchHelper.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
1818
import org.chromium.chrome.browser.tabmodel.TabModelSelectorTabModelObserver;
1919
import org.chromium.chrome.browser.util.UrlUtilities;
20+
import org.chromium.content_public.browser.WebContents;
2021

2122
import java.util.HashMap;
2223
import java.util.Map;
@@ -38,6 +39,11 @@ interface Delegate {
3839
* Called when the state should be reset e.g. when the user navigates away from a webpage.
3940
*/
4041
void clearState();
42+
43+
/**
44+
* Called when a document becomes eligible for fetching but the fetch is being delayed.
45+
*/
46+
void reportFetchDelayed(WebContents webContents);
4147
}
4248

4349
/** State of the tab with respect to fetching readiness */
@@ -71,8 +77,8 @@ boolean isTrackingPage() {
7177
}
7278

7379
/**
74-
* Sets the baseline from which the fetch delay is calculated (conceptually starting the
75-
* timer).
80+
* Sets the baseline from which the fetch delay is calculated if it was not
81+
* already set (conceptually starting the timer).
7682
* @param fetchTimeBaselineMillis The new value to set the baseline fetch time to.
7783
* @return Whether the fetch time baseline was set.
7884
*/
@@ -142,17 +148,23 @@ public void onUpdateUrl(Tab tab, String url) {
142148

143149
@Override
144150
public void didFirstVisuallyNonEmptyPaint(Tab tab) {
145-
assert !tab.isIncognito();
146-
if (getTabFetchReadinessState(tab).setFetchTimeBaselineMillis(
147-
SystemClock.uptimeMillis())) {
148-
maybeStartFetch(tab);
149-
}
151+
setTimeBaselineAndMaybeFetch(tab);
150152
}
151153

152154
@Override
153155
public void onPageLoadFinished(Tab tab) {
156+
setTimeBaselineAndMaybeFetch(tab);
157+
}
158+
159+
@Override
160+
public void onLoadStopped(Tab tab, boolean toDifferentDocument) {
161+
setTimeBaselineAndMaybeFetch(tab);
162+
}
163+
164+
private void setTimeBaselineAndMaybeFetch(Tab tab) {
154165
assert !tab.isIncognito();
155-
if (maybeSetFetchReadinessBaseline(tab)) {
166+
if (getTabFetchReadinessState(tab).setFetchTimeBaselineMillis(
167+
SystemClock.uptimeMillis())) {
156168
maybeStartFetch(tab);
157169
}
158170
}
@@ -254,6 +266,7 @@ private void maybeStartFetch(Tab tab) {
254266
}
255267

256268
private void postDelayedFetch(final String url, final Tab tab, long delayMillis) {
269+
mDelegate.reportFetchDelayed(tab.getWebContents());
257270
ThreadUtils.postOnUiThreadDelayed(new Runnable() {
258271
@Override
259272
public void run() {

chrome/browser/android/contextual_suggestions/contextual_suggestions_bridge.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ void ContextualSuggestionsBridge::ReportEvent(
134134
ukm::SourceId ukm_source_id =
135135
ukm::GetSourceIdForWebContentsDocument(web_contents);
136136

137+
// It's possible but unlikely to be in this state; it can happen if we are
138+
// triggering a fetch for a WebContents that does not have a committed
139+
// navigation. This can happen, e.g., if we switched tabs and a navigation
140+
// took a very long time to commit. TODO(pnoland): Check against this
141+
// possiblity by deferring event reporting and fetching until we observe a
142+
// commit.
143+
if (ukm_source_id == ukm::kInvalidSourceId)
144+
return;
145+
137146
contextual_suggestions::ContextualSuggestionsEvent event =
138147
static_cast<contextual_suggestions::ContextualSuggestionsEvent>(
139148
j_event_id);

components/ntp_snippets/contextual/contextual_suggestions_fetch.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ void ContextualSuggestionsFetch::Start(
132132
ReportFetchMetricsCallback metrics_callback,
133133
const scoped_refptr<network::SharedURLLoaderFactory>& loader_factory) {
134134
request_completed_callback_ = std::move(callback);
135-
metrics_callback.Run(FETCH_REQUESTED);
136135
url_loader_ = MakeURLLoader();
137136
url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
138137
loader_factory.get(),

components/ntp_snippets/contextual/contextual_suggestions_fetcher_impl_unittest.cc

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ TEST_F(ContextualSuggestionsFetcherTest, SingleSuggestionResponse) {
269269
ExpectResponsesMatch(std::move(callback), "Peek Text", DefaultClusters());
270270
EXPECT_EQ(metrics_callback.events,
271271
std::vector<ContextualSuggestionsEvent>(
272-
{contextual_suggestions::FETCH_REQUESTED,
273-
contextual_suggestions::FETCH_COMPLETED}));
272+
{contextual_suggestions::FETCH_COMPLETED}));
274273
}
275274

276275
TEST_F(ContextualSuggestionsFetcherTest,
@@ -403,8 +402,7 @@ TEST_F(ContextualSuggestionsFetcherTest, ProtocolError) {
403402
ElementsAre(base::Bucket(/*min=*/net::HTTP_NOT_FOUND, /*count=*/1)));
404403
EXPECT_EQ(metrics_callback.events,
405404
std::vector<ContextualSuggestionsEvent>(
406-
{contextual_suggestions::FETCH_REQUESTED,
407-
contextual_suggestions::FETCH_ERROR}));
405+
{contextual_suggestions::FETCH_ERROR}));
408406
}
409407

410408
TEST_F(ContextualSuggestionsFetcherTest, ServerUnavailable) {
@@ -424,8 +422,7 @@ TEST_F(ContextualSuggestionsFetcherTest, ServerUnavailable) {
424422
/*count=*/1)));
425423
EXPECT_EQ(metrics_callback.events,
426424
std::vector<ContextualSuggestionsEvent>(
427-
{contextual_suggestions::FETCH_REQUESTED,
428-
contextual_suggestions::FETCH_SERVER_BUSY}));
425+
{contextual_suggestions::FETCH_SERVER_BUSY}));
429426
}
430427

431428
TEST_F(ContextualSuggestionsFetcherTest, NetworkError) {
@@ -448,8 +445,7 @@ TEST_F(ContextualSuggestionsFetcherTest, NetworkError) {
448445

449446
EXPECT_EQ(metrics_callback.events,
450447
std::vector<ContextualSuggestionsEvent>(
451-
{contextual_suggestions::FETCH_REQUESTED,
452-
contextual_suggestions::FETCH_ERROR}));
448+
{contextual_suggestions::FETCH_ERROR}));
453449
}
454450

455451
TEST_F(ContextualSuggestionsFetcherTest, EmptyResponse) {
@@ -464,8 +460,7 @@ TEST_F(ContextualSuggestionsFetcherTest, EmptyResponse) {
464460

465461
EXPECT_EQ(metrics_callback.events,
466462
std::vector<ContextualSuggestionsEvent>(
467-
{contextual_suggestions::FETCH_REQUESTED,
468-
contextual_suggestions::FETCH_EMPTY}));
463+
{contextual_suggestions::FETCH_EMPTY}));
469464
}
470465

471466
TEST_F(ContextualSuggestionsFetcherTest, ResponseWithUnsetFields) {

0 commit comments

Comments
 (0)