[email protected] | 11713fd | 2012-01-30 17:50:36 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [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 | |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 5 | #include "chrome/common/child_process_logging.h" |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 6 | |
| 7 | #import <Foundation/Foundation.h> |
| 8 | |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 10 | #include "base/string_number_conversions.h" |
[email protected] | 6724e59 | 2012-03-16 04:49:14 | [diff] [blame] | 11 | #include "base/string_split.h" |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | 317e270 | 2011-03-09 17:53:01 | [diff] [blame] | 13 | #include "base/stringprintf.h" |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 14 | #include "base/sys_string_conversions.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 16 | #include "chrome/installer/util/google_update_settings.h" |
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 17 | #include "content/public/common/gpu_info.h" |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 18 | #include "googleurl/src/gurl.h" |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 19 | |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 20 | namespace child_process_logging { |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 21 | |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 22 | using base::mac::SetCrashKeyValueFuncPtr; |
| 23 | using base::mac::ClearCrashKeyValueFuncPtr; |
| 24 | using base::mac::SetCrashKeyValue; |
| 25 | using base::mac::ClearCrashKey; |
| 26 | |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 27 | const int kMaxNumCrashURLChunks = 8; |
| 28 | const int kMaxNumURLChunkValueLength = 255; |
| 29 | const char *kUrlChunkFormatStr = "url-chunk-%d"; |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 30 | const char *kGuidParamName = "guid"; |
[email protected] | cfc7db31 | 2011-09-23 18:49:15 | [diff] [blame] | 31 | const char *kGPUVendorIdParamName = "gpu-venid"; |
[email protected] | 409d8d46 | 2010-10-13 18:47:14 | [diff] [blame] | 32 | const char *kGPUDeviceIdParamName = "gpu-devid"; |
| 33 | const char *kGPUDriverVersionParamName = "gpu-driver"; |
| 34 | const char *kGPUPixelShaderVersionParamName = "gpu-psver"; |
| 35 | const char *kGPUVertexShaderVersionParamName = "gpu-vsver"; |
| 36 | const char *kGPUGLVersionParamName = "gpu-glver"; |
[email protected] | 3231c2e | 2010-09-02 12:41:05 | [diff] [blame] | 37 | const char *kNumberOfViews = "num-views"; |
[email protected] | 77bf960 | 2010-11-11 00:37:59 | [diff] [blame] | 38 | NSString* const kNumExtensionsName = @"num-extensions"; |
| 39 | NSString* const kExtensionNameFormat = @"extension-%d"; |
[email protected] | b4883c1 | 2012-03-16 10:01:46 | [diff] [blame] | 40 | NSString* const kPrinterInfoNameFormat = @"prn-info-%zu"; |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 41 | |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 42 | // Account for the terminating null character. |
| 43 | static const size_t kClientIdSize = 32 + 1; |
| 44 | static char g_client_id[kClientIdSize]; |
| 45 | |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 46 | void SetActiveURLImpl(const GURL& url, |
| 47 | SetCrashKeyValueFuncPtr set_key_func, |
| 48 | ClearCrashKeyValueFuncPtr clear_key_func) { |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 49 | |
| 50 | NSString *kUrlChunkFormatStr_utf8 = [NSString |
| 51 | stringWithUTF8String:kUrlChunkFormatStr]; |
| 52 | |
| 53 | // First remove any old url chunks we might have lying around. |
| 54 | for (int i = 0; i < kMaxNumCrashURLChunks; i++) { |
| 55 | // On Windows the url-chunk items are 1-based, so match that. |
| 56 | NSString *key = [NSString stringWithFormat:kUrlChunkFormatStr_utf8, i+1]; |
| 57 | clear_key_func(key); |
| 58 | } |
| 59 | |
| 60 | const std::string& raw_url_utf8 = url.possibly_invalid_spec(); |
| 61 | NSString *raw_url = [NSString stringWithUTF8String:raw_url_utf8.c_str()]; |
| 62 | size_t raw_url_length = [raw_url length]; |
| 63 | |
| 64 | // Bail on zero-length URLs. |
| 65 | if (raw_url_length == 0) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // Parcel the URL up into up to 8, 255 byte segments. |
| 70 | size_t start_ofs = 0; |
| 71 | for (int i = 0; |
| 72 | i < kMaxNumCrashURLChunks && start_ofs < raw_url_length; |
| 73 | ++i) { |
| 74 | |
| 75 | // On Windows the url-chunk items are 1-based, so match that. |
| 76 | NSString *key = [NSString stringWithFormat:kUrlChunkFormatStr_utf8, i+1]; |
| 77 | NSRange range; |
| 78 | range.location = start_ofs; |
| 79 | range.length = std::min((size_t)kMaxNumURLChunkValueLength, |
| 80 | raw_url_length - start_ofs); |
| 81 | NSString *value = [raw_url substringWithRange:range]; |
| 82 | set_key_func(key, value); |
| 83 | |
| 84 | // Next chunk. |
| 85 | start_ofs += kMaxNumURLChunkValueLength; |
| 86 | } |
| 87 | } |
| 88 | |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 89 | void SetClientIdImpl(const std::string& client_id, |
| 90 | SetCrashKeyValueFuncPtr set_key_func) { |
| 91 | NSString *key = [NSString stringWithUTF8String:kGuidParamName]; |
| 92 | NSString *value = [NSString stringWithUTF8String:client_id.c_str()]; |
| 93 | set_key_func(key, value); |
| 94 | } |
| 95 | |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 96 | void SetActiveURL(const GURL& url) { |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 97 | SetActiveURLImpl(url, SetCrashKeyValue, ClearCrashKey); |
[email protected] | fcf1954 | 2009-03-30 21:24:07 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 100 | void SetClientId(const std::string& client_id) { |
| 101 | std::string str(client_id); |
| 102 | ReplaceSubstringsAfterOffset(&str, 0, "-", ""); |
| 103 | |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 104 | base::strlcpy(g_client_id, str.c_str(), kClientIdSize); |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 105 | SetClientIdImpl(str, SetCrashKeyValue); |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 106 | |
| 107 | std::wstring wstr = ASCIIToWide(str); |
| 108 | GoogleUpdateSettings::SetMetricsId(wstr); |
| 109 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 110 | |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 111 | std::string GetClientId() { |
| 112 | return std::string(g_client_id); |
| 113 | } |
| 114 | |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 115 | void SetActiveExtensions(const std::set<std::string>& extension_ids) { |
[email protected] | 77bf960 | 2010-11-11 00:37:59 | [diff] [blame] | 116 | // Log the count separately to track heavy users. |
| 117 | const int count = static_cast<int>(extension_ids.size()); |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 118 | SetCrashKeyValue(kNumExtensionsName, |
| 119 | [NSString stringWithFormat:@"%i", count]); |
[email protected] | 77bf960 | 2010-11-11 00:37:59 | [diff] [blame] | 120 | |
| 121 | // Record up to |kMaxReportedActiveExtensions| extensions, clearing |
| 122 | // keys if there aren't that many. |
| 123 | std::set<std::string>::const_iterator iter = extension_ids.begin(); |
| 124 | for (int i = 0; i < kMaxReportedActiveExtensions; ++i) { |
| 125 | NSString* key = [NSString stringWithFormat:kExtensionNameFormat, i]; |
| 126 | if (iter != extension_ids.end()) { |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 127 | SetCrashKeyValue(key, [NSString stringWithUTF8String:iter->c_str()]); |
[email protected] | 77bf960 | 2010-11-11 00:37:59 | [diff] [blame] | 128 | ++iter; |
| 129 | } else { |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 130 | ClearCrashKey(key); |
[email protected] | 77bf960 | 2010-11-11 00:37:59 | [diff] [blame] | 131 | } |
| 132 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 135 | void SetGpuKeyValue(const char* param_name, const std::string& value_str, |
| 136 | SetCrashKeyValueFuncPtr set_key_func) { |
| 137 | NSString *key = [NSString stringWithUTF8String:param_name]; |
| 138 | NSString *value = [NSString stringWithUTF8String:value_str.c_str()]; |
| 139 | set_key_func(key, value); |
| 140 | } |
| 141 | |
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 142 | void SetGpuInfoImpl(const content::GPUInfo& gpu_info, |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 143 | SetCrashKeyValueFuncPtr set_key_func) { |
| 144 | SetGpuKeyValue(kGPUVendorIdParamName, |
[email protected] | a094e2c | 2012-05-10 23:02:42 | [diff] [blame] | 145 | base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id), |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 146 | set_key_func); |
| 147 | SetGpuKeyValue(kGPUDeviceIdParamName, |
[email protected] | a094e2c | 2012-05-10 23:02:42 | [diff] [blame] | 148 | base::StringPrintf("0x%04x", gpu_info.gpu.device_id), |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 149 | set_key_func); |
| 150 | SetGpuKeyValue(kGPUDriverVersionParamName, |
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 151 | gpu_info.driver_version, |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 152 | set_key_func); |
| 153 | SetGpuKeyValue(kGPUPixelShaderVersionParamName, |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 154 | gpu_info.pixel_shader_version, |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 155 | set_key_func); |
| 156 | SetGpuKeyValue(kGPUVertexShaderVersionParamName, |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 157 | gpu_info.vertex_shader_version, |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 158 | set_key_func); |
[email protected] | 409d8d46 | 2010-10-13 18:47:14 | [diff] [blame] | 159 | SetGpuKeyValue(kGPUGLVersionParamName, |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 160 | gpu_info.gl_version, |
[email protected] | 409d8d46 | 2010-10-13 18:47:14 | [diff] [blame] | 161 | set_key_func); |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 164 | void SetGpuInfo(const content::GPUInfo& gpu_info) { |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 165 | SetGpuInfoImpl(gpu_info, SetCrashKeyValue); |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 6724e59 | 2012-03-16 04:49:14 | [diff] [blame] | 168 | void SetPrinterInfo(const char* printer_info) { |
| 169 | std::vector<std::string> info; |
| 170 | base::SplitString(printer_info, L';', &info); |
| 171 | info.resize(kMaxReportedPrinterRecords); |
| 172 | for (size_t i = 0; i < info.size(); ++i) { |
| 173 | NSString* key = [NSString stringWithFormat:kPrinterInfoNameFormat, i]; |
| 174 | ClearCrashKey(key); |
| 175 | if (!info[i].empty()) { |
| 176 | NSString *value = [NSString stringWithUTF8String:info[i].c_str()]; |
| 177 | SetCrashKeyValue(key, value); |
| 178 | } |
| 179 | } |
| 180 | } |
[email protected] | 3231c2e | 2010-09-02 12:41:05 | [diff] [blame] | 181 | |
| 182 | void SetNumberOfViewsImpl(int number_of_views, |
| 183 | SetCrashKeyValueFuncPtr set_key_func) { |
| 184 | NSString *key = [NSString stringWithUTF8String:kNumberOfViews]; |
| 185 | NSString *value = [NSString stringWithFormat:@"%d", number_of_views]; |
| 186 | set_key_func(key, value); |
| 187 | } |
| 188 | |
| 189 | void SetNumberOfViews(int number_of_views) { |
[email protected] | 812befe3 | 2011-09-11 11:29:49 | [diff] [blame] | 190 | SetNumberOfViewsImpl(number_of_views, SetCrashKeyValue); |
[email protected] | 3231c2e | 2010-09-02 12:41:05 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 193 | void SetCommandLine(const CommandLine* command_line) { |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 194 | DCHECK(command_line); |
[email protected] | c443fe4e | 2011-12-06 07:29:58 | [diff] [blame] | 195 | if (!command_line) |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 196 | return; |
| 197 | |
| 198 | // These should match the corresponding strings in breakpad_win.cc. |
| 199 | NSString* const kNumSwitchesKey = @"num-switches"; |
[email protected] | ae35150 | 2012-02-04 22:09:03 | [diff] [blame] | 200 | NSString* const kSwitchKeyFormat = @"switch-%zu"; // 1-based. |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 201 | |
| 202 | // Note the total number of switches, not including the exec path. |
| 203 | const CommandLine::StringVector& argv = command_line->argv(); |
| 204 | SetCrashKeyValue(kNumSwitchesKey, |
[email protected] | 11713fd | 2012-01-30 17:50:36 | [diff] [blame] | 205 | [NSString stringWithFormat:@"%zu", argv.size() - 1]); |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 206 | |
| 207 | size_t key_i = 0; |
| 208 | for (size_t i = 1; i < argv.size() && key_i < kMaxSwitches; ++i) { |
| 209 | // TODO(shess): Skip boring switches. |
[email protected] | c0229d8 | 2011-09-30 20:53:06 | [diff] [blame] | 210 | NSString* key = [NSString stringWithFormat:kSwitchKeyFormat, (key_i + 1)]; |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 211 | NSString* value = base::SysUTF8ToNSString(argv[i]); |
| 212 | SetCrashKeyValue(key, value); |
| 213 | key_i++; |
| 214 | } |
| 215 | |
| 216 | // Clear out any stale keys. |
| 217 | for (; key_i < kMaxSwitches; ++key_i) { |
[email protected] | c0229d8 | 2011-09-30 20:53:06 | [diff] [blame] | 218 | NSString* key = [NSString stringWithFormat:kSwitchKeyFormat, (key_i + 1)]; |
[email protected] | 19f9aa38 | 2011-09-26 21:57:52 | [diff] [blame] | 219 | ClearCrashKey(key); |
| 220 | } |
[email protected] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 223 | void SetExperimentList(const std::vector<string16>& state) { |
| 224 | // TODO(mad): Implement this. |
| 225 | } |
| 226 | |
[email protected] | 2ec5a48 | 2012-02-22 00:01:18 | [diff] [blame] | 227 | void SetChannel(const std::string& channel) { |
| 228 | // This should match the corresponding string in breakpad_win.cc. |
| 229 | NSString* const kChannelKey = @"channel"; |
| 230 | |
| 231 | SetCrashKeyValue(kChannelKey, base::SysUTF8ToNSString(channel)); |
| 232 | } |
| 233 | |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 234 | } // namespace child_process_logging |