1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/chromeos/child_accounts/child_user_service.h" 6 7 #include "base/time/time.h" 8 #include "chrome/browser/chromeos/child_accounts/time_limits/app_activity_registry.h" 9 #include "chrome/browser/chromeos/child_accounts/time_limits/app_time_controller.h" 10 #include "chrome/browser/chromeos/child_accounts/time_limits/app_types.h" 11 #include "chrome/browser/chromeos/child_accounts/time_limits/web_time_limit_enforcer.h" 12 #include "chrome/browser/profiles/profile.h" 13 #include "content/public/browser/browser_context.h" 14 #include "extensions/common/constants.h" 15 #include "url/gurl.h" 16 17 namespace chromeos { 18 19 // static 20 const char ChildUserService::kFamilyLinkHelperAppPackageName[] = 21 "com.google.android.apps.kids.familylinkhelper"; 22 // static 23 const char ChildUserService::kFamilyLinkHelperAppPlayStoreURL[] = 24 "https://play.google.com/store/apps/" 25 "details?id=com.google.android.apps.kids.familylinkhelper"; 26 TestApi(ChildUserService * service)27ChildUserService::TestApi::TestApi(ChildUserService* service) 28 : service_(service) {} 29 30 ChildUserService::TestApi::~TestApi() = default; 31 app_time_controller()32app_time::AppTimeController* ChildUserService::TestApi::app_time_controller() { 33 return service_->app_time_controller_.get(); 34 } 35 web_time_enforcer()36app_time::WebTimeLimitEnforcer* ChildUserService::TestApi::web_time_enforcer() { 37 return app_time_controller() 38 ? service_->app_time_controller_->web_time_enforcer() 39 : nullptr; 40 } 41 ChildUserService(content::BrowserContext * context)42ChildUserService::ChildUserService(content::BrowserContext* context) { 43 DCHECK(context); 44 if (app_time::AppTimeController::ArePerAppTimeLimitsEnabled()) { 45 app_time_controller_ = std::make_unique<app_time::AppTimeController>( 46 Profile::FromBrowserContext(context)); 47 } 48 } 49 50 ChildUserService::~ChildUserService() = default; 51 PauseWebActivity(const std::string & app_service_id)52void ChildUserService::PauseWebActivity(const std::string& app_service_id) { 53 DCHECK(app_time_controller_); 54 55 // Pause web activity only if the app is chrome. 56 if (app_service_id != extension_misc::kChromeAppId) 57 return; 58 59 app_time::WebTimeLimitEnforcer* web_time_enforcer = 60 app_time_controller_->web_time_enforcer(); 61 DCHECK(web_time_enforcer); 62 63 const base::Optional<app_time::AppLimit>& time_limit = 64 app_time_controller_->app_registry()->GetWebTimeLimit(); 65 DCHECK(time_limit.has_value()); 66 DCHECK_EQ(time_limit->restriction(), app_time::AppRestriction::kTimeLimit); 67 DCHECK(time_limit->daily_limit().has_value()); 68 69 web_time_enforcer->OnWebTimeLimitReached(time_limit->daily_limit().value()); 70 } 71 ResumeWebActivity(const std::string & app_service_id)72void ChildUserService::ResumeWebActivity(const std::string& app_service_id) { 73 DCHECK(app_time_controller_); 74 75 // Only unpause web activity if the app is chrome. 76 if (app_service_id != extension_misc::kChromeAppId) 77 return; 78 79 app_time::WebTimeLimitEnforcer* web_time_enforcer = 80 app_time_controller_->web_time_enforcer(); 81 DCHECK(web_time_enforcer); 82 83 web_time_enforcer->OnWebTimeLimitEnded(); 84 } 85 GetTimeLimitForApp(const std::string & app_service_id,apps::mojom::AppType app_type)86base::Optional<base::TimeDelta> ChildUserService::GetTimeLimitForApp( 87 const std::string& app_service_id, 88 apps::mojom::AppType app_type) { 89 if (!app_time_controller_) 90 return base::nullopt; 91 92 return app_time_controller_->GetTimeLimitForApp(app_service_id, app_type); 93 } 94 95 app_time::AppActivityReportInterface::ReportParams GenerateAppActivityReport(enterprise_management::ChildStatusReportRequest * report)96ChildUserService::GenerateAppActivityReport( 97 enterprise_management::ChildStatusReportRequest* report) { 98 DCHECK(app_time_controller_); 99 app_time_controller_->app_registry()->GenerateHiddenApps(report); 100 return app_time_controller_->app_registry()->GenerateAppActivityReport( 101 report); 102 } 103 AppActivityReportSubmitted(base::Time report_generation_timestamp)104void ChildUserService::AppActivityReportSubmitted( 105 base::Time report_generation_timestamp) { 106 DCHECK(app_time_controller_); 107 app_time_controller_->app_registry()->OnSuccessfullyReported( 108 report_generation_timestamp); 109 } 110 WebTimeLimitReached() const111bool ChildUserService::WebTimeLimitReached() const { 112 if (!app_time_controller_ || !app_time_controller_->web_time_enforcer()) 113 return false; 114 return app_time_controller_->web_time_enforcer()->blocked(); 115 } 116 WebTimeLimitAllowlistedURL(const GURL & url) const117bool ChildUserService::WebTimeLimitAllowlistedURL(const GURL& url) const { 118 if (!app_time_controller_) 119 return false; 120 DCHECK(app_time_controller_->web_time_enforcer()); 121 return app_time_controller_->web_time_enforcer()->IsURLAllowlisted(url); 122 } 123 AppTimeLimitAllowlistedApp(const app_time::AppId & app_id) const124bool ChildUserService::AppTimeLimitAllowlistedApp( 125 const app_time::AppId& app_id) const { 126 if (!app_time_controller_) 127 return false; 128 return app_time_controller_->app_registry()->IsAllowlistedApp(app_id); 129 } 130 GetWebTimeLimit() const131base::TimeDelta ChildUserService::GetWebTimeLimit() const { 132 DCHECK(app_time_controller_); 133 DCHECK(app_time_controller_->web_time_enforcer()); 134 return app_time_controller_->web_time_enforcer()->time_limit(); 135 } 136 Shutdown()137void ChildUserService::Shutdown() { 138 if (app_time_controller_) { 139 app_time_controller_->app_registry()->SaveAppActivity(); 140 app_time_controller_->RecordMetricsOnShutdown(); 141 app_time_controller_.reset(); 142 } 143 } 144 145 } // namespace chromeos 146