Commit fff28b03 authored by yilkal's avatar yilkal Committed by Commit Bot

Adds WebTimeNavigationObserver.

WebTimeNavigationObserver is used to notify its event
listeners when a navigation finishes.

Bug: 1034551
Change-Id: Ic233f11bbf17b6c0c6eccda9b9f52c8ded3feea7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2004994Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarAga Wronska <agawronska@chromium.org>
Commit-Queue: Yilkal Abe <yilkal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#734987}
parent ba650e7b
...@@ -833,6 +833,8 @@ source_set("chromeos") { ...@@ -833,6 +833,8 @@ source_set("chromeos") {
"child_accounts/time_limits/web_time_limit_interface.h", "child_accounts/time_limits/web_time_limit_interface.h",
"child_accounts/time_limits/web_time_limit_navigation_throttle.cc", "child_accounts/time_limits/web_time_limit_navigation_throttle.cc",
"child_accounts/time_limits/web_time_limit_navigation_throttle.h", "child_accounts/time_limits/web_time_limit_navigation_throttle.h",
"child_accounts/time_limits/web_time_navigation_observer.cc",
"child_accounts/time_limits/web_time_navigation_observer.h",
"child_accounts/usage_time_limit_processor.cc", "child_accounts/usage_time_limit_processor.cc",
"child_accounts/usage_time_limit_processor.h", "child_accounts/usage_time_limit_processor.h",
"child_accounts/usage_time_state_notifier.cc", "child_accounts/usage_time_state_notifier.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/child_accounts/time_limits/web_time_navigation_observer.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/chromeos/child_accounts/time_limits/app_time_controller.h"
#include "chrome/browser/chromeos/child_accounts/time_limits/web_time_limit_enforcer.h"
#include "chrome/browser/web_applications/components/web_app_tab_helper_base.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
namespace chromeos {
namespace app_time {
// static
void WebTimeNavigationObserver::MaybeCreateForWebContents(
content::WebContents* web_contents) {
DCHECK(web_contents);
if (!AppTimeController::ArePerAppTimeLimitsEnabled() ||
!WebTimeLimitEnforcer::IsEnabled()) {
return;
}
if (!FromWebContents(web_contents)) {
web_contents->SetUserData(
UserDataKey(),
base::WrapUnique(new WebTimeNavigationObserver(web_contents)));
}
}
WebTimeNavigationObserver::~WebTimeNavigationObserver() = default;
void WebTimeNavigationObserver::AddObserver(
WebTimeNavigationObserver::EventListener* listener) {
listeners_.AddObserver(listener);
}
void WebTimeNavigationObserver::RemoveObserver(
WebTimeNavigationObserver::EventListener* listener) {
listeners_.RemoveObserver(listener);
}
bool WebTimeNavigationObserver::IsWebApp() const {
const web_app::WebAppTabHelperBase* web_app_helper =
web_app::WebAppTabHelperBase::FromWebContents(web_contents());
return !web_app_helper->GetAppId().empty();
}
void WebTimeNavigationObserver::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// TODO(yilkal): Handle case when navigation didn't happen in the main frame.
if (!navigation_handle->IsInMainFrame())
return;
last_navigation_info_.navigation_finish_time = base::Time::Now();
last_navigation_info_.is_error = navigation_handle->IsErrorPage();
last_navigation_info_.is_web_app = IsWebApp();
last_navigation_info_.url = navigation_handle->GetURL();
last_navigation_info_.web_contents = web_contents();
for (auto& listener : listeners_)
listener.OnWebActivityChanged(last_navigation_info_);
}
void WebTimeNavigationObserver::WebContentsDestroyed() {
for (auto& listener : listeners_)
listener.WebTimeNavigationObserverDestroyed(this);
}
WebTimeNavigationObserver::WebTimeNavigationObserver(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {}
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebTimeNavigationObserver)
} // namespace app_time
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_NAVIGATION_OBSERVER_H_
#define CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_NAVIGATION_OBSERVER_H_
#include "base/observer_list_types.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/child_accounts/time_limits/app_types.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
namespace content {
class WebContents;
class NavigationHandle;
} // namespace content
class GURL;
namespace chromeos {
namespace app_time {
class WebTimeNavigationObserver
: public content::WebContentsUserData<WebTimeNavigationObserver>,
public content::WebContentsObserver {
public:
struct NavigationInfo {
base::Time navigation_finish_time;
// Boolean to specify if the navigation ended in an error page.
bool is_error;
// Boolean to specify if the WebContents is hosting a web app.
bool is_web_app;
// The url that is being hosted in WebContents.
GURL url;
// The WebContent where the navigation has taken place.
content::WebContents* web_contents;
};
class EventListener : public base::CheckedObserver {
public:
virtual void OnWebActivityChanged(const NavigationInfo& info) {}
virtual void WebTimeNavigationObserverDestroyed(
const WebTimeNavigationObserver* observer) {}
};
static void MaybeCreateForWebContents(content::WebContents* web_contents);
static void CreateForWebContents(content::WebContents* web_contents) = delete;
~WebTimeNavigationObserver() override;
void AddObserver(EventListener* listener);
void RemoveObserver(EventListener* listener);
bool IsWebApp() const;
// content::WebContentsObserver:
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
void WebContentsDestroyed() override;
const NavigationInfo& last_navigation_info() const {
return last_navigation_info_;
}
private:
friend class content::WebContentsUserData<WebTimeNavigationObserver>;
explicit WebTimeNavigationObserver(content::WebContents* web_contents);
WebTimeNavigationObserver(const WebTimeNavigationObserver&) = delete;
WebTimeNavigationObserver& operator=(const WebTimeNavigationObserver&) =
delete;
base::ObserverList<EventListener> listeners_;
NavigationInfo last_navigation_info_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
} // namespace app_time
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_NAVIGATION_OBSERVER_H_
...@@ -164,6 +164,10 @@ ...@@ -164,6 +164,10 @@
#include "chrome/browser/supervised_user/supervised_user_navigation_observer.h" #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
#endif #endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/child_accounts/time_limits/web_time_navigation_observer.h"
#endif
using content::WebContents; using content::WebContents;
namespace { namespace {
...@@ -377,6 +381,11 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) { ...@@ -377,6 +381,11 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
SupervisedUserNavigationObserver::MaybeCreateForWebContents(web_contents); SupervisedUserNavigationObserver::MaybeCreateForWebContents(web_contents);
#endif #endif
#if defined(OS_CHROMEOS)
chromeos::app_time::WebTimeNavigationObserver::MaybeCreateForWebContents(
web_contents);
#endif
if (predictors::LoadingPredictorFactory::GetForProfile(profile)) if (predictors::LoadingPredictorFactory::GetForProfile(profile))
predictors::LoadingPredictorTabHelper::CreateForWebContents(web_contents); predictors::LoadingPredictorTabHelper::CreateForWebContents(web_contents);
......
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