blob: fe6940b6dc3eed73fb977f4ed5c2552d2eab1bba [file] [log] [blame]
// Copyright (c) 2012 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/app/android/chrome_main_delegate_android.h"
#include "base/android/jni_android.h"
#include "base/base_paths_android.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/android/chrome_startup_flags.h"
#include "chrome/browser/android/metrics/uma_utils.h"
#include "chrome/common/profiler/main_thread_stack_sampling_profiler.h"
#include "components/policy/core/browser/android/android_combined_policy_provider.h"
#include "components/safe_browsing/buildflags.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "content/public/browser/browser_main_runner.h"
#include "components/safe_browsing/android/safe_browsing_api_handler.h"
#include "components/safe_browsing/android/safe_browsing_api_handler_bridge.h"
using safe_browsing::SafeBrowsingApiHandler;
// ChromeMainDelegateAndroid is created when the library is loaded. It is always
// done in the process' main Java thread. But for a non-browser process, e.g.
// renderer process, it is not the native Chrome's main thread.
ChromeMainDelegateAndroid::ChromeMainDelegateAndroid() = default;
ChromeMainDelegateAndroid::~ChromeMainDelegateAndroid() = default;
bool ChromeMainDelegateAndroid::BasicStartupComplete(int* exit_code) {
// Start the sampling profiler as early as possible.
sampling_profiler_ = std::make_unique<MainThreadStackSamplingProfiler>();
new safe_browsing::SafeBrowsingApiHandlerBridge());
return ChromeMainDelegate::BasicStartupComplete(exit_code);
void ChromeMainDelegateAndroid::SandboxInitialized(
const std::string& process_type) {
void ChromeMainDelegateAndroid::SecureDataDirectory() {
// By default, Android creates the directory accessible by others.
// We'd like to tighten security and make it accessible only by
// the browser process.
// TODO( Remove this once minsdk >= 21,
// at which point this will be handled by
base::FilePath data_path;
bool ok = base::PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path);
if (ok) {
ok = base::SetPosixFilePermissions(data_path,
if (!ok) {
LOG(ERROR) << "Failed to set data directory permissions";
int ChromeMainDelegateAndroid::RunProcess(
const std::string& process_type,
const content::MainFunctionParams& main_function_params) {
TRACE_EVENT0("startup", "ChromeMainDelegateAndroid::RunProcess");
// Defer to the default main method outside the browser process.
if (!process_type.empty())
return -1;
// Because the browser process can be started asynchronously as a series of
// UI thread tasks a second request to start it can come in while the
// first request is still being processed. Chrome must keep the same
// browser runner for the second request.
// Also only record the start time the first time round, since this is the
// start time of the application, and will be same for all requests.
if (!browser_runner_) {
browser_runner_ = content::BrowserMainRunner::Create();
int exit_code = browser_runner_->Initialize(main_function_params);
// On Android we do not run BrowserMain(), so the above initialization of a
// BrowserMainRunner is all we want to occur. Return >= 0 to avoid running
// BrowserMain, while preserving any error codes > 0.
if (exit_code > 0)
return exit_code;
return 0;
void ChromeMainDelegateAndroid::ProcessExiting(
const std::string& process_type) {