Commit 8743f5d2 authored by yilkal's avatar yilkal Committed by Commit Bot

Refactor web time activity usage.

This cl refactors WebTimeActivityProvider from
keeping a map between active browser instances and
their active web contents. The map is not necessary and
has been replaced by active browser instances. We can easily
get access to the active web content via the tab strip model
of the browser instance.

There is no change in the functionality of the code.

Bug: 1051201
Change-Id: I38c928118fe0eb68c5593188e3db2d570e78ce9e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2181186
Commit-Queue: Yilkal Abe <yilkal@chromium.org>
Reviewed-by: default avatarAga Wronska <agawronska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#765355}
parent c2e064fe
......@@ -88,14 +88,13 @@ void WebTimeActivityProvider::OnWebActivityChanged(
// The browser window is not active. This may happen when a navigation
// finishes in the background.
if (!base::Contains(browser_activity_, browser))
if (!base::Contains(active_browsers_, browser))
return;
// Navigation finished in a background tab. Return.
if (browser->tab_strip_model()->GetActiveWebContents() != info.web_contents)
return;
browser_activity_[browser] = info.web_contents;
MaybeNotifyStateChange(base::Time::Now());
}
......@@ -115,7 +114,7 @@ void WebTimeActivityProvider::OnTabStripModelChanged(
const Browser* browser = GetBrowserForTabStripModel(tab_strip_model);
// If the Browser is not the active browser, simply return.
if (!base::Contains(browser_activity_, browser))
if (!base::Contains(active_browsers_, browser))
return;
// Let's check if the active tab changed, or the content::WebContents in the
......@@ -127,7 +126,6 @@ void WebTimeActivityProvider::OnTabStripModelChanged(
if (!(active_tab_changed || web_content_replaced))
return;
browser_activity_[browser] = tab_strip_model->GetActiveWebContents();
MaybeNotifyStateChange(base::Time::Now());
}
......@@ -136,9 +134,9 @@ void WebTimeActivityProvider::OnBrowserAdded(Browser* browser) {
}
void WebTimeActivityProvider::OnBrowserRemoved(Browser* browser) {
if (!base::Contains(browser_activity_, browser))
if (!base::Contains(active_browsers_, browser))
return;
browser_activity_.erase(browser);
active_browsers_.erase(browser);
MaybeNotifyStateChange(base::Time::Now());
}
......@@ -152,8 +150,7 @@ void WebTimeActivityProvider::OnAppActive(const AppId& app_id,
if (!browser)
return;
browser_activity_[browser] =
browser->tab_strip_model()->GetActiveWebContents();
active_browsers_.insert(browser);
MaybeNotifyStateChange(timestamp);
}
......@@ -167,10 +164,10 @@ void WebTimeActivityProvider::OnAppInactive(const AppId& app_id,
if (!browser)
return;
if (!base::Contains(browser_activity_, browser))
if (!base::Contains(active_browsers_, browser))
return;
browser_activity_.erase(browser);
active_browsers_.erase(browser);
MaybeNotifyStateChange(timestamp);
}
......@@ -208,13 +205,15 @@ WebTimeActivityProvider::CalculateChromeAppActivityState() const {
int active_count = 0;
int active_whitelisted_count = 0;
for (const std::pair<const Browser* const, content::WebContents*>& elem :
browser_activity_) {
if (!elem.second)
for (const Browser* browser : active_browsers_) {
const content::WebContents* contents =
browser->tab_strip_model()->GetActiveWebContents();
// If the active web content is null, return.
if (!contents)
continue;
const WebTimeNavigationObserver* observer =
WebTimeNavigationObserver::FromWebContents(elem.second);
WebTimeNavigationObserver::FromWebContents(contents);
// If |observer| is not instantiated, that means that
// WebTimeNavigationObserver::MaybeCreateForWebContents didn't create it.
......
......@@ -5,7 +5,6 @@
#ifndef CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_ACTIVITY_PROVIDER_H_
#define CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_ACTIVITY_PROVIDER_H_
#include <map>
#include <set>
#include "base/observer_list.h"
......@@ -23,10 +22,6 @@ namespace base {
class Time;
} // namespace base
namespace content {
class WebContents;
} // namespace content
class Browser;
namespace chromeos {
......@@ -96,8 +91,8 @@ class WebTimeActivityProvider : public WebTimeNavigationObserver::EventListener,
// The set of navigation observers |this| instance is listening to.
std::set<WebTimeNavigationObserver*> navigation_observers_;
// A map between active browser instances and their selected WebContents.
std::map<const Browser*, content::WebContents*> browser_activity_;
// A set of active browser instances.
std::set<const Browser*> active_browsers_;
// The default chrome app activity state.
ChromeAppActivityState chrome_app_activity_state_ =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment