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) 255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace { 265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Show the Windows "Open With" dialog box to ask the user to pick an app to 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// open the file with. 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool OpenItemWithExternalApp(const base::string16& full_path) { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SHELLEXECUTEINFO sei = { sizeof(sei) }; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.fMask = SEE_MASK_FLAG_DDEWAIT; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.nShow = SW_SHOWNORMAL; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.lpVerb = L"openas"; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.lpFile = full_path.c_str(); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return (TRUE == ::ShellExecuteExW(&sei)); 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} // namespace 395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool OpenAnyViaShell(const base::string16& full_path, 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::string16& directory, 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::string16& args, 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DWORD mask) { 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SHELLEXECUTEINFO sei = { sizeof(sei) }; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.fMask = mask; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.nShow = SW_SHOWNORMAL; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.lpFile = full_path.c_str(); 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.lpDirectory = directory.c_str(); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!args.empty()) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sei.lpParameters = args.c_str(); 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (::ShellExecuteExW(&sei)) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (::GetLastError() == ERROR_NO_ASSOCIATION) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return OpenItemWithExternalApp(full_path); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool OpenItemViaShell(const base::FilePath& full_path) { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return OpenAnyViaShell(full_path.value(), full_path.DirName().value(), 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16(), 0); 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool PreventWindowFromPinning(HWND hwnd) { 653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // This functionality is only available on Win7+. It also doesn't make sense 663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // to do this for Chrome Metro. 673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (base::win::GetVersion() < base::win::VERSION_WIN7 || 683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) base::win::IsMetroProcess()) 693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return false; 703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) base::win::ScopedComPtr<IPropertyStore> pps; 713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) HRESULT result = SHGetPropertyStoreForWindow( 723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) hwnd, __uuidof(*pps), reinterpret_cast<void**>(pps.Receive())); 733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (FAILED(result)) 743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return false; 753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return base::win::SetBooleanValueForPropertyStore( 773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pps, PKEY_AppUserModel_PreventPinning, true); 783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// TODO(calamity): investigate moving this out of the UI thread as COM 810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// operations may spawn nested message loops which can cause issues. 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetAppDetailsForWindow(const base::string16& app_id, 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::string16& app_icon, 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::string16& relaunch_command, 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::string16& relaunch_display_name, 860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HWND hwnd) { 870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // This functionality is only available on Win7+. It also doesn't make sense 880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // to do this for Chrome Metro. 890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (base::win::GetVersion() < base::win::VERSION_WIN7 || 900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::win::IsMetroProcess()) 910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return; 920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::win::ScopedComPtr<IPropertyStore> pps; 930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HRESULT result = SHGetPropertyStoreForWindow( 940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) hwnd, __uuidof(*pps), reinterpret_cast<void**>(pps.Receive())); 950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (S_OK == result) { 960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!app_id.empty()) 970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::win::SetAppIdForPropertyStore(pps, app_id.c_str()); 980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!app_icon.empty()) { 990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::win::SetStringValueForPropertyStore( 1000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) pps, PKEY_AppUserModel_RelaunchIconResource, app_icon.c_str()); 1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!relaunch_command.empty()) { 1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::win::SetStringValueForPropertyStore( 1040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) pps, PKEY_AppUserModel_RelaunchCommand, relaunch_command.c_str()); 1050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!relaunch_display_name.empty()) { 1070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::win::SetStringValueForPropertyStore( 1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) pps, PKEY_AppUserModel_RelaunchDisplayNameResource, 1090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) relaunch_display_name.c_str()); 1100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetAppIdForWindow(const base::string16& app_id, HWND hwnd) { 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SetAppDetailsForWindow(app_id, 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16(), 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16(), 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16(), 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) hwnd); 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetAppIconForWindow(const base::string16& app_icon, HWND hwnd) { 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SetAppDetailsForWindow(base::string16(), 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) app_icon, 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16(), 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16(), 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) hwnd); 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SetRelaunchDetailsForWindow(const base::string16& relaunch_command, 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::string16& display_name, 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HWND hwnd) { 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SetAppDetailsForWindow(base::string16(), 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16(), 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) relaunch_command, 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) display_name, 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) hwnd); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsAeroGlassEnabled() { 141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // For testing in Win8 (where it is not possible to disable composition) the 142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // user can specify this command line switch to mimic the behavior. In this 143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // mode, cross-HWND transparency is not supported and various types of 144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // widgets fallback to more simplified rendering behavior. 145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (CommandLine::ForCurrentProcess()->HasSwitch( 146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch switches::kDisableDwmComposition)) 147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return false; 148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (base::win::GetVersion() < base::win::VERSION_VISTA) 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If composition is not enabled, we behave like on XP. 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BOOL enabled = FALSE; 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace win 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace ui 158