blob: ab3d02167219e6ec387f3b091bb9e145adec3d70 [file] [log] [blame]
[email protected]f24a1e2b2011-04-08 01:48:481// Copyright (c) 2011 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]528c56d2010-07-30 19:28:449#include "base/string_number_conversions.h"
[email protected]fcf19542009-03-30 21:24:0710#include "base/string_util.h"
[email protected]317e2702011-03-09 17:53:0111#include "base/stringprintf.h"
[email protected]528c56d2010-07-30 19:28:4412#include "base/utf_string_conversions.h"
[email protected]157d5472009-11-05 22:31:0313#include "chrome/installer/util/google_update_settings.h"
[email protected]f24a1e2b2011-04-08 01:48:4814#include "content/common/gpu/gpu_info.h"
[email protected]fcf19542009-03-30 21:24:0715#include "googleurl/src/gurl.h"
[email protected]fcf19542009-03-30 21:24:0716
[email protected]ef916272009-07-08 21:40:5517namespace child_process_logging {
[email protected]fcf19542009-03-30 21:24:0718
19const int kMaxNumCrashURLChunks = 8;
20const int kMaxNumURLChunkValueLength = 255;
21const char *kUrlChunkFormatStr = "url-chunk-%d";
[email protected]157d5472009-11-05 22:31:0322const char *kGuidParamName = "guid";
[email protected]409d8d462010-10-13 18:47:1423const char *kGPUVendorIdParamName = "gpu-vendid";
24const char *kGPUDeviceIdParamName = "gpu-devid";
25const char *kGPUDriverVersionParamName = "gpu-driver";
26const char *kGPUPixelShaderVersionParamName = "gpu-psver";
27const char *kGPUVertexShaderVersionParamName = "gpu-vsver";
28const char *kGPUGLVersionParamName = "gpu-glver";
[email protected]3231c2e2010-09-02 12:41:0529const char *kNumberOfViews = "num-views";
[email protected]77bf9602010-11-11 00:37:5930NSString* const kNumExtensionsName = @"num-extensions";
31NSString* const kExtensionNameFormat = @"extension-%d";
[email protected]fcf19542009-03-30 21:24:0732
[email protected]35941fd2009-07-09 01:55:4333static SetCrashKeyValueFuncPtr g_set_key_func;
34static ClearCrashKeyValueFuncPtr g_clear_key_func;
35
[email protected]6dde9d72010-08-26 08:55:2236// Account for the terminating null character.
37static const size_t kClientIdSize = 32 + 1;
38static char g_client_id[kClientIdSize];
39
[email protected]35941fd2009-07-09 01:55:4340void SetCrashKeyFunctions(SetCrashKeyValueFuncPtr set_key_func,
41 ClearCrashKeyValueFuncPtr clear_key_func) {
42 g_set_key_func = set_key_func;
43 g_clear_key_func = clear_key_func;
44}
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]35941fd2009-07-09 01:55:4397 if (g_set_key_func && g_clear_key_func)
98 SetActiveURLImpl(url, g_set_key_func, g_clear_key_func);
[email protected]fcf19542009-03-30 21:24:0799}
100
[email protected]157d5472009-11-05 22:31:03101void SetClientId(const std::string& client_id) {
102 std::string str(client_id);
103 ReplaceSubstringsAfterOffset(&str, 0, "-", "");
104
[email protected]6dde9d72010-08-26 08:55:22105 base::strlcpy(g_client_id, str.c_str(), kClientIdSize);
[email protected]157d5472009-11-05 22:31:03106 if (g_set_key_func)
107 SetClientIdImpl(str, g_set_key_func);
108
109 std::wstring wstr = ASCIIToWide(str);
110 GoogleUpdateSettings::SetMetricsId(wstr);
111}
[email protected]aab98a52009-12-02 03:22:35112
[email protected]6dde9d72010-08-26 08:55:22113std::string GetClientId() {
114 return std::string(g_client_id);
115}
116
[email protected]c8865962009-12-16 07:47:39117void SetActiveExtensions(const std::set<std::string>& extension_ids) {
[email protected]77bf9602010-11-11 00:37:59118 if (!g_set_key_func)
119 return;
120
121 // Log the count separately to track heavy users.
122 const int count = static_cast<int>(extension_ids.size());
123 g_set_key_func(kNumExtensionsName, [NSString stringWithFormat:@"%i", count]);
124
125 // Record up to |kMaxReportedActiveExtensions| extensions, clearing
126 // keys if there aren't that many.
127 std::set<std::string>::const_iterator iter = extension_ids.begin();
128 for (int i = 0; i < kMaxReportedActiveExtensions; ++i) {
129 NSString* key = [NSString stringWithFormat:kExtensionNameFormat, i];
130 if (iter != extension_ids.end()) {
131 g_set_key_func(key, [NSString stringWithUTF8String:iter->c_str()]);
132 ++iter;
133 } else {
134 g_clear_key_func(key);
135 }
136 }
[email protected]aab98a52009-12-02 03:22:35137}
138
[email protected]a110dd12010-07-19 07:51:33139void SetGpuKeyValue(const char* param_name, const std::string& value_str,
140 SetCrashKeyValueFuncPtr set_key_func) {
141 NSString *key = [NSString stringWithUTF8String:param_name];
142 NSString *value = [NSString stringWithUTF8String:value_str.c_str()];
143 set_key_func(key, value);
144}
145
146void SetGpuInfoImpl(const GPUInfo& gpu_info,
147 SetCrashKeyValueFuncPtr set_key_func) {
148 SetGpuKeyValue(kGPUVendorIdParamName,
[email protected]317e2702011-03-09 17:53:01149 base::StringPrintf("0x%04x", gpu_info.vendor_id),
[email protected]a110dd12010-07-19 07:51:33150 set_key_func);
151 SetGpuKeyValue(kGPUDeviceIdParamName,
[email protected]317e2702011-03-09 17:53:01152 base::StringPrintf("0x%04x", gpu_info.device_id),
[email protected]a110dd12010-07-19 07:51:33153 set_key_func);
154 SetGpuKeyValue(kGPUDriverVersionParamName,
[email protected]a61508e52011-03-08 17:59:42155 gpu_info.driver_version,
[email protected]a110dd12010-07-19 07:51:33156 set_key_func);
157 SetGpuKeyValue(kGPUPixelShaderVersionParamName,
[email protected]6cc8ece62011-03-14 20:05:47158 gpu_info.pixel_shader_version,
[email protected]a110dd12010-07-19 07:51:33159 set_key_func);
160 SetGpuKeyValue(kGPUVertexShaderVersionParamName,
[email protected]6cc8ece62011-03-14 20:05:47161 gpu_info.vertex_shader_version,
[email protected]a110dd12010-07-19 07:51:33162 set_key_func);
[email protected]409d8d462010-10-13 18:47:14163 SetGpuKeyValue(kGPUGLVersionParamName,
[email protected]6cc8ece62011-03-14 20:05:47164 gpu_info.gl_version,
[email protected]409d8d462010-10-13 18:47:14165 set_key_func);
[email protected]a110dd12010-07-19 07:51:33166}
167
168void SetGpuInfo(const GPUInfo& gpu_info) {
169 if (g_set_key_func)
170 SetGpuInfoImpl(gpu_info, g_set_key_func);
171}
172
[email protected]3231c2e2010-09-02 12:41:05173
174void SetNumberOfViewsImpl(int number_of_views,
175 SetCrashKeyValueFuncPtr set_key_func) {
176 NSString *key = [NSString stringWithUTF8String:kNumberOfViews];
177 NSString *value = [NSString stringWithFormat:@"%d", number_of_views];
178 set_key_func(key, value);
179}
180
181void SetNumberOfViews(int number_of_views) {
182 if (g_set_key_func)
183 SetNumberOfViewsImpl(number_of_views, g_set_key_func);
184}
185
[email protected]ef916272009-07-08 21:40:55186} // namespace child_process_logging