diff --git a/base/threading/thread_restrictions.h b/base/threading/thread_restrictions.h index 4c9ff4452fa1..d3675fb734eb 100644 --- a/base/threading/thread_restrictions.h +++ b/base/threading/thread_restrictions.h @@ -211,6 +211,11 @@ namespace chrome_cleaner { class ResetShortcutsComponent; class SystemReportComponent; } // namespace chrome_cleaner +#if BUILDFLAG(IS_COBALT) +namespace cobalt { +class CobaltPathProvider; +} // namespace cobalt +#endif // BUILDFLAG(IS_COBALT) namespace content { class BrowserGpuChannelHostFactory; class BrowserMainLoop; @@ -601,6 +606,9 @@ class BASE_EXPORT [[maybe_unused, nodiscard]] ScopedAllowBlocking { friend class base::win::ScopedAllowBlockingForUserAccountControl; friend class blink::DiskDataAllocator; friend class chromecast::CrashUtil; +#if BUILDFLAG(IS_COBALT) + friend class cobalt::CobaltPathProvider; +#endif // BUILDFLAG(IS_COBALT) friend class content::BrowserProcessIOThread; friend class content::DWriteFontProxyImpl; friend class content::NetworkServiceInstancePrivate; diff --git a/cobalt/BUILD.gn b/cobalt/BUILD.gn index 4e0e67a158c3..7fb74283b0d5 100644 --- a/cobalt/BUILD.gn +++ b/cobalt/BUILD.gn @@ -32,6 +32,8 @@ if (!is_android) { "cobalt_content_browser_client.h", "cobalt_main_delegate.cc", "cobalt_main_delegate.h", + "cobalt_paths.cc", + "cobalt_paths.h", ] defines = [] diff --git a/cobalt/cobalt_main_delegate.cc b/cobalt/cobalt_main_delegate.cc index 969e792c9d4d..217d983f39d2 100644 --- a/cobalt/cobalt_main_delegate.cc +++ b/cobalt/cobalt_main_delegate.cc @@ -13,7 +13,41 @@ // limitations under the License. #include "cobalt/cobalt_main_delegate.h" + +#include + +#include "base/path_service.h" #include "cobalt/cobalt_content_browser_client.h" +#include "cobalt/cobalt_paths.h" +#include "content/public/common/content_switches.h" +#include "content/shell/common/shell_switches.h" + +#if !BUILDFLAG(IS_ANDROID) +#include "content/web_test/browser/web_test_browser_main_runner.h" // nogncheck +#include "content/web_test/browser/web_test_content_browser_client.h" // nogncheck +#include "content/web_test/renderer/web_test_content_renderer_client.h" // nogncheck +#endif + +namespace { + +void InitLogging(const base::CommandLine& command_line) { + base::FilePath log_filename = + command_line.GetSwitchValuePath(switches::kLogFile); + if (log_filename.empty()) { + base::PathService::Get(base::DIR_EXE, &log_filename); + log_filename = log_filename.AppendASCII("cobalt.log"); + } + + logging::LoggingSettings settings; + settings.logging_dest = logging::LOG_TO_ALL; + settings.log_file_path = log_filename.value().c_str(); + settings.delete_old = logging::DELETE_OLD_LOG_FILE; + logging::InitLogging(settings); + logging::SetLogItems(true /* Process ID */, true /* Thread ID */, + true /* Timestamp */, false /* Tick count */); +} + +} // namespace namespace cobalt { @@ -22,6 +56,30 @@ CobaltMainDelegate::CobaltMainDelegate(bool is_content_browsertests) CobaltMainDelegate::~CobaltMainDelegate() {} +absl::optional CobaltMainDelegate::BasicStartupComplete() { + base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); +#if BUILDFLAG(IS_ANDROID) + content::Compositor::Initialize(); +#endif + + InitLogging(command_line); + +#if !BUILDFLAG(IS_ANDROID) + if (switches::IsRunWebTestsSwitchPresent()) { + const bool browser_process = + command_line.GetSwitchValueASCII(switches::kProcessType).empty(); + if (browser_process) { + web_test_runner_ = std::make_unique(); + web_test_runner_->Initialize(); + } + } +#endif + + RegisterCobaltPathProvider(); + + return absl::nullopt; +} + content::ContentBrowserClient* CobaltMainDelegate::CreateContentBrowserClient() { browser_client_ = std::make_unique(); diff --git a/cobalt/cobalt_main_delegate.h b/cobalt/cobalt_main_delegate.h index fa7138531df3..5a319bae2def 100644 --- a/cobalt/cobalt_main_delegate.h +++ b/cobalt/cobalt_main_delegate.h @@ -28,6 +28,7 @@ class CobaltMainDelegate : public content::ShellMainDelegate { CobaltMainDelegate& operator=(const CobaltMainDelegate&) = delete; // ContentMainDelegate implementation: + absl::optional BasicStartupComplete() override; content::ContentBrowserClient* CreateContentBrowserClient() override; ~CobaltMainDelegate() override; diff --git a/cobalt/cobalt_paths.cc b/cobalt/cobalt_paths.cc new file mode 100644 index 000000000000..915f0d972f52 --- /dev/null +++ b/cobalt/cobalt_paths.cc @@ -0,0 +1,71 @@ +// Copyright 2021 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cobalt/cobalt_paths.h" + +#include "base/base_paths.h" +#include "base/environment.h" +#include "base/files/file_util.h" +#include "base/path_service.h" +#include "base/threading/thread_restrictions.h" +#include "build/build_config.h" + +#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) +#include "base/nix/xdg_util.h" +#endif + +namespace cobalt { + +namespace { + +bool GetDefaultUserDataDirectory(base::FilePath* result) { +#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) + std::unique_ptr env(base::Environment::Create()); + base::FilePath config_dir(base::nix::GetXDGDirectory( + env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir)); + *result = config_dir.Append("cobalt"); +#elif BUILDFLAG(IS_ANDROID) + CHECK(base::PathService::Get(base::DIR_ANDROID_APP_DATA, result)); + *result = result->Append(FILE_PATH_LITERAL("cobalt")); +#else + NOTIMPLEMENTED(); + return false; +#endif + return true; +} + +} // namespace + +class CobaltPathProvider { + public: + static void CreateDir(const base::FilePath& path) { + base::ScopedAllowBlocking allow_io; + if (!base::PathExists(path)) { + base::CreateDirectory(path); + } + } +}; + +bool CobaltPathProvider(int key, base::FilePath* result) { + base::FilePath cur; + + switch (key) { + case SHELL_DIR_USER_DATA: { + bool rv = GetDefaultUserDataDirectory(result); + if (rv) { + CobaltPathProvider::CreateDir(*result); + } + return rv; + } + default: + return false; + } +} + +void RegisterCobaltPathProvider() { + base::PathService::RegisterProvider(CobaltPathProvider, SHELL_PATH_START, + SHELL_PATH_END); +} + +} // namespace cobalt diff --git a/cobalt/cobalt_paths.h b/cobalt/cobalt_paths.h new file mode 100644 index 000000000000..d2d93635bef3 --- /dev/null +++ b/cobalt/cobalt_paths.h @@ -0,0 +1,29 @@ +// Copyright 2021 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_SHELL_BROWSER_SHELL_PATHS_H_ +#define CONTENT_SHELL_BROWSER_SHELL_PATHS_H_ + +#include "build/build_config.h" + +namespace cobalt { + +enum { + SHELL_PATH_START = 12000, + + // Directory where user data can be written. + SHELL_DIR_USER_DATA = SHELL_PATH_START, + + // TODO(jam): move from content/common since it's test only. + // DIR_TEST_DATA, + + SHELL_PATH_END +}; + +// Call once to register the provider for the path keys defined above. +void RegisterCobaltPathProvider(); + +} // namespace cobalt + +#endif // CONTENT_SHELL_BROWSER_SHELL_PATHS_H_