0

[ios] Create ScreenTimeHistoryDeleter

* ScreenTimeHistoryDeleter observes synced history deletions and
makes the corresponding deletions on ScreenTime.

Bug: 1123704
Change-Id: I935c2c15e8dc0aa98bc022e81e63e65c2b59d65c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2393978
Commit-Queue: edchin <edchin@chromium.org>
Reviewed-by: Rohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807194}
This commit is contained in:
edchin
2020-09-15 22:00:13 +00:00
committed by Commit Bot
parent d9daac114a
commit 0ee9e7c0c9
9 changed files with 244 additions and 0 deletions

@ -117,6 +117,8 @@ source_set("browser_state_impl") {
"//ios/chrome/browser/prefs:browser_prefs",
"//ios/chrome/browser/reading_list",
"//ios/chrome/browser/safe_browsing",
"//ios/chrome/browser/screen_time",
"//ios/chrome/browser/screen_time:feature_flags",
"//ios/chrome/browser/search_engines",
"//ios/chrome/browser/send_tab_to_self",
"//ios/chrome/browser/sessions",

@ -42,6 +42,7 @@
#include "ios/chrome/browser/reading_list/reading_list_model_factory.h"
#import "ios/chrome/browser/safe_browsing/real_time_url_lookup_service_factory.h"
#import "ios/chrome/browser/safe_browsing/verdict_cache_manager_factory.h"
#import "ios/chrome/browser/screen_time/features.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#include "ios/chrome/browser/signin/about_signin_internals_factory.h"
#include "ios/chrome/browser/signin/account_consistency_service_factory.h"
@ -64,6 +65,10 @@
#include "ios/chrome/browser/unified_consent/unified_consent_service_factory.h"
#include "ios/chrome/browser/webdata_services/web_data_service_factory.h"
#if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
#import "ios/chrome/browser/screen_time/screen_time_history_deleter_factory.h"
#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@ -138,4 +143,12 @@ void EnsureBrowserStateKeyedServiceFactoriesBuilt() {
if (IsURLBlocklistEnabled()) {
PolicyBlocklistServiceFactory::GetInstance();
}
#if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
if (@available(iOS 14, *)) {
if (IsScreenTimeIntegrationEnabled()) {
ScreenTimeHistoryDeleterFactory::GetInstance();
}
}
#endif // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
}

@ -2,6 +2,31 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/config/ios/ios_sdk.gni")
source_set("screen_time") {
# TODO(crbug.com/1123704): remove when it is recommended to build Chromium
# with version 12.0 or later of Xcode.
if (xcode_version_int >= 1200) {
sources = [
"screen_time_history_deleter.h",
"screen_time_history_deleter.mm",
"screen_time_history_deleter_factory.h",
"screen_time_history_deleter_factory.mm",
]
configs += [ "//build/config/compiler:enable_arc" ]
deps = [
"//base",
"//components/history/core/browser",
"//components/keyed_service/core",
"//components/keyed_service/ios",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/history",
"//net",
]
}
}
source_set("feature_flags") {
sources = [
"features.h",

@ -0,0 +1,41 @@
// 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 IOS_CHROME_BROWSER_SCREEN_TIME_SCREEN_TIME_HISTORY_DELETER_H_
#define IOS_CHROME_BROWSER_SCREEN_TIME_SCREEN_TIME_HISTORY_DELETER_H_
#include "base/macros.h"
#include "components/history/core/browser/history_service_observer.h"
#include "components/keyed_service/core/keyed_service.h"
namespace history {
class HistoryService;
}
@class STWebHistory;
// ScreenTimeHistoryDeleter is responsible for deleting ScreenTime history when
// Chrome history is deleted.
class API_AVAILABLE(ios(14.0)) ScreenTimeHistoryDeleter
: public KeyedService,
public history::HistoryServiceObserver {
public:
explicit ScreenTimeHistoryDeleter(history::HistoryService* history_service);
~ScreenTimeHistoryDeleter() override;
// KeyedService:
void Shutdown() override;
private:
// history::HistoryServiceObserver:
void OnURLsDeleted(history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) override;
history::HistoryService* history_service_;
STWebHistory* screen_time_history_;
DISALLOW_COPY_AND_ASSIGN(ScreenTimeHistoryDeleter);
};
#endif // IOS_CHROME_BROWSER_SCREEN_TIME_SCREEN_TIME_HISTORY_DELETER_H_

@ -0,0 +1,58 @@
// 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.
#import "ios/chrome/browser/screen_time/screen_time_history_deleter.h"
#import <ScreenTime/ScreenTime.h>
#include "base/time/time.h"
#include "components/history/core/browser/history_service.h"
#include "net/base/mac/url_conversions.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Converts base::Time to NSDate.
NSDate* NSDateFromTime(const base::Time& time) {
return [NSDate dateWithTimeIntervalSince1970:time.ToDoubleT()];
}
} // namespace
ScreenTimeHistoryDeleter::ScreenTimeHistoryDeleter(
history::HistoryService* history_service)
: history_service_(history_service) {
DCHECK(history_service_);
history_service_->AddObserver(this);
screen_time_history_ = [[STWebHistory alloc] init];
}
ScreenTimeHistoryDeleter::~ScreenTimeHistoryDeleter() = default;
void ScreenTimeHistoryDeleter::Shutdown() {
if (history_service_)
history_service_->RemoveObserver(this);
history_service_ = nullptr;
screen_time_history_ = nil;
}
void ScreenTimeHistoryDeleter::OnURLsDeleted(
history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) {
if (deletion_info.IsAllHistory()) {
[screen_time_history_ deleteAllHistory];
} else if (deletion_info.time_range().IsValid()) {
const history::DeletionTimeRange& range = deletion_info.time_range();
NSDateInterval* interval =
[[NSDateInterval alloc] initWithStartDate:NSDateFromTime(range.begin())
endDate:NSDateFromTime(range.end())];
[screen_time_history_ deleteHistoryDuringInterval:interval];
} else {
for (const history::URLRow& row : deletion_info.deleted_rows()) {
NSURL* url = net::NSURLWithGURL(row.url());
[screen_time_history_ deleteHistoryForURL:url];
}
}
}

@ -0,0 +1,41 @@
// 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 IOS_CHROME_BROWSER_SCREEN_TIME_SCREEN_TIME_HISTORY_DELETER_FACTORY_H_
#define IOS_CHROME_BROWSER_SCREEN_TIME_SCREEN_TIME_HISTORY_DELETER_FACTORY_H_
#include "base/macros.h"
#include "base/no_destructor.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
class ChromeBrowserState;
class ScreenTimeHistoryDeleter;
// Factory that owns and associates a ScreenTimeHistoryDeleter with
// ChromeBrowserState.
class API_AVAILABLE(ios(14.0)) ScreenTimeHistoryDeleterFactory
: public BrowserStateKeyedServiceFactory {
public:
static ScreenTimeHistoryDeleter* GetForBrowserState(
ChromeBrowserState* browser_state);
static ScreenTimeHistoryDeleterFactory* GetInstance();
private:
friend class base::NoDestructor<ScreenTimeHistoryDeleterFactory>;
ScreenTimeHistoryDeleterFactory();
~ScreenTimeHistoryDeleterFactory() override;
// BrowserStateKeyedServiceFactory implementation.
std::unique_ptr<KeyedService> BuildServiceInstanceFor(
web::BrowserState* context) const override;
web::BrowserState* GetBrowserStateToUse(
web::BrowserState* context) const override;
bool ServiceIsNULLWhileTesting() const override;
DISALLOW_COPY_AND_ASSIGN(ScreenTimeHistoryDeleterFactory);
};
#endif // IOS_CHROME_BROWSER_SCREEN_TIME_SCREEN_TIME_HISTORY_DELETER_FACTORY_H_

@ -0,0 +1,59 @@
// 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.
#import "ios/chrome/browser/screen_time/screen_time_history_deleter_factory.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/history/history_service_factory.h"
#import "ios/chrome/browser/screen_time/screen_time_history_deleter.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
// static
ScreenTimeHistoryDeleter* ScreenTimeHistoryDeleterFactory::GetForBrowserState(
ChromeBrowserState* browser_state) {
return static_cast<ScreenTimeHistoryDeleter*>(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}
// static
ScreenTimeHistoryDeleterFactory*
ScreenTimeHistoryDeleterFactory::GetInstance() {
static base::NoDestructor<ScreenTimeHistoryDeleterFactory> instance;
return instance.get();
}
ScreenTimeHistoryDeleterFactory::ScreenTimeHistoryDeleterFactory()
: BrowserStateKeyedServiceFactory(
"ScreenTimeHistoryDeleter",
BrowserStateDependencyManager::GetInstance()) {
DependsOn(ios::HistoryServiceFactory::GetInstance());
}
ScreenTimeHistoryDeleterFactory::~ScreenTimeHistoryDeleterFactory() {}
std::unique_ptr<KeyedService>
ScreenTimeHistoryDeleterFactory::BuildServiceInstanceFor(
web::BrowserState* context) const {
ChromeBrowserState* browser_state =
ChromeBrowserState::FromBrowserState(context);
history::HistoryService* history_service =
ios::HistoryServiceFactory::GetForBrowserState(
browser_state, ServiceAccessType::EXPLICIT_ACCESS);
return std::make_unique<ScreenTimeHistoryDeleter>(history_service);
}
web::BrowserState* ScreenTimeHistoryDeleterFactory::GetBrowserStateToUse(
web::BrowserState* context) const {
return GetBrowserStateRedirectedInIncognito(context);
}
bool ScreenTimeHistoryDeleterFactory::ServiceIsNULLWhileTesting() const {
return true;
}

@ -26,6 +26,7 @@ source_set("screen_time") {
"//base",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/main:public",
"//ios/chrome/browser/screen_time",
"//ios/chrome/browser/ui/coordinators:chrome_coordinators",
"//ios/chrome/browser/web_state_list",
"//ios/chrome/common/ui/util",

@ -6,6 +6,7 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/main/browser.h"
#import "ios/chrome/browser/screen_time/screen_time_history_deleter_factory.h"
#import "ios/chrome/browser/ui/screen_time/screen_time_mediator.h"
#import "ios/chrome/browser/ui/screen_time/screen_time_view_controller.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"
@ -34,6 +35,9 @@
self.mediator = [[ScreenTimeMediator alloc]
initWithWebStateList:self.browser->GetWebStateList()
suppressUsageRecording:self.browser->GetBrowserState()->IsOffTheRecord()];
ScreenTimeHistoryDeleterFactory::GetForBrowserState(
self.browser->GetBrowserState());
}
- (void)stop {