blob: 3c9ac6c4e73bab470bb2cd6a819bee9c3f22ec98 [file] [log] [blame]
[email protected]11713fd2012-01-30 17:50:361// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fcf19542009-03-30 21:24:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ef916272009-07-08 21:40:555#include "chrome/common/child_process_logging.h"
[email protected]fcf19542009-03-30 21:24:076
7#import <Foundation/Foundation.h>
8
[email protected]19f9aa382011-09-26 21:57:529#include "base/command_line.h"
[email protected]528c56d2010-07-30 19:28:4410#include "base/string_number_conversions.h"
[email protected]6724e592012-03-16 04:49:1411#include "base/string_split.h"
[email protected]fcf19542009-03-30 21:24:0712#include "base/string_util.h"
[email protected]317e2702011-03-09 17:53:0113#include "base/stringprintf.h"
[email protected]19f9aa382011-09-26 21:57:5214#include "base/sys_string_conversions.h"
[email protected]528c56d2010-07-30 19:28:4415#include "base/utf_string_conversions.h"
[email protected]157d5472009-11-05 22:31:0316#include "chrome/installer/util/google_update_settings.h"
[email protected]a80f5ece2011-10-20 23:56:5517#include "content/public/common/gpu_info.h"
[email protected]fcf19542009-03-30 21:24:0718#include "googleurl/src/gurl.h"
[email protected]fcf19542009-03-30 21:24:0719
[email protected]ef916272009-07-08 21:40:5520namespace child_process_logging {
[email protected]fcf19542009-03-30 21:24:0721
[email protected]812befe32011-09-11 11:29:4922using base::mac::SetCrashKeyValueFuncPtr;
23using base::mac::ClearCrashKeyValueFuncPtr;
24using base::mac::SetCrashKeyValue;
25using base::mac::ClearCrashKey;
26
[email protected]fcf19542009-03-30 21:24:0727const int kMaxNumCrashURLChunks = 8;
28const int kMaxNumURLChunkValueLength = 255;
29const char *kUrlChunkFormatStr = "url-chunk-%d";
[email protected]157d5472009-11-05 22:31:0330const char *kGuidParamName = "guid";
[email protected]cfc7db312011-09-23 18:49:1531const char *kGPUVendorIdParamName = "gpu-venid";
[email protected]409d8d462010-10-13 18:47:1432const char *kGPUDeviceIdParamName = "gpu-devid";
33const char *kGPUDriverVersionParamName = "gpu-driver";
34const char *kGPUPixelShaderVersionParamName = "gpu-psver";
35const char *kGPUVertexShaderVersionParamName = "gpu-vsver";
36const char *kGPUGLVersionParamName = "gpu-glver";
[email protected]3231c2e2010-09-02 12:41:0537const char *kNumberOfViews = "num-views";
[email protected]77bf9602010-11-11 00:37:5938NSString* const kNumExtensionsName = @"num-extensions";
39NSString* const kExtensionNameFormat = @"extension-%d";
[email protected]b4883c12012-03-16 10:01:4640NSString* const kPrinterInfoNameFormat = @"prn-info-%zu";
[email protected]fcf19542009-03-30 21:24:0741
[email protected]6dde9d72010-08-26 08:55:2242// Account for the terminating null character.
43static const size_t kClientIdSize = 32 + 1;
44static char g_client_id[kClientIdSize];
45
[email protected]ef916272009-07-08 21:40:5546void SetActiveURLImpl(const GURL& url,
47 SetCrashKeyValueFuncPtr set_key_func,
48 ClearCrashKeyValueFuncPtr clear_key_func) {
[email protected]fcf19542009-03-30 21:24:0749
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]157d5472009-11-05 22:31:0389void 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]ef916272009-07-08 21:40:5596void SetActiveURL(const GURL& url) {
[email protected]812befe32011-09-11 11:29:4997 SetActiveURLImpl(url, SetCrashKeyValue, ClearCrashKey);
[email protected]fcf19542009-03-30 21:24:0798}
99
[email protected]157d5472009-11-05 22:31:03100void SetClientId(const std::string& client_id) {
101 std::string str(client_id);
102 ReplaceSubstringsAfterOffset(&str, 0, "-", "");
103
[email protected]6dde9d72010-08-26 08:55:22104 base::strlcpy(g_client_id, str.c_str(), kClientIdSize);
[email protected]812befe32011-09-11 11:29:49105 SetClientIdImpl(str, SetCrashKeyValue);
[email protected]157d5472009-11-05 22:31:03106
107 std::wstring wstr = ASCIIToWide(str);
108 GoogleUpdateSettings::SetMetricsId(wstr);
109}
[email protected]aab98a52009-12-02 03:22:35110
[email protected]6dde9d72010-08-26 08:55:22111std::string GetClientId() {
112 return std::string(g_client_id);
113}
114
[email protected]c8865962009-12-16 07:47:39115void SetActiveExtensions(const std::set<std::string>& extension_ids) {
[email protected]77bf9602010-11-11 00:37:59116 // Log the count separately to track heavy users.
117 const int count = static_cast<int>(extension_ids.size());
[email protected]812befe32011-09-11 11:29:49118 SetCrashKeyValue(kNumExtensionsName,
119 [NSString stringWithFormat:@"%i", count]);
[email protected]77bf9602010-11-11 00:37:59120
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]812befe32011-09-11 11:29:49127 SetCrashKeyValue(key, [NSString stringWithUTF8String:iter->c_str()]);
[email protected]77bf9602010-11-11 00:37:59128 ++iter;
129 } else {
[email protected]812befe32011-09-11 11:29:49130 ClearCrashKey(key);
[email protected]77bf9602010-11-11 00:37:59131 }
132 }
[email protected]aab98a52009-12-02 03:22:35133}
134
[email protected]a110dd12010-07-19 07:51:33135void 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]a80f5ece2011-10-20 23:56:55142void SetGpuInfoImpl(const content::GPUInfo& gpu_info,
[email protected]a110dd12010-07-19 07:51:33143 SetCrashKeyValueFuncPtr set_key_func) {
144 SetGpuKeyValue(kGPUVendorIdParamName,
[email protected]a094e2c2012-05-10 23:02:42145 base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id),
[email protected]a110dd12010-07-19 07:51:33146 set_key_func);
147 SetGpuKeyValue(kGPUDeviceIdParamName,
[email protected]a094e2c2012-05-10 23:02:42148 base::StringPrintf("0x%04x", gpu_info.gpu.device_id),
[email protected]a110dd12010-07-19 07:51:33149 set_key_func);
150 SetGpuKeyValue(kGPUDriverVersionParamName,
[email protected]a61508e52011-03-08 17:59:42151 gpu_info.driver_version,
[email protected]a110dd12010-07-19 07:51:33152 set_key_func);
153 SetGpuKeyValue(kGPUPixelShaderVersionParamName,
[email protected]6cc8ece62011-03-14 20:05:47154 gpu_info.pixel_shader_version,
[email protected]a110dd12010-07-19 07:51:33155 set_key_func);
156 SetGpuKeyValue(kGPUVertexShaderVersionParamName,
[email protected]6cc8ece62011-03-14 20:05:47157 gpu_info.vertex_shader_version,
[email protected]a110dd12010-07-19 07:51:33158 set_key_func);
[email protected]409d8d462010-10-13 18:47:14159 SetGpuKeyValue(kGPUGLVersionParamName,
[email protected]6cc8ece62011-03-14 20:05:47160 gpu_info.gl_version,
[email protected]409d8d462010-10-13 18:47:14161 set_key_func);
[email protected]a110dd12010-07-19 07:51:33162}
163
[email protected]a80f5ece2011-10-20 23:56:55164void SetGpuInfo(const content::GPUInfo& gpu_info) {
[email protected]812befe32011-09-11 11:29:49165 SetGpuInfoImpl(gpu_info, SetCrashKeyValue);
[email protected]a110dd12010-07-19 07:51:33166}
167
[email protected]6724e592012-03-16 04:49:14168void 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]3231c2e2010-09-02 12:41:05181
182void 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
189void SetNumberOfViews(int number_of_views) {
[email protected]812befe32011-09-11 11:29:49190 SetNumberOfViewsImpl(number_of_views, SetCrashKeyValue);
[email protected]3231c2e2010-09-02 12:41:05191}
192
[email protected]19f9aa382011-09-26 21:57:52193void SetCommandLine(const CommandLine* command_line) {
[email protected]19f9aa382011-09-26 21:57:52194 DCHECK(command_line);
[email protected]c443fe4e2011-12-06 07:29:58195 if (!command_line)
[email protected]19f9aa382011-09-26 21:57:52196 return;
197
198 // These should match the corresponding strings in breakpad_win.cc.
199 NSString* const kNumSwitchesKey = @"num-switches";
[email protected]ae351502012-02-04 22:09:03200 NSString* const kSwitchKeyFormat = @"switch-%zu"; // 1-based.
[email protected]19f9aa382011-09-26 21:57:52201
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]11713fd2012-01-30 17:50:36205 [NSString stringWithFormat:@"%zu", argv.size() - 1]);
[email protected]19f9aa382011-09-26 21:57:52206
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]c0229d82011-09-30 20:53:06210 NSString* key = [NSString stringWithFormat:kSwitchKeyFormat, (key_i + 1)];
[email protected]19f9aa382011-09-26 21:57:52211 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]c0229d82011-09-30 20:53:06218 NSString* key = [NSString stringWithFormat:kSwitchKeyFormat, (key_i + 1)];
[email protected]19f9aa382011-09-26 21:57:52219 ClearCrashKey(key);
220 }
[email protected]783f06f2011-09-14 01:36:11221}
222
[email protected]4cb3c1fc2012-04-05 07:23:48223void SetExperimentList(const std::vector<string16>& state) {
224 // TODO(mad): Implement this.
225}
226
[email protected]2ec5a482012-02-22 00:01:18227void 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]ef916272009-07-08 21:40:55234} // namespace child_process_logging