blob: 36a37d41eabc25f3e9e53220e4fab5ce37de1d0f [file] [log] [blame]
[email protected]6724e592012-03-16 04:49:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ef916272009-07-08 21:40:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/common/child_process_logging.h"
6
7#include <windows.h>
8
[email protected]783f06f2011-09-14 01:36:119#include "base/command_line.h"
[email protected]ef916272009-07-08 21:40:5510#include "base/string_util.h"
[email protected]528c56d2010-07-30 19:28:4411#include "base/string_number_conversions.h"
[email protected]317e2702011-03-09 17:53:0112#include "base/stringprintf.h"
[email protected]d55194ca2010-03-11 18:25:4513#include "base/utf_string_conversions.h"
[email protected]ef916272009-07-08 21:40:5514#include "chrome/common/chrome_constants.h"
[email protected]157d5472009-11-05 22:31:0315#include "chrome/installer/util/google_update_settings.h"
[email protected]a80f5ece2011-10-20 23:56:5516#include "content/public/common/gpu_info.h"
[email protected]ef916272009-07-08 21:40:5517#include "googleurl/src/gurl.h"
18
19namespace child_process_logging {
[email protected]603e1f32012-05-22 20:53:1620
21namespace {
22
[email protected]192bae92009-10-21 18:41:4423// exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetActiveURL.
[email protected]ef916272009-07-08 21:40:5524typedef void (__cdecl *MainSetActiveURL)(const wchar_t*);
25
[email protected]157d5472009-11-05 22:31:0326// exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetClientId.
27typedef void (__cdecl *MainSetClientId)(const wchar_t*);
28
[email protected]aab98a52009-12-02 03:22:3529// exported in breakpad_win.cc:
[email protected]80ddb042010-10-05 13:50:0630// void __declspec(dllexport) __cdecl SetNumberOfExtensions.
31typedef void (__cdecl *MainSetNumberOfExtensions)(int);
32
33// exported in breakpad_win.cc:
[email protected]aab98a52009-12-02 03:22:3534// void __declspec(dllexport) __cdecl SetExtensionID.
35typedef void (__cdecl *MainSetExtensionID)(size_t, const wchar_t*);
36
[email protected]a110dd12010-07-19 07:51:3337// exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetGpuInfo.
38typedef void (__cdecl *MainSetGpuInfo)(const wchar_t*, const wchar_t*,
39 const wchar_t*, const wchar_t*,
40 const wchar_t*);
41
[email protected]3231c2e2010-09-02 12:41:0542// exported in breakpad_win.cc:
[email protected]6724e592012-03-16 04:49:1443// void __declspec(dllexport) __cdecl SetPrinterInfo.
44typedef void (__cdecl *MainSetPrinterInfo)(const wchar_t*);
45
46// exported in breakpad_win.cc:
[email protected]3231c2e2010-09-02 12:41:0547// void __declspec(dllexport) __cdecl SetNumberOfViews.
48typedef void (__cdecl *MainSetNumberOfViews)(int);
49
[email protected]783f06f2011-09-14 01:36:1150// exported in breakpad_win.cc:
[email protected]dc1b5a62012-05-25 00:08:0951// void __declspec(dllexport) __cdecl SetCommandLine2
[email protected]603e1f32012-05-22 20:53:1652typedef void (__cdecl *MainSetCommandLine)(const wchar_t**, size_t);
[email protected]783f06f2011-09-14 01:36:1153
[email protected]4cb3c1fc2012-04-05 07:23:4854// exported in breakpad_field_trial_win.cc:
[email protected]dc1b5a62012-05-25 00:08:0955// void __declspec(dllexport) __cdecl SetExperimentList2
[email protected]603e1f32012-05-22 20:53:1656typedef void (__cdecl *MainSetExperimentList)(const wchar_t**, size_t);
57
58// Copied from breakpad_win.cc.
59void StringVectorToCStringVector(const std::vector<std::wstring>& wstrings,
60 std::vector<const wchar_t*>* cstrings) {
61 cstrings->clear();
62 cstrings->reserve(wstrings.size());
63 for (size_t i = 0; i < wstrings.size(); ++i)
64 cstrings->push_back(wstrings[i].c_str());
65}
66
67} // namespace
[email protected]4cb3c1fc2012-04-05 07:23:4868
[email protected]ef916272009-07-08 21:40:5569void SetActiveURL(const GURL& url) {
[email protected]192bae92009-10-21 18:41:4470 static MainSetActiveURL set_active_url = NULL;
71 // note: benign race condition on set_active_url.
72 if (!set_active_url) {
73 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
74 if (!exe_module)
75 return;
76 set_active_url = reinterpret_cast<MainSetActiveURL>(
77 GetProcAddress(exe_module, "SetActiveURL"));
78 if (!set_active_url)
79 return;
80 }
[email protected]ef916272009-07-08 21:40:5581
82 (set_active_url)(UTF8ToWide(url.possibly_invalid_spec()).c_str());
83}
84
[email protected]157d5472009-11-05 22:31:0385void SetClientId(const std::string& client_id) {
86 std::string str(client_id);
87 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
88 ReplaceSubstringsAfterOffset(&str, 0, "-", "");
89
90 if (str.empty())
91 return;
92
93 std::wstring wstr = ASCIIToWide(str);
94 std::wstring old_wstr;
95 if (!GoogleUpdateSettings::GetMetricsId(&old_wstr) ||
96 wstr != old_wstr)
97 GoogleUpdateSettings::SetMetricsId(wstr);
98
99 static MainSetClientId set_client_id = NULL;
[email protected]4cb3c1fc2012-04-05 07:23:48100 // note: benign race condition on set_client_id.
[email protected]157d5472009-11-05 22:31:03101 if (!set_client_id) {
102 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
103 if (!exe_module)
104 return;
105 set_client_id = reinterpret_cast<MainSetClientId>(
106 GetProcAddress(exe_module, "SetClientId"));
107 if (!set_client_id)
108 return;
109 }
110 (set_client_id)(wstr.c_str());
111}
112
[email protected]6dde9d72010-08-26 08:55:22113std::string GetClientId() {
114 std::wstring wstr_client_id;
115 if (GoogleUpdateSettings::GetMetricsId(&wstr_client_id))
116 return WideToASCII(wstr_client_id);
117 else
118 return std::string();
119}
120
[email protected]c8865962009-12-16 07:47:39121void SetActiveExtensions(const std::set<std::string>& extension_ids) {
[email protected]80ddb042010-10-05 13:50:06122 static MainSetNumberOfExtensions set_number_of_extensions = NULL;
[email protected]4cb3c1fc2012-04-05 07:23:48123 // note: benign race condition on set_number_of_extensions.
[email protected]80ddb042010-10-05 13:50:06124 if (!set_number_of_extensions) {
125 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
126 if (!exe_module)
127 return;
128 set_number_of_extensions = reinterpret_cast<MainSetNumberOfExtensions>(
129 GetProcAddress(exe_module, "SetNumberOfExtensions"));
130 if (!set_number_of_extensions)
131 return;
132 }
133
[email protected]aab98a52009-12-02 03:22:35134 static MainSetExtensionID set_extension_id = NULL;
[email protected]4cb3c1fc2012-04-05 07:23:48135 // note: benign race condition on set_extension_id.
[email protected]aab98a52009-12-02 03:22:35136 if (!set_extension_id) {
137 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
138 if (!exe_module)
139 return;
140 set_extension_id = reinterpret_cast<MainSetExtensionID>(
141 GetProcAddress(exe_module, "SetExtensionID"));
142 if (!set_extension_id)
143 return;
144 }
145
[email protected]80ddb042010-10-05 13:50:06146 (set_number_of_extensions)(static_cast<int>(extension_ids.size()));
147
[email protected]c8865962009-12-16 07:47:39148 std::set<std::string>::const_iterator iter = extension_ids.begin();
[email protected]aab98a52009-12-02 03:22:35149 for (size_t i = 0; i < kMaxReportedActiveExtensions; ++i) {
[email protected]c8865962009-12-16 07:47:39150 if (iter != extension_ids.end()) {
151 (set_extension_id)(i, ASCIIToWide(iter->c_str()).c_str());
152 ++iter;
153 } else {
[email protected]aab98a52009-12-02 03:22:35154 (set_extension_id)(i, L"");
[email protected]c8865962009-12-16 07:47:39155 }
[email protected]aab98a52009-12-02 03:22:35156 }
157}
158
[email protected]a80f5ece2011-10-20 23:56:55159void SetGpuInfo(const content::GPUInfo& gpu_info) {
[email protected]a110dd12010-07-19 07:51:33160 static MainSetGpuInfo set_gpu_info = NULL;
[email protected]4cb3c1fc2012-04-05 07:23:48161 // note: benign race condition on set_gpu_info.
[email protected]a110dd12010-07-19 07:51:33162 if (!set_gpu_info) {
163 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
164 if (!exe_module)
165 return;
166 set_gpu_info = reinterpret_cast<MainSetGpuInfo>(
167 GetProcAddress(exe_module, "SetGpuInfo"));
168 if (!set_gpu_info)
169 return;
170 }
[email protected]528c56d2010-07-30 19:28:44171 (set_gpu_info)(
[email protected]a094e2c2012-05-10 23:02:42172 base::StringPrintf(L"0x%04x", gpu_info.gpu.vendor_id).c_str(),
173 base::StringPrintf(L"0x%04x", gpu_info.gpu.device_id).c_str(),
[email protected]a61508e52011-03-08 17:59:42174 UTF8ToUTF16(gpu_info.driver_version).c_str(),
[email protected]6cc8ece62011-03-14 20:05:47175 UTF8ToUTF16(gpu_info.pixel_shader_version).c_str(),
176 UTF8ToUTF16(gpu_info.vertex_shader_version).c_str());
[email protected]a110dd12010-07-19 07:51:33177}
178
[email protected]6724e592012-03-16 04:49:14179void SetPrinterInfo(const char* printer_info) {
180 static MainSetPrinterInfo set_printer_info = NULL;
[email protected]4cb3c1fc2012-04-05 07:23:48181 // note: benign race condition on set_printer_info.
[email protected]6724e592012-03-16 04:49:14182 if (!set_printer_info) {
183 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
184 if (!exe_module)
185 return;
186 set_printer_info = reinterpret_cast<MainSetPrinterInfo>(
187 GetProcAddress(exe_module, "SetPrinterInfo"));
188 if (!set_printer_info)
189 return;
190 }
191 (set_printer_info)(UTF8ToWide(printer_info).c_str());
192}
193
[email protected]783f06f2011-09-14 01:36:11194void SetCommandLine(const CommandLine* command_line) {
195 static MainSetCommandLine set_command_line = NULL;
[email protected]4cb3c1fc2012-04-05 07:23:48196 // note: benign race condition on set_command_line.
[email protected]783f06f2011-09-14 01:36:11197 if (!set_command_line) {
198 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
199 if (!exe_module)
200 return;
201 set_command_line = reinterpret_cast<MainSetCommandLine>(
[email protected]dc1b5a62012-05-25 00:08:09202 GetProcAddress(exe_module, "SetCommandLine2"));
[email protected]783f06f2011-09-14 01:36:11203 if (!set_command_line)
204 return;
205 }
[email protected]603e1f32012-05-22 20:53:16206
207 std::vector<const wchar_t*> cstrings;
208 StringVectorToCStringVector(command_line->argv(), &cstrings);
209 (set_command_line)(&cstrings[0], cstrings.size());
[email protected]783f06f2011-09-14 01:36:11210}
211
[email protected]4cb3c1fc2012-04-05 07:23:48212void SetExperimentList(const std::vector<string16>& state) {
213 static MainSetExperimentList set_experiment_list = NULL;
214 // note: benign race condition on set_experiment_list.
215 if (!set_experiment_list) {
216 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
217 if (!exe_module)
218 return;
219 set_experiment_list = reinterpret_cast<MainSetExperimentList>(
[email protected]dc1b5a62012-05-25 00:08:09220 GetProcAddress(exe_module, "SetExperimentList2"));
[email protected]4cb3c1fc2012-04-05 07:23:48221 if (!set_experiment_list)
222 return;
223 }
[email protected]603e1f32012-05-22 20:53:16224
225 std::vector<const wchar_t*> cstrings;
226 StringVectorToCStringVector(state, &cstrings);
227 (set_experiment_list)(&cstrings[0], cstrings.size());
[email protected]4cb3c1fc2012-04-05 07:23:48228}
229
[email protected]3231c2e2010-09-02 12:41:05230void SetNumberOfViews(int number_of_views) {
231 static MainSetNumberOfViews set_number_of_views = NULL;
[email protected]4cb3c1fc2012-04-05 07:23:48232 // note: benign race condition on set_number_of_views.
[email protected]3231c2e2010-09-02 12:41:05233 if (!set_number_of_views) {
234 HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
235 if (!exe_module)
236 return;
237 set_number_of_views = reinterpret_cast<MainSetNumberOfViews>(
238 GetProcAddress(exe_module, "SetNumberOfViews"));
239 if (!set_number_of_views)
240 return;
241 }
242 (set_number_of_views)(number_of_views);
243}
244
[email protected]ef916272009-07-08 21:40:55245} // namespace child_process_logging