Index: chrome/browser/page_load_metrics/metrics_web_contents_observer.cc |
diff --git a/chrome/browser/page_load_metrics/metrics_web_contents_observer.cc b/chrome/browser/page_load_metrics/metrics_web_contents_observer.cc |
index 0a1bf48d2475c23ef94aaf2c9fa5c120d7301306..1ca66e37badf183e35d83311e16ae78205437a41 100644 |
--- a/chrome/browser/page_load_metrics/metrics_web_contents_observer.cc |
+++ b/chrome/browser/page_load_metrics/metrics_web_contents_observer.cc |
@@ -33,6 +33,19 @@ |
DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
page_load_metrics::MetricsWebContentsObserver); |
+// This macro invokes the specified method on each observer, passing the |
+// variable length arguments as the method's arguments, and removes the observer |
+// from the list of observers if the given method returns STOP_OBSERVING. |
+#define INVOKE_AND_PRUNE_OBSERVERS(observers, Method, ...) \ |
+ for (auto it = observers.begin(); it != observers.end();) { \ |
+ if ((*it)->Method(__VA_ARGS__) == \ |
+ PageLoadMetricsObserver::STOP_OBSERVING) { \ |
+ it = observers.erase(it); \ |
+ } else { \ |
+ ++it; \ |
+ } \ |
+ } |
+ |
namespace page_load_metrics { |
namespace internal { |
@@ -442,9 +455,8 @@ void PageLoadTracker::Commit(content::NavigationHandle* navigation_handle) { |
// Some transitions (like CLIENT_REDIRECT) are only known at commit time. |
page_transition_ = navigation_handle->GetPageTransition(); |
user_gesture_ = navigation_handle->HasUserGesture(); |
- for (const auto& observer : observers_) { |
- observer->OnCommit(navigation_handle); |
- } |
+ |
+ INVOKE_AND_PRUNE_OBSERVERS(observers_, OnCommit, navigation_handle); |
LogAbortChainHistograms(navigation_handle); |
} |
@@ -475,14 +487,8 @@ void PageLoadTracker::FlushMetricsOnAppEnterBackground() { |
} |
const PageLoadExtraInfo info = ComputePageLoadExtraInfo(); |
- for (auto it = observers_.begin(); it != observers_.end();) { |
- if ((*it)->FlushMetricsOnAppEnterBackground(timing_, info) == |
- PageLoadMetricsObserver::STOP_OBSERVING) { |
- it = observers_.erase(it); |
- } else { |
- ++it; |
- } |
- } |
+ INVOKE_AND_PRUNE_OBSERVERS(observers_, FlushMetricsOnAppEnterBackground, |
+ timing_, info); |
} |
void PageLoadTracker::NotifyClientRedirectTo( |