chrome_native_app_window_views_win.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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. 4 5#include "chrome/browser/ui/views/apps/chrome_native_app_window_views_win.h" 6 7#include "apps/app_window.h" 8#include "apps/app_window_registry.h" 9#include "ash/shell.h" 10#include "base/command_line.h" 11#include "base/file_util.h" 12#include "base/path_service.h" 13#include "base/strings/utf_string_conversions.h" 14#include "base/threading/sequenced_worker_pool.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/web_applications/web_app.h" 22#include "chrome/browser/web_applications/web_app_win.h" 23#include "chrome/common/chrome_icon_resources_win.h" 24#include "chrome/common/chrome_switches.h" 25#include "content/public/browser/browser_thread.h" 26#include "extensions/common/extension.h" 27#include "grit/generated_resources.h" 28#include "ui/aura/remote_window_tree_host_win.h" 29#include "ui/base/l10n/l10n_util.h" 30#include "ui/base/win/shell.h" 31#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" 32#include "ui/views/win/hwnd_util.h" 33 34namespace { 35 36void CreateIconAndSetRelaunchDetails( 37 const base::FilePath& web_app_path, 38 const base::FilePath& icon_file, 39 const web_app::ShortcutInfo& shortcut_info, 40 const HWND hwnd) { 41 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 42 43 // Set the relaunch data so "Pin this program to taskbar" has the app's 44 // information. 45 CommandLine command_line = ShellIntegration::CommandLineArgsForLauncher( 46 shortcut_info.url, 47 shortcut_info.extension_id, 48 shortcut_info.profile_path); 49 50 base::FilePath chrome_exe; 51 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 52 NOTREACHED(); 53 return; 54 } 55 command_line.SetProgram(chrome_exe); 56 ui::win::SetRelaunchDetailsForWindow(command_line.GetCommandLineString(), 57 shortcut_info.title, hwnd); 58 59 if (!base::PathExists(web_app_path) && 60 !base::CreateDirectory(web_app_path)) { 61 return; 62 } 63 64 ui::win::SetAppIconForWindow(icon_file.value(), hwnd); 65 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); 66} 67 68} // namespace 69 70ChromeNativeAppWindowViewsWin::ChromeNativeAppWindowViewsWin() 71 : weak_ptr_factory_(this) {} 72 73void ChromeNativeAppWindowViewsWin::ActivateParentDesktopIfNecessary() { 74 if (!ash::Shell::HasInstance()) 75 return; 76 77 views::Widget* widget = 78 implicit_cast<views::WidgetDelegate*>(this)->GetWidget(); 79 chrome::HostDesktopType host_desktop_type = 80 chrome::GetHostDesktopTypeForNativeWindow(widget->GetNativeWindow()); 81 // Only switching into Ash from Native is supported. Tearing the user out of 82 // Metro mode can only be done by launching a process from Metro mode itself. 83 // This is done for launching apps, but not regular activations. 84 if (host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH && 85 chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_NATIVE) { 86 chrome::ActivateMetroChrome(); 87 } 88} 89 90void ChromeNativeAppWindowViewsWin::OnShortcutInfoLoaded( 91 const web_app::ShortcutInfo& shortcut_info) { 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 93 94 HWND hwnd = GetNativeAppWindowHWND(); 95 96 // Set window's icon to the one we're about to create/update in the web app 97 // path. The icon cache will refresh on icon creation. 98 base::FilePath web_app_path = web_app::GetWebAppDataDirectory( 99 shortcut_info.profile_path, shortcut_info.extension_id, 100 shortcut_info.url); 101 base::FilePath icon_file = web_app_path 102 .Append(web_app::internals::GetSanitizedFileName(shortcut_info.title)) 103 .ReplaceExtension(FILE_PATH_LITERAL(".ico")); 104 105 content::BrowserThread::PostBlockingPoolTask( 106 FROM_HERE, 107 base::Bind(&CreateIconAndSetRelaunchDetails, 108 web_app_path, icon_file, shortcut_info, hwnd)); 109} 110 111HWND ChromeNativeAppWindowViewsWin::GetNativeAppWindowHWND() const { 112 return views::HWNDForWidget(widget()->GetTopLevelWidget()); 113} 114 115void ChromeNativeAppWindowViewsWin::OnBeforeWidgetInit( 116 views::Widget::InitParams* init_params, 117 views::Widget* widget) { 118 content::BrowserContext* browser_context = app_window()->browser_context(); 119 const extensions::Extension* extension = app_window()->extension(); 120 // If an app has any existing windows, ensure new ones are created on the 121 // same desktop. 122 apps::AppWindow* any_existing_window = 123 apps::AppWindowRegistry::Get(browser_context) 124 ->GetCurrentAppWindowForApp(extension->id()); 125 chrome::HostDesktopType desktop_type; 126 if (any_existing_window) { 127 desktop_type = chrome::GetHostDesktopTypeForNativeWindow( 128 any_existing_window->GetNativeWindow()); 129 } else { 130 PerAppSettingsService* settings = 131 PerAppSettingsServiceFactory::GetForBrowserContext(browser_context); 132 if (settings->HasDesktopLastLaunchedFrom(extension->id())) { 133 desktop_type = settings->GetDesktopLastLaunchedFrom(extension->id()); 134 } else { 135 // We don't know what desktop this app was last launched from, so take our 136 // best guess as to what desktop the user is on. 137 desktop_type = chrome::GetActiveDesktop(); 138 } 139 } 140 if (desktop_type == chrome::HOST_DESKTOP_TYPE_ASH) 141 init_params->context = ash::Shell::GetPrimaryRootWindow(); 142 else 143 init_params->native_widget = new views::DesktopNativeWidgetAura(widget); 144} 145 146void ChromeNativeAppWindowViewsWin::InitializeDefaultWindow( 147 const apps::AppWindow::CreateParams& create_params) { 148 ChromeNativeAppWindowViews::InitializeDefaultWindow(create_params); 149 150 const extensions::Extension* extension = app_window()->extension(); 151 std::string app_name = 152 web_app::GenerateApplicationNameFromExtensionId(extension->id()); 153 base::string16 app_name_wide = base::UTF8ToWide(app_name); 154 HWND hwnd = GetNativeAppWindowHWND(); 155 Profile* profile = 156 Profile::FromBrowserContext(app_window()->browser_context()); 157 app_model_id_ = 158 ShellIntegration::GetAppModelIdForProfile(app_name_wide, 159 profile->GetPath()); 160 ui::win::SetAppIdForWindow(app_model_id_, hwnd); 161 162 web_app::UpdateShortcutInfoAndIconForApp( 163 extension, 164 profile, 165 base::Bind(&ChromeNativeAppWindowViewsWin::OnShortcutInfoLoaded, 166 weak_ptr_factory_.GetWeakPtr())); 167 168 UpdateShelfMenu(); 169} 170 171void ChromeNativeAppWindowViewsWin::Show() { 172 ActivateParentDesktopIfNecessary(); 173 ChromeNativeAppWindowViews::Show(); 174} 175 176void ChromeNativeAppWindowViewsWin::Activate() { 177 ActivateParentDesktopIfNecessary(); 178 ChromeNativeAppWindowViews::Activate(); 179} 180 181void ChromeNativeAppWindowViewsWin::UpdateShelfMenu() { 182 if (!JumpListUpdater::IsEnabled()) 183 return; 184 185 // Currently the only option is related to ephemeral apps, so avoid updating 186 // the app's jump list when the feature is not enabled. 187 if (!CommandLine::ForCurrentProcess()->HasSwitch( 188 switches::kEnableEphemeralApps)) { 189 return; 190 } 191 192 // For the icon resources. 193 base::FilePath chrome_path; 194 if (!PathService::Get(base::FILE_EXE, &chrome_path)) 195 return; 196 197 JumpListUpdater jumplist_updater(app_model_id_); 198 if (!jumplist_updater.BeginUpdate()) 199 return; 200 201 // Add item to install ephemeral apps. 202 const extensions::Extension* extension = app_window()->extension(); 203 DCHECK(extension); 204 if (extension->is_ephemeral()) { 205 scoped_refptr<ShellLinkItem> link(new ShellLinkItem()); 206 link->set_title(l10n_util::GetStringUTF16(IDS_APP_INSTALL_TITLE)); 207 link->set_icon(chrome_path.value(), 208 icon_resources::kInstallPackagedAppIndex); 209 ShellIntegration::AppendProfileArgs( 210 app_window()->browser_context()->GetPath(), link->GetCommandLine()); 211 link->GetCommandLine()->AppendSwitchASCII(switches::kInstallFromWebstore, 212 extension->id()); 213 214 ShellLinkItemList items; 215 items.push_back(link); 216 jumplist_updater.AddTasks(items); 217 } 218 219 jumplist_updater.CommitUpdate(); 220} 221