[email protected] | 466a922 | 2010-06-08 01:03:16 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // chrome_tab.cc : Implementation of DLL Exports. |
[email protected] | 3f55e87 | 2009-10-17 04:48:37 | [diff] [blame] | 6 | |
| 7 | // Include without path to make GYP build see it. |
| 8 | #include "chrome_tab.h" // NOLINT |
| 9 | |
| 10 | #include <atlsecurity.h> |
| 11 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 12 | #include "base/at_exit.h" |
| 13 | #include "base/command_line.h" |
| 14 | #include "base/file_util.h" |
| 15 | #include "base/file_version_info.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 16 | #include "base/logging.h" |
[email protected] | 5ee3413 | 2009-11-25 21:32:26 | [diff] [blame] | 17 | #include "base/logging_win.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 18 | #include "base/path_service.h" |
[email protected] | c9c07779 | 2010-08-03 14:39:42 | [diff] [blame] | 19 | #include "base/string_number_conversions.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 20 | #include "base/string_piece.h" |
| 21 | #include "base/string_util.h" |
| 22 | #include "base/sys_string_conversions.h" |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 23 | #include "base/win/registry.h" |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 24 | #include "base/win/windows_version.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 25 | #include "chrome/common/chrome_constants.h" |
[email protected] | 02ea78a | 2010-07-15 16:52:28 | [diff] [blame] | 26 | #include "chrome/common/chrome_switches.h" |
| 27 | #include "chrome/installer/util/google_update_settings.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 28 | #include "chrome_frame/bho.h" |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 29 | #include "chrome_frame/chrome_active_document.h" |
| 30 | #include "chrome_frame/chrome_frame_activex.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 31 | #include "chrome_frame/chrome_frame_automation.h" |
[email protected] | 53556e1 | 2009-10-15 21:49:22 | [diff] [blame] | 32 | #include "chrome_frame/chrome_frame_reporting.h" |
[email protected] | 79b663c | 2010-05-28 17:27:17 | [diff] [blame] | 33 | #include "chrome_frame/chrome_launcher_utils.h" |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 34 | #include "chrome_frame/chrome_protocol.h" |
[email protected] | c6352e9 | 2010-12-03 01:57:47 | [diff] [blame] | 35 | #include "chrome_frame/dll_redirector.h" |
| 36 | #include "chrome_frame/exception_barrier.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 37 | #include "chrome_frame/resource.h" |
| 38 | #include "chrome_frame/utils.h" |
[email protected] | cebd413 | 2010-03-25 15:34:22 | [diff] [blame] | 39 | #include "googleurl/src/url_util.h" |
[email protected] | c6352e9 | 2010-12-03 01:57:47 | [diff] [blame] | 40 | #include "grit/chrome_frame_resources.h" |
[email protected] | cebd413 | 2010-03-25 15:34:22 | [diff] [blame] | 41 | |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 42 | using base::win::RegKey; |
| 43 | |
[email protected] | cebd413 | 2010-03-25 15:34:22 | [diff] [blame] | 44 | namespace { |
| 45 | // This function has the side effect of initializing an unprotected |
| 46 | // vector pointer inside GoogleUrl. If this is called during DLL loading, |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 47 | // it has the effect of avoiding an initialization race on that pointer. |
[email protected] | cebd413 | 2010-03-25 15:34:22 | [diff] [blame] | 48 | // TODO(siggi): fix GoogleUrl. |
| 49 | void InitGoogleUrl() { |
| 50 | static const char kDummyUrl[] = "http://www.google.com"; |
| 51 | |
| 52 | url_util::IsStandard(kDummyUrl, |
| 53 | url_parse::MakeRange(0, arraysize(kDummyUrl))); |
| 54 | } |
[email protected] | cebd413 | 2010-03-25 15:34:22 | [diff] [blame] | 55 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 56 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 57 | static const wchar_t kBhoRegistryPath[] = |
| 58 | L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" |
| 59 | L"\\Browser Helper Objects"; |
| 60 | |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 61 | const wchar_t kInternetSettings[] = |
| 62 | L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"; |
| 63 | |
[email protected] | 552fb601 | 2010-02-03 17:24:29 | [diff] [blame] | 64 | const wchar_t kProtocolHandlers[] = |
| 65 | L"Software\\Classes\\Protocols\\Handler"; |
| 66 | |
[email protected] | 02ea78a | 2010-07-15 16:52:28 | [diff] [blame] | 67 | const wchar_t kRunOnce[] = |
| 68 | L"Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce"; |
| 69 | |
[email protected] | c5c6c8c | 2010-09-07 13:57:21 | [diff] [blame] | 70 | const wchar_t kRunKeyName[] = L"ChromeFrameHelper"; |
| 71 | |
| 72 | const wchar_t kChromeFrameHelperExe[] = L"chrome_frame_helper.exe"; |
| 73 | |
| 74 | // Window class and window names. |
| 75 | // TODO(robertshield): These and other constants need to be refactored into |
| 76 | // a common chrome_frame_constants.h|cc and built into a separate lib |
| 77 | // (either chrome_frame_utils or make another one). |
| 78 | const wchar_t kChromeFrameHelperWindowClassName[] = |
| 79 | L"ChromeFrameHelperWindowClass"; |
| 80 | const wchar_t kChromeFrameHelperWindowName[] = |
| 81 | L"ChromeFrameHelperWindowName"; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 82 | |
[email protected] | 5ee3413 | 2009-11-25 21:32:26 | [diff] [blame] | 83 | // {0562BFC3-2550-45b4-BD8E-A310583D3A6F} |
| 84 | static const GUID kChromeFrameProvider = |
| 85 | { 0x562bfc3, 0x2550, 0x45b4, |
| 86 | { 0xbd, 0x8e, 0xa3, 0x10, 0x58, 0x3d, 0x3a, 0x6f } }; |
| 87 | |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 88 | // Object entries go here instead of with each object, so that we can move |
| 89 | // the objects to a lib. Also reduces magic. |
| 90 | OBJECT_ENTRY_AUTO(CLSID_ChromeFrameBHO, Bho) |
| 91 | OBJECT_ENTRY_AUTO(__uuidof(ChromeActiveDocument), ChromeActiveDocument) |
| 92 | OBJECT_ENTRY_AUTO(__uuidof(ChromeFrame), ChromeFrameActivex) |
| 93 | OBJECT_ENTRY_AUTO(__uuidof(ChromeProtocol), ChromeProtocol) |
[email protected] | 5ee3413 | 2009-11-25 21:32:26 | [diff] [blame] | 94 | |
[email protected] | 8adbf7e5 | 2010-04-14 20:03:23 | [diff] [blame] | 95 | |
| 96 | // See comments in DllGetClassObject. |
[email protected] | 5274984 | 2010-05-07 21:34:41 | [diff] [blame] | 97 | LPFNGETCLASSOBJECT g_dll_get_class_object_redir_ptr = NULL; |
[email protected] | 8adbf7e5 | 2010-04-14 20:03:23 | [diff] [blame] | 98 | |
[email protected] | 6bdc0c0 | 2010-07-20 14:52:13 | [diff] [blame] | 99 | class ChromeTabModule : public CAtlDllModuleT<ChromeTabModule> { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 100 | public: |
[email protected] | 6bdc0c0 | 2010-07-20 14:52:13 | [diff] [blame] | 101 | typedef CAtlDllModuleT<ChromeTabModule> ParentClass; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 102 | |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 103 | ChromeTabModule() : do_system_registration_(true) {} |
| 104 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 105 | DECLARE_LIBID(LIBID_ChromeTabLib) |
| 106 | DECLARE_REGISTRY_APPID_RESOURCEID(IDR_CHROMETAB, |
| 107 | "{FD9B1B31-F4D8-436A-8F4F-D3C2E36733D3}") |
| 108 | |
| 109 | // Override to add our SYSTIME binary value to registry scripts. |
| 110 | // See chrome_frame_activex.rgs for usage. |
| 111 | virtual HRESULT AddCommonRGSReplacements(IRegistrarBase* registrar) throw() { |
| 112 | HRESULT hr = ParentClass::AddCommonRGSReplacements(registrar); |
| 113 | |
| 114 | if (SUCCEEDED(hr)) { |
| 115 | SYSTEMTIME local_time; |
| 116 | ::GetSystemTime(&local_time); |
[email protected] | c9c07779 | 2010-08-03 14:39:42 | [diff] [blame] | 117 | std::string hex(base::HexEncode(&local_time, sizeof(local_time))); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 118 | base::StringPiece sp_hex(hex); |
| 119 | hr = registrar->AddReplacement(L"SYSTIME", |
| 120 | base::SysNativeMBToWide(sp_hex).c_str()); |
| 121 | DCHECK(SUCCEEDED(hr)); |
| 122 | } |
| 123 | |
| 124 | if (SUCCEEDED(hr)) { |
[email protected] | 66ff735 | 2009-10-15 05:09:50 | [diff] [blame] | 125 | FilePath app_path = |
| 126 | chrome_launcher::GetChromeExecutablePath().DirName(); |
| 127 | hr = registrar->AddReplacement(L"CHROME_APPPATH", |
| 128 | app_path.value().c_str()); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 129 | DCHECK(SUCCEEDED(hr)); |
| 130 | } |
| 131 | |
| 132 | if (SUCCEEDED(hr)) { |
| 133 | hr = registrar->AddReplacement(L"CHROME_APPNAME", |
| 134 | chrome::kBrowserProcessExecutableName); |
| 135 | DCHECK(SUCCEEDED(hr)); |
| 136 | |
| 137 | // Fill in VERSION from the VERSIONINFO stored in the DLL's resources. |
| 138 | scoped_ptr<FileVersionInfo> module_version_info( |
| 139 | FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 140 | DCHECK(module_version_info != NULL); |
| 141 | std::wstring file_version(module_version_info->file_version()); |
| 142 | hr = registrar->AddReplacement(L"VERSION", file_version.c_str()); |
| 143 | DCHECK(SUCCEEDED(hr)); |
| 144 | } |
| 145 | |
| 146 | if (SUCCEEDED(hr)) { |
| 147 | // Add the directory of chrome_launcher.exe. This will be the same |
| 148 | // as the directory for the current DLL. |
| 149 | std::wstring module_dir; |
| 150 | FilePath module_path; |
| 151 | if (PathService::Get(base::FILE_MODULE, &module_path)) { |
[email protected] | 466a922 | 2010-06-08 01:03:16 | [diff] [blame] | 152 | module_dir = module_path.DirName().value(); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 153 | } else { |
| 154 | NOTREACHED(); |
| 155 | } |
| 156 | hr = registrar->AddReplacement(L"CHROME_LAUNCHER_APPPATH", |
| 157 | module_dir.c_str()); |
| 158 | DCHECK(SUCCEEDED(hr)); |
| 159 | } |
| 160 | |
| 161 | if (SUCCEEDED(hr)) { |
| 162 | // Add the filename of chrome_launcher.exe |
| 163 | hr = registrar->AddReplacement(L"CHROME_LAUNCHER_APPNAME", |
| 164 | chrome_launcher::kLauncherExeBaseName); |
| 165 | DCHECK(SUCCEEDED(hr)); |
| 166 | } |
| 167 | |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 168 | if (SUCCEEDED(hr)) { |
| 169 | // Add the registry hive to use. |
| 170 | // Note: This is ugly as hell. I'd rather use the pMapEntries parameter |
| 171 | // to CAtlModule::UpdateRegistryFromResource, unfortunately we have a |
| 172 | // few components that are registered by calling their |
| 173 | // static T::UpdateRegistry() methods directly, which doesn't allow |
| 174 | // pMapEntries to be passed through :-( |
| 175 | if (do_system_registration_) { |
| 176 | hr = registrar->AddReplacement(L"HIVE", L"HKLM"); |
| 177 | } else { |
| 178 | hr = registrar->AddReplacement(L"HIVE", L"HKCU"); |
| 179 | } |
| 180 | DCHECK(SUCCEEDED(hr)); |
| 181 | } |
| 182 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 183 | return hr; |
| 184 | } |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 185 | |
| 186 | // See comments in AddCommonRGSReplacements |
| 187 | bool do_system_registration_; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | ChromeTabModule _AtlModule; |
| 191 | |
| 192 | base::AtExitManager* g_exit_manager = NULL; |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 193 | bool RegisterSecuredMimeHandler(bool enable, bool is_system); // forward |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 194 | |
| 195 | // DLL Entry Point |
| 196 | extern "C" BOOL WINAPI DllMain(HINSTANCE instance, |
| 197 | DWORD reason, |
| 198 | LPVOID reserved) { |
| 199 | UNREFERENCED_PARAMETER(instance); |
| 200 | if (reason == DLL_PROCESS_ATTACH) { |
| 201 | #ifndef NDEBUG |
| 202 | // Silence traces from the ATL registrar to reduce the log noise. |
| 203 | ATL::CTrace::s_trace.ChangeCategory(atlTraceRegistrar, 0, |
| 204 | ATLTRACESTATUS_DISABLED); |
| 205 | #endif |
[email protected] | cebd413 | 2010-03-25 15:34:22 | [diff] [blame] | 206 | InitGoogleUrl(); |
| 207 | |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 208 | g_exit_manager = new base::AtExitManager(); |
[email protected] | 74d1eec | 2009-11-04 22:18:57 | [diff] [blame] | 209 | CommandLine::Init(0, NULL); |
[email protected] | 53556e1 | 2009-10-15 21:49:22 | [diff] [blame] | 210 | InitializeCrashReporting(); |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 211 | logging::InitLogging( |
| 212 | NULL, |
| 213 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 214 | logging::LOCK_LOG_FILE, |
| 215 | logging::DELETE_OLD_LOG_FILE, |
| 216 | logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
[email protected] | 5274984 | 2010-05-07 21:34:41 | [diff] [blame] | 217 | |
[email protected] | 687b960 | 2010-12-08 10:43:08 | [diff] [blame] | 218 | DllRedirector* dll_redirector = DllRedirector::GetInstance(); |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 219 | DCHECK(dll_redirector); |
| 220 | |
| 221 | if (!dll_redirector->RegisterAsFirstCFModule()) { |
| 222 | // Someone else was here first, try and get a pointer to their |
| 223 | // DllGetClassObject export: |
| 224 | g_dll_get_class_object_redir_ptr = |
| 225 | dll_redirector->GetDllGetClassObjectPtr(); |
| 226 | DCHECK(g_dll_get_class_object_redir_ptr != NULL) |
| 227 | << "Found CF module with no DllGetClassObject export."; |
[email protected] | 5274984 | 2010-05-07 21:34:41 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 5ee3413 | 2009-11-25 21:32:26 | [diff] [blame] | 230 | // Enable ETW logging. |
| 231 | logging::LogEventProvider::Initialize(kChromeFrameProvider); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 232 | } else if (reason == DLL_PROCESS_DETACH) { |
[email protected] | 687b960 | 2010-12-08 10:43:08 | [diff] [blame] | 233 | DllRedirector* dll_redirector = DllRedirector::GetInstance(); |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 234 | DCHECK(dll_redirector); |
| 235 | |
| 236 | dll_redirector->UnregisterAsFirstCFModule(); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 237 | g_patch_helper.UnpatchIfNeeded(); |
| 238 | delete g_exit_manager; |
| 239 | g_exit_manager = NULL; |
| 240 | ShutdownCrashReporting(); |
| 241 | } |
| 242 | return _AtlModule.DllMain(reason, reserved); |
| 243 | } |
| 244 | |
| 245 | #ifdef _MANAGED |
| 246 | #pragma managed(pop) |
| 247 | #endif |
| 248 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 249 | HRESULT RefreshElevationPolicy() { |
| 250 | const wchar_t kIEFrameDll[] = L"ieframe.dll"; |
| 251 | const char kIERefreshPolicy[] = "IERefreshElevationPolicy"; |
| 252 | HRESULT hr = E_NOTIMPL; |
[email protected] | 1f08d68 | 2010-05-19 18:48:31 | [diff] [blame] | 253 | |
| 254 | // Stick an SEH in the chain to prevent the VEH from picking up on first |
| 255 | // chance exceptions caused by loading ieframe.dll. Use the vanilla |
| 256 | // ExceptionBarrier to report any exceptions that do make their way to us |
| 257 | // though. |
| 258 | ExceptionBarrier barrier; |
| 259 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 260 | HMODULE ieframe_module = LoadLibrary(kIEFrameDll); |
| 261 | if (ieframe_module) { |
| 262 | typedef HRESULT (__stdcall *IERefreshPolicy)(); |
| 263 | IERefreshPolicy ie_refresh_policy = reinterpret_cast<IERefreshPolicy>( |
| 264 | GetProcAddress(ieframe_module, kIERefreshPolicy)); |
| 265 | |
| 266 | if (ie_refresh_policy) { |
| 267 | hr = ie_refresh_policy(); |
| 268 | } else { |
| 269 | hr = HRESULT_FROM_WIN32(GetLastError()); |
| 270 | } |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 271 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 272 | FreeLibrary(ieframe_module); |
| 273 | } else { |
| 274 | hr = HRESULT_FROM_WIN32(GetLastError()); |
| 275 | } |
| 276 | |
| 277 | return hr; |
| 278 | } |
| 279 | |
[email protected] | 02ea78a | 2010-07-15 16:52:28 | [diff] [blame] | 280 | // Experimental boot prefetch optimization for Chrome Frame |
| 281 | // |
| 282 | // If chrome is warmed up during a single reboot, it gets paged |
| 283 | // in for subsequent reboots and the cold startup times essentially |
| 284 | // look like warm times thereafter! The 'warm up' is done by |
| 285 | // setting up a 'RunOnce' key during DLLRegisterServer of |
| 286 | // npchrome_frame.dll. |
| 287 | // |
| 288 | // This works because chrome prefetch becomes part of boot |
| 289 | // prefetch file ntosboot-b00dfaad.pf and paged in on subsequent |
| 290 | // reboots. As long as the sytem does not undergo significant |
| 291 | // memory pressure those pages remain in memory and we get pretty |
| 292 | // amazing startup times, down to about 300 ms from 1200 ms |
| 293 | // |
| 294 | // The downside is: |
| 295 | // - Whether chrome frame is used or not, there's a read penalty |
| 296 | // (1200-300 =) 900 ms for every boot. |
| 297 | // - Heavy system memory usage after reboot will nullify the benefits |
| 298 | // but the user will still pay the cost. |
| 299 | // - Overall the time saved will always be less than total time spent |
| 300 | // paging in chrome |
| 301 | // - We are not sure when the chrome 'warm up' will age out from the |
| 302 | // boot prefetch file. |
| 303 | // |
| 304 | // The idea here is to try this out on chrome frame dev channel |
| 305 | // and see if it produces a significant drift in startup numbers. |
| 306 | HRESULT SetupRunOnce() { |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 307 | HRESULT result = E_FAIL; |
[email protected] | 02ea78a | 2010-07-15 16:52:28 | [diff] [blame] | 308 | |
| 309 | std::wstring channel_name; |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 310 | if (base::win::GetVersion() < base::win::VERSION_VISTA && |
| 311 | GoogleUpdateSettings::GetChromeChannel(true, &channel_name)) { |
| 312 | std::transform(channel_name.begin(), channel_name.end(), |
| 313 | channel_name.begin(), tolower); |
| 314 | // Use this only for the dev channel and CEEE channels. |
| 315 | if (channel_name.find(L"dev") != std::wstring::npos || |
| 316 | channel_name.find(L"ceee") != std::wstring::npos) { |
[email protected] | 542bdfe | 2010-11-30 03:55:47 | [diff] [blame] | 317 | |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 318 | HKEY hive = HKEY_CURRENT_USER; |
| 319 | if (IsSystemProcess()) { |
| 320 | // For system installs, our updates will be running as SYSTEM which |
| 321 | // makes writing to a RunOnce key under HKCU not so terribly useful. |
| 322 | hive = HKEY_LOCAL_MACHINE; |
| 323 | } |
[email protected] | 74ecb5a4 | 2010-09-03 00:44:35 | [diff] [blame] | 324 | |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 325 | RegKey run_once; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 326 | LONG ret = run_once.Create(hive, kRunOnce, KEY_READ | KEY_WRITE); |
| 327 | if (ret == ERROR_SUCCESS) { |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 328 | CommandLine run_once_cmd(chrome_launcher::GetChromeExecutablePath()); |
| 329 | run_once_cmd.AppendSwitchASCII(switches::kAutomationClientChannelID, |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 330 | "0"); |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 331 | run_once_cmd.AppendSwitch(switches::kChromeFrame); |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 332 | ret = run_once.WriteValue(L"A", |
| 333 | run_once_cmd.command_line_string().c_str()); |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 334 | } |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 335 | result = HRESULT_FROM_WIN32(ret); |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 336 | } else { |
| 337 | result = S_FALSE; |
| 338 | } |
| 339 | } else { |
| 340 | // We're on a non-XP version of Windows or on a stable channel. Nothing |
| 341 | // needs doing. |
| 342 | result = S_FALSE; |
[email protected] | 552fb601 | 2010-02-03 17:24:29 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | 7fc3e692 | 2010-11-18 17:56:54 | [diff] [blame] | 345 | return result; |
[email protected] | 552fb601 | 2010-02-03 17:24:29 | [diff] [blame] | 346 | } |
| 347 | |
[email protected] | c5c6c8c | 2010-09-07 13:57:21 | [diff] [blame] | 348 | // Helper method called for user-level installs where we don't have admin |
| 349 | // permissions. Starts up the long running process and registers it to get it |
| 350 | // started at next boot. |
| 351 | void SetupUserLevelHelper() { |
| 352 | // Remove existing run-at-startup entry. |
[email protected] | a8e2058 | 2010-12-31 17:18:50 | [diff] [blame] | 353 | base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, kRunKeyName); |
[email protected] | c5c6c8c | 2010-09-07 13:57:21 | [diff] [blame] | 354 | |
| 355 | // Build the chrome_frame_helper command line. |
| 356 | FilePath module_path; |
| 357 | FilePath helper_path; |
| 358 | if (PathService::Get(base::FILE_MODULE, &module_path)) { |
| 359 | module_path = module_path.DirName(); |
| 360 | helper_path = module_path.Append(kChromeFrameHelperExe); |
| 361 | if (!file_util::PathExists(helper_path)) { |
| 362 | // If we can't find the helper in the current directory, try looking |
| 363 | // one up (this is the layout in the build output folder). |
| 364 | module_path = module_path.DirName(); |
| 365 | helper_path = module_path.Append(kChromeFrameHelperExe); |
| 366 | DCHECK(file_util::PathExists(helper_path)) << |
| 367 | "Could not find chrome_frame_helper.exe."; |
| 368 | } |
| 369 | } else { |
| 370 | NOTREACHED(); |
| 371 | } |
| 372 | |
| 373 | // Find window handle of existing instance. |
| 374 | HWND old_window = FindWindow(kChromeFrameHelperWindowClassName, |
| 375 | kChromeFrameHelperWindowName); |
| 376 | |
| 377 | if (file_util::PathExists(helper_path)) { |
| 378 | // Add new run-at-startup entry. |
[email protected] | a8e2058 | 2010-12-31 17:18:50 | [diff] [blame] | 379 | base::win::AddCommandToAutoRun(HKEY_CURRENT_USER, kRunKeyName, |
[email protected] | c5c6c8c | 2010-09-07 13:57:21 | [diff] [blame] | 380 | helper_path.value()); |
| 381 | |
| 382 | // Start new instance. |
| 383 | bool launched = base::LaunchApp(helper_path.value(), false, true, NULL); |
| 384 | if (!launched) { |
| 385 | NOTREACHED(); |
| 386 | LOG(ERROR) << "Could not launch helper process."; |
| 387 | } |
| 388 | |
| 389 | // Kill old instance using window handle. |
| 390 | if (IsWindow(old_window)) { |
| 391 | BOOL result = PostMessage(old_window, WM_CLOSE, 0, 0); |
| 392 | if (!result) { |
| 393 | LOG(ERROR) << "Failed to post close message to old helper process: " |
| 394 | << GetLastError(); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 401 | // Used to determine whether the DLL can be unloaded by OLE |
| 402 | STDAPI DllCanUnloadNow() { |
| 403 | return _AtlModule.DllCanUnloadNow(); |
| 404 | } |
| 405 | |
| 406 | // Returns a class factory to create an object of the requested type |
| 407 | STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv) { |
[email protected] | 5274984 | 2010-05-07 21:34:41 | [diff] [blame] | 408 | // If we found another module present when we were loaded, then delegate to |
| 409 | // that: |
| 410 | if (g_dll_get_class_object_redir_ptr) { |
| 411 | return g_dll_get_class_object_redir_ptr(rclsid, riid, ppv); |
[email protected] | 8adbf7e5 | 2010-04-14 20:03:23 | [diff] [blame] | 412 | } |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 413 | |
| 414 | // Enable sniffing and switching only if asked for BHO |
| 415 | // (we use BHO to get loaded in IE). |
| 416 | if (rclsid == CLSID_ChromeFrameBHO) { |
| 417 | g_patch_helper.InitializeAndPatchProtocolsIfNeeded(); |
| 418 | } |
| 419 | |
| 420 | return _AtlModule.DllGetClassObject(rclsid, riid, ppv); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 6a8515e4 | 2010-12-16 19:38:52 | [diff] [blame] | 423 | const wchar_t kPostPlatformUAKey[] = |
| 424 | L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\" |
| 425 | L"User Agent\\Post Platform"; |
| 426 | const wchar_t kChromeFramePrefix[] = L"chromeframe/"; |
| 427 | |
| 428 | // To delete the user agent, set value to NULL. |
| 429 | // The is_system parameter indicates whether this is a per machine or a per |
| 430 | // user installation. |
| 431 | HRESULT SetChromeFrameUA(bool is_system, const wchar_t* value) { |
| 432 | HRESULT hr = E_FAIL; |
| 433 | HKEY parent_hive = is_system ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 434 | |
| 435 | RegKey ua_key; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 436 | if (ua_key.Create(parent_hive, kPostPlatformUAKey, |
| 437 | KEY_READ | KEY_WRITE) == ERROR_SUCCESS) { |
[email protected] | 45b716b | 2011-01-14 18:43:53 | [diff] [blame] | 438 | // Make sure that we unregister ChromeFrame UA strings registered previously |
[email protected] | 9b7baa96 | 2011-01-14 20:07:07 | [diff] [blame] | 439 | wchar_t value_name[MAX_PATH + 1] = {}; |
| 440 | wchar_t value_data[MAX_PATH + 1] = {}; |
[email protected] | 45b716b | 2011-01-14 18:43:53 | [diff] [blame] | 441 | |
| 442 | DWORD value_index = 0; |
| 443 | while (value_index < ua_key.ValueCount()) { |
[email protected] | 9b7baa96 | 2011-01-14 20:07:07 | [diff] [blame] | 444 | DWORD name_size = arraysize(value_name); |
| 445 | DWORD value_size = arraysize(value_data); |
[email protected] | 45b716b | 2011-01-14 18:43:53 | [diff] [blame] | 446 | DWORD type = 0; |
[email protected] | 9b7baa96 | 2011-01-14 20:07:07 | [diff] [blame] | 447 | LRESULT ret = ::RegEnumValue(ua_key.Handle(), value_index, value_name, |
[email protected] | 45b716b | 2011-01-14 18:43:53 | [diff] [blame] | 448 | &name_size, NULL, &type, |
[email protected] | 9b7baa96 | 2011-01-14 20:07:07 | [diff] [blame] | 449 | reinterpret_cast<BYTE*>(value_data), |
[email protected] | 45b716b | 2011-01-14 18:43:53 | [diff] [blame] | 450 | &value_size); |
| 451 | if (ret == ERROR_SUCCESS) { |
[email protected] | 9b7baa96 | 2011-01-14 20:07:07 | [diff] [blame] | 452 | if (StartsWith(value_name, kChromeFramePrefix, false)) { |
| 453 | ua_key.DeleteValue(value_name); |
[email protected] | 45b716b | 2011-01-14 18:43:53 | [diff] [blame] | 454 | } else { |
| 455 | ++value_index; |
| 456 | } |
| 457 | } else { |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | |
[email protected] | 6a8515e4 | 2010-12-16 19:38:52 | [diff] [blame] | 462 | std::wstring chrome_frame_ua_value_name = kChromeFramePrefix; |
| 463 | chrome_frame_ua_value_name += GetCurrentModuleVersion(); |
| 464 | if (value) { |
| 465 | ua_key.WriteValue(chrome_frame_ua_value_name.c_str(), value); |
[email protected] | 6a8515e4 | 2010-12-16 19:38:52 | [diff] [blame] | 466 | } |
| 467 | hr = S_OK; |
| 468 | } else { |
| 469 | DLOG(ERROR) << __FUNCTION__ << ": " << kPostPlatformUAKey; |
| 470 | hr = E_UNEXPECTED; |
| 471 | } |
| 472 | return hr; |
| 473 | } |
| 474 | |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 475 | enum RegistrationFlags { |
| 476 | ACTIVEX = 0x0001, |
| 477 | ACTIVEDOC = 0x0002, |
| 478 | GCF_PROTOCOL = 0x0004, |
| 479 | BHO_CLSID = 0x0008, |
| 480 | BHO_REGISTRATION = 0x0010, |
| 481 | TYPELIB = 0x0020, |
| 482 | |
| 483 | NPAPI_PLUGIN = 0x1000, |
| 484 | |
| 485 | ALL = 0xFFFF |
| 486 | }; |
| 487 | |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 488 | STDAPI CustomRegistration(UINT reg_flags, BOOL reg, bool is_system) { |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 489 | UINT flags = reg_flags; |
| 490 | |
| 491 | if (reg && (flags & (ACTIVEDOC | ACTIVEX))) |
[email protected] | 0fcfffb | 2011-02-03 19:35:22 | [diff] [blame] | 492 | flags |= (TYPELIB | GCF_PROTOCOL); |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 493 | |
| 494 | HRESULT hr = S_OK; |
| 495 | |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 496 | // Set the flag that gets checked in AddCommonRGSReplacements before doing |
| 497 | // registration work. |
| 498 | _AtlModule.do_system_registration_ = is_system; |
| 499 | |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 500 | if ((hr == S_OK) && (flags & ACTIVEDOC)) { |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 501 | // Don't fail to unregister if we can't undo the secure mime |
| 502 | // handler registration. This was observed getting hit during |
| 503 | // uninstallation. |
| 504 | if (!RegisterSecuredMimeHandler(reg ? true : false, is_system) && reg) |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 505 | return E_FAIL; |
| 506 | hr = ChromeActiveDocument::UpdateRegistry(reg); |
| 507 | } |
| 508 | |
| 509 | if ((hr == S_OK) && (flags & ACTIVEX)) { |
| 510 | // We have to call the static T::UpdateRegistry function instead of |
| 511 | // _AtlModule.UpdateRegistryFromResourceS(IDR_CHROMEFRAME_ACTIVEX, reg) |
| 512 | // because there is specific OLEMISC replacement. |
| 513 | hr = ChromeFrameActivex::UpdateRegistry(reg); |
[email protected] | 0fcfffb | 2011-02-03 19:35:22 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Register the elevation policy. We do this only for developer convenience |
| 517 | // as the installer is really responsible for doing this. |
| 518 | // Because of that, we do not unregister this policy and just leave that up |
| 519 | // to the installer. |
| 520 | if (hr == S_OK && (flags & (ACTIVEDOC | ACTIVEX)) && reg) { |
| 521 | _AtlModule.UpdateRegistryFromResourceS(IDR_CHROMEFRAME_ELEVATION, reg); |
[email protected] | 0f5945b1 | 2010-08-11 21:03:30 | [diff] [blame] | 522 | RefreshElevationPolicy(); |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if ((hr == S_OK) && (flags & GCF_PROTOCOL)) { |
| 526 | hr = _AtlModule.UpdateRegistryFromResourceS(IDR_CHROMEPROTOCOL, reg); |
| 527 | } |
| 528 | |
| 529 | if ((hr == S_OK) && (flags & BHO_CLSID)) { |
| 530 | hr = Bho::UpdateRegistry(reg); |
| 531 | } |
| 532 | |
[email protected] | c5c6c8c | 2010-09-07 13:57:21 | [diff] [blame] | 533 | if ((hr == S_OK) && (flags & BHO_REGISTRATION)) { |
| 534 | if (is_system) { |
| 535 | _AtlModule.UpdateRegistryFromResourceS(IDR_REGISTER_BHO, reg); |
| 536 | } else { |
| 537 | if (reg) { |
| 538 | // Setup the long running process: |
| 539 | SetupUserLevelHelper(); |
| 540 | } else { |
| 541 | // Unschedule the user-level helper. Note that we don't kill it here so |
| 542 | // that during updates we don't have a time window with no running |
| 543 | // helper. Uninstalls and updates will explicitly kill the helper from |
| 544 | // within the installer. Unregister existing run-at-startup entry. |
[email protected] | a8e2058 | 2010-12-31 17:18:50 | [diff] [blame] | 545 | base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, kRunKeyName); |
[email protected] | c5c6c8c | 2010-09-07 13:57:21 | [diff] [blame] | 546 | } |
| 547 | } |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if ((hr == S_OK) && (flags & TYPELIB)) { |
[email protected] | e6190aa | 2011-02-04 19:36:14 | [diff] [blame^] | 551 | if (reg && !is_system) { |
| 552 | // Enables the RegisterTypeLib Function function to override default |
| 553 | // registry mappings under Windows Vista Service Pack 1 (SP1), |
| 554 | // Windows Server 2008, and later operating system versions |
| 555 | typedef void (WINAPI* OaEnablePerUserTypeLibReg)(void); |
| 556 | OaEnablePerUserTypeLibReg per_user_typelib_func = |
| 557 | reinterpret_cast<OaEnablePerUserTypeLibReg>( |
| 558 | GetProcAddress(GetModuleHandle(L"oleaut32.dll"), |
| 559 | "OaEnablePerUserTLibRegistration")); |
| 560 | if (per_user_typelib_func) { |
| 561 | (*per_user_typelib_func)(); |
| 562 | } |
| 563 | } |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 564 | hr = (reg)? |
| 565 | UtilRegisterTypeLib(_AtlComModule.m_hInstTypeLib, NULL, !is_system) : |
| 566 | UtilUnRegisterTypeLib(_AtlComModule.m_hInstTypeLib, NULL, !is_system); |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | if ((hr == S_OK) && (flags & NPAPI_PLUGIN)) { |
| 570 | hr = _AtlModule.UpdateRegistryFromResourceS(IDR_CHROMEFRAME_NPAPI, reg); |
| 571 | } |
| 572 | |
| 573 | if (hr == S_OK) { |
| 574 | hr = _AtlModule.UpdateRegistryAppId(reg); |
| 575 | } |
| 576 | |
[email protected] | 6a8515e4 | 2010-12-16 19:38:52 | [diff] [blame] | 577 | if (hr == S_OK) { |
| 578 | if (reg) { |
| 579 | hr = SetChromeFrameUA(is_system, L"1"); |
| 580 | } else { |
| 581 | hr = SetChromeFrameUA(is_system, NULL); |
| 582 | } |
| 583 | } |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 584 | return hr; |
| 585 | } |
| 586 | |
| 587 | |
| 588 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 589 | // DllRegisterServer - Adds entries to the system registry |
| 590 | STDAPI DllRegisterServer() { |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 591 | UINT flags = ACTIVEX | ACTIVEDOC | TYPELIB | GCF_PROTOCOL | |
| 592 | BHO_CLSID | BHO_REGISTRATION; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 593 | |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 594 | if (UtilIsPersistentNPAPIMarkerSet()) { |
[email protected] | 6cc98d3 | 2010-11-27 01:54:13 | [diff] [blame] | 595 | flags |= NPAPI_PLUGIN; |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 596 | } |
| 597 | |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 598 | HRESULT hr = CustomRegistration(flags, TRUE, true); |
[email protected] | ee54aec9 | 2010-07-21 15:52:24 | [diff] [blame] | 599 | if (SUCCEEDED(hr)) { |
| 600 | SetupRunOnce(); |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 601 | } |
| 602 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 603 | return hr; |
| 604 | } |
| 605 | |
| 606 | // DllUnregisterServer - Removes entries from the system registry |
| 607 | STDAPI DllUnregisterServer() { |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 608 | HRESULT hr = CustomRegistration(ALL, FALSE, true); |
| 609 | return hr; |
| 610 | } |
| 611 | |
| 612 | // DllRegisterServer - Adds entries to the HKCU hive in the registry |
| 613 | STDAPI DllRegisterUserServer() { |
[email protected] | 1a5790a4 | 2010-09-10 13:46:26 | [diff] [blame] | 614 | UINT flags = ACTIVEX | ACTIVEDOC | TYPELIB | GCF_PROTOCOL | |
| 615 | BHO_CLSID | BHO_REGISTRATION; |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 616 | |
| 617 | if (UtilIsPersistentNPAPIMarkerSet()) { |
[email protected] | 6cc98d3 | 2010-11-27 01:54:13 | [diff] [blame] | 618 | flags |= NPAPI_PLUGIN; |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | HRESULT hr = CustomRegistration(flags, TRUE, false); |
| 622 | if (SUCCEEDED(hr)) { |
| 623 | SetupRunOnce(); |
| 624 | } |
| 625 | |
| 626 | return hr; |
| 627 | } |
| 628 | |
| 629 | // DllRegisterServer - Removes entries from the HKCU hive in the registry. |
| 630 | STDAPI DllUnregisterUserServer() { |
| 631 | HRESULT hr = CustomRegistration(ALL, FALSE, false); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 632 | return hr; |
| 633 | } |
| 634 | |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 635 | // Registers the NPAPI plugin and sets the persistent marker that tells us |
| 636 | // to re-register it through updates. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 637 | STDAPI RegisterNPAPIPlugin() { |
| 638 | HRESULT hr = _AtlModule.UpdateRegistryFromResourceS(IDR_CHROMEFRAME_NPAPI, |
| 639 | TRUE); |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 640 | if (SUCCEEDED(hr)) { |
| 641 | if (!UtilChangePersistentNPAPIMarker(true)) { |
| 642 | hr = E_FAIL; |
| 643 | } |
| 644 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 645 | return hr; |
| 646 | } |
| 647 | |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 648 | // Unregisters the NPAPI plugin and clears the persistent marker that tells us |
| 649 | // to re-register it through updates. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 650 | STDAPI UnregisterNPAPIPlugin() { |
| 651 | HRESULT hr = _AtlModule.UpdateRegistryFromResourceS(IDR_CHROMEFRAME_NPAPI, |
| 652 | FALSE); |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 653 | if (SUCCEEDED(hr)) { |
| 654 | if (!UtilChangePersistentNPAPIMarker(false)) { |
| 655 | hr = E_FAIL; |
| 656 | } |
| 657 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 658 | return hr; |
| 659 | } |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 660 | |
| 661 | class SecurityDescBackup { |
| 662 | public: |
| 663 | explicit SecurityDescBackup(const std::wstring& backup_key) |
| 664 | : backup_key_name_(backup_key) {} |
| 665 | ~SecurityDescBackup() {} |
| 666 | |
| 667 | // Save given security descriptor to the backup key. |
| 668 | bool SaveSecurity(const CSecurityDesc& sd) { |
| 669 | CString str; |
| 670 | if (!sd.ToString(&str)) |
| 671 | return false; |
| 672 | |
| 673 | RegKey backup_key(HKEY_LOCAL_MACHINE, backup_key_name_.c_str(), |
| 674 | KEY_READ | KEY_WRITE); |
| 675 | if (backup_key.Valid()) { |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 676 | return backup_key.WriteValue(NULL, str.GetString()) == ERROR_SUCCESS; |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | // Restore security descriptor from backup key to given key name. |
| 683 | bool RestoreSecurity(const wchar_t* key_name) { |
| 684 | std::wstring sddl; |
| 685 | if (!ReadBackupKey(&sddl)) |
| 686 | return false; |
| 687 | |
| 688 | // Create security descriptor from string. |
| 689 | CSecurityDesc sd; |
| 690 | if (!sd.FromString(sddl.c_str())) |
| 691 | return false; |
| 692 | |
| 693 | bool result = true; |
| 694 | // Restore DACL and Owner of the key from saved security descriptor. |
| 695 | CDacl dacl; |
| 696 | CSid owner; |
| 697 | sd.GetDacl(&dacl); |
| 698 | sd.GetOwner(&owner); |
| 699 | |
| 700 | DWORD error = ::SetNamedSecurityInfo(const_cast<wchar_t*>(key_name), |
| 701 | SE_REGISTRY_KEY, OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, |
| 702 | const_cast<SID*>(owner.GetPSID()), NULL, |
| 703 | const_cast<ACL*>(dacl.GetPACL()), NULL); |
| 704 | |
| 705 | DeleteBackupKey(); |
| 706 | return (error == ERROR_SUCCESS); |
| 707 | } |
| 708 | |
| 709 | private: |
| 710 | // Read SDDL string from backup key |
| 711 | bool ReadBackupKey(std::wstring* sddl) { |
| 712 | RegKey backup_key(HKEY_LOCAL_MACHINE, backup_key_name_.c_str(), KEY_READ); |
| 713 | if (!backup_key.Valid()) |
| 714 | return false; |
| 715 | |
| 716 | DWORD len = 0; |
| 717 | DWORD reg_type = REG_NONE; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 718 | if (backup_key.ReadValue(NULL, NULL, &len, ®_type) != ERROR_SUCCESS) |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 719 | return false; |
| 720 | |
| 721 | if (reg_type != REG_SZ) |
| 722 | return false; |
| 723 | |
| 724 | size_t wchar_count = 1 + len / sizeof(wchar_t); |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 725 | if (backup_key.ReadValue(NULL, WriteInto(sddl, wchar_count), &len, |
| 726 | ®_type) != ERROR_SUCCESS) { |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 727 | return false; |
| 728 | } |
| 729 | |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | void DeleteBackupKey() { |
| 734 | ::RegDeleteKey(HKEY_LOCAL_MACHINE, backup_key_name_.c_str()); |
| 735 | } |
| 736 | |
| 737 | std::wstring backup_key_name_; |
| 738 | }; |
| 739 | |
| 740 | struct TokenWithPrivileges { |
| 741 | TokenWithPrivileges() { |
| 742 | token_.GetEffectiveToken(TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY); |
| 743 | token_.GetUser(&user_); |
| 744 | } |
| 745 | |
| 746 | ~TokenWithPrivileges() { |
| 747 | token_.EnableDisablePrivileges(take_ownership_); |
| 748 | token_.EnableDisablePrivileges(restore_); |
| 749 | } |
| 750 | |
| 751 | bool EnablePrivileges() { |
| 752 | if (take_ownership_.GetCount() == 0) |
| 753 | if (!token_.EnablePrivilege(L"SeTakeOwnershipPrivilege", |
| 754 | &take_ownership_)) |
| 755 | return false; |
| 756 | |
| 757 | if (restore_.GetCount() == 0) |
| 758 | if (!token_.EnablePrivilege(L"SeRestorePrivilege", &restore_)) |
| 759 | return false; |
| 760 | |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | const CSid& GetUser() const { |
| 765 | return user_; |
| 766 | } |
| 767 | |
| 768 | private: |
| 769 | CAccessToken token_; |
| 770 | CTokenPrivileges take_ownership_; |
| 771 | CTokenPrivileges restore_; |
| 772 | CSid user_; |
| 773 | }; |
| 774 | |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 775 | static bool SetOrDeleteMimeHandlerKey(bool set, HKEY root_key) { |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 776 | std::wstring key_name = kInternetSettings; |
| 777 | key_name.append(L"\\Secure Mime Handlers"); |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 778 | RegKey key(root_key, key_name.c_str(), KEY_READ | KEY_WRITE); |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 779 | if (!key.Valid()) |
| 780 | return false; |
| 781 | |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 782 | LONG result1 = ERROR_SUCCESS; |
| 783 | LONG result2 = ERROR_SUCCESS; |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 784 | if (set) { |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 785 | result1 = key.WriteValue(L"ChromeTab.ChromeActiveDocument", 1); |
| 786 | result2 = key.WriteValue(L"ChromeTab.ChromeActiveDocument.1", 1); |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 787 | } else { |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 788 | result1 = key.DeleteValue(L"ChromeTab.ChromeActiveDocument"); |
| 789 | result2 = key.DeleteValue(L"ChromeTab.ChromeActiveDocument.1"); |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 790 | } |
| 791 | |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 792 | return (result2 == ERROR_SUCCESS) && (result2 == ERROR_SUCCESS); |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 793 | } |
| 794 | |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 795 | bool RegisterSecuredMimeHandler(bool enable, bool is_system) { |
| 796 | if (!is_system) { |
| 797 | return SetOrDeleteMimeHandlerKey(enable, HKEY_CURRENT_USER); |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 798 | } else if (base::win::GetVersion() < base::win::VERSION_VISTA) { |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 799 | return SetOrDeleteMimeHandlerKey(enable, HKEY_LOCAL_MACHINE); |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | std::wstring mime_key = kInternetSettings; |
| 803 | mime_key.append(L"\\Secure Mime Handlers"); |
| 804 | std::wstring backup_key = kInternetSettings; |
| 805 | backup_key.append(L"\\__backup_SMH__"); |
| 806 | std::wstring object_name = L"MACHINE\\"; |
| 807 | object_name.append(mime_key); |
| 808 | |
| 809 | TokenWithPrivileges token_; |
| 810 | if (!token_.EnablePrivileges()) |
| 811 | return false; |
| 812 | |
| 813 | // If there is a backup key - something bad happened; try to restore |
| 814 | // security on "Secure Mime Handlers" from the backup. |
| 815 | SecurityDescBackup backup(backup_key); |
| 816 | backup.RestoreSecurity(object_name.c_str()); |
| 817 | |
| 818 | // Read old security descriptor of the Mime key first. |
| 819 | CSecurityDesc sd; |
| 820 | if (!AtlGetSecurityDescriptor(object_name.c_str(), SE_REGISTRY_KEY, &sd)) { |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | backup.SaveSecurity(sd); |
| 825 | bool result = false; |
| 826 | // set new owner |
| 827 | if (AtlSetOwnerSid(object_name.c_str(), SE_REGISTRY_KEY, token_.GetUser())) { |
| 828 | // set new dacl |
| 829 | CDacl new_dacl; |
| 830 | sd.GetDacl(&new_dacl); |
| 831 | new_dacl.AddAllowedAce(token_.GetUser(), GENERIC_WRITE | GENERIC_READ); |
| 832 | if (AtlSetDacl(object_name.c_str(), SE_REGISTRY_KEY, new_dacl)) { |
[email protected] | 59b34a7 | 2010-09-01 17:30:35 | [diff] [blame] | 833 | result = SetOrDeleteMimeHandlerKey(enable, HKEY_LOCAL_MACHINE); |
[email protected] | 357219de | 2009-10-06 23:40:22 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
| 837 | backup.RestoreSecurity(object_name.c_str()); |
| 838 | return result; |
| 839 | } |