PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ash/mus/sysui_application.cc

https://gitlab.com/0072016/Facebook-SDK-
C++ | 301 lines | 225 code | 43 blank | 33 comment | 16 complexity | 97e2661ca07f14a129c42fc6487c9c2d MD5 | raw file
  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. #include "ash/mus/sysui_application.h"
  5. #include "ash/desktop_background/desktop_background_controller.h"
  6. #include "ash/host/ash_window_tree_host_init_params.h"
  7. #include "ash/host/ash_window_tree_host_platform.h"
  8. #include "ash/mus/keyboard_ui_mus.h"
  9. #include "ash/mus/shell_delegate_mus.h"
  10. #include "ash/mus/stub_context_factory.h"
  11. #include "ash/root_window_settings.h"
  12. #include "ash/shell.h"
  13. #include "ash/shell_init_params.h"
  14. #include "ash/shell_window_ids.h"
  15. #include "base/bind.h"
  16. #include "base/files/file_path.h"
  17. #include "base/path_service.h"
  18. #include "base/threading/sequenced_worker_pool.h"
  19. #include "components/mus/public/cpp/property_type_converters.h"
  20. #include "mash/wm/public/interfaces/ash_window_type.mojom.h"
  21. #include "mash/wm/public/interfaces/container.mojom.h"
  22. #include "ui/aura/env.h"
  23. #include "ui/base/resource/resource_bundle.h"
  24. #include "ui/base/ui_base_paths.h"
  25. #include "ui/message_center/message_center.h"
  26. #include "ui/platform_window/stub/stub_window.h"
  27. #include "ui/views/mus/aura_init.h"
  28. #include "ui/views/mus/native_widget_mus.h"
  29. #include "ui/views/mus/window_manager_connection.h"
  30. #include "ui/views/views_delegate.h"
  31. #if defined(OS_CHROMEOS)
  32. #include "chromeos/audio/cras_audio_handler.h"
  33. #include "chromeos/dbus/dbus_thread_manager.h"
  34. #include "device/bluetooth/dbus/bluez_dbus_manager.h"
  35. #include "ui/events/devices/device_data_manager.h"
  36. #endif
  37. using views::ViewsDelegate;
  38. namespace ash {
  39. namespace sysui {
  40. namespace {
  41. // Tries to determine the corresponding mash container from widget init params.
  42. mash::wm::mojom::Container GetContainerId(
  43. const views::Widget::InitParams& params) {
  44. const int id = params.parent->id();
  45. if (id == kShellWindowId_DesktopBackgroundContainer)
  46. return mash::wm::mojom::Container::USER_BACKGROUND;
  47. // mash::wm::ShelfLayout manages both the shelf and the status area.
  48. if (id == kShellWindowId_ShelfContainer ||
  49. id == kShellWindowId_StatusContainer) {
  50. return mash::wm::mojom::Container::USER_SHELF;
  51. }
  52. // Determine the container based on Widget type.
  53. switch (params.type) {
  54. case views::Widget::InitParams::Type::TYPE_BUBBLE:
  55. return mash::wm::mojom::Container::BUBBLES;
  56. case views::Widget::InitParams::Type::TYPE_MENU:
  57. return mash::wm::mojom::Container::MENUS;
  58. case views::Widget::InitParams::Type::TYPE_TOOLTIP:
  59. return mash::wm::mojom::Container::TOOLTIPS;
  60. default:
  61. return mash::wm::mojom::Container::COUNT;
  62. }
  63. }
  64. // Tries to determine the corresponding ash window type from the ash container
  65. // for the widget.
  66. mash::wm::mojom::AshWindowType GetAshWindowType(aura::Window* container) {
  67. DCHECK(container);
  68. int id = container->id();
  69. if (id == kShellWindowId_ShelfContainer)
  70. return mash::wm::mojom::AshWindowType::SHELF;
  71. if (id == kShellWindowId_StatusContainer)
  72. return mash::wm::mojom::AshWindowType::STATUS_AREA;
  73. return mash::wm::mojom::AshWindowType::COUNT;
  74. }
  75. // Creates a StubWindow, which means this window never receives any input event,
  76. // or displays anything to the user.
  77. class AshWindowTreeHostMus : public AshWindowTreeHostPlatform {
  78. public:
  79. explicit AshWindowTreeHostMus(const gfx::Rect& initial_bounds)
  80. : AshWindowTreeHostPlatform() {
  81. std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this));
  82. window->SetBounds(initial_bounds);
  83. SetPlatformWindow(std::move(window));
  84. }
  85. ~AshWindowTreeHostMus() override {}
  86. void OnBoundsChanged(const gfx::Rect& bounds) override {
  87. if (platform_window())
  88. AshWindowTreeHostPlatform::OnBoundsChanged(bounds);
  89. }
  90. };
  91. AshWindowTreeHost* CreateWindowTreeHostMus(
  92. const AshWindowTreeHostInitParams& init_params) {
  93. return new AshWindowTreeHostMus(init_params.initial_bounds);
  94. }
  95. // Responsible for setting up a RootWindowSettings object for the root-window
  96. // created for the views::Widget objects.
  97. class NativeWidgetFactory {
  98. public:
  99. NativeWidgetFactory() {
  100. ViewsDelegate* views_delegate = ViewsDelegate::GetInstance();
  101. DCHECK(views_delegate);
  102. views_delegate->set_native_widget_factory(base::Bind(
  103. &NativeWidgetFactory::InitNativeWidget, base::Unretained(this)));
  104. }
  105. ~NativeWidgetFactory() {
  106. ViewsDelegate::GetInstance()->set_native_widget_factory(
  107. ViewsDelegate::NativeWidgetFactory());
  108. }
  109. private:
  110. views::NativeWidget* InitNativeWidget(
  111. const views::Widget::InitParams& params,
  112. views::internal::NativeWidgetDelegate* delegate) {
  113. std::map<std::string, std::vector<uint8_t>> properties;
  114. if (params.parent) {
  115. mash::wm::mojom::Container container = GetContainerId(params);
  116. if (container != mash::wm::mojom::Container::COUNT) {
  117. properties[mash::wm::mojom::kWindowContainer_Property] =
  118. mojo::ConvertTo<std::vector<uint8_t>>(
  119. static_cast<int32_t>(container));
  120. }
  121. mash::wm::mojom::AshWindowType type = GetAshWindowType(params.parent);
  122. if (type != mash::wm::mojom::AshWindowType::COUNT) {
  123. properties[mash::wm::mojom::kAshWindowType_Property] =
  124. mojo::ConvertTo<std::vector<uint8_t>>(static_cast<int32_t>(type));
  125. }
  126. }
  127. // AshInit installs a stub implementation of ui::ContextFactory, so that the
  128. // AshWindowTreeHost instances created do not actually show anything to the
  129. // user. However, when creating a views::Widget instance, the
  130. // WindowManagerConnection creates a WindowTreeHostMus instance, which
  131. // creates a ui::Compositor, and it needs a real ui::ContextFactory
  132. // implementation. So, unset the context-factory here temporarily when
  133. // creating NativeWidgetMus. But restore the stub instance afterwards, so
  134. // that it is used when new AshWindowTreeHost instances are created (e.g.
  135. // when a new monitor is attached).
  136. ui::ContextFactory* factory = aura::Env::GetInstance()->context_factory();
  137. aura::Env::GetInstance()->set_context_factory(nullptr);
  138. views::NativeWidgetMus* native_widget =
  139. static_cast<views::NativeWidgetMus*>(
  140. views::WindowManagerConnection::Get()->CreateNativeWidgetMus(
  141. properties, params, delegate));
  142. aura::Env::GetInstance()->set_context_factory(factory);
  143. // TODO: Set the correct display id here.
  144. InitRootWindowSettings(native_widget->GetRootWindow())->display_id =
  145. Shell::GetInstance()
  146. ->display_manager()
  147. ->GetPrimaryDisplayCandidate()
  148. .id();
  149. return native_widget;
  150. }
  151. DISALLOW_COPY_AND_ASSIGN(NativeWidgetFactory);
  152. };
  153. } // namespace
  154. class AshInit {
  155. public:
  156. AshInit() : worker_pool_(new base::SequencedWorkerPool(2, "AshWorkerPool")) {
  157. ui::RegisterPathProvider();
  158. InitializeResourceBundle();
  159. }
  160. ~AshInit() { worker_pool_->Shutdown(); }
  161. aura::Window* root() { return ash::Shell::GetPrimaryRootWindow(); }
  162. void Initialize(mojo::Connector* connector) {
  163. aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak"));
  164. views::WindowManagerConnection::Create(connector);
  165. gfx::Screen* screen = gfx::Screen::GetScreen();
  166. DCHECK(screen);
  167. gfx::Size size = screen->GetPrimaryDisplay().bounds().size();
  168. // Uninstall the ScreenMus installed by WindowManagerConnection, so that ash
  169. // installs and uses the ScreenAsh. This can be removed once ash learns to
  170. // talk to mus for managing displays.
  171. gfx::Screen::SetScreenInstance(nullptr);
  172. // Install some hook so that the WindowTreeHostMus created for widgets can
  173. // be hooked up correctly.
  174. native_widget_factory_.reset(new NativeWidgetFactory());
  175. ash::AshWindowTreeHost::SetFactory(base::Bind(&CreateWindowTreeHostMus));
  176. ash_delegate_ = new ShellDelegateMus;
  177. InitializeComponents();
  178. ash::ShellInitParams init_params;
  179. init_params.delegate = ash_delegate_;
  180. init_params.context_factory = new StubContextFactory;
  181. init_params.blocking_pool = worker_pool_.get();
  182. init_params.in_mus = true;
  183. init_params.keyboard_factory =
  184. base::Bind(&KeyboardUIMus::Create, connector);
  185. ash::Shell::CreateInstance(init_params);
  186. ash::Shell::GetInstance()->CreateShelf();
  187. ash::Shell::GetInstance()->UpdateAfterLoginStatusChange(
  188. ash::user::LOGGED_IN_USER);
  189. ash::Shell::GetPrimaryRootWindow()->GetHost()->Show();
  190. SetupWallpaper(SkColorSetARGB(255, 0, 255, 0));
  191. }
  192. void InitializeResourceBundle() {
  193. // Load ash resources and en-US strings; not 'common' (Chrome) resources.
  194. // TODO(msw): Do not load 'test' resources (include sys lang; rename paks?).
  195. // TODO(msw): Use the ResourceProvider interface to load these pak files.
  196. // TODO(msw): Check ResourceBundle::IsScaleFactorSupported; load 300% etc.
  197. base::FilePath path;
  198. PathService::Get(base::DIR_MODULE, &path);
  199. base::FilePath ash_test_strings =
  200. path.Append(FILE_PATH_LITERAL("ash_test_strings.pak"));
  201. base::FilePath ash_test_resources_100 =
  202. path.Append(FILE_PATH_LITERAL("ash_test_resources_100_percent.pak"));
  203. base::FilePath ash_test_resources_200 =
  204. path.Append(FILE_PATH_LITERAL("ash_test_resources_200_percent.pak"));
  205. ui::ResourceBundle::InitSharedInstanceWithPakPath(ash_test_strings);
  206. ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  207. rb.AddDataPackFromPath(ash_test_resources_100, ui::SCALE_FACTOR_100P);
  208. rb.AddDataPackFromPath(ash_test_resources_200, ui::SCALE_FACTOR_200P);
  209. }
  210. void SetupWallpaper(SkColor color) {
  211. SkBitmap bitmap;
  212. bitmap.allocN32Pixels(16, 16);
  213. bitmap.eraseColor(color);
  214. #if !defined(NDEBUG)
  215. // In debug builds we generate a simple pattern that allows visually
  216. // notice if transparency is broken.
  217. {
  218. SkAutoLockPixels alp(bitmap);
  219. *bitmap.getAddr32(0, 0) = SkColorSetRGB(0, 0, 0);
  220. }
  221. #endif
  222. gfx::ImageSkia wallpaper = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
  223. ash::Shell::GetInstance()
  224. ->desktop_background_controller()
  225. ->SetWallpaperImage(wallpaper, wallpaper::WALLPAPER_LAYOUT_TILE);
  226. }
  227. void InitializeComponents() {
  228. message_center::MessageCenter::Initialize();
  229. #if defined(OS_CHROMEOS)
  230. ui::DeviceDataManager::CreateInstance();
  231. chromeos::DBusThreadManager::Initialize();
  232. bluez::BluezDBusManager::Initialize(
  233. chromeos::DBusThreadManager::Get()->GetSystemBus(),
  234. chromeos::DBusThreadManager::Get()->IsUsingStub(
  235. chromeos::DBusClientBundle::BLUETOOTH));
  236. chromeos::CrasAudioHandler::InitializeForTesting();
  237. #endif
  238. }
  239. private:
  240. scoped_refptr<base::SequencedWorkerPool> worker_pool_;
  241. std::unique_ptr<views::AuraInit> aura_init_;
  242. ShellDelegateMus* ash_delegate_ = nullptr;
  243. std::unique_ptr<NativeWidgetFactory> native_widget_factory_;
  244. DISALLOW_COPY_AND_ASSIGN(AshInit);
  245. };
  246. SysUIApplication::SysUIApplication() {}
  247. SysUIApplication::~SysUIApplication() {}
  248. void SysUIApplication::Initialize(mojo::Connector* connector,
  249. const mojo::Identity& identity,
  250. uint32_t id) {
  251. ash_init_.reset(new AshInit());
  252. ash_init_->Initialize(connector);
  253. }
  254. bool SysUIApplication::AcceptConnection(mojo::Connection* connection) {
  255. return true;
  256. }
  257. } // namespace sysui
  258. } // namespace ash