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