| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <atlbase.h> | 5 #include <atlbase.h> |
| 6 #include <atlcom.h> | 6 #include <atlcom.h> |
| 7 #include <atlctl.h> | 7 #include <atlctl.h> |
| 8 #include <initguid.h> | 8 #include <initguid.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 | 10 |
| 11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/process_util.h" |
| 14 #include "base/string16.h" | 15 #include "base/string16.h" |
| 15 #include "base/string_number_conversions.h" | |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "base/win/scoped_com_initializer.h" | 17 #include "base/win/scoped_com_initializer.h" |
| 18 #include "base/win/scoped_handle.h" | 18 #include "base/win/scoped_handle.h" |
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "command_execute_impl.h" | 20 #include "command_execute_impl.h" |
| 21 #include "win8/delegate_execute/delegate_execute_operation.h" | 21 #include "win8/delegate_execute/delegate_execute_operation.h" |
| 22 #include "win8/delegate_execute/resource.h" | 22 #include "win8/delegate_execute/resource.h" |
| 23 | 23 |
| 24 using namespace ATL; | 24 using namespace ATL; |
| 25 | 25 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 46 | 46 |
| 47 hr = registrar->AddReplacement(L"DELEGATE_EXECUTE_CLSID", | 47 hr = registrar->AddReplacement(L"DELEGATE_EXECUTE_CLSID", |
| 48 delegate_execute_clsid); | 48 delegate_execute_clsid); |
| 49 ATLASSERT(SUCCEEDED(hr)); | 49 ATLASSERT(SUCCEEDED(hr)); |
| 50 return hr; | 50 return hr; |
| 51 } | 51 } |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 DelegateExecuteModule _AtlModule; | 54 DelegateExecuteModule _AtlModule; |
| 55 | 55 |
| 56 // Relaunch metro Chrome by ShellExecute on |shortcut| with --force-immersive. | 56 using delegate_execute::DelegateExecuteOperation; |
| 57 // |handle_value| is an optional handle on which this function will wait before | 57 using base::win::ScopedHandle; |
| 58 // performing the relaunch. | |
| 59 int RelaunchChrome(const FilePath& shortcut, const string16& handle_value) { | |
| 60 base::win::ScopedHandle handle; | |
| 61 | 58 |
| 62 if (!handle_value.empty()) { | 59 int RelaunchChrome(const DelegateExecuteOperation& operation) { |
| 63 uint32 the_handle = 0; | 60 AtlTrace("Relaunching [%ls] with flags [%s]\n", |
| 64 if (!base::StringToUint(handle_value, &the_handle)) { | 61 operation.mutex().c_str(), operation.relaunch_flags()); |
| 65 // Failed to parse the handle value. Skip the wait but proceed with the | 62 ScopedHandle mutex(OpenMutexW(SYNCHRONIZE, FALSE, operation.mutex().c_str())); |
| 66 // relaunch. | 63 if (mutex.IsValid()) { |
| 67 AtlTrace(L"Failed to parse handle value %ls\n", handle_value.c_str()); | 64 const int kWaitSeconds = 5; |
| 65 DWORD result = ::WaitForSingleObject(mutex, kWaitSeconds * 1000); |
| 66 if (result == WAIT_ABANDONED) { |
| 67 // This is the normal case. Chrome exits and windows marks the mutex as |
| 68 // abandoned. |
| 69 } else if (result == WAIT_OBJECT_0) { |
| 70 // This is unexpected. Check if somebody is not closing the mutex on |
| 71 // RelaunchChromehelper, the mutex should not be closed. |
| 72 AtlTrace("Unexpected release of the relaunch mutex!!\n"); |
| 73 } else if (result == WAIT_TIMEOUT) { |
| 74 // This could mean that Chrome is hung. Proceed to exterminate. |
| 75 DWORD pid = operation.GetParentPid(); |
| 76 AtlTrace("%ds timeout. Killing Chrome %d\n", kWaitSeconds, pid); |
| 77 base::KillProcessById(pid, 0, false); |
| 68 } else { | 78 } else { |
| 69 handle.Set(reinterpret_cast<HANDLE>(the_handle)); | 79 AtlTrace("Failed to wait for relaunch mutex, result is 0x%x\n", result); |
| 70 } | 80 } |
| 71 } | 81 } else { |
| 72 | 82 // It is possible that chrome exits so fast that the mutex is not there. |
| 73 if (handle.IsValid()) { | 83 AtlTrace("No relaunch mutex found\n"); |
| 74 AtlTrace(L"Waiting for chrome.exe to exit.\n"); | |
| 75 DWORD result = ::WaitForSingleObject(handle, 10 * 1000); | |
| 76 AtlTrace(L"And we're back.\n"); | |
| 77 if (result != WAIT_OBJECT_0) { | |
| 78 AtlTrace(L"Failed to wait for parent to exit; result=%u.\n", result); | |
| 79 // This could mean that Chrome has hung. Conservatively proceed with | |
| 80 // the relaunch anyway. | |
| 81 } | |
| 82 handle.Close(); | |
| 83 } | 84 } |
| 84 | 85 |
| 85 base::win::ScopedCOMInitializer com_initializer; | 86 base::win::ScopedCOMInitializer com_initializer; |
| 86 | 87 |
| 87 AtlTrace(L"Launching Chrome via %ls.\n", shortcut.value().c_str()); | 88 string16 flags(ASCIIToWide(operation.relaunch_flags())); |
| 88 int ser = reinterpret_cast<int>( | 89 SHELLEXECUTEINFO sei = { sizeof(sei) }; |
| 89 ::ShellExecute(NULL, NULL, shortcut.value().c_str(), | 90 sei.fMask = SEE_MASK_FLAG_LOG_USAGE; |
| 90 ASCIIToWide(switches::kForceImmersive).c_str(), NULL, | 91 sei.nShow = SW_SHOWNORMAL; |
| 91 SW_SHOWNORMAL)); | 92 sei.lpFile = operation.shortcut().value().c_str(); |
| 92 AtlTrace(L"ShellExecute returned %d.\n", ser); | 93 sei.lpParameters = flags.c_str(); |
| 93 return ser <= 32; | 94 |
| 95 AtlTrace(L"Relaunching Chrome via shortcut [%ls]\n", sei.lpFile); |
| 96 |
| 97 if (!::ShellExecuteExW(&sei)) { |
| 98 int error = HRESULT_FROM_WIN32(::GetLastError()); |
| 99 AtlTrace("ShellExecute returned 0x%08X\n", error); |
| 100 return error; |
| 101 } |
| 102 return S_OK; |
| 94 } | 103 } |
| 95 | 104 |
| 96 // | 105 extern "C" int WINAPI _tWinMain(HINSTANCE , HINSTANCE, LPTSTR, int nShowCmd) { |
| 97 extern "C" int WINAPI _tWinMain(HINSTANCE , HINSTANCE, | |
| 98 LPTSTR, int nShowCmd) { | |
| 99 using delegate_execute::DelegateExecuteOperation; | |
| 100 base::AtExitManager exit_manager; | 106 base::AtExitManager exit_manager; |
| 107 AtlTrace("delegate_execute enter\n"); |
| 101 | 108 |
| 102 CommandLine::Init(0, NULL); | 109 CommandLine::Init(0, NULL); |
| 103 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 110 HRESULT ret_code = E_UNEXPECTED; |
| 104 DelegateExecuteOperation operation; | 111 DelegateExecuteOperation operation; |
| 105 | 112 if (operation.Init(CommandLine::ForCurrentProcess())) { |
| 106 operation.Initialize(cmd_line); | 113 switch (operation.operation_type()) { |
| 107 switch (operation.operation_type()) { | 114 case DelegateExecuteOperation::DELEGATE_EXECUTE: |
| 108 case DelegateExecuteOperation::EXE_MODULE: | 115 ret_code = _AtlModule.WinMain(nShowCmd); |
| 109 return _AtlModule.WinMain(nShowCmd); | 116 break; |
| 110 | 117 case DelegateExecuteOperation::RELAUNCH_CHROME: |
| 111 case DelegateExecuteOperation::RELAUNCH_CHROME: | 118 ret_code = RelaunchChrome(operation); |
| 112 return RelaunchChrome(operation.relaunch_shortcut(), | 119 break; |
| 113 cmd_line->GetSwitchValueNative(switches::kWaitForHandle)); | 120 default: |
| 114 | 121 NOTREACHED(); |
| 115 default: | 122 } |
| 116 NOTREACHED(); | |
| 117 } | 123 } |
| 118 | 124 AtlTrace("delegate_execute exit, code = %d\n", ret_code); |
| 119 return 1; | 125 return ret_code; |
| 120 } | 126 } |
| OLD | NEW |