[email protected] | a21d808 | 2012-01-12 19:23:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | c9349d08 | 2008-08-22 21:16:47 | [diff] [blame] | 4 | |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 5 | #include <windows.h> |
| 6 | #include <shlwapi.h> |
[email protected] | c9349d08 | 2008-08-22 21:16:47 | [diff] [blame] | 7 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 9 | #include "base/debug/trace_event.h" |
[email protected] | ae0f077 | 2010-08-13 04:54:10 | [diff] [blame] | 10 | #include "base/environment.h" |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 11 | #include "base/file_version_info.h" |
[email protected] | ae0f077 | 2010-08-13 04:54:10 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | bffce7c | 2013-09-13 17:30:07 | [diff] [blame] | 14 | #include "base/rand_util.h" // For PreRead experiment. |
| 15 | #include "base/sha1.h" // For PreRead experiment. |
[email protected] | ee202066 | 2013-06-10 18:56:21 | [diff] [blame] | 16 | #include "base/strings/string16.h" |
| 17 | #include "base/strings/string_util.h" |
| 18 | #include "base/strings/stringprintf.h" |
[email protected] | 12bfb61 | 2013-06-07 19:54:02 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 8576935 | 2010-11-16 14:24:30 | [diff] [blame] | 20 | #include "base/version.h" |
[email protected] | 797b7de4 | 2013-07-11 08:01:42 | [diff] [blame] | 21 | #include "base/win/windows_version.h" |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 22 | #include "chrome/app/client_util.h" |
[email protected] | 797b7de4 | 2013-07-11 08:01:42 | [diff] [blame] | 23 | #include "chrome/app/image_pre_reader_win.h" |
[email protected] | 03d21b8 | 2010-12-07 19:37:22 | [diff] [blame] | 24 | #include "chrome/common/chrome_constants.h" |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 25 | #include "chrome/common/chrome_result_codes.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 26 | #include "chrome/common/chrome_switches.h" |
[email protected] | e3824ec | 2013-02-08 01:02:40 | [diff] [blame] | 27 | #include "chrome/common/env_vars.h" |
[email protected] | 74d1eec | 2009-11-04 22:18:57 | [diff] [blame] | 28 | #include "chrome/installer/util/browser_distribution.h" |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 29 | #include "chrome/installer/util/channel_info.h" |
[email protected] | 2414e84 | 2008-11-07 01:27:57 | [diff] [blame] | 30 | #include "chrome/installer/util/google_update_constants.h" |
[email protected] | fd59f82 | 2011-05-12 18:07:18 | [diff] [blame] | 31 | #include "chrome/installer/util/google_update_settings.h" |
[email protected] | 12bfb61 | 2013-06-07 19:54:02 | [diff] [blame] | 32 | #include "chrome/installer/util/install_util.h" |
[email protected] | 28c1969 | 2008-11-07 23:40:38 | [diff] [blame] | 33 | #include "chrome/installer/util/util_constants.h" |
[email protected] | 0b818f7 | 2013-10-22 00:11:03 | [diff] [blame] | 34 | #include "components/breakpad/app/breakpad_win.h" |
[email protected] | 28c1969 | 2008-11-07 23:40:38 | [diff] [blame] | 35 | |
| 36 | namespace { |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 37 | // The entry point signature of chrome.dll. |
[email protected] | d85d670 | 2011-09-01 15:51:53 | [diff] [blame] | 38 | typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*); |
[email protected] | 28c1969 | 2008-11-07 23:40:38 | [diff] [blame] | 39 | |
[email protected] | 3cdacd4 | 2010-04-30 18:55:53 | [diff] [blame] | 40 | typedef void (*RelaunchChromeBrowserWithNewCommandLineIfNeededFunc)(); |
| 41 | |
[email protected] | bffce7c | 2013-09-13 17:30:07 | [diff] [blame] | 42 | // Returns true if the build date for this module precedes the expiry date |
| 43 | // for the pre-read experiment. |
| 44 | bool PreReadExperimentIsActive() { |
| 45 | const int kPreReadExpiryYear = 2014; |
| 46 | const int kPreReadExpiryMonth = 7; |
| 47 | const int kPreReadExpiryDay = 1; |
| 48 | const char kBuildTimeStr[] = __DATE__ " " __TIME__; |
| 49 | |
| 50 | // Get the timestamp of the build. |
| 51 | base::Time build_time; |
| 52 | bool result = base::Time::FromString(kBuildTimeStr, &build_time); |
| 53 | DCHECK(result); |
| 54 | |
| 55 | // Get the timestamp at which the experiment expires. |
| 56 | base::Time::Exploded exploded = {0}; |
| 57 | exploded.year = kPreReadExpiryYear; |
| 58 | exploded.month = kPreReadExpiryMonth; |
| 59 | exploded.day_of_month = kPreReadExpiryDay; |
| 60 | base::Time expiration_time = base::Time::FromLocalExploded(exploded); |
| 61 | |
| 62 | // Return true if the build time predates the expiration time.. |
| 63 | return build_time < expiration_time; |
| 64 | } |
| 65 | |
| 66 | // Get random unit values, i.e., in the range (0, 1), denoting a die-toss for |
| 67 | // being in an experiment population and experimental group thereof. |
| 68 | void GetPreReadPopulationAndGroup(double* population, double* group) { |
| 69 | // By default we use the metrics id for the user as stable pseudo-random |
| 70 | // input to a hash. |
[email protected] | 264c0acac | 2013-10-01 13:33:30 | [diff] [blame] | 71 | std::string metrics_id; |
[email protected] | bffce7c | 2013-09-13 17:30:07 | [diff] [blame] | 72 | GoogleUpdateSettings::GetMetricsId(&metrics_id); |
| 73 | |
| 74 | // If this user has not metrics id, we fall back to a purely random value |
| 75 | // per browser session. |
| 76 | const size_t kLength = 16; |
| 77 | std::string random_value(metrics_id.empty() ? base::RandBytesAsString(kLength) |
[email protected] | 264c0acac | 2013-10-01 13:33:30 | [diff] [blame] | 78 | : metrics_id); |
[email protected] | bffce7c | 2013-09-13 17:30:07 | [diff] [blame] | 79 | |
| 80 | // To interpret the value as a random number we hash it and read the first 8 |
| 81 | // bytes of the hash as a unit-interval representing a die-toss for being in |
| 82 | // the experiment population and the second 8 bytes as a die-toss for being |
| 83 | // in various experiment groups. |
| 84 | unsigned char sha1_hash[base::kSHA1Length]; |
| 85 | base::SHA1HashBytes( |
| 86 | reinterpret_cast<const unsigned char*>(random_value.c_str()), |
| 87 | random_value.size() * sizeof(random_value[0]), |
| 88 | sha1_hash); |
| 89 | COMPILE_ASSERT(2 * sizeof(uint64) < sizeof(sha1_hash), need_more_data); |
| 90 | const uint64* random_bits = reinterpret_cast<uint64*>(&sha1_hash[0]); |
| 91 | |
| 92 | // Convert the bits into unit-intervals and return. |
| 93 | *population = base::BitsToOpenEndedUnitInterval(random_bits[0]); |
| 94 | *group = base::BitsToOpenEndedUnitInterval(random_bits[1]); |
| 95 | } |
| 96 | |
| 97 | // Gets the amount of pre-read to use as well as the experiment group in which |
| 98 | // the user falls. |
| 99 | size_t InitPreReadPercentage() { |
| 100 | // By default use the old behaviour: read 100%. |
| 101 | const int kDefaultPercentage = 100; |
| 102 | const char kDefaultFormatStr[] = "%d-pct-default"; |
| 103 | const char kControlFormatStr[] = "%d-pct-control"; |
| 104 | const char kGroupFormatStr[] = "%d-pct"; |
| 105 | |
| 106 | COMPILE_ASSERT(kDefaultPercentage <= 100, default_percentage_too_large); |
| 107 | COMPILE_ASSERT(kDefaultPercentage % 5 == 0, default_percentage_not_mult_5); |
| 108 | |
| 109 | // Roll the dice to determine if this user is in the experiment and if so, |
| 110 | // in which experimental group. |
| 111 | double population = 0.0; |
| 112 | double group = 0.0; |
| 113 | GetPreReadPopulationAndGroup(&population, &group); |
| 114 | |
| 115 | // We limit experiment populations to 1% of the Stable and 10% of each of |
| 116 | // the other channels. |
| 117 | const string16 channel(GoogleUpdateSettings::GetChromeChannel( |
| 118 | GoogleUpdateSettings::IsSystemInstall())); |
| 119 | double threshold = (channel == installer::kChromeChannelStable) ? 0.01 : 0.10; |
| 120 | |
| 121 | // If the experiment has expired use the default pre-read level. Otherwise, |
| 122 | // those not in the experiment population also use the default pre-read level. |
| 123 | size_t value = kDefaultPercentage; |
| 124 | const char* format_str = kDefaultFormatStr; |
| 125 | if (PreReadExperimentIsActive() && (population <= threshold)) { |
| 126 | // We divide the experiment population into groups pre-reading at 5 percent |
| 127 | // increments in the range [0, 100]. |
| 128 | value = static_cast<size_t>(group * 21.0) * 5; |
| 129 | DCHECK_LE(value, 100u); |
| 130 | DCHECK_EQ(0u, value % 5); |
| 131 | format_str = |
| 132 | (value == kDefaultPercentage) ? kControlFormatStr : kGroupFormatStr; |
| 133 | } |
| 134 | |
| 135 | // Generate the group name corresponding to this percentage value. |
| 136 | std::string group_name; |
| 137 | base::SStringPrintf(&group_name, format_str, value); |
| 138 | |
| 139 | // Persist the group name to the environment so that it can be used for |
| 140 | // reporting. |
| 141 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 142 | env->SetVar(chrome::kPreReadEnvironmentVariable, group_name); |
| 143 | |
| 144 | // Return the percentage value to be used. |
| 145 | return value; |
| 146 | } |
| 147 | |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 148 | // Expects that |dir| has a trailing backslash. |dir| is modified so it |
| 149 | // contains the full path that was tried. Caller must check for the return |
[email protected] | 8576935 | 2010-11-16 14:24:30 | [diff] [blame] | 150 | // value not being null to determine if this path contains a valid dll. |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 151 | HMODULE LoadChromeWithDirectory(string16* dir) { |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 152 | ::SetCurrentDirectoryW(dir->c_str()); |
[email protected] | a4d1690 | 2013-07-20 20:41:37 | [diff] [blame] | 153 | const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 154 | #if !defined(CHROME_MULTIPLE_DLL) |
| 155 | const wchar_t* dll_name = installer::kChromeDll; |
| 156 | #else |
[email protected] | dfb3b28 | 2013-10-08 01:25:36 | [diff] [blame] | 157 | const wchar_t* dll_name = |
| 158 | cmd_line.HasSwitch(switches::kProcessType) && |
| 159 | cmd_line.GetSwitchValueASCII(switches::kProcessType) != "service" |
| 160 | ? installer::kChromeChildDll |
| 161 | : installer::kChromeDll; |
[email protected] | a4d1690 | 2013-07-20 20:41:37 | [diff] [blame] | 162 | #endif |
| 163 | dir->append(dll_name); |
[email protected] | 00b7341 | 2010-06-21 21:01:36 | [diff] [blame] | 164 | |
[email protected] | 797b7de4 | 2013-07-11 08:01:42 | [diff] [blame] | 165 | #if !defined(WIN_DISABLE_PREREAD) |
[email protected] | bffce7c | 2013-09-13 17:30:07 | [diff] [blame] | 166 | // We pre-read the binary to warm the memory caches (fewer hard faults to |
| 167 | // page parts of the binary in). |
[email protected] | a4d1690 | 2013-07-20 20:41:37 | [diff] [blame] | 168 | if (!cmd_line.HasSwitch(switches::kProcessType)) { |
| 169 | const size_t kStepSize = 1024 * 1024; |
[email protected] | bffce7c | 2013-09-13 17:30:07 | [diff] [blame] | 170 | size_t percentage = InitPreReadPercentage(); |
| 171 | ImagePreReader::PartialPreReadImage(dir->c_str(), percentage, kStepSize); |
[email protected] | a4d1690 | 2013-07-20 20:41:37 | [diff] [blame] | 172 | } |
[email protected] | 797b7de4 | 2013-07-11 08:01:42 | [diff] [blame] | 173 | #endif |
| 174 | |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 175 | return ::LoadLibraryExW(dir->c_str(), NULL, |
| 176 | LOAD_WITH_ALTERED_SEARCH_PATH); |
| 177 | } |
| 178 | |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 179 | void RecordDidRun(const string16& dll_path) { |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 180 | bool system_level = !InstallUtil::IsPerUserInstall(dll_path.c_str()); |
[email protected] | fd59f82 | 2011-05-12 18:07:18 | [diff] [blame] | 181 | GoogleUpdateSettings::UpdateDidRunState(true, system_level); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 184 | void ClearDidRun(const string16& dll_path) { |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 185 | bool system_level = !InstallUtil::IsPerUserInstall(dll_path.c_str()); |
[email protected] | fd59f82 | 2011-05-12 18:07:18 | [diff] [blame] | 186 | GoogleUpdateSettings::UpdateDidRunState(false, system_level); |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 187 | } |
[email protected] | fd59f82 | 2011-05-12 18:07:18 | [diff] [blame] | 188 | |
[email protected] | 28f576f | 2011-08-26 20:46:55 | [diff] [blame] | 189 | } // namespace |
[email protected] | f068b41 | 2012-04-02 20:24:48 | [diff] [blame] | 190 | |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 191 | string16 GetExecutablePath() { |
[email protected] | f068b41 | 2012-04-02 20:24:48 | [diff] [blame] | 192 | wchar_t path[MAX_PATH]; |
| 193 | ::GetModuleFileNameW(NULL, path, MAX_PATH); |
| 194 | if (!::PathRemoveFileSpecW(path)) |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 195 | return string16(); |
| 196 | string16 exe_path(path); |
[email protected] | f068b41 | 2012-04-02 20:24:48 | [diff] [blame] | 197 | return exe_path.append(1, L'\\'); |
| 198 | } |
| 199 | |
[email protected] | 18ee9f96 | 2013-07-01 22:38:48 | [diff] [blame] | 200 | string16 GetCurrentModuleVersion() { |
| 201 | scoped_ptr<FileVersionInfo> file_version_info( |
| 202 | FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 203 | if (file_version_info.get()) { |
| 204 | string16 version_string(file_version_info->file_version()); |
| 205 | if (Version(WideToASCII(version_string)).IsValid()) |
| 206 | return version_string; |
| 207 | } |
| 208 | return string16(); |
| 209 | } |
| 210 | |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 211 | //============================================================================= |
| 212 | |
| 213 | MainDllLoader::MainDllLoader() : dll_(NULL) { |
| 214 | } |
| 215 | |
| 216 | MainDllLoader::~MainDllLoader() { |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 217 | } |
| 218 | |
[email protected] | 8576935 | 2010-11-16 14:24:30 | [diff] [blame] | 219 | // Loading chrome is an interesting affair. First we try loading from the |
| 220 | // current directory to support run-what-you-compile and other development |
| 221 | // scenarios. |
[email protected] | 18ee9f96 | 2013-07-01 22:38:48 | [diff] [blame] | 222 | // If that fails then we look at the --chrome-version command line flag to |
| 223 | // determine if we should stick with an older dll version even if a new one is |
| 224 | // available to support upgrade-in-place scenarios. |
| 225 | // If that fails then finally we look at the version resource in the current |
| 226 | // module. This is the expected path for chrome.exe browser instances in an |
| 227 | // installed build. |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 228 | HMODULE MainDllLoader::Load(string16* out_version, string16* out_file) { |
[email protected] | 00d0c6d | 2013-05-16 23:52:03 | [diff] [blame] | 229 | const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 230 | const string16 dir(GetExecutablePath()); |
[email protected] | 8576935 | 2010-11-16 14:24:30 | [diff] [blame] | 231 | *out_file = dir; |
[email protected] | 5bca9ee | 2011-09-03 00:18:42 | [diff] [blame] | 232 | HMODULE dll = LoadChromeWithDirectory(out_file); |
[email protected] | 00d0c6d | 2013-05-16 23:52:03 | [diff] [blame] | 233 | if (!dll) { |
| 234 | // Loading from same directory (for developers) failed. |
| 235 | string16 version_string; |
[email protected] | 00d0c6d | 2013-05-16 23:52:03 | [diff] [blame] | 236 | if (cmd_line.HasSwitch(switches::kChromeVersion)) { |
| 237 | // This is used to support Chrome Frame, see http://crbug.com/88589. |
| 238 | version_string = cmd_line.GetSwitchValueNative(switches::kChromeVersion); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 239 | |
[email protected] | 18ee9f96 | 2013-07-01 22:38:48 | [diff] [blame] | 240 | if (!Version(WideToASCII(version_string)).IsValid()) { |
[email protected] | 00d0c6d | 2013-05-16 23:52:03 | [diff] [blame] | 241 | // If a bogus command line flag was given, then abort. |
| 242 | LOG(ERROR) << "Invalid command line version: " << version_string; |
| 243 | return NULL; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // If no version on the command line, then look at the version resource in |
| 248 | // the current module and try loading that. |
[email protected] | 18ee9f96 | 2013-07-01 22:38:48 | [diff] [blame] | 249 | if (version_string.empty()) |
| 250 | version_string = GetCurrentModuleVersion(); |
[email protected] | 8576935 | 2010-11-16 14:24:30 | [diff] [blame] | 251 | |
[email protected] | 18ee9f96 | 2013-07-01 22:38:48 | [diff] [blame] | 252 | if (version_string.empty()) { |
[email protected] | 00d0c6d | 2013-05-16 23:52:03 | [diff] [blame] | 253 | LOG(ERROR) << "No valid Chrome version found"; |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | *out_file = dir; |
| 258 | *out_version = version_string; |
| 259 | out_file->append(*out_version).append(1, L'\\'); |
| 260 | dll = LoadChromeWithDirectory(out_file); |
| 261 | if (!dll) { |
[email protected] | 18ee9f96 | 2013-07-01 22:38:48 | [diff] [blame] | 262 | PLOG(ERROR) << "Failed to load Chrome DLL from " << *out_file; |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 263 | return NULL; |
[email protected] | 8576935 | 2010-11-16 14:24:30 | [diff] [blame] | 264 | } |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 18ee9f96 | 2013-07-01 22:38:48 | [diff] [blame] | 267 | DCHECK(dll); |
| 268 | |
[email protected] | f068b41 | 2012-04-02 20:24:48 | [diff] [blame] | 269 | return dll; |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Launching is a matter of loading the right dll, setting the CHROME_VERSION |
| 273 | // environment variable and just calling the entry point. Derived classes can |
| 274 | // add custom code in the OnBeforeLaunch callback. |
| 275 | int MainDllLoader::Launch(HINSTANCE instance, |
| 276 | sandbox::SandboxInterfaceInfo* sbox_info) { |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 277 | string16 version; |
| 278 | string16 file; |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 279 | dll_ = Load(&version, &file); |
| 280 | if (!dll_) |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 281 | return chrome::RESULT_CODE_MISSING_DATA; |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 282 | |
[email protected] | ae0f077 | 2010-08-13 04:54:10 | [diff] [blame] | 283 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 284 | env->SetVar(chrome::kChromeVersionEnvVar, WideToUTF8(version)); |
[email protected] | e3824ec | 2013-02-08 01:02:40 | [diff] [blame] | 285 | // TODO(erikwright): Remove this when http://crbug.com/174953 is fixed and |
| 286 | // widely deployed. |
| 287 | env->UnSetVar(env_vars::kGoogleUpdateIsMachineEnvVar); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 288 | |
[email protected] | 8dc338c | 2013-12-09 16:28:48 | [diff] [blame] | 289 | const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 290 | std::string process_type = |
| 291 | cmd_line.GetSwitchValueASCII(switches::kProcessType); |
| 292 | breakpad::InitCrashReporter(process_type); |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 293 | OnBeforeLaunch(file); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 294 | |
| 295 | DLL_MAIN entry_point = |
| 296 | reinterpret_cast<DLL_MAIN>(::GetProcAddress(dll_, "ChromeMain")); |
| 297 | if (!entry_point) |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 298 | return chrome::RESULT_CODE_BAD_PROCESS_TYPE; |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 299 | |
[email protected] | d85d670 | 2011-09-01 15:51:53 | [diff] [blame] | 300 | int rc = entry_point(instance, sbox_info); |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 301 | return OnBeforeExit(rc, file); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 3cdacd4 | 2010-04-30 18:55:53 | [diff] [blame] | 304 | void MainDllLoader::RelaunchChromeBrowserWithNewCommandLineIfNeeded() { |
| 305 | RelaunchChromeBrowserWithNewCommandLineIfNeededFunc relaunch_function = |
| 306 | reinterpret_cast<RelaunchChromeBrowserWithNewCommandLineIfNeededFunc>( |
| 307 | ::GetProcAddress(dll_, |
| 308 | "RelaunchChromeBrowserWithNewCommandLineIfNeeded")); |
| 309 | if (!relaunch_function) { |
| 310 | LOG(ERROR) << "Could not find exported function " |
| 311 | << "RelaunchChromeBrowserWithNewCommandLineIfNeeded"; |
| 312 | } else { |
| 313 | relaunch_function(); |
| 314 | } |
| 315 | } |
| 316 | |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 317 | //============================================================================= |
| 318 | |
| 319 | class ChromeDllLoader : public MainDllLoader { |
| 320 | public: |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 321 | virtual string16 GetRegistryPath() { |
| 322 | string16 key(google_update::kRegPathClients); |
[email protected] | 5f2cee8 | 2009-11-05 04:59:48 | [diff] [blame] | 323 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 324 | key.append(L"\\").append(dist->GetAppGuid()); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 325 | return key; |
| 326 | } |
| 327 | |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 328 | virtual void OnBeforeLaunch(const string16& dll_path) { |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 329 | RecordDidRun(dll_path); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 330 | } |
[email protected] | 42d7510c | 2009-12-19 01:48:30 | [diff] [blame] | 331 | |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 332 | virtual int OnBeforeExit(int return_code, const string16& dll_path) { |
[email protected] | 42d7510c | 2009-12-19 01:48:30 | [diff] [blame] | 333 | // NORMAL_EXIT_CANCEL is used for experiments when the user cancels |
| 334 | // so we need to reset the did_run signal so omaha does not count |
| 335 | // this run as active usage. |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 336 | if (chrome::RESULT_CODE_NORMAL_EXIT_CANCEL == return_code) { |
[email protected] | 2544a7a | 2011-03-14 18:25:19 | [diff] [blame] | 337 | ClearDidRun(dll_path); |
[email protected] | 42d7510c | 2009-12-19 01:48:30 | [diff] [blame] | 338 | } |
| 339 | return return_code; |
| 340 | } |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 341 | }; |
| 342 | |
| 343 | //============================================================================= |
| 344 | |
| 345 | class ChromiumDllLoader : public MainDllLoader { |
| 346 | public: |
[email protected] | 6a0464c8 | 2012-08-07 05:58:28 | [diff] [blame] | 347 | virtual string16 GetRegistryPath() { |
[email protected] | 74d1eec | 2009-11-04 22:18:57 | [diff] [blame] | 348 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 349 | return dist->GetVersionKey(); |
[email protected] | 4512f3ac | 2009-11-04 03:39:22 | [diff] [blame] | 350 | } |
| 351 | }; |
| 352 | |
| 353 | MainDllLoader* MakeMainDllLoader() { |
| 354 | #if defined(GOOGLE_CHROME_BUILD) |
| 355 | return new ChromeDllLoader(); |
| 356 | #else |
| 357 | return new ChromiumDllLoader(); |
| 358 | #endif |
| 359 | } |