Move NavigationInfo to WebTimeActivityProvider Bug: 443082966 Change-Id: I52274320c15aebfeaa54ce18f73e1a90e0ff63b4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6913843 Commit-Queue: Michael Checo <michaelcheco@google.com> Reviewed-by: Wenyu Zhang <zhangwenyu@google.com> Cr-Commit-Position: refs/heads/main@{#1512483}
diff --git a/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.cc b/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.cc index 44258dc4..69d88980 100644 --- a/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.cc +++ b/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.cc
@@ -95,11 +95,19 @@ void WebTimeActivityProvider::OnWebActivityChanged( const WebTimeNavigationObserver::NavigationInfo& info) { - if (info.is_web_app) { + if (info.web_contents == nullptr) { return; } - if (info.web_contents == nullptr) { + WebTimeNavigationObserver* observer = + WebTimeNavigationObserver::FromWebContents(info.web_contents); + + // Only cache info for observers that this provider is tracking. + if (observer && web_time_navigation_observers_.IsObservingSource(observer)) { + navigation_info_map_[observer] = info; + } + + if (info.is_web_app) { return; } @@ -122,6 +130,7 @@ void WebTimeActivityProvider::WebTimeNavigationObserverDestroyed( WebTimeNavigationObserver* navigation_observer) { web_time_navigation_observers_.RemoveObservation(navigation_observer); + navigation_info_map_.erase(navigation_observer); } void WebTimeActivityProvider::OnTabStripModelChanged( @@ -249,17 +258,18 @@ CHECK(observer) << "This code should not run if ChromeActivityReporting " "feature is disabled"; - const std::optional<WebTimeNavigationObserver::NavigationInfo>& info = - observer->last_navigation_info(); + const auto it = navigation_info_map_.find(observer); // The first navigation has not occurred yet. - if (!info.has_value()) { + if (it == navigation_info_map_.end()) { continue; } + const WebTimeNavigationObserver::NavigationInfo& info = it->second; + // Web apps opened in the browser are reported separately from the browser // activity. - if (info->is_web_app) { + if (info.is_web_app) { continue; }
diff --git a/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.h b/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.h index 6cae49b8..bec78583 100644 --- a/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.h +++ b/chrome/browser/ash/child_accounts/time_limits/web_time_activity_provider.h
@@ -5,6 +5,7 @@ #ifndef CHROME_BROWSER_ASH_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_ACTIVITY_PROVIDER_H_ #define CHROME_BROWSER_ASH_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_ACTIVITY_PROVIDER_H_ +#include <map> #include <memory> #include <set> #include <vector> @@ -91,6 +92,12 @@ ChromeAppActivityState chrome_app_activity_state_ = ChromeAppActivityState::kInactive; + // A map from a navigation observer to its most recently reported navigation + // info. + std::map<const WebTimeNavigationObserver*, + WebTimeNavigationObserver::NavigationInfo> + navigation_info_map_; + base::ScopedObservation<AppServiceWrapper, AppServiceWrapper::EventListener> app_service_wrapper_observation_{this};
diff --git a/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.cc b/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.cc index f0420d3..515830c 100644 --- a/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.cc +++ b/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.cc
@@ -55,18 +55,15 @@ } void WebTimeNavigationObserver::PrimaryPageChanged(content::Page& page) { - if (!last_navigation_info_.has_value()) { - last_navigation_info_ = NavigationInfo(); - } - - last_navigation_info_->navigation_finish_time = base::Time::Now(); - last_navigation_info_->is_error = page.GetMainDocument().IsErrorDocument(); - last_navigation_info_->is_web_app = IsWebApp(); - last_navigation_info_->url = page.GetMainDocument().GetLastCommittedURL(); - last_navigation_info_->web_contents = web_contents(); + NavigationInfo info; + info.navigation_finish_time = base::Time::Now(); + info.is_error = page.GetMainDocument().IsErrorDocument(); + info.is_web_app = IsWebApp(); + info.url = page.GetMainDocument().GetLastCommittedURL(); + info.web_contents = web_contents(); for (auto& listener : listeners_) { - listener.OnWebActivityChanged(last_navigation_info_.value()); + listener.OnWebActivityChanged(info); } }
diff --git a/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.h b/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.h index 6b610be..d967e44 100644 --- a/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.h +++ b/chrome/browser/ash/child_accounts/time_limits/web_time_navigation_observer.h
@@ -5,8 +5,6 @@ #ifndef CHROME_BROWSER_ASH_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_NAVIGATION_OBSERVER_H_ #define CHROME_BROWSER_ASH_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_NAVIGATION_OBSERVER_H_ -#include <optional> - #include "base/memory/raw_ptr.h" #include "base/observer_list.h" #include "base/observer_list_types.h" @@ -67,10 +65,6 @@ void PrimaryPageChanged(content::Page& page) override; void WebContentsDestroyed() override; - const std::optional<NavigationInfo>& last_navigation_info() const { - return last_navigation_info_; - } - private: friend class content::WebContentsUserData<WebTimeNavigationObserver>; @@ -78,8 +72,6 @@ base::ObserverList<EventListener> listeners_; - std::optional<NavigationInfo> last_navigation_info_ = std::nullopt; - WEB_CONTENTS_USER_DATA_KEY_DECL(); };