PSA: HostDesktopType is your new friend.

21 views
Skip to first unread message

Gabriel Charette

unread,
Feb 21, 2013, 6:53:06 PM2/21/13
to Chromium-dev
tl;dr; Many methods in chrome now take chrome::HostDesktopType as a parameter and you should almost never hardcode HOST_DESKTOP_TYPE_X constants.

Hello all,

Aura now makes it possible for the same browser process to render to multiple desktops simultaneously (e.g., Native Desktop and Metro Desktop on Windows 8).

To enable this, the Chromium codebase had to be made multi-desktop aware (i.e., chrome::HostDesktopType parameters were added in several locations).

So... a bunch of methods now take chrome::HostDesktopType, but how do you get the right HostDesktopType when you need to provide one?
There are many ways:
  1. If you have a Browser: take its host_desktop_type() member.
  2. If you have a gfx::NativeView: use chrome::GetHostDesktopTypeForNativeView().
  3. If you have a gfx::NativeWindow: use chrome::GetHostDesktopTypeForNativeWindow().
  4. If you have a WebContents* w: you can use (2) passing in w->GetNativeView()  OR you might be able to get a Browser with chrome::FindBrowserWithWebContents() and use (1)
  5. If all else fails and you have absolutely NO way of getting desktop context (e.g., a background extension that wants to open a new window...): you can use chrome::GetActiveDesktop() to get the type of the last user-activated Chrome desktop, but be aware that this is inherently racy (i.e., the user can switch desktops at any time).
  6. You should almost never hardcode HOST_DESKTOP_TYPE_X constants (except in tests for now).
  7. If you work on Chrome OS and your code is under ash/, please use HOST_DESKTOP_TYPE_ASH (this is the same as HOST_DESKTOP_TYPE_NATIVE on Chrome OS, but not on other platforms).
Most of the methods mentioned above are found in src/chrome/browser/ui/host_desktop.h.

Note: If you hardcode HOST_DESKTOP_TYPE_NATIVE it will look like it's working if you're in a single-desktop environment (which most of you are), but what it will really do is: one day someone will trigger your feature from the Ash desktop (e.g., in Windows 8 Metro) and it will open a window/tab on the native desktop (invisible to the user who is immersed in the Ash environment)...

There is an ongoing effort to get rid of all calls that hardcode HOST_DESKTOP_TYPE_NATIVE when they shouldn't; please do not introduce new ones; and, if you feel bold, feel free to fix some hardcoded calls that you think should be multi-desktop aware.


Cheers!
Gab
Reply all
Reply to author
Forward
0 new messages