shell.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/win/shell.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <dwmapi.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <shlobj.h>  // Must be before propkey.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <propkey.h>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <shellapi.h>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/command_line.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/native_library.h"
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/metro.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/scoped_comptr.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/win_util.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/windows_version.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/base/ui_base_switches.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace win {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Show the Windows "Open With" dialog box to ask the user to pick an app to
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// open the file with.
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool OpenItemWithExternalApp(const base::string16& full_path) {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SHELLEXECUTEINFO sei = { sizeof(sei) };
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.fMask = SEE_MASK_FLAG_DDEWAIT;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.nShow = SW_SHOWNORMAL;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.lpVerb = L"openas";
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.lpFile = full_path.c_str();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (TRUE == ::ShellExecuteExW(&sei));
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool OpenAnyViaShell(const base::string16& full_path,
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const base::string16& directory,
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const base::string16& args,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     DWORD mask) {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SHELLEXECUTEINFO sei = { sizeof(sei) };
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.fMask = mask;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.nShow = SW_SHOWNORMAL;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.lpFile = full_path.c_str();
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sei.lpDirectory = directory.c_str();
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!args.empty())
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sei.lpParameters = args.c_str();
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (::ShellExecuteExW(&sei))
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (::GetLastError() == ERROR_NO_ASSOCIATION)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return OpenItemWithExternalApp(full_path);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool OpenItemViaShell(const base::FilePath& full_path) {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return OpenAnyViaShell(full_path.value(), full_path.DirName().value(),
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16(), 0);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool OpenItemViaShellNoZoneCheck(const base::FilePath& full_path) {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return OpenAnyViaShell(full_path.value(), base::string16(), base::string16(),
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         SEE_MASK_NOZONECHECKS | SEE_MASK_FLAG_DDEWAIT);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool PreventWindowFromPinning(HWND hwnd) {
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // This functionality is only available on Win7+. It also doesn't make sense
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // to do this for Chrome Metro.
683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (base::win::GetVersion() < base::win::VERSION_WIN7 ||
693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::win::IsMetroProcess())
703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return false;
713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::win::ScopedComPtr<IPropertyStore> pps;
723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  HRESULT result = SHGetPropertyStoreForWindow(
733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      hwnd, __uuidof(*pps), reinterpret_cast<void**>(pps.Receive()));
743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (FAILED(result))
753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return false;
763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return base::win::SetBooleanValueForPropertyStore(
783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)             pps, PKEY_AppUserModel_PreventPinning, true);
793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// TODO(calamity): investigate moving this out of the UI thread as COM
820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// operations may spawn nested message loops which can cause issues.
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetAppDetailsForWindow(const base::string16& app_id,
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            const base::string16& app_icon,
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            const base::string16& relaunch_command,
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            const base::string16& relaunch_display_name,
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                            HWND hwnd) {
880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // This functionality is only available on Win7+. It also doesn't make sense
890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // to do this for Chrome Metro.
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (base::win::GetVersion() < base::win::VERSION_WIN7 ||
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::win::IsMetroProcess())
920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::win::ScopedComPtr<IPropertyStore> pps;
940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HRESULT result = SHGetPropertyStoreForWindow(
950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      hwnd, __uuidof(*pps), reinterpret_cast<void**>(pps.Receive()));
960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (S_OK == result) {
970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (!app_id.empty())
980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::win::SetAppIdForPropertyStore(pps, app_id.c_str());
990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (!app_icon.empty()) {
1000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::win::SetStringValueForPropertyStore(
1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          pps, PKEY_AppUserModel_RelaunchIconResource, app_icon.c_str());
1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (!relaunch_command.empty()) {
1040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::win::SetStringValueForPropertyStore(
1050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          pps, PKEY_AppUserModel_RelaunchCommand, relaunch_command.c_str());
1060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
1070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (!relaunch_display_name.empty()) {
1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::win::SetStringValueForPropertyStore(
1090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          pps, PKEY_AppUserModel_RelaunchDisplayNameResource,
1100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          relaunch_display_name.c_str());
1110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
1120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetAppIdForWindow(const base::string16& app_id, HWND hwnd) {
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetAppDetailsForWindow(app_id,
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16(),
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16(),
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16(),
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         hwnd);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetAppIconForWindow(const base::string16& app_icon, HWND hwnd) {
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetAppDetailsForWindow(base::string16(),
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         app_icon,
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16(),
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16(),
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         hwnd);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetRelaunchDetailsForWindow(const base::string16& relaunch_command,
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const base::string16& display_name,
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 HWND hwnd) {
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetAppDetailsForWindow(base::string16(),
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         base::string16(),
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         relaunch_command,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         display_name,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         hwnd);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsAeroGlassEnabled() {
142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // For testing in Win8 (where it is not possible to disable composition) the
143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // user can specify this command line switch to mimic the behavior.  In this
144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // mode, cross-HWND transparency is not supported and various types of
145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // widgets fallback to more simplified rendering behavior.
146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (CommandLine::ForCurrentProcess()->HasSwitch(
147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      switches::kDisableDwmComposition))
148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (base::win::GetVersion() < base::win::VERSION_VISTA)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If composition is not enabled, we behave like on XP.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BOOL enabled = FALSE;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace win
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ui
159