Commit 3121011a authored by yilkal's avatar yilkal Committed by Commit Bot

Adds WebTimeLimitEnforcer.

This CL adds WebTimeLimitEnforcer. The enforcer class finds and
reloads all WebContents that can be found by iterating through
the TabStripModel of each of the browsers in BrowserList.
Reloading triggers a new navigation which will trigger
WebTimeLimitNavigationThrottle to block the WebContents.



Bug: 1015661
Change-Id: I3d5e86f34c2eeb48f7a8ad4ca126cb4d4cde1cca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1885510
Commit-Queue: Yilkal Abe <yilkal@chromium.org>
Reviewed-by: default avatarAga Wronska <agawronska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713507}
parent 7d8dbba1
......@@ -775,6 +775,8 @@ source_set("chromeos") {
"child_accounts/time_limit_notifier.h",
"child_accounts/time_limit_override.cc",
"child_accounts/time_limit_override.h",
"child_accounts/time_limits/web_time_limit_enforcer.cc",
"child_accounts/time_limits/web_time_limit_enforcer.h",
"child_accounts/usage_time_limit_processor.cc",
"child_accounts/usage_time_limit_processor.h",
"child_accounts/usage_time_state_notifier.cc",
......
// Copyright 2019 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_limit_enforcer.h"
#include <memory>
#include "base/feature_list.h"
#include "base/stl_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_features.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/reload_type.h"
#include "content/public/browser/web_contents.h"
namespace {
// URL schemes not on this list: (e.g., file:// and chrome://,
// chrome-extension:// ...) will always be allowed.
const char* const kFilteredSchemes[] = {"http", "https", "ftp", "ws", "wss"};
bool IsSchemeFiltered(const GURL& url) {
for (const auto* i : kFilteredSchemes) {
if (i == url.scheme())
return true;
}
return false;
}
} // namespace
// static
bool WebTimeLimitEnforcer::IsEnabled() {
return base::FeatureList::IsEnabled(features::kWebTimeLimits);
}
WebTimeLimitEnforcer::WebTimeLimitEnforcer() = default;
WebTimeLimitEnforcer::~WebTimeLimitEnforcer() = default;
void WebTimeLimitEnforcer::OnWebTimeLimitReached() {
if (chrome_blocked_)
return;
chrome_blocked_ = true;
ReloadAllWebContents();
}
void WebTimeLimitEnforcer::OnWebTimeLimitEnded() {
if (!chrome_blocked_)
return;
chrome_blocked_ = false;
ReloadAllWebContents();
}
void WebTimeLimitEnforcer::OnWhitelistAdded(const GURL& url) {
auto result = whitelisted_urls_.insert(url);
bool inserted = result.second;
if (inserted && blocked())
ReloadAllWebContents();
}
void WebTimeLimitEnforcer::OnWhitelistRemoved(const GURL& url) {
if (!base::Contains(whitelisted_urls_, url))
return;
whitelisted_urls_.erase(url);
if (blocked())
ReloadAllWebContents();
}
bool WebTimeLimitEnforcer::IsURLWhitelisted(const GURL& url) const {
if (!IsSchemeFiltered(url))
return true;
return base::Contains(whitelisted_urls_, url);
}
void WebTimeLimitEnforcer::ReloadAllWebContents() {
auto* browser_list = BrowserList::GetInstance();
for (auto* browser : *browser_list) {
auto* tab_strip_model = browser->tab_strip_model();
for (int i = 0; i < tab_strip_model->count(); i++) {
auto* web_content = tab_strip_model->GetWebContentsAt(i);
web_content->GetController().Reload(content::ReloadType::NORMAL,
/* check_for_repost */ false);
}
}
}
// Copyright 2019 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_LIMIT_ENFORCER_H_
#define CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_LIMIT_ENFORCER_H_
#include <algorithm>
#include <memory>
#include <set>
#include "base/time/time.h"
class GURL;
class WebTimeLimitEnforcer {
public:
static bool IsEnabled();
WebTimeLimitEnforcer();
~WebTimeLimitEnforcer();
// Delete copy constructor and copy assignment operator.
WebTimeLimitEnforcer(const WebTimeLimitEnforcer& enforcer) = delete;
WebTimeLimitEnforcer& operator=(const WebTimeLimitEnforcer& enforcer) =
delete;
// Delete move constructor and move assignment operator.
WebTimeLimitEnforcer(WebTimeLimitEnforcer&& enforcer) = delete;
WebTimeLimitEnforcer& operator=(WebTimeLimitEnforcer&& enforcer) = delete;
// TODO(crbug/1015661) The following should be private observer calls once the
// observer pattern has been set up for this.
void OnWebTimeLimitReached();
void OnWebTimeLimitEnded();
void OnWhitelistAdded(const GURL& url);
void OnWhitelistRemoved(const GURL& url);
bool IsURLWhitelisted(const GURL& url) const;
void set_time_limit(base::TimeDelta time_limit) { time_limit_ = time_limit; }
bool blocked() const { return chrome_blocked_; }
base::TimeDelta time_limit() const { return time_limit_; }
private:
void ReloadAllWebContents();
bool chrome_blocked_ = false;
base::TimeDelta time_limit_;
std::set<GURL> whitelisted_urls_;
};
#endif // CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMITS_WEB_TIME_LIMIT_ENFORCER_H_
......@@ -877,4 +877,10 @@ const base::Feature kAccessibilityInternalsPageImprovements{
"AccessibilityInternalsPageImprovements",
base::FEATURE_DISABLED_BY_DEFAULT};
// Enables setting time limit for Chrome and PWA's on child user device.
#if defined(OS_CHROMEOS)
const base::Feature kWebTimeLimits{"WebTimeLimits",
base::FEATURE_DISABLED_BY_DEFAULT};
#endif // defined(OS_CHROMEOS)
} // namespace features
......@@ -531,6 +531,11 @@ extern const base::Feature kWriteBasicSystemProfileToPersistentHistogramsFile;
COMPONENT_EXPORT(CHROME_FEATURES)
extern const base::Feature kAccessibilityInternalsPageImprovements;
#if defined(OS_CHROMEOS)
COMPONENT_EXPORT(CHROME_FEATURES)
extern const base::Feature kWebTimeLimits;
#endif // defined(OS_CHROMEOS)
bool PrefServiceEnabled();
// DON'T ADD RANDOM STUFF HERE. Put it in the main section above in
......
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