Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / apps / chrome_native_app_window_views_win.cc
blobeaa1ec6a0cf1a5665c8882206e45a62f4689536e
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"
8 #include "ash/shell.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())
58 return false;
60 views::Widget* widget = GetWidget();
61 chrome::HostDesktopType host_desktop_type =
62 chrome::GetHostDesktopTypeForNativeWindow(widget->GetNativeWindow());
63 return host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH;
66 void ChromeNativeAppWindowViewsWin::EnsureCaptionStyleSet() {
67 // Windows seems to have issues maximizing windows without WS_CAPTION.
68 // The default views / Aura implementation will remove this if we are using
69 // frameless or colored windows, so we put it back here.
70 HWND hwnd = GetNativeAppWindowHWND();
71 int current_style = ::GetWindowLong(hwnd, GWL_STYLE);
72 ::SetWindowLong(hwnd, GWL_STYLE, current_style | WS_CAPTION);
75 void ChromeNativeAppWindowViewsWin::OnBeforeWidgetInit(
76 const extensions::AppWindow::CreateParams& create_params,
77 views::Widget::InitParams* init_params,
78 views::Widget* widget) {
79 ChromeNativeAppWindowViewsAura::OnBeforeWidgetInit(create_params, init_params,
80 widget);
82 content::BrowserContext* browser_context = app_window()->browser_context();
83 std::string extension_id = app_window()->extension_id();
84 // If an app has any existing windows, ensure new ones are created on the
85 // same desktop.
86 extensions::AppWindow* any_existing_window =
87 extensions::AppWindowRegistry::Get(browser_context)
88 ->GetCurrentAppWindowForApp(extension_id);
89 chrome::HostDesktopType desktop_type;
90 if (any_existing_window) {
91 desktop_type = chrome::GetHostDesktopTypeForNativeWindow(
92 any_existing_window->GetNativeWindow());
93 } else {
94 PerAppSettingsService* settings =
95 PerAppSettingsServiceFactory::GetForBrowserContext(browser_context);
96 if (settings->HasDesktopLastLaunchedFrom(extension_id)) {
97 desktop_type = settings->GetDesktopLastLaunchedFrom(extension_id);
98 } else {
99 // We don't know what desktop this app was last launched from, so take our
100 // best guess as to what desktop the user is on.
101 desktop_type = chrome::GetActiveDesktop();
104 if (desktop_type == chrome::HOST_DESKTOP_TYPE_ASH)
105 init_params->context = ash::Shell::GetPrimaryRootWindow();
106 else
107 init_params->native_widget = new AppWindowDesktopNativeWidgetAuraWin(this);
109 is_translucent_ =
110 init_params->opacity == views::Widget::InitParams::TRANSLUCENT_WINDOW;
113 void ChromeNativeAppWindowViewsWin::InitializeDefaultWindow(
114 const extensions::AppWindow::CreateParams& create_params) {
115 ChromeNativeAppWindowViewsAura::InitializeDefaultWindow(create_params);
117 // Remaining initialization is for Windows shell integration, which doesn't
118 // apply to app windows in Ash.
119 if (IsRunningInAsh())
120 return;
122 const extensions::Extension* extension = app_window()->GetExtension();
123 if (!extension)
124 return;
126 std::string app_name =
127 web_app::GenerateApplicationNameFromExtensionId(extension->id());
128 base::string16 app_name_wide = base::UTF8ToWide(app_name);
129 HWND hwnd = GetNativeAppWindowHWND();
130 Profile* profile =
131 Profile::FromBrowserContext(app_window()->browser_context());
132 app_model_id_ =
133 ShellIntegration::GetAppModelIdForProfile(app_name_wide,
134 profile->GetPath());
135 ui::win::SetAppIdForWindow(app_model_id_, hwnd);
136 web_app::UpdateRelaunchDetailsForApp(profile, extension, hwnd);
138 if (!create_params.alpha_enabled)
139 EnsureCaptionStyleSet();
142 views::NonClientFrameView*
143 ChromeNativeAppWindowViewsWin::CreateStandardDesktopAppFrame() {
144 glass_frame_view_ = NULL;
145 if (ui::win::IsAeroGlassEnabled()) {
146 glass_frame_view_ = new GlassAppWindowFrameViewWin(this, widget());
147 return glass_frame_view_;
149 return ChromeNativeAppWindowViewsAura::CreateStandardDesktopAppFrame();
152 void ChromeNativeAppWindowViewsWin::Show() {
153 ActivateParentDesktopIfNecessary();
154 ChromeNativeAppWindowViewsAura::Show();
157 void ChromeNativeAppWindowViewsWin::Activate() {
158 ActivateParentDesktopIfNecessary();
159 ChromeNativeAppWindowViewsAura::Activate();
162 bool ChromeNativeAppWindowViewsWin::CanMinimize() const {
163 // Resizing on Windows breaks translucency if the window also has shape.
164 // See http://crbug.com/417947.
165 return ChromeNativeAppWindowViewsAura::CanMinimize() &&
166 !(WidgetHasHitTestMask() && is_translucent_);