1 // Copyright 2014 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.
5 #include "chrome/browser/ui/views/apps/chrome_native_app_window_views_win.h"
7 #include "apps/ui/views/app_window_frame_view.h"
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/apps/per_app_settings_service.h"
14 #include "chrome/browser/apps/per_app_settings_service_factory.h"
15 #include "chrome/browser/metro_utils/metro_chrome_win.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/shell_integration.h"
18 #include "chrome/browser/ui/views/apps/app_window_desktop_native_widget_aura_win.h"
19 #include "chrome/browser/ui/views/apps/glass_app_window_frame_view_win.h"
20 #include "chrome/browser/web_applications/web_app.h"
21 #include "chrome/browser/web_applications/web_app_win.h"
22 #include "chrome/common/chrome_icon_resources_win.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "extensions/browser/app_window/app_window.h"
27 #include "extensions/browser/app_window/app_window_registry.h"
28 #include "extensions/common/extension.h"
29 #include "ui/aura/remote_window_tree_host_win.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/win/shell.h"
32 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
33 #include "ui/views/win/hwnd_util.h"
35 ChromeNativeAppWindowViewsWin::ChromeNativeAppWindowViewsWin()
36 : glass_frame_view_(NULL
), is_translucent_(false), weak_ptr_factory_(this) {
39 ChromeNativeAppWindowViewsWin::~ChromeNativeAppWindowViewsWin() {
42 void ChromeNativeAppWindowViewsWin::ActivateParentDesktopIfNecessary() {
43 // Only switching into Ash from Native is supported. Tearing the user out of
44 // Metro mode can only be done by launching a process from Metro mode itself.
45 // This is done for launching apps, but not regular activations.
46 if (IsRunningInAsh() &&
47 chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_NATIVE
) {
48 chrome::ActivateMetroChrome();
52 HWND
ChromeNativeAppWindowViewsWin::GetNativeAppWindowHWND() const {
53 return views::HWNDForWidget(widget()->GetTopLevelWidget());
56 bool ChromeNativeAppWindowViewsWin::IsRunningInAsh() {
57 if (!ash::Shell::HasInstance())
60 views::Widget
* widget
=
61 implicit_cast
<views::WidgetDelegate
*>(this)->GetWidget();
62 chrome::HostDesktopType host_desktop_type
=
63 chrome::GetHostDesktopTypeForNativeWindow(widget
->GetNativeWindow());
64 return host_desktop_type
== chrome::HOST_DESKTOP_TYPE_ASH
;
67 void ChromeNativeAppWindowViewsWin::EnsureCaptionStyleSet() {
68 // Windows seems to have issues maximizing windows without WS_CAPTION.
69 // The default views / Aura implementation will remove this if we are using
70 // frameless or colored windows, so we put it back here.
71 HWND hwnd
= GetNativeAppWindowHWND();
72 int current_style
= ::GetWindowLong(hwnd
, GWL_STYLE
);
73 ::SetWindowLong(hwnd
, GWL_STYLE
, current_style
| WS_CAPTION
);
76 void ChromeNativeAppWindowViewsWin::OnBeforeWidgetInit(
77 const extensions::AppWindow::CreateParams
& create_params
,
78 views::Widget::InitParams
* init_params
,
79 views::Widget
* widget
) {
80 ChromeNativeAppWindowViewsAura::OnBeforeWidgetInit(create_params
, init_params
,
83 content::BrowserContext
* browser_context
= app_window()->browser_context();
84 std::string extension_id
= app_window()->extension_id();
85 // If an app has any existing windows, ensure new ones are created on the
87 extensions::AppWindow
* any_existing_window
=
88 extensions::AppWindowRegistry::Get(browser_context
)
89 ->GetCurrentAppWindowForApp(extension_id
);
90 chrome::HostDesktopType desktop_type
;
91 if (any_existing_window
) {
92 desktop_type
= chrome::GetHostDesktopTypeForNativeWindow(
93 any_existing_window
->GetNativeWindow());
95 PerAppSettingsService
* settings
=
96 PerAppSettingsServiceFactory::GetForBrowserContext(browser_context
);
97 if (settings
->HasDesktopLastLaunchedFrom(extension_id
)) {
98 desktop_type
= settings
->GetDesktopLastLaunchedFrom(extension_id
);
100 // We don't know what desktop this app was last launched from, so take our
101 // best guess as to what desktop the user is on.
102 desktop_type
= chrome::GetActiveDesktop();
105 if (desktop_type
== chrome::HOST_DESKTOP_TYPE_ASH
)
106 init_params
->context
= ash::Shell::GetPrimaryRootWindow();
108 init_params
->native_widget
= new AppWindowDesktopNativeWidgetAuraWin(this);
111 init_params
->opacity
== views::Widget::InitParams::TRANSLUCENT_WINDOW
;
114 void ChromeNativeAppWindowViewsWin::InitializeDefaultWindow(
115 const extensions::AppWindow::CreateParams
& create_params
) {
116 ChromeNativeAppWindowViewsAura::InitializeDefaultWindow(create_params
);
118 // Remaining initialization is for Windows shell integration, which doesn't
119 // apply to app windows in Ash.
120 if (IsRunningInAsh())
123 const extensions::Extension
* extension
= app_window()->GetExtension();
127 std::string app_name
=
128 web_app::GenerateApplicationNameFromExtensionId(extension
->id());
129 base::string16 app_name_wide
= base::UTF8ToWide(app_name
);
130 HWND hwnd
= GetNativeAppWindowHWND();
132 Profile::FromBrowserContext(app_window()->browser_context());
134 ShellIntegration::GetAppModelIdForProfile(app_name_wide
,
136 ui::win::SetAppIdForWindow(app_model_id_
, hwnd
);
137 web_app::UpdateRelaunchDetailsForApp(profile
, extension
, hwnd
);
139 if (!create_params
.alpha_enabled
)
140 EnsureCaptionStyleSet();
143 views::NonClientFrameView
*
144 ChromeNativeAppWindowViewsWin::CreateStandardDesktopAppFrame() {
145 glass_frame_view_
= NULL
;
146 if (ui::win::IsAeroGlassEnabled()) {
147 glass_frame_view_
= new GlassAppWindowFrameViewWin(this, widget());
148 return glass_frame_view_
;
150 return ChromeNativeAppWindowViewsAura::CreateStandardDesktopAppFrame();
153 void ChromeNativeAppWindowViewsWin::Show() {
154 ActivateParentDesktopIfNecessary();
155 ChromeNativeAppWindowViewsAura::Show();
158 void ChromeNativeAppWindowViewsWin::Activate() {
159 ActivateParentDesktopIfNecessary();
160 ChromeNativeAppWindowViewsAura::Activate();
163 bool ChromeNativeAppWindowViewsWin::CanMinimize() const {
164 // Resizing on Windows breaks translucency if the window also has shape.
165 // See http://crbug.com/417947.
166 return ChromeNativeAppWindowViewsAura::CanMinimize() &&
167 !(WidgetHasHitTestMask() && is_translucent_
);