| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/sysui/sysui_application.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "ash/common/login_status.h" | |
| 13 #include "ash/common/material_design/material_design_controller.h" | |
| 14 #include "ash/common/shell_window_ids.h" | |
| 15 #include "ash/common/wm_shell.h" | |
| 16 #include "ash/display/display_manager.h" | |
| 17 #include "ash/host/ash_window_tree_host_init_params.h" | |
| 18 #include "ash/host/ash_window_tree_host_platform.h" | |
| 19 #include "ash/public/interfaces/ash_window_type.mojom.h" | |
| 20 #include "ash/public/interfaces/container.mojom.h" | |
| 21 #include "ash/root_window_settings.h" | |
| 22 #include "ash/shell.h" | |
| 23 #include "ash/shell_init_params.h" | |
| 24 #include "ash/sysui/keyboard_ui_mus.h" | |
| 25 #include "ash/sysui/shell_delegate_mus.h" | |
| 26 #include "ash/sysui/stub_context_factory.h" | |
| 27 #include "base/bind.h" | |
| 28 #include "base/files/file_path.h" | |
| 29 #include "base/path_service.h" | |
| 30 #include "base/threading/sequenced_worker_pool.h" | |
| 31 #include "services/catalog/public/cpp/resource_loader.h" | |
| 32 #include "services/shell/public/cpp/connector.h" | |
| 33 #include "services/ui/public/cpp/property_type_converters.h" | |
| 34 #include "services/ui/public/interfaces/input_devices/input_device_server.mojom.
h" | |
| 35 #include "ui/aura/env.h" | |
| 36 #include "ui/base/resource/resource_bundle.h" | |
| 37 #include "ui/base/ui_base_paths.h" | |
| 38 #include "ui/display/display.h" | |
| 39 #include "ui/display/screen.h" | |
| 40 #include "ui/message_center/message_center.h" | |
| 41 #include "ui/platform_window/stub/stub_window.h" | |
| 42 #include "ui/views/mus/aura_init.h" | |
| 43 #include "ui/views/mus/native_widget_mus.h" | |
| 44 #include "ui/views/mus/window_manager_connection.h" | |
| 45 #include "ui/views/views_delegate.h" | |
| 46 | |
| 47 #if defined(OS_CHROMEOS) | |
| 48 #include "chromeos/audio/cras_audio_handler.h" | |
| 49 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 50 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 51 #endif | |
| 52 | |
| 53 using views::ViewsDelegate; | |
| 54 | |
| 55 namespace ash { | |
| 56 namespace sysui { | |
| 57 | |
| 58 namespace { | |
| 59 | |
| 60 const char kResourceFileStrings[] = "ash_test_strings.pak"; | |
| 61 const char kResourceFile100[] = "ash_test_resources_100_percent.pak"; | |
| 62 const char kResourceFile200[] = "ash_test_resources_200_percent.pak"; | |
| 63 | |
| 64 // Tries to determine the corresponding mash container from widget init params. | |
| 65 bool GetContainerForWidget(const views::Widget::InitParams& params, | |
| 66 ash::mojom::Container* container) { | |
| 67 switch (params.parent->id()) { | |
| 68 case kShellWindowId_WallpaperContainer: | |
| 69 *container = ash::mojom::Container::WALLPAPER; | |
| 70 return true; | |
| 71 | |
| 72 case kShellWindowId_ShelfContainer: | |
| 73 *container = ash::mojom::Container::USER_PRIVATE_SHELF; | |
| 74 return true; | |
| 75 | |
| 76 case kShellWindowId_StatusContainer: | |
| 77 *container = ash::mojom::Container::STATUS; | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 // Determine the container based on Widget type. | |
| 82 switch (params.type) { | |
| 83 case views::Widget::InitParams::Type::TYPE_BUBBLE: | |
| 84 *container = ash::mojom::Container::BUBBLES; | |
| 85 return true; | |
| 86 | |
| 87 case views::Widget::InitParams::Type::TYPE_MENU: | |
| 88 *container = ash::mojom::Container::MENUS; | |
| 89 return true; | |
| 90 | |
| 91 case views::Widget::InitParams::Type::TYPE_TOOLTIP: | |
| 92 *container = ash::mojom::Container::DRAG_AND_TOOLTIPS; | |
| 93 return true; | |
| 94 | |
| 95 default: | |
| 96 break; | |
| 97 } | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 // Tries to determine the corresponding ash window type from the ash container | |
| 102 // for the widget. | |
| 103 ash::mojom::AshWindowType GetAshWindowType(aura::Window* container) { | |
| 104 DCHECK(container); | |
| 105 int id = container->id(); | |
| 106 if (id == kShellWindowId_ShelfContainer) | |
| 107 return ash::mojom::AshWindowType::SHELF; | |
| 108 if (id == kShellWindowId_StatusContainer) | |
| 109 return ash::mojom::AshWindowType::STATUS_AREA; | |
| 110 return ash::mojom::AshWindowType::COUNT; | |
| 111 } | |
| 112 | |
| 113 // Creates a StubWindow, which means this window never receives any input event, | |
| 114 // or displays anything to the user. | |
| 115 class AshWindowTreeHostMus : public AshWindowTreeHostPlatform { | |
| 116 public: | |
| 117 explicit AshWindowTreeHostMus(const gfx::Rect& initial_bounds) | |
| 118 : AshWindowTreeHostPlatform() { | |
| 119 std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this)); | |
| 120 window->SetBounds(initial_bounds); | |
| 121 SetPlatformWindow(std::move(window)); | |
| 122 } | |
| 123 | |
| 124 ~AshWindowTreeHostMus() override {} | |
| 125 | |
| 126 void OnBoundsChanged(const gfx::Rect& bounds) override { | |
| 127 if (platform_window()) | |
| 128 AshWindowTreeHostPlatform::OnBoundsChanged(bounds); | |
| 129 } | |
| 130 }; | |
| 131 | |
| 132 AshWindowTreeHost* CreateWindowTreeHostMus( | |
| 133 const AshWindowTreeHostInitParams& init_params) { | |
| 134 return new AshWindowTreeHostMus(init_params.initial_bounds); | |
| 135 } | |
| 136 | |
| 137 // Responsible for setting up a RootWindowSettings object for the root-window | |
| 138 // created for the views::Widget objects. | |
| 139 class NativeWidgetFactory { | |
| 140 public: | |
| 141 NativeWidgetFactory() { | |
| 142 ViewsDelegate* views_delegate = ViewsDelegate::GetInstance(); | |
| 143 DCHECK(views_delegate); | |
| 144 views_delegate->set_native_widget_factory(base::Bind( | |
| 145 &NativeWidgetFactory::InitNativeWidget, base::Unretained(this))); | |
| 146 } | |
| 147 | |
| 148 ~NativeWidgetFactory() { | |
| 149 ViewsDelegate::GetInstance()->set_native_widget_factory( | |
| 150 ViewsDelegate::NativeWidgetFactory()); | |
| 151 } | |
| 152 | |
| 153 private: | |
| 154 views::NativeWidget* InitNativeWidget( | |
| 155 const views::Widget::InitParams& params, | |
| 156 views::internal::NativeWidgetDelegate* delegate) { | |
| 157 std::map<std::string, std::vector<uint8_t>> properties; | |
| 158 if (params.parent) { | |
| 159 ash::mojom::Container container; | |
| 160 if (GetContainerForWidget(params, &container)) { | |
| 161 properties[ash::mojom::kWindowContainer_Property] = | |
| 162 mojo::ConvertTo<std::vector<uint8_t>>( | |
| 163 static_cast<int32_t>(container)); | |
| 164 } | |
| 165 ash::mojom::AshWindowType type = GetAshWindowType(params.parent); | |
| 166 if (type != ash::mojom::AshWindowType::COUNT) { | |
| 167 properties[ash::mojom::kAshWindowType_Property] = | |
| 168 mojo::ConvertTo<std::vector<uint8_t>>(static_cast<int32_t>(type)); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 // AshInit installs a stub implementation of ui::ContextFactory, so that the | |
| 173 // AshWindowTreeHost instances created do not actually show anything to the | |
| 174 // user. However, when creating a views::Widget instance, the | |
| 175 // WindowManagerConnection creates a WindowTreeHostMus instance, which | |
| 176 // creates a ui::Compositor, and it needs a real ui::ContextFactory | |
| 177 // implementation. So, unset the context-factory here temporarily when | |
| 178 // creating NativeWidgetMus. But restore the stub instance afterwards, so | |
| 179 // that it is used when new AshWindowTreeHost instances are created (e.g. | |
| 180 // when a new monitor is attached). | |
| 181 ui::ContextFactory* factory = aura::Env::GetInstance()->context_factory(); | |
| 182 aura::Env::GetInstance()->set_context_factory(nullptr); | |
| 183 views::NativeWidgetMus* native_widget = | |
| 184 static_cast<views::NativeWidgetMus*>( | |
| 185 views::WindowManagerConnection::Get()->CreateNativeWidgetMus( | |
| 186 properties, params, delegate)); | |
| 187 aura::Env::GetInstance()->set_context_factory(factory); | |
| 188 | |
| 189 // TODO: Set the correct display id here. | |
| 190 InitRootWindowSettings(native_widget->GetRootWindow())->display_id = | |
| 191 Shell::GetInstance() | |
| 192 ->display_manager() | |
| 193 ->GetPrimaryDisplayCandidate() | |
| 194 .id(); | |
| 195 return native_widget; | |
| 196 } | |
| 197 | |
| 198 DISALLOW_COPY_AND_ASSIGN(NativeWidgetFactory); | |
| 199 }; | |
| 200 | |
| 201 } // namespace | |
| 202 | |
| 203 class AshInit { | |
| 204 public: | |
| 205 AshInit() | |
| 206 : worker_pool_( | |
| 207 new base::SequencedWorkerPool(2, | |
| 208 "AshWorkerPool", | |
| 209 base::TaskPriority::USER_BLOCKING)) { | |
| 210 ui::RegisterPathProvider(); | |
| 211 } | |
| 212 | |
| 213 ~AshInit() { worker_pool_->Shutdown(); } | |
| 214 | |
| 215 aura::Window* root() { return Shell::GetPrimaryRootWindow(); } | |
| 216 | |
| 217 void Initialize(::shell::Connector* connector, | |
| 218 const ::shell::Identity& identity) { | |
| 219 InitializeResourceBundle(connector); | |
| 220 aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); | |
| 221 MaterialDesignController::Initialize(); | |
| 222 window_manager_connection_ = | |
| 223 views::WindowManagerConnection::Create(connector, identity); | |
| 224 | |
| 225 // Uninstall the ScreenMus installed by WindowManagerConnection, so that ash | |
| 226 // installs and uses the ScreenAsh. This can be removed once ash learns to | |
| 227 // talk to mus for managing displays. | |
| 228 // TODO(mfomitchev): We need to fix this. http://crbug.com/607300 | |
| 229 display::Screen::SetScreenInstance(nullptr); | |
| 230 | |
| 231 // Install some hook so that the WindowTreeHostMus created for widgets can | |
| 232 // be hooked up correctly. | |
| 233 native_widget_factory_.reset(new NativeWidgetFactory()); | |
| 234 | |
| 235 AshWindowTreeHost::SetFactory(base::Bind(&CreateWindowTreeHostMus)); | |
| 236 | |
| 237 ash_delegate_ = new ShellDelegateMus(); | |
| 238 | |
| 239 InitializeComponents(); | |
| 240 | |
| 241 ShellInitParams init_params; | |
| 242 init_params.delegate = ash_delegate_; | |
| 243 init_params.context_factory = new StubContextFactory; | |
| 244 init_params.blocking_pool = worker_pool_.get(); | |
| 245 init_params.in_mus = true; | |
| 246 init_params.keyboard_factory = | |
| 247 base::Bind(&KeyboardUIMus::Create, connector); | |
| 248 Shell::CreateInstance(init_params); | |
| 249 Shell::GetInstance()->CreateShelf(); | |
| 250 Shell::GetInstance()->UpdateAfterLoginStatusChange(LoginStatus::USER); | |
| 251 | |
| 252 Shell::GetPrimaryRootWindow()->GetHost()->Show(); | |
| 253 } | |
| 254 | |
| 255 void InitializeResourceBundle(::shell::Connector* connector) { | |
| 256 if (ui::ResourceBundle::HasSharedInstance()) | |
| 257 return; | |
| 258 | |
| 259 std::set<std::string> resource_paths; | |
| 260 resource_paths.insert(kResourceFileStrings); | |
| 261 resource_paths.insert(kResourceFile100); | |
| 262 resource_paths.insert(kResourceFile200); | |
| 263 | |
| 264 catalog::ResourceLoader loader; | |
| 265 filesystem::mojom::DirectoryPtr directory; | |
| 266 connector->ConnectToInterface("mojo:catalog", &directory); | |
| 267 CHECK(loader.OpenFiles(std::move(directory), resource_paths)); | |
| 268 | |
| 269 // Load ash resources and strings; not 'common' (Chrome) resources. | |
| 270 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion( | |
| 271 loader.TakeFile(kResourceFileStrings), | |
| 272 base::MemoryMappedFile::Region::kWholeFile); | |
| 273 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 274 if (ui::ResourceBundle::IsScaleFactorSupported(ui::SCALE_FACTOR_100P)) { | |
| 275 rb.AddDataPackFromFile(loader.TakeFile(kResourceFile100), | |
| 276 ui::SCALE_FACTOR_100P); | |
| 277 } | |
| 278 if (ui::ResourceBundle::IsScaleFactorSupported(ui::SCALE_FACTOR_200P)) { | |
| 279 rb.AddDataPackFromFile(loader.TakeFile(kResourceFile200), | |
| 280 ui::SCALE_FACTOR_200P); | |
| 281 } | |
| 282 } | |
| 283 | |
| 284 void InitializeComponents() { | |
| 285 message_center::MessageCenter::Initialize(); | |
| 286 | |
| 287 #if defined(OS_CHROMEOS) | |
| 288 chromeos::DBusThreadManager::Initialize(); | |
| 289 bluez::BluezDBusManager::Initialize( | |
| 290 chromeos::DBusThreadManager::Get()->GetSystemBus(), | |
| 291 chromeos::DBusThreadManager::Get()->IsUsingStub( | |
| 292 chromeos::DBusClientBundle::BLUETOOTH)); | |
| 293 chromeos::CrasAudioHandler::InitializeForTesting(); | |
| 294 #endif | |
| 295 } | |
| 296 | |
| 297 private: | |
| 298 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | |
| 299 std::unique_ptr<views::AuraInit> aura_init_; | |
| 300 ShellDelegateMus* ash_delegate_ = nullptr; | |
| 301 std::unique_ptr<NativeWidgetFactory> native_widget_factory_; | |
| 302 std::unique_ptr<views::WindowManagerConnection> window_manager_connection_; | |
| 303 | |
| 304 DISALLOW_COPY_AND_ASSIGN(AshInit); | |
| 305 }; | |
| 306 | |
| 307 SysUIApplication::SysUIApplication() {} | |
| 308 | |
| 309 SysUIApplication::~SysUIApplication() {} | |
| 310 | |
| 311 void SysUIApplication::OnStart(const ::shell::Identity& identity) { | |
| 312 ash_init_.reset(new AshInit()); | |
| 313 ash_init_->Initialize(connector(), identity); | |
| 314 | |
| 315 ui::mojom::InputDeviceServerPtr server; | |
| 316 connector()->ConnectToInterface("mojo:ui", &server); | |
| 317 input_device_client_.Connect(std::move(server)); | |
| 318 } | |
| 319 | |
| 320 bool SysUIApplication::OnConnect(const ::shell::Identity& remote_identity, | |
| 321 ::shell::InterfaceRegistry* registry) { | |
| 322 return true; | |
| 323 } | |
| 324 | |
| 325 } // namespace sysui | |
| 326 } // namespace ash | |
| OLD | NEW |