Skip to content

Commit

Permalink
Add clean copy url service interface for iOS (uplift to 1.58.x) (#20017)
Browse files Browse the repository at this point in the history
Add clean copy url service interface for iOS (#19079)

Co-authored-by: bridiver <[email protected]>
  • Loading branch information
cuba and bridiver authored Sep 6, 2023
1 parent 8d6180b commit 0290ad5
Show file tree
Hide file tree
Showing 21 changed files with 480 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import("//brave/ios/browser/api/query_filter/headers.gni")
import("//brave/ios/browser/api/session_restore/headers.gni")
import("//brave/ios/browser/api/skus/headers.gni")
import("//brave/ios/browser/api/url/headers.gni")
import("//brave/ios/browser/api/url_sanitizer/headers.gni")
import("//brave/ios/browser/api/web/web_state/headers.gni")
import("//brave/ios/browser/url_sanitizer/headers.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/ios/rules.gni")

Expand Down Expand Up @@ -68,6 +70,8 @@ brave_core_public_headers = [

brave_core_public_headers += ads_public_headers
brave_core_public_headers += brave_shields_public_headers
brave_core_public_headers += browser_api_url_sanitizer_public_headers
brave_core_public_headers += browser_url_sanitizer_public_headers
brave_core_public_headers += brave_stats_public_headers
brave_core_public_headers += brave_wallet_public_headers
brave_core_public_headers += browser_api_certificate_public_headers
Expand Down
2 changes: 1 addition & 1 deletion ios/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ source_set("browser") {
"//base",
"//brave/browser/sync",
"//brave/chromium_src/ios/chrome/browser/shared/model/prefs",
"//brave/ios/browser/application_context",
"//brave/ios/browser/browser_state",
"//components/flags_ui",
"//components/metrics",
Expand All @@ -51,7 +52,6 @@ source_set("browser") {
"//ios/chrome/browser/browser_state:browser_state",
"//ios/chrome/browser/flags",
"//ios/chrome/browser/flags:client_side",
"//ios/chrome/browser/shared/model/application_context",
"//ios/chrome/browser/shared/model/browser_state",
"//ios/chrome/browser/shared/model/paths",
"//ios/chrome/browser/shared/model/url:constants",
Expand Down
20 changes: 20 additions & 0 deletions ios/browser/api/url_sanitizer/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2023 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

source_set("url_sanitizer") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"url_sanitizer_service+private.h",
"url_sanitizer_service.h",
"url_sanitizer_service.mm",
]
deps = [
"//base",
"//brave/components/url_sanitizer/browser",
"//net",
"//url",
]
frameworks = [ "Foundation.framework" ]
}
7 changes: 7 additions & 0 deletions ios/browser/api/url_sanitizer/headers.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2023 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

browser_api_url_sanitizer_public_headers =
[ "//brave/ios/browser/api/url_sanitizer/url_sanitizer_service.h" ]
26 changes: 26 additions & 0 deletions ios/browser/api/url_sanitizer/url_sanitizer_service+private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_IOS_BROWSER_API_URL_SANITIZER_URL_SANITIZER_SERVICE_PRIVATE_H_
#define BRAVE_IOS_BROWSER_API_URL_SANITIZER_URL_SANITIZER_SERVICE_PRIVATE_H_

#import <Foundation/Foundation.h>

#include "base/memory/raw_ptr.h"
#include "brave/components/url_sanitizer/browser/url_sanitizer_service.h"
#include "brave/ios/browser/api/url_sanitizer/url_sanitizer_service.h"

NS_ASSUME_NONNULL_BEGIN

@interface URLSanitizerService (Private)

- (instancetype)initWithURLSanitizerService:
(brave::URLSanitizerService*)urlSanitizer;

@end

NS_ASSUME_NONNULL_END

#endif // BRAVE_IOS_BROWSER_API_URL_SANITIZER_URL_SANITIZER_SERVICE_PRIVATE_H_
29 changes: 29 additions & 0 deletions ios/browser/api/url_sanitizer/url_sanitizer_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_IOS_BROWSER_API_URL_SANITIZER_URL_SANITIZER_SERVICE_H_
#define BRAVE_IOS_BROWSER_API_URL_SANITIZER_URL_SANITIZER_SERVICE_H_

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

OBJC_EXPORT
@interface URLSanitizerService : NSObject
- (instancetype)init NS_UNAVAILABLE;

/**
Sanitizes the given URL.
@param url The URL to be sanitized.
@return A sanitized NSURL object.
*/
- (nullable NSURL*)sanitizeURL:(NSURL*)url;

@end

NS_ASSUME_NONNULL_END

#endif // BRAVE_IOS_BROWSER_API_URL_SANITIZER_URL_SANITIZER_SERVICE_H_
39 changes: 39 additions & 0 deletions ios/browser/api/url_sanitizer/url_sanitizer_service.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/ios/browser/api/url_sanitizer/url_sanitizer_service+private.h"

#include <string>

#include "base/memory/raw_ptr.h"
#include "brave/components/url_sanitizer/browser/url_sanitizer_service.h"
#import "net/base/mac/url_conversions.h"
#include "url/gurl.h"

@interface URLSanitizerService () {
raw_ptr<brave::URLSanitizerService> urlSanitizer_; // NOT OWNED
}

@end

@implementation URLSanitizerService

- (instancetype)initWithURLSanitizerService:
(brave::URLSanitizerService*)urlSanitizer {
self = [super init];
if (self) {
urlSanitizer_ = urlSanitizer;
}
return self;
}

- (nullable NSURL*)sanitizeURL:(NSURL*)url {
DCHECK(urlSanitizer_);
GURL gurl = net::GURLWithNSURL(url);
GURL cleanURL = urlSanitizer_->SanitizeURL(gurl);
return net::NSURLWithGURL(cleanURL);
}

@end
21 changes: 21 additions & 0 deletions ios/browser/application_context/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2023 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

import("//build/config/ios/rules.gni")
import("//ios/build/config.gni")

source_set("application_context") {
sources = [
"brave_application_context_impl.h",
"brave_application_context_impl.mm",
]
deps = [
"//base",
"//brave/components/brave_component_updater/browser",
"//brave/components/url_sanitizer/browser",
"//ios/chrome/browser/application_context",
"//ios/chrome/browser/shared/model/application_context",
]
}
57 changes: 57 additions & 0 deletions ios/browser/application_context/brave_application_context_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_IOS_BROWSER_APPLICATION_CONTEXT_BRAVE_APPLICATION_CONTEXT_IMPL_H_
#define BRAVE_IOS_BROWSER_APPLICATION_CONTEXT_BRAVE_APPLICATION_CONTEXT_IMPL_H_

#include <memory>
#include <string>

#include "brave/components/brave_component_updater/browser/brave_component.h"
#include "brave/components/url_sanitizer/browser/url_sanitizer_component_installer.h"
#include "ios/chrome/browser/application_context/application_context_impl.h"

namespace base {
class CommandLine;
class SequencedTaskRunner;
} // namespace base

/// This extends the behaviors of the ApplicationContext
class BraveApplicationContextImpl : public ApplicationContextImpl {
public:
// Out-of-line constructor declaration
BraveApplicationContextImpl(
base::SequencedTaskRunner* local_state_task_runner,
const base::CommandLine& command_line,
const std::string& locale,
const std::string& country);

brave::URLSanitizerComponentInstaller* url_sanitizer_component_installer();

// Disable copy constructor and assignment operator
BraveApplicationContextImpl(const BraveApplicationContextImpl&) = delete;
BraveApplicationContextImpl& operator=(const BraveApplicationContextImpl&) =
delete;

// Start any services that we may need later
void StartBraveServices();

// Out-of-line destructor declaration
~BraveApplicationContextImpl() override;

private:
brave_component_updater::BraveComponent::Delegate*
brave_component_updater_delegate();
brave_component_updater::LocalDataFilesService* local_data_files_service();

std::unique_ptr<brave_component_updater::BraveComponent::Delegate>
brave_component_updater_delegate_;
std::unique_ptr<brave_component_updater::LocalDataFilesService>
local_data_files_service_;
std::unique_ptr<brave::URLSanitizerComponentInstaller>
url_sanitizer_component_installer_;
};

#endif // BRAVE_IOS_BROWSER_APPLICATION_CONTEXT_BRAVE_APPLICATION_CONTEXT_IMPL_H_
69 changes: 69 additions & 0 deletions ios/browser/application_context/brave_application_context_impl.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/ios/browser/application_context/brave_application_context_impl.h"

#include <string>

#import "base/command_line.h"
#import "base/task/sequenced_task_runner.h"
#include "brave/components/brave_component_updater/browser/brave_component.h"
#include "brave/components/brave_component_updater/browser/brave_component_updater_delegate.h"
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
#include "brave/components/url_sanitizer/browser/url_sanitizer_component_installer.h"
#include "ios/chrome/browser/shared/model/application_context/application_context.h"

BraveApplicationContextImpl::BraveApplicationContextImpl(
base::SequencedTaskRunner* local_state_task_runner,
const base::CommandLine& command_line,
const std::string& locale,
const std::string& country)
: ApplicationContextImpl(local_state_task_runner,
command_line,
locale,
country) {}

inline BraveApplicationContextImpl::~BraveApplicationContextImpl() = default;

brave_component_updater::BraveComponent::Delegate*
BraveApplicationContextImpl::brave_component_updater_delegate() {
if (!brave_component_updater_delegate_) {
brave_component_updater_delegate_ =
std::make_unique<brave::BraveComponentUpdaterDelegate>(
GetComponentUpdateService(), GetLocalState(),
GetApplicationLocale());
}

return brave_component_updater_delegate_.get();
}

brave_component_updater::LocalDataFilesService*
BraveApplicationContextImpl::local_data_files_service() {
if (!local_data_files_service_) {
local_data_files_service_ =
brave_component_updater::LocalDataFilesServiceFactory(
brave_component_updater_delegate());
}
return local_data_files_service_.get();
}

brave::URLSanitizerComponentInstaller*
BraveApplicationContextImpl::url_sanitizer_component_installer() {
if (!url_sanitizer_component_installer_) {
url_sanitizer_component_installer_ =
std::make_unique<brave::URLSanitizerComponentInstaller>(
local_data_files_service());
}
return url_sanitizer_component_installer_.get();
}

void BraveApplicationContextImpl::StartBraveServices() {
// We need to Initialize the component installer
// before calling Start on the local_data_files_service
url_sanitizer_component_installer();

// Start the local data file service
local_data_files_service()->Start();
}
4 changes: 2 additions & 2 deletions ios/browser/brave_web_main_parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "ios/chrome/browser/flags/ios_chrome_field_trials.h"
#include "ios/web/public/init/web_main_parts.h"

class ApplicationContextImpl;
class BraveApplicationContextImpl;
class PrefService;

namespace base {
Expand All @@ -36,7 +36,7 @@ class BraveWebMainParts : public web::WebMainParts {
void PostCreateThreads() override;
void SetupFieldTrials();

std::unique_ptr<ApplicationContextImpl> application_context_;
std::unique_ptr<BraveApplicationContextImpl> application_context_;

// Statistical testing infrastructure for the entire browser. NULL until
// SetUpMetricsAndFieldTrials is called.
Expand Down
6 changes: 5 additions & 1 deletion ios/browser/brave_web_main_parts.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/strings/sys_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "brave/ios/browser/application_context/brave_application_context_impl.h"
#include "brave/ios/browser/browser_state/brave_browser_state_keyed_service_factories.h"
#include "components/flags_ui/pref_service_flags_storage.h"
#include "components/metrics/metrics_service.h"
Expand Down Expand Up @@ -66,7 +67,7 @@

base::FilePath local_state_path;
CHECK(base::PathService::Get(ios::FILE_LOCAL_STATE, &local_state_path));
application_context_.reset(new ApplicationContextImpl(
application_context_.reset(new BraveApplicationContextImpl(
local_state_task_runner.get(), *base::CommandLine::ForCurrentProcess(),
l10n_util::GetLocaleOverride(),
base::SysNSStringToUTF8(
Expand Down Expand Up @@ -153,6 +154,9 @@
// TODO(crbug.com/786494): Investigate whether metrics recording can be
// initialized consistently across iOS and non-iOS platforms
SetupMetrics();

// Start the brave services
application_context_->StartBraveServices();
}

void BraveWebMainParts::PostMainMessageLoopRun() {
Expand Down
5 changes: 4 additions & 1 deletion ios/browser/browser_state/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import("//build/config/ios/rules.gni")
import("//ios/build/config.gni")

source_set("browser_state") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"brave_browser_state_keyed_service_factories.cc",
"brave_browser_state_keyed_service_factories.h",
"brave_browser_state_keyed_service_factories.mm",
]
deps = [
"//base",
"//brave/ios/browser/brave_wallet",
"//brave/ios/browser/component_updater",
"//brave/ios/browser/favicon",
"//brave/ios/browser/skus",
"//brave/ios/browser/url_sanitizer",
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/ios/browser/browser_state/brave_browser_state_keyed_service_factories.h"

Expand All @@ -12,6 +12,7 @@
#include "brave/ios/browser/brave_wallet/swap_service_factory.h"
#include "brave/ios/browser/brave_wallet/tx_service_factory.h"
#include "brave/ios/browser/skus/skus_service_factory.h"
#include "brave/ios/browser/url_sanitizer/url_sanitizer_service_factory+private.h"

namespace brave {

Expand All @@ -23,6 +24,7 @@ void EnsureBrowserStateKeyedServiceFactoriesBuilt() {
brave_wallet::KeyringServiceFactory::GetInstance();
brave_wallet::SwapServiceFactory::GetInstance();
skus::SkusServiceFactory::GetInstance();
brave::URLSanitizerServiceFactory::GetInstance();
}

} // namespace brave
Loading

0 comments on commit 0290ad5

Please sign in to comment.