1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/shell/browser/shell_browser_main_parts.h"
#include <memory>
#include <string>
#include "apps/browser_context_keyed_service_factories.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/pref_service.h"
#include "components/sessions/core/session_id_generator.h"
#include "components/storage_monitor/storage_monitor.h"
#include "components/update_client/update_query_params.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/context_factory.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/media_session_service.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "content/shell/browser/shell_devtools_manager_delegate.h"
#include "extensions/browser/browser_context_keyed_service_factories.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/updater/update_service.h"
#include "extensions/common/constants.h"
#include "extensions/shell/browser/desktop_controller.h"
#include "extensions/shell/browser/shell_browser_context.h"
#include "extensions/shell/browser/shell_browser_context_keyed_service_factories.h"
#include "extensions/shell/browser/shell_browser_main_delegate.h"
#include "extensions/shell/browser/shell_extension_system.h"
#include "extensions/shell/browser/shell_extension_system_factory.h"
#include "extensions/shell/browser/shell_extensions_browser_client.h"
#include "extensions/shell/browser/shell_prefs.h"
#include "extensions/shell/browser/shell_update_query_params_delegate.h"
#include "extensions/shell/common/shell_extensions_client.h"
#include "extensions/shell/common/switches.h"
#include "ui/base/ime/init/input_method_initializer.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(USE_AURA)
#include "ui/aura/env.h"
#endif
#if BUILDFLAG(IS_LINUX)
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/dbus/dbus_bluez_manager_wrapper_linux.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/audio/audio_devices_pref_handler_impl.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "chromeos/ash/components/dbus/audio/cras_audio_client.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "chromeos/ash/components/dbus/dbus_thread_manager.h"
#include "chromeos/ash/components/dbus/hermes/hermes_clients.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"
#include "extensions/shell/browser/shell_audio_controller_chromeos.h"
#include "extensions/shell/browser/shell_network_controller_chromeos.h"
#endif
using base::CommandLine;
using content::BrowserContext;
namespace extensions {
ShellBrowserMainParts::ShellBrowserMainParts(
ShellBrowserMainDelegate* browser_main_delegate,
bool is_integration_test)
: extension_system_(nullptr),
browser_main_delegate_(browser_main_delegate),
is_integration_test_(is_integration_test) {}
ShellBrowserMainParts::~ShellBrowserMainParts() = default;
void ShellBrowserMainParts::PostCreateMainMessageLoop() {
#if BUILDFLAG(IS_CHROMEOS)
// Perform initialization of D-Bus objects here rather than in the below
// helper classes so those classes' tests can initialize stub versions of the
// D-Bus objects.
ash::DBusThreadManager::Initialize();
dbus::Bus* bus = ash::DBusThreadManager::Get()->GetSystemBus();
if (bus) {
bluez::BluezDBusManager::Initialize(bus);
} else {
bluez::BluezDBusManager::InitializeFake();
}
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_CHROMEOS)
if (bus) {
ash::shill_clients::Initialize(bus);
ash::hermes_clients::Initialize(bus);
ash::CrasAudioClient::Initialize(bus);
ash::CrosDisksClient::Initialize(bus);
chromeos::PowerManagerClient::Initialize(bus);
} else {
ash::shill_clients::InitializeFakes();
ash::hermes_clients::InitializeFakes();
ash::CrasAudioClient::InitializeFake();
ash::CrosDisksClient::InitializeFake();
chromeos::PowerManagerClient::InitializeFake();
}
// Depends on CrosDisksClient.
ash::disks::DiskMountManager::Initialize();
ash::NetworkHandler::Initialize();
network_controller_ = std::make_unique<ShellNetworkController>(
base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
switches::kAppShellPreferredNetwork));
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAppShellAllowRoaming)) {
network_controller_->SetCellularAllowRoaming(true);
}
#elif BUILDFLAG(IS_LINUX)
// app_shell doesn't need GTK, so the fake input method context can work.
// See crbug.com/381852 and revision fb69f142.
// TODO(michaelpg): Verify this works for target environments.
ui::InitializeInputMethodForTesting();
#else
ui::InitializeInputMethodForTesting();
#endif
#if BUILDFLAG(IS_LINUX)
bluez::DBusBluezManagerWrapperLinux::Initialize();
#endif
}
int ShellBrowserMainParts::PreEarlyInitialization() {
return content::RESULT_CODE_NORMAL_EXIT;
}
int ShellBrowserMainParts::PreCreateThreads() {
// TODO(jamescook): Initialize ash::CrosSettings here?
content::ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
kExtensionScheme);
// Return no error.
return 0;
}
int ShellBrowserMainParts::PreMainMessageLoopRun() {
extensions_client_ = std::make_unique<ShellExtensionsClient>();
ExtensionsClient::Set(extensions_client_.get());
// BrowserContextKeyedAPIServiceFactories require an ExtensionsBrowserClient.
extensions_browser_client_ = std::make_unique<ShellExtensionsBrowserClient>();
ExtensionsBrowserClient::Set(extensions_browser_client_.get());
apps::EnsureBrowserContextKeyedServiceFactoriesBuilt();
EnsureBrowserContextKeyedServiceFactoriesBuilt();
shell::EnsureBrowserContextKeyedServiceFactoriesBuilt();
// Initialize our "profile" equivalent.
browser_context_ = std::make_unique<ShellBrowserContext>();
// app_shell only supports a single user, so all preferences live in the user
// data directory, including the device-wide local state.
local_state_ = shell_prefs::CreateLocalState(browser_context_->GetPath());
sessions::SessionIdGenerator::GetInstance()->Init(local_state_.get());
user_pref_service_ =
shell_prefs::CreateUserPrefService(browser_context_.get());
extensions_browser_client_->InitWithBrowserContext(browser_context_.get(),
user_pref_service_.get());
#if BUILDFLAG(IS_CHROMEOS)
mojo::PendingRemote<media_session::mojom::MediaControllerManager>
media_controller_manager;
content::GetMediaSessionService().BindMediaControllerManager(
media_controller_manager.InitWithNewPipeAndPassReceiver());
ash::CrasAudioHandler::Initialize(
std::move(media_controller_manager),
base::MakeRefCounted<ash::AudioDevicesPrefHandlerImpl>(
local_state_.get()));
audio_controller_ = std::make_unique<ShellAudioController>();
#endif
// Create BrowserContextKeyedServices now that we have an
// ExtensionsBrowserClient that BrowserContextKeyedAPIServices can query.
BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
browser_context_.get());
#if defined(USE_AURA)
aura::Env::GetInstance()->set_context_factory(content::GetContextFactory());
#endif
storage_monitor::StorageMonitor::Create();
desktop_controller_.reset(
browser_main_delegate_->CreateDesktopController(browser_context_.get()));
// TODO(jamescook): Initialize user_manager::UserManager.
update_query_params_delegate_ =
std::make_unique<ShellUpdateQueryParamsDelegate>();
update_client::UpdateQueryParams::SetDelegate(
update_query_params_delegate_.get());
InitExtensionSystem();
content::ShellDevToolsManagerDelegate::StartHttpHandler(
browser_context_.get());
// Skip these steps in integration tests.
if (!is_integration_test_) {
browser_main_delegate_->Start(browser_context_.get());
desktop_controller_->PreMainMessageLoopRun();
}
return content::RESULT_CODE_NORMAL_EXIT;
}
void ShellBrowserMainParts::WillRunMainMessageLoop(
std::unique_ptr<base::RunLoop>& run_loop) {
desktop_controller_->WillRunMainMessageLoop(run_loop);
}
void ShellBrowserMainParts::PostMainMessageLoopRun() {
desktop_controller_->PostMainMessageLoopRun();
// Close apps before shutting down browser context and extensions system.
desktop_controller_->CloseAppWindows();
// NOTE: Please destroy objects in the reverse order of their creation.
browser_main_delegate_->Shutdown();
content::ShellDevToolsManagerDelegate::StopHttpHandler();
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
browser_context_.get());
extension_system_ = nullptr;
desktop_controller_.reset();
storage_monitor::StorageMonitor::Destroy();
#if BUILDFLAG(IS_CHROMEOS)
audio_controller_.reset();
ash::CrasAudioHandler::Shutdown();
#endif
sessions::SessionIdGenerator::GetInstance()->Shutdown();
user_pref_service_->CommitPendingWrite();
user_pref_service_.reset();
local_state_->CommitPendingWrite();
local_state_.reset();
browser_context_.reset();
}
void ShellBrowserMainParts::PostDestroyThreads() {
extensions_browser_client_.reset();
ExtensionsBrowserClient::Set(nullptr);
#if BUILDFLAG(IS_CHROMEOS)
device::BluetoothAdapterFactory::Shutdown();
bluez::BluezDBusManager::Shutdown();
#elif BUILDFLAG(IS_LINUX)
device::BluetoothAdapterFactory::Shutdown();
bluez::DBusBluezManagerWrapperLinux::Shutdown();
#endif
#if BUILDFLAG(IS_CHROMEOS)
network_controller_.reset();
ash::NetworkHandler::Shutdown();
ash::disks::DiskMountManager::Shutdown();
chromeos::PowerManagerClient::Shutdown();
ash::CrosDisksClient::Shutdown();
ash::CrasAudioClient::Shutdown();
ash::shill_clients::Shutdown();
#endif
#if BUILDFLAG(IS_CHROMEOS)
ash::DBusThreadManager::Shutdown();
#endif
}
void ShellBrowserMainParts::InitExtensionSystem() {
DCHECK(browser_context_);
extension_system_ = static_cast<ShellExtensionSystem*>(
ExtensionSystem::Get(browser_context_.get()));
extension_system_->InitForRegularProfile(true /* extensions_enabled */);
}
} // namespace extensions
|