[email protected] | 6724e59 | 2012-03-16 04:49:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ef91627 | 2009-07-08 21:40: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. |
| 4 | |
| 5 | #include "chrome/common/child_process_logging.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
[email protected] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 11 | #include "base/string_number_conversions.h" |
[email protected] | 317e270 | 2011-03-09 17:53:01 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 14 | #include "chrome/common/chrome_constants.h" |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 15 | #include "chrome/installer/util/google_update_settings.h" |
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 16 | #include "content/public/common/gpu_info.h" |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 17 | #include "googleurl/src/gurl.h" |
| 18 | |
| 19 | namespace child_process_logging { |
[email protected] | 603e1f3 | 2012-05-22 20:53:16 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
[email protected] | 192bae9 | 2009-10-21 18:41:44 | [diff] [blame] | 23 | // exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetActiveURL. |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 24 | typedef void (__cdecl *MainSetActiveURL)(const wchar_t*); |
| 25 | |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 26 | // exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetClientId. |
| 27 | typedef void (__cdecl *MainSetClientId)(const wchar_t*); |
| 28 | |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 29 | // exported in breakpad_win.cc: |
[email protected] | 80ddb04 | 2010-10-05 13:50:06 | [diff] [blame] | 30 | // void __declspec(dllexport) __cdecl SetNumberOfExtensions. |
| 31 | typedef void (__cdecl *MainSetNumberOfExtensions)(int); |
| 32 | |
| 33 | // exported in breakpad_win.cc: |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 34 | // void __declspec(dllexport) __cdecl SetExtensionID. |
| 35 | typedef void (__cdecl *MainSetExtensionID)(size_t, const wchar_t*); |
| 36 | |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 37 | // exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetGpuInfo. |
| 38 | typedef void (__cdecl *MainSetGpuInfo)(const wchar_t*, const wchar_t*, |
| 39 | const wchar_t*, const wchar_t*, |
| 40 | const wchar_t*); |
| 41 | |
[email protected] | 3231c2e | 2010-09-02 12:41:05 | [diff] [blame] | 42 | // exported in breakpad_win.cc: |
[email protected] | 6724e59 | 2012-03-16 04:49:14 | [diff] [blame] | 43 | // void __declspec(dllexport) __cdecl SetPrinterInfo. |
| 44 | typedef void (__cdecl *MainSetPrinterInfo)(const wchar_t*); |
| 45 | |
| 46 | // exported in breakpad_win.cc: |
[email protected] | 3231c2e | 2010-09-02 12:41:05 | [diff] [blame] | 47 | // void __declspec(dllexport) __cdecl SetNumberOfViews. |
| 48 | typedef void (__cdecl *MainSetNumberOfViews)(int); |
| 49 | |
[email protected] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 50 | // exported in breakpad_win.cc: |
[email protected] | dc1b5a6 | 2012-05-25 00:08:09 | [diff] [blame] | 51 | // void __declspec(dllexport) __cdecl SetCommandLine2 |
[email protected] | 603e1f3 | 2012-05-22 20:53:16 | [diff] [blame] | 52 | typedef void (__cdecl *MainSetCommandLine)(const wchar_t**, size_t); |
[email protected] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 53 | |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 54 | // exported in breakpad_field_trial_win.cc: |
[email protected] | dc1b5a6 | 2012-05-25 00:08:09 | [diff] [blame] | 55 | // void __declspec(dllexport) __cdecl SetExperimentList2 |
[email protected] | 603e1f3 | 2012-05-22 20:53:16 | [diff] [blame] | 56 | typedef void (__cdecl *MainSetExperimentList)(const wchar_t**, size_t); |
| 57 | |
| 58 | // Copied from breakpad_win.cc. |
| 59 | void 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] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 68 | |
[email protected] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 69 | void SetActiveURL(const GURL& url) { |
[email protected] | 192bae9 | 2009-10-21 18:41:44 | [diff] [blame] | 70 | 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] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 81 | |
| 82 | (set_active_url)(UTF8ToWide(url.possibly_invalid_spec()).c_str()); |
| 83 | } |
| 84 | |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 85 | void 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] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 100 | // note: benign race condition on set_client_id. |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 101 | 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] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 113 | std::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] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 121 | void SetActiveExtensions(const std::set<std::string>& extension_ids) { |
[email protected] | 80ddb04 | 2010-10-05 13:50:06 | [diff] [blame] | 122 | static MainSetNumberOfExtensions set_number_of_extensions = NULL; |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 123 | // note: benign race condition on set_number_of_extensions. |
[email protected] | 80ddb04 | 2010-10-05 13:50:06 | [diff] [blame] | 124 | 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] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 134 | static MainSetExtensionID set_extension_id = NULL; |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 135 | // note: benign race condition on set_extension_id. |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 136 | 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] | 80ddb04 | 2010-10-05 13:50:06 | [diff] [blame] | 146 | (set_number_of_extensions)(static_cast<int>(extension_ids.size())); |
| 147 | |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 148 | std::set<std::string>::const_iterator iter = extension_ids.begin(); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 149 | for (size_t i = 0; i < kMaxReportedActiveExtensions; ++i) { |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 150 | if (iter != extension_ids.end()) { |
| 151 | (set_extension_id)(i, ASCIIToWide(iter->c_str()).c_str()); |
| 152 | ++iter; |
| 153 | } else { |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 154 | (set_extension_id)(i, L""); |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 155 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 159 | void SetGpuInfo(const content::GPUInfo& gpu_info) { |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 160 | static MainSetGpuInfo set_gpu_info = NULL; |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 161 | // note: benign race condition on set_gpu_info. |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 162 | 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] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 171 | (set_gpu_info)( |
[email protected] | a094e2c | 2012-05-10 23:02:42 | [diff] [blame] | 172 | 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] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 174 | UTF8ToUTF16(gpu_info.driver_version).c_str(), |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 175 | UTF8ToUTF16(gpu_info.pixel_shader_version).c_str(), |
| 176 | UTF8ToUTF16(gpu_info.vertex_shader_version).c_str()); |
[email protected] | a110dd1 | 2010-07-19 07:51:33 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 6724e59 | 2012-03-16 04:49:14 | [diff] [blame] | 179 | void SetPrinterInfo(const char* printer_info) { |
| 180 | static MainSetPrinterInfo set_printer_info = NULL; |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 181 | // note: benign race condition on set_printer_info. |
[email protected] | 6724e59 | 2012-03-16 04:49:14 | [diff] [blame] | 182 | 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] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 194 | void SetCommandLine(const CommandLine* command_line) { |
| 195 | static MainSetCommandLine set_command_line = NULL; |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 196 | // note: benign race condition on set_command_line. |
[email protected] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 197 | 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] | dc1b5a6 | 2012-05-25 00:08:09 | [diff] [blame] | 202 | GetProcAddress(exe_module, "SetCommandLine2")); |
[email protected] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 203 | if (!set_command_line) |
| 204 | return; |
| 205 | } |
[email protected] | 603e1f3 | 2012-05-22 20:53:16 | [diff] [blame] | 206 | |
| 207 | std::vector<const wchar_t*> cstrings; |
| 208 | StringVectorToCStringVector(command_line->argv(), &cstrings); |
| 209 | (set_command_line)(&cstrings[0], cstrings.size()); |
[email protected] | 783f06f | 2011-09-14 01:36:11 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 212 | void 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] | dc1b5a6 | 2012-05-25 00:08:09 | [diff] [blame] | 220 | GetProcAddress(exe_module, "SetExperimentList2")); |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 221 | if (!set_experiment_list) |
| 222 | return; |
| 223 | } |
[email protected] | 603e1f3 | 2012-05-22 20:53:16 | [diff] [blame] | 224 | |
| 225 | std::vector<const wchar_t*> cstrings; |
| 226 | StringVectorToCStringVector(state, &cstrings); |
| 227 | (set_experiment_list)(&cstrings[0], cstrings.size()); |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 3231c2e | 2010-09-02 12:41:05 | [diff] [blame] | 230 | void SetNumberOfViews(int number_of_views) { |
| 231 | static MainSetNumberOfViews set_number_of_views = NULL; |
[email protected] | 4cb3c1fc | 2012-04-05 07:23:48 | [diff] [blame] | 232 | // note: benign race condition on set_number_of_views. |
[email protected] | 3231c2e | 2010-09-02 12:41:05 | [diff] [blame] | 233 | 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] | ef91627 | 2009-07-08 21:40:55 | [diff] [blame] | 245 | } // namespace child_process_logging |