| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/apps/native_app_window_views_win.h" | 5 #include "chrome/browser/ui/views/apps/native_app_window_views_win.h" |
| 6 | 6 |
| 7 #include "apps/shell_window.h" | 7 #include "apps/shell_window.h" |
| 8 #include "apps/shell_window_registry.h" | 8 #include "apps/shell_window_registry.h" |
| 9 #include "ash/shell.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" |
| 10 #include "chrome/browser/apps/per_app_settings_service.h" | 14 #include "chrome/browser/apps/per_app_settings_service.h" |
| 11 #include "chrome/browser/apps/per_app_settings_service_factory.h" | 15 #include "chrome/browser/apps/per_app_settings_service_factory.h" |
| 12 #include "chrome/browser/metro_utils/metro_chrome_win.h" | 16 #include "chrome/browser/metro_utils/metro_chrome_win.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/views/apps/jumplist_updater_win.h" |
| 19 #include "chrome/browser/ui/web_applications/web_app_ui.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_switches.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 14 #include "extensions/common/extension.h" | 24 #include "extensions/common/extension.h" |
| 25 #include "grit/chromium_strings.h" |
| 15 #include "ui/aura/remote_root_window_host_win.h" | 26 #include "ui/aura/remote_root_window_host_win.h" |
| 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/win/shell.h" |
| 16 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" | 29 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" |
| 30 #include "ui/views/win/hwnd_util.h" |
| 17 | 31 |
| 18 NativeAppWindowViewsWin::NativeAppWindowViewsWin() { | 32 namespace { |
| 33 |
| 34 void CreateIconAndSetRelaunchDetails( |
| 35 const base::FilePath web_app_path, |
| 36 const base::FilePath icon_file, |
| 37 const ShellIntegration::ShortcutInfo& shortcut_info, |
| 38 const HWND hwnd) { |
| 39 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 40 |
| 41 // Set the relaunch data so "Pin this program to taskbar" has the app's |
| 42 // information. |
| 43 CommandLine command_line = ShellIntegration::CommandLineArgsForLauncher( |
| 44 shortcut_info.url, |
| 45 shortcut_info.extension_id, |
| 46 shortcut_info.profile_path); |
| 47 |
| 48 base::FilePath chrome_exe; |
| 49 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 50 NOTREACHED(); |
| 51 return; |
| 52 } |
| 53 command_line.SetProgram(chrome_exe); |
| 54 ui::win::SetRelaunchDetailsForWindow(command_line.GetCommandLineString(), |
| 55 shortcut_info.title, hwnd); |
| 56 |
| 57 if (!base::PathExists(web_app_path) && |
| 58 !base::CreateDirectory(web_app_path)) |
| 59 return; |
| 60 |
| 61 ui::win::SetAppIconForWindow(icon_file.value(), hwnd); |
| 62 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); |
| 63 } |
| 64 |
| 65 base::FilePath GetInstallIconPath() { |
| 66 const wchar_t kInstallIconSubdir[] = L"icons"; |
| 67 const wchar_t kInstallIconFileName[] = L"star.ico"; |
| 68 |
| 69 base::FilePath icon_path; |
| 70 if (!PathService::Get(base::DIR_MODULE, &icon_path)) |
| 71 return base::FilePath(); |
| 72 |
| 73 return icon_path.Append(kInstallIconSubdir).Append(kInstallIconFileName); |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
| 78 NativeAppWindowViewsWin::NativeAppWindowViewsWin() |
| 79 : weak_ptr_factory_(this) { |
| 19 } | 80 } |
| 20 | 81 |
| 21 void NativeAppWindowViewsWin::ActivateParentDesktopIfNecessary() { | 82 void NativeAppWindowViewsWin::ActivateParentDesktopIfNecessary() { |
| 22 if (!ash::Shell::HasInstance()) | 83 if (!ash::Shell::HasInstance()) |
| 23 return; | 84 return; |
| 24 | 85 |
| 25 views::Widget* widget = | 86 views::Widget* widget = |
| 26 implicit_cast<views::WidgetDelegate*>(this)->GetWidget(); | 87 implicit_cast<views::WidgetDelegate*>(this)->GetWidget(); |
| 27 chrome::HostDesktopType host_desktop_type = | 88 chrome::HostDesktopType host_desktop_type = |
| 28 chrome::GetHostDesktopTypeForNativeWindow(widget->GetNativeWindow()); | 89 chrome::GetHostDesktopTypeForNativeWindow(widget->GetNativeWindow()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 56 // best guess as to what desktop the user is on. | 117 // best guess as to what desktop the user is on. |
| 57 desktop_type = chrome::GetActiveDesktop(); | 118 desktop_type = chrome::GetActiveDesktop(); |
| 58 } | 119 } |
| 59 } | 120 } |
| 60 if (desktop_type == chrome::HOST_DESKTOP_TYPE_ASH) | 121 if (desktop_type == chrome::HOST_DESKTOP_TYPE_ASH) |
| 61 init_params->context = ash::Shell::GetPrimaryRootWindow(); | 122 init_params->context = ash::Shell::GetPrimaryRootWindow(); |
| 62 else | 123 else |
| 63 init_params->native_widget = new views::DesktopNativeWidgetAura(widget); | 124 init_params->native_widget = new views::DesktopNativeWidgetAura(widget); |
| 64 } | 125 } |
| 65 | 126 |
| 127 void NativeAppWindowViewsWin::InitializeDefaultWindow( |
| 128 const apps::ShellWindow::CreateParams& create_params) { |
| 129 NativeAppWindowViews::InitializeDefaultWindow(create_params); |
| 130 |
| 131 std::string app_name = |
| 132 web_app::GenerateApplicationNameFromExtensionId(extension()->id()); |
| 133 base::string16 app_name_wide = base::UTF8ToWide(app_name); |
| 134 HWND hwnd = GetNativeAppWindowHWND(); |
| 135 base::string16 app_id = ShellIntegration::GetAppModelIdForProfile( |
| 136 app_name_wide, profile()->GetPath()); |
| 137 ui::win::SetAppIdForWindow(app_id, hwnd); |
| 138 |
| 139 web_app::UpdateShortcutInfoAndIconForApp( |
| 140 *extension(), profile(), |
| 141 base::Bind(&NativeAppWindowViewsWin::OnShortcutInfoLoaded, |
| 142 weak_ptr_factory_.GetWeakPtr())); |
| 143 |
| 144 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 145 switches::kEnableEphemeralApps)) { |
| 146 jumplist_updater_.reset(new JumpListUpdater(profile(), app_id)); |
| 147 UpdateAppMenu(); |
| 148 } |
| 149 } |
| 150 |
| 66 void NativeAppWindowViewsWin::Show() { | 151 void NativeAppWindowViewsWin::Show() { |
| 67 ActivateParentDesktopIfNecessary(); | 152 ActivateParentDesktopIfNecessary(); |
| 68 NativeAppWindowViews::Show(); | 153 NativeAppWindowViews::Show(); |
| 69 } | 154 } |
| 70 | 155 |
| 71 void NativeAppWindowViewsWin::Activate() { | 156 void NativeAppWindowViewsWin::Activate() { |
| 72 ActivateParentDesktopIfNecessary(); | 157 ActivateParentDesktopIfNecessary(); |
| 73 NativeAppWindowViews::Activate(); | 158 NativeAppWindowViews::Activate(); |
| 74 } | 159 } |
| 160 |
| 161 void NativeAppWindowViewsWin::UpdateAppMenu() { |
| 162 if (!jumplist_updater_.get()) |
| 163 return; |
| 164 |
| 165 std::vector<JumpListUpdater::ListItem> list_items; |
| 166 |
| 167 // Add item to install ephemeral apps. |
| 168 if (extension()->is_ephemeral()) { |
| 169 JumpListUpdater::ListItem install_item( |
| 170 l10n_util::GetStringUTF16(IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE), |
| 171 GetInstallIconPath()); |
| 172 JumpListUpdater::AppendArgs(switches::kInstallFromWebstore, |
| 173 base::UTF8ToWide(extension()->id()), |
| 174 &install_item.command_line_args); |
| 175 list_items.push_back(install_item); |
| 176 } |
| 177 |
| 178 jumplist_updater_->Update(list_items); |
| 179 } |
| 180 |
| 181 void NativeAppWindowViewsWin::OnShortcutInfoLoaded( |
| 182 const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 183 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 184 |
| 185 HWND hwnd = GetNativeAppWindowHWND(); |
| 186 |
| 187 // Set window's icon to the one we're about to create/update in the web app |
| 188 // path. The icon cache will refresh on icon creation. |
| 189 base::FilePath web_app_path = web_app::GetWebAppDataDirectory( |
| 190 shortcut_info.profile_path, shortcut_info.extension_id, |
| 191 shortcut_info.url); |
| 192 base::FilePath icon_file = web_app_path |
| 193 .Append(web_app::internals::GetSanitizedFileName(shortcut_info.title)) |
| 194 .ReplaceExtension(FILE_PATH_LITERAL(".ico")); |
| 195 |
| 196 content::BrowserThread::PostBlockingPoolTask( |
| 197 FROM_HERE, |
| 198 base::Bind(&CreateIconAndSetRelaunchDetails, |
| 199 web_app_path, icon_file, shortcut_info, hwnd)); |
| 200 } |
| 201 |
| 202 HWND NativeAppWindowViewsWin::GetNativeAppWindowHWND() const { |
| 203 return views::HWNDForWidget(window()->GetTopLevelWidget()); |
| 204 } |
| OLD | NEW |