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/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/browser/apps/ephemeral_app_launcher.h"
15 #include "chrome/browser/apps/per_app_settings_service.h"
16 #include "chrome/browser/apps/per_app_settings_service_factory.h"
17 #include "chrome/browser/jumplist_updater_win.h"
18 #include "chrome/browser/metro_utils/metro_chrome_win.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/shell_integration.h"
21 #include "chrome/browser/ui/views/apps/app_window_desktop_native_widget_aura_win.h"
22 #include "chrome/browser/ui/views/apps/glass_app_window_frame_view_win.h"
23 #include "chrome/browser/web_applications/web_app.h"
24 #include "chrome/browser/web_applications/web_app_win.h"
25 #include "chrome/common/chrome_icon_resources_win.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "extensions/browser/app_window/app_window.h"
30 #include "extensions/browser/app_window/app_window_registry.h"
31 #include "extensions/browser/extension_util.h"
32 #include "extensions/common/extension.h"
33 #include "ui/aura/remote_window_tree_host_win.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/win/shell.h"
36 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
37 #include "ui/views/win/hwnd_util.h"
39 ChromeNativeAppWindowViewsWin::ChromeNativeAppWindowViewsWin()
40 : glass_frame_view_(NULL
), is_translucent_(false), weak_ptr_factory_(this) {
43 void ChromeNativeAppWindowViewsWin::ActivateParentDesktopIfNecessary() {
44 // Only switching into Ash from Native is supported. Tearing the user out of
45 // Metro mode can only be done by launching a process from Metro mode itself.
46 // This is done for launching apps, but not regular activations.
47 if (IsRunningInAsh() &&
48 chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_NATIVE
) {
49 chrome::ActivateMetroChrome();
53 HWND
ChromeNativeAppWindowViewsWin::GetNativeAppWindowHWND() const {
54 return views::HWNDForWidget(widget()->GetTopLevelWidget());
57 bool ChromeNativeAppWindowViewsWin::IsRunningInAsh() {
58 if (!ash::Shell::HasInstance())
61 views::Widget
* widget
=
62 implicit_cast
<views::WidgetDelegate
*>(this)->GetWidget();
63 chrome::HostDesktopType host_desktop_type
=
64 chrome::GetHostDesktopTypeForNativeWindow(widget
->GetNativeWindow());
65 return host_desktop_type
== chrome::HOST_DESKTOP_TYPE_ASH
;
68 void ChromeNativeAppWindowViewsWin::EnsureCaptionStyleSet() {
69 // Windows seems to have issues maximizing windows without WS_CAPTION.
70 // The default views / Aura implementation will remove this if we are using
71 // frameless or colored windows, so we put it back here.
72 HWND hwnd
= GetNativeAppWindowHWND();
73 int current_style
= ::GetWindowLong(hwnd
, GWL_STYLE
);
74 ::SetWindowLong(hwnd
, GWL_STYLE
, current_style
| WS_CAPTION
);
77 void ChromeNativeAppWindowViewsWin::OnBeforeWidgetInit(
78 const extensions::AppWindow::CreateParams
& create_params
,
79 views::Widget::InitParams
* init_params
,
80 views::Widget
* widget
) {
81 ChromeNativeAppWindowViewsAura::OnBeforeWidgetInit(create_params
, init_params
,
84 content::BrowserContext
* browser_context
= app_window()->browser_context();
85 std::string extension_id
= app_window()->extension_id();
86 // If an app has any existing windows, ensure new ones are created on the
88 extensions::AppWindow
* any_existing_window
=
89 extensions::AppWindowRegistry::Get(browser_context
)
90 ->GetCurrentAppWindowForApp(extension_id
);
91 chrome::HostDesktopType desktop_type
;
92 if (any_existing_window
) {
93 desktop_type
= chrome::GetHostDesktopTypeForNativeWindow(
94 any_existing_window
->GetNativeWindow());
96 PerAppSettingsService
* settings
=
97 PerAppSettingsServiceFactory::GetForBrowserContext(browser_context
);
98 if (settings
->HasDesktopLastLaunchedFrom(extension_id
)) {
99 desktop_type
= settings
->GetDesktopLastLaunchedFrom(extension_id
);
101 // We don't know what desktop this app was last launched from, so take our
102 // best guess as to what desktop the user is on.
103 desktop_type
= chrome::GetActiveDesktop();
106 if (desktop_type
== chrome::HOST_DESKTOP_TYPE_ASH
)
107 init_params
->context
= ash::Shell::GetPrimaryRootWindow();
109 init_params
->native_widget
= new AppWindowDesktopNativeWidgetAuraWin(this);
112 init_params
->opacity
== views::Widget::InitParams::TRANSLUCENT_WINDOW
;
115 void ChromeNativeAppWindowViewsWin::InitializeDefaultWindow(
116 const extensions::AppWindow::CreateParams
& create_params
) {
117 ChromeNativeAppWindowViewsAura::InitializeDefaultWindow(create_params
);
119 // Remaining initialization is for Windows shell integration, which doesn't
120 // apply to app windows in Ash.
121 if (IsRunningInAsh())
124 const extensions::Extension
* extension
= app_window()->GetExtension();
128 std::string app_name
=
129 web_app::GenerateApplicationNameFromExtensionId(extension
->id());
130 base::string16 app_name_wide
= base::UTF8ToWide(app_name
);
131 HWND hwnd
= GetNativeAppWindowHWND();
133 Profile::FromBrowserContext(app_window()->browser_context());
135 ShellIntegration::GetAppModelIdForProfile(app_name_wide
,
137 ui::win::SetAppIdForWindow(app_model_id_
, hwnd
);
138 web_app::UpdateRelaunchDetailsForApp(profile
, extension
, hwnd
);
140 if (!create_params
.alpha_enabled
)
141 EnsureCaptionStyleSet();
145 views::NonClientFrameView
*
146 ChromeNativeAppWindowViewsWin::CreateStandardDesktopAppFrame() {
147 glass_frame_view_
= NULL
;
148 if (ui::win::IsAeroGlassEnabled()) {
149 glass_frame_view_
= new GlassAppWindowFrameViewWin(this, widget());
150 return glass_frame_view_
;
152 return ChromeNativeAppWindowViewsAura::CreateStandardDesktopAppFrame();
155 void ChromeNativeAppWindowViewsWin::Show() {
156 ActivateParentDesktopIfNecessary();
157 ChromeNativeAppWindowViewsAura::Show();
160 void ChromeNativeAppWindowViewsWin::Activate() {
161 ActivateParentDesktopIfNecessary();
162 ChromeNativeAppWindowViewsAura::Activate();
165 bool ChromeNativeAppWindowViewsWin::CanMinimize() const {
166 // Resizing on Windows breaks translucency if the window also has shape.
167 // See http://crbug.com/417947.
168 return ChromeNativeAppWindowViewsAura::CanMinimize() &&
169 !(WidgetHasHitTestMask() && is_translucent_
);
172 void ChromeNativeAppWindowViewsWin::UpdateShelfMenu() {
173 if (!JumpListUpdater::IsEnabled() || IsRunningInAsh())
176 // Currently the only option is related to ephemeral apps, so avoid updating
177 // the app's jump list when the feature is not enabled.
178 if (!EphemeralAppLauncher::IsFeatureEnabled())
181 const extensions::Extension
* extension
= app_window()->GetExtension();
185 // For the icon resources.
186 base::FilePath chrome_path
;
187 if (!PathService::Get(base::FILE_EXE
, &chrome_path
))
190 DCHECK(!app_model_id_
.empty());
192 JumpListUpdater
jumplist_updater(app_model_id_
);
193 if (!jumplist_updater
.BeginUpdate())
196 // Add item to install ephemeral apps.
197 if (extensions::util::IsEphemeralApp(extension
->id(),
198 app_window()->browser_context())) {
199 scoped_refptr
<ShellLinkItem
> link(new ShellLinkItem());
200 link
->set_title(l10n_util::GetStringUTF16(IDS_APP_INSTALL_TITLE
));
201 link
->set_icon(chrome_path
.value(),
202 icon_resources::kInstallPackagedAppIndex
);
203 ShellIntegration::AppendProfileArgs(
204 app_window()->browser_context()->GetPath(), link
->GetCommandLine());
205 link
->GetCommandLine()->AppendSwitchASCII(
206 switches::kInstallEphemeralAppFromWebstore
, extension
->id());
208 ShellLinkItemList items
;
209 items
.push_back(link
);
210 jumplist_updater
.AddTasks(items
);
213 // Note that an empty jumplist must still be committed to clear all items.
214 jumplist_updater
.CommitUpdate();