OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <atlbase.h> |
| 6 #include <atlcom.h> |
| 7 #include <atlctl.h> |
| 8 #include <initguid.h> |
| 9 #include <shellapi.h> |
| 10 |
| 11 #include "base/at_exit.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/file_util.h" |
| 14 #include "base/string16.h" |
| 15 #include "base/string_number_conversions.h" |
| 16 #include "base/utf_string_conversions.h" |
| 17 #include "base/win/scoped_com_initializer.h" |
| 18 #include "base/win/scoped_handle.h" |
| 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "command_execute_impl.h" |
| 21 #include "win8/delegate_execute/delegate_execute_operation.h" |
| 22 #include "win8/delegate_execute/resource.h" |
| 23 |
| 24 using namespace ATL; |
| 25 |
| 26 class DelegateExecuteModule |
| 27 : public ATL::CAtlExeModuleT< DelegateExecuteModule > { |
| 28 public : |
| 29 typedef ATL::CAtlExeModuleT<DelegateExecuteModule> ParentClass; |
| 30 |
| 31 DECLARE_REGISTRY_APPID_RESOURCEID(IDR_DELEGATEEXECUTE, |
| 32 "{B1935DA1-112F-479A-975B-AB8588ABA636}") |
| 33 |
| 34 virtual HRESULT AddCommonRGSReplacements(IRegistrarBase* registrar) throw() { |
| 35 AtlTrace(L"In %hs\n", __FUNCTION__); |
| 36 HRESULT hr = ParentClass::AddCommonRGSReplacements(registrar); |
| 37 if (FAILED(hr)) |
| 38 return hr; |
| 39 |
| 40 wchar_t delegate_execute_clsid[MAX_PATH] = {0}; |
| 41 if (!StringFromGUID2(__uuidof(CommandExecuteImpl), delegate_execute_clsid, |
| 42 ARRAYSIZE(delegate_execute_clsid))) { |
| 43 ATLASSERT(false); |
| 44 return E_FAIL; |
| 45 } |
| 46 |
| 47 hr = registrar->AddReplacement(L"DELEGATE_EXECUTE_CLSID", |
| 48 delegate_execute_clsid); |
| 49 ATLASSERT(SUCCEEDED(hr)); |
| 50 return hr; |
| 51 } |
| 52 }; |
| 53 |
| 54 DelegateExecuteModule _AtlModule; |
| 55 |
| 56 // Relaunch metro Chrome by ShellExecute on |shortcut| with --force-immersive. |
| 57 // |handle_value| is an optional handle on which this function will wait before |
| 58 // performing the relaunch. |
| 59 int RelaunchChrome(const FilePath& shortcut, const string16& handle_value) { |
| 60 base::win::ScopedHandle handle; |
| 61 |
| 62 if (!handle_value.empty()) { |
| 63 uint32 the_handle = 0; |
| 64 if (!base::StringToUint(handle_value, &the_handle)) { |
| 65 // Failed to parse the handle value. Skip the wait but proceed with the |
| 66 // relaunch. |
| 67 AtlTrace(L"Failed to parse handle value %ls\n", handle_value.c_str()); |
| 68 } else { |
| 69 handle.Set(reinterpret_cast<HANDLE>(the_handle)); |
| 70 } |
| 71 } |
| 72 |
| 73 if (handle.IsValid()) { |
| 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 |
| 85 base::win::ScopedCOMInitializer com_initializer; |
| 86 |
| 87 AtlTrace(L"Launching Chrome via %ls.\n", shortcut.value().c_str()); |
| 88 int ser = reinterpret_cast<int>( |
| 89 ::ShellExecute(NULL, NULL, shortcut.value().c_str(), |
| 90 ASCIIToWide(switches::kForceImmersive).c_str(), NULL, |
| 91 SW_SHOWNORMAL)); |
| 92 AtlTrace(L"ShellExecute returned %d.\n", ser); |
| 93 return ser <= 32; |
| 94 } |
| 95 |
| 96 // |
| 97 extern "C" int WINAPI _tWinMain(HINSTANCE , HINSTANCE, |
| 98 LPTSTR, int nShowCmd) { |
| 99 using delegate_execute::DelegateExecuteOperation; |
| 100 base::AtExitManager exit_manager; |
| 101 |
| 102 CommandLine::Init(0, NULL); |
| 103 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 104 DelegateExecuteOperation operation; |
| 105 |
| 106 operation.Initialize(cmd_line); |
| 107 switch (operation.operation_type()) { |
| 108 case DelegateExecuteOperation::EXE_MODULE: |
| 109 return _AtlModule.WinMain(nShowCmd); |
| 110 |
| 111 case DelegateExecuteOperation::RELAUNCH_CHROME: |
| 112 return RelaunchChrome(operation.relaunch_shortcut(), |
| 113 cmd_line->GetSwitchValueNative(switches::kWaitForHandle)); |
| 114 |
| 115 default: |
| 116 NOTREACHED(); |
| 117 } |
| 118 |
| 119 return 1; |
| 120 } |
OLD | NEW |