summaryrefslogtreecommitdiffstats
path: root/chrome/common/child_process_logging_win.cc
blob: 18fe62a67d12038fb5ee40172b5e31cf04009c9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/child_process_logging.h"

#include <windows.h>

#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/metrics/variations/variations_util.h"
#include "chrome/installer/util/google_update_settings.h"
#include "googleurl/src/gurl.h"
#include "gpu/config/gpu_info.h"

namespace child_process_logging {

namespace {

// exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetActiveURL.
typedef void (__cdecl *MainSetActiveURL)(const wchar_t*);

// exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetClientId.
typedef void (__cdecl *MainSetClientId)(const wchar_t*);

// exported in breakpad_win.cc:
//   void __declspec(dllexport) __cdecl SetNumberOfExtensions.
typedef void (__cdecl *MainSetNumberOfExtensions)(int);

// exported in breakpad_win.cc:
// void __declspec(dllexport) __cdecl SetExtensionID.
typedef void (__cdecl *MainSetExtensionID)(size_t, const wchar_t*);

// exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetGpuInfo.
typedef void (__cdecl *MainSetGpuInfo)(const wchar_t*, const wchar_t*,
                                       const wchar_t*, const wchar_t*,
                                       const wchar_t*);

// exported in breakpad_win.cc:
//     void __declspec(dllexport) __cdecl SetPrinterInfo.
typedef void (__cdecl *MainSetPrinterInfo)(const wchar_t*);

// exported in breakpad_win.cc:
//   void __declspec(dllexport) __cdecl SetNumberOfViews.
typedef void (__cdecl *MainSetNumberOfViews)(int);

// exported in breakpad_win.cc:
//   void __declspec(dllexport) __cdecl SetCommandLine2
typedef void (__cdecl *MainSetCommandLine)(const wchar_t**, size_t);

// exported in breakpad_field_trial_win.cc:
//   void __declspec(dllexport) __cdecl SetExperimentList3
typedef void (__cdecl *MainSetExperimentList)(const wchar_t**, size_t, size_t);

// Copied from breakpad_win.cc.
void StringVectorToCStringVector(const std::vector<std::wstring>& wstrings,
                                 std::vector<const wchar_t*>* cstrings) {
  cstrings->clear();
  cstrings->reserve(wstrings.size());
  for (size_t i = 0; i < wstrings.size(); ++i)
    cstrings->push_back(wstrings[i].c_str());
}

}  // namespace

void SetActiveURL(const GURL& url) {
  static MainSetActiveURL set_active_url = NULL;
  // note: benign race condition on set_active_url.
  if (!set_active_url) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_active_url = reinterpret_cast<MainSetActiveURL>(
        GetProcAddress(exe_module, "SetActiveURL"));
    if (!set_active_url)
      return;
  }

  (set_active_url)(UTF8ToWide(url.possibly_invalid_spec()).c_str());
}

void SetClientId(const std::string& client_id) {
  std::string str(client_id);
  // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
  ReplaceSubstringsAfterOffset(&str, 0, "-", "");

  if (str.empty())
    return;

  std::wstring wstr = ASCIIToWide(str);
  std::wstring old_wstr;
  if (!GoogleUpdateSettings::GetMetricsId(&old_wstr) ||
      wstr != old_wstr)
    GoogleUpdateSettings::SetMetricsId(wstr);

  static MainSetClientId set_client_id = NULL;
  // note: benign race condition on set_client_id.
  if (!set_client_id) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_client_id = reinterpret_cast<MainSetClientId>(
        GetProcAddress(exe_module, "SetClientId"));
    if (!set_client_id)
      return;
  }
  (set_client_id)(wstr.c_str());
}

std::string GetClientId() {
  std::wstring wstr_client_id;
  if (GoogleUpdateSettings::GetMetricsId(&wstr_client_id))
    return WideToASCII(wstr_client_id);
  else
    return std::string();
}

void SetActiveExtensions(const std::set<std::string>& extension_ids) {
  static MainSetNumberOfExtensions set_number_of_extensions = NULL;
  // note: benign race condition on set_number_of_extensions.
  if (!set_number_of_extensions) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_number_of_extensions = reinterpret_cast<MainSetNumberOfExtensions>(
        GetProcAddress(exe_module, "SetNumberOfExtensions"));
    if (!set_number_of_extensions)
      return;
  }

  static MainSetExtensionID set_extension_id = NULL;
  // note: benign race condition on set_extension_id.
  if (!set_extension_id) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_extension_id = reinterpret_cast<MainSetExtensionID>(
        GetProcAddress(exe_module, "SetExtensionID"));
    if (!set_extension_id)
      return;
  }

  (set_number_of_extensions)(static_cast<int>(extension_ids.size()));

  std::set<std::string>::const_iterator iter = extension_ids.begin();
  for (size_t i = 0; i < kMaxReportedActiveExtensions; ++i) {
    if (iter != extension_ids.end()) {
      (set_extension_id)(i, ASCIIToWide(iter->c_str()).c_str());
      ++iter;
    } else {
      (set_extension_id)(i, L"");
    }
  }
}

void SetGpuInfo(const gpu::GPUInfo& gpu_info) {
  static MainSetGpuInfo set_gpu_info = NULL;
  // note: benign race condition on set_gpu_info.
  if (!set_gpu_info) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_gpu_info = reinterpret_cast<MainSetGpuInfo>(
        GetProcAddress(exe_module, "SetGpuInfo"));
    if (!set_gpu_info)
      return;
  }
  (set_gpu_info)(
      base::StringPrintf(L"0x%04x", gpu_info.gpu.vendor_id).c_str(),
      base::StringPrintf(L"0x%04x", gpu_info.gpu.device_id).c_str(),
      UTF8ToUTF16(gpu_info.driver_version).c_str(),
      UTF8ToUTF16(gpu_info.pixel_shader_version).c_str(),
      UTF8ToUTF16(gpu_info.vertex_shader_version).c_str());
}

void SetPrinterInfo(const char* printer_info) {
  static MainSetPrinterInfo set_printer_info = NULL;
  // note: benign race condition on set_printer_info.
  if (!set_printer_info) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_printer_info = reinterpret_cast<MainSetPrinterInfo>(
        GetProcAddress(exe_module, "SetPrinterInfo"));
    if (!set_printer_info)
      return;
  }
  (set_printer_info)(UTF8ToWide(printer_info).c_str());
}

void SetCommandLine(const CommandLine* command_line) {
  static MainSetCommandLine set_command_line = NULL;
  // note: benign race condition on set_command_line.
  if (!set_command_line) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_command_line = reinterpret_cast<MainSetCommandLine>(
        GetProcAddress(exe_module, "SetCommandLine2"));
    if (!set_command_line)
      return;
  }

  if (command_line->argv().empty())
    return;

  std::vector<const wchar_t*> cstrings;
  StringVectorToCStringVector(command_line->argv(), &cstrings);
  (set_command_line)(&cstrings[0], cstrings.size());
}

void SetExperimentList(const std::vector<string16>& experiments) {
  static MainSetExperimentList set_experiment_list = NULL;
  // note: benign race condition on set_experiment_list.
  if (!set_experiment_list) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_experiment_list = reinterpret_cast<MainSetExperimentList>(
        GetProcAddress(exe_module, "SetExperimentList3"));
    if (!set_experiment_list)
      return;
  }

  std::vector<string16> chunks;
  chrome_variations::GenerateVariationChunks(experiments, &chunks);

  // If the list is empty, notify the child process of the number of experiments
  // and exit early.
  if (chunks.empty()) {
    (set_experiment_list)(NULL, 0, 0);
    return;
  }

  std::vector<const wchar_t*> cstrings;
  StringVectorToCStringVector(chunks, &cstrings);
  (set_experiment_list)(&cstrings[0], cstrings.size(), experiments.size());
}

void SetNumberOfViews(int number_of_views) {
  static MainSetNumberOfViews set_number_of_views = NULL;
  // note: benign race condition on set_number_of_views.
  if (!set_number_of_views) {
    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    if (!exe_module)
      return;
    set_number_of_views = reinterpret_cast<MainSetNumberOfViews>(
        GetProcAddress(exe_module, "SetNumberOfViews"));
    if (!set_number_of_views)
      return;
  }
  (set_number_of_views)(number_of_views);
}

}  // namespace child_process_logging