Files
brave-core/ios/app/brave_core_main.mm
T

223 lines
7.9 KiB
Plaintext

/* 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/. */
#import "brave/ios/app/brave_core_main.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "brave/components/brave_component_updater/browser/brave_on_demand_updater.h"
#include "brave/components/brave_wallet/browser/erc_token_registry.h"
#include "brave/components/brave_wallet/browser/wallet_data_files_installer.h"
#include "brave/ios/app/brave_main_delegate.h"
#include "brave/ios/browser/api/bookmarks/brave_bookmarks_api+private.h"
#include "brave/ios/browser/api/brave_wallet/brave_wallet.mojom.objc+private.h"
#include "brave/ios/browser/api/history/brave_history_api+private.h"
#include "brave/ios/browser/api/sync/brave_sync_api+private.h"
#include "brave/ios/browser/api/sync/driver/brave_sync_profile_service+private.h"
#include "brave/ios/browser/brave_web_client.h"
#include "brave/ios/browser/component_updater/component_updater_utils.h"
#include "components/history/core/browser/history_service.h"
#include "components/keyed_service/core/service_access_type.h"
#include "ios/chrome/app/startup/provider_registration.h"
#include "ios/chrome/app/startup_tasks.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/bookmarks/bookmark_model_factory.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state_manager.h"
#include "ios/chrome/browser/history/history_service_factory.h"
#include "ios/chrome/browser/history/web_history_service_factory.h"
#include "ios/chrome/browser/sync/sync_service_factory.h"
#include "ios/chrome/browser/undo/bookmark_undo_service_factory.h"
#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
#include "ios/web/public/init/web_main.h"
// Chromium logging is global, therefore we cannot link this to the instance in
// question
static BraveCoreLogHandler _Nullable _logHandler = nil;
@interface BraveCoreMain () {
std::unique_ptr<BraveWebClient> _webClient;
std::unique_ptr<BraveMainDelegate> _delegate;
std::unique_ptr<web::WebMain> _webMain;
ChromeBrowserState* _mainBrowserState;
}
@property(nonatomic) BraveBookmarksAPI* bookmarksAPI;
@property(nonatomic) BraveHistoryAPI* historyAPI;
@property(nonatomic) BraveSyncAPI* syncAPI;
@property(nonatomic) BraveSyncProfileServiceIOS* syncProfileService;
@end
@implementation BraveCoreMain
- (instancetype)initWithUserAgent:(NSString*)userAgent {
return [self initWithUserAgent:userAgent syncServiceURL:@""];
}
- (instancetype)initWithUserAgent:(NSString*)userAgent
syncServiceURL:(NSString*)syncServiceURL {
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onAppEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onAppEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onAppWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];
// Register all providers before calling any Chromium code.
[ProviderRegistration registerProviders];
_webClient.reset(new BraveWebClient());
_webClient->SetUserAgent(base::SysNSStringToUTF8(userAgent));
web::SetWebClient(_webClient.get());
_delegate.reset(new BraveMainDelegate());
_delegate->SetSyncServiceURL(base::SysNSStringToUTF8(syncServiceURL));
web::WebMainParams params(_delegate.get());
_webMain = std::make_unique<web::WebMain>(std::move(params));
ios::GetChromeBrowserProvider().Initialize();
[self registerComponentsForUpdate];
ios::ChromeBrowserStateManager* browserStateManager =
GetApplicationContext()->GetChromeBrowserStateManager();
ChromeBrowserState* chromeBrowserState =
browserStateManager->GetLastUsedBrowserState();
_mainBrowserState = chromeBrowserState;
}
return self;
}
- (void)onAppEnterBackground:(NSNotification*)notification {
auto* context = GetApplicationContext();
if (context)
context->OnAppEnterBackground();
}
- (void)onAppEnterForeground:(NSNotification*)notification {
auto* context = GetApplicationContext();
if (context)
context->OnAppEnterForeground();
}
- (void)onAppWillTerminate:(NSNotification*)notification {
_webMain.reset();
}
- (void)scheduleLowPriorityStartupTasks {
[StartupTasks scheduleDeferredBrowserStateInitialization:_mainBrowserState];
}
- (void)registerComponentsForUpdate {
brave_component_updater::BraveOnDemandUpdater::GetInstance()
->RegisterOnDemandUpdateCallback(
base::BindRepeating(&component_updater::BraveOnDemandUpdate));
component_updater::ComponentUpdateService* cus =
GetApplicationContext()->GetComponentUpdateService();
DCHECK(cus);
brave_wallet::RegisterWalletDataFilesComponent(cus);
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
_mainBrowserState = nullptr;
_webMain.reset();
_delegate.reset();
_webClient.reset();
}
+ (void)setLogHandler:(BraveCoreLogHandler)logHandler {
_logHandler = logHandler;
logging::SetLogMessageHandler(&CustomLogHandler);
}
static bool CustomLogHandler(int severity,
const char* file,
int line,
size_t message_start,
const std::string& str) {
if (!_logHandler) {
return false;
}
const int vlog_level = logging::GetVlogLevelHelper(file, strlen(file));
if (severity <= vlog_level) {
return _logHandler(severity, base::SysUTF8ToNSString(file), line,
message_start, base::SysUTF8ToNSString(str));
}
return true;
}
#pragma mark -
- (BraveBookmarksAPI*)bookmarksAPI {
if (!_bookmarksAPI) {
bookmarks::BookmarkModel* bookmark_model_ =
ios::BookmarkModelFactory::GetForBrowserState(_mainBrowserState);
BookmarkUndoService* bookmark_undo_service_ =
ios::BookmarkUndoServiceFactory::GetForBrowserState(_mainBrowserState);
_bookmarksAPI = [[BraveBookmarksAPI alloc]
initWithBookmarkModel:bookmark_model_
bookmarkUndoService:bookmark_undo_service_];
}
return _bookmarksAPI;
}
- (BraveHistoryAPI*)historyAPI {
if (!_historyAPI) {
history::HistoryService* history_service_ =
ios::HistoryServiceFactory::GetForBrowserState(
_mainBrowserState, ServiceAccessType::EXPLICIT_ACCESS);
history::WebHistoryService* web_history_service_ =
ios::WebHistoryServiceFactory::GetForBrowserState(_mainBrowserState);
_historyAPI =
[[BraveHistoryAPI alloc] initWithHistoryService:history_service_
webHistoryService:web_history_service_];
}
return _historyAPI;
}
- (BraveSyncAPI*)syncAPI {
if (!_syncAPI) {
_syncAPI = [[BraveSyncAPI alloc] initWithBrowserState:_mainBrowserState];
}
return _syncAPI;
}
- (BraveSyncProfileServiceIOS*)syncProfileService {
if (!_syncProfileService) {
syncer::SyncService* sync_service_ =
SyncServiceFactory::GetForBrowserState(_mainBrowserState);
_syncProfileService = [[BraveSyncProfileServiceIOS alloc]
initWithProfileSyncService:sync_service_];
}
return _syncProfileService;
}
+ (id<BraveWalletERCTokenRegistry>)ercTokenRegistry {
auto* registry = brave_wallet::ERCTokenRegistry::GetInstance();
return [[BraveWalletERCTokenRegistryImpl alloc]
initWithERCTokenRegistry:registry];
}
@end