Home | History | Annotate | Download | only in common
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      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 
      9 #include "base/string_util.h"
     10 #include "base/string_number_conversions.h"
     11 #include "base/stringprintf.h"
     12 #include "base/utf_string_conversions.h"
     13 #include "chrome/common/chrome_constants.h"
     14 #include "chrome/installer/util/google_update_settings.h"
     15 #include "content/common/gpu/gpu_info.h"
     16 #include "googleurl/src/gurl.h"
     17 
     18 namespace child_process_logging {
     19 // exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetActiveURL.
     20 typedef void (__cdecl *MainSetActiveURL)(const wchar_t*);
     21 
     22 // exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetClientId.
     23 typedef void (__cdecl *MainSetClientId)(const wchar_t*);
     24 
     25 // exported in breakpad_win.cc:
     26 //   void __declspec(dllexport) __cdecl SetNumberOfExtensions.
     27 typedef void (__cdecl *MainSetNumberOfExtensions)(int);
     28 
     29 // exported in breakpad_win.cc:
     30 // void __declspec(dllexport) __cdecl SetExtensionID.
     31 typedef void (__cdecl *MainSetExtensionID)(size_t, const wchar_t*);
     32 
     33 // exported in breakpad_win.cc: void __declspec(dllexport) __cdecl SetGpuInfo.
     34 typedef void (__cdecl *MainSetGpuInfo)(const wchar_t*, const wchar_t*,
     35                                        const wchar_t*, const wchar_t*,
     36                                        const wchar_t*);
     37 
     38 // exported in breakpad_win.cc:
     39 //   void __declspec(dllexport) __cdecl SetNumberOfViews.
     40 typedef void (__cdecl *MainSetNumberOfViews)(int);
     41 
     42 void SetActiveURL(const GURL& url) {
     43   static MainSetActiveURL set_active_url = NULL;
     44   // note: benign race condition on set_active_url.
     45   if (!set_active_url) {
     46     HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
     47     if (!exe_module)
     48       return;
     49     set_active_url = reinterpret_cast<MainSetActiveURL>(
     50         GetProcAddress(exe_module, "SetActiveURL"));
     51     if (!set_active_url)
     52       return;
     53   }
     54 
     55   (set_active_url)(UTF8ToWide(url.possibly_invalid_spec()).c_str());
     56 }
     57 
     58 void SetClientId(const std::string& client_id) {
     59   std::string str(client_id);
     60   // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
     61   ReplaceSubstringsAfterOffset(&str, 0, "-", "");
     62 
     63   if (str.empty())
     64     return;
     65 
     66   std::wstring wstr = ASCIIToWide(str);
     67   std::wstring old_wstr;
     68   if (!GoogleUpdateSettings::GetMetricsId(&old_wstr) ||
     69       wstr != old_wstr)
     70     GoogleUpdateSettings::SetMetricsId(wstr);
     71 
     72   static MainSetClientId set_client_id = NULL;
     73   if (!set_client_id) {
     74     HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
     75     if (!exe_module)
     76       return;
     77     set_client_id = reinterpret_cast<MainSetClientId>(
     78         GetProcAddress(exe_module, "SetClientId"));
     79     if (!set_client_id)
     80       return;
     81   }
     82   (set_client_id)(wstr.c_str());
     83 }
     84 
     85 std::string GetClientId() {
     86   std::wstring wstr_client_id;
     87   if (GoogleUpdateSettings::GetMetricsId(&wstr_client_id))
     88     return WideToASCII(wstr_client_id);
     89   else
     90     return std::string();
     91 }
     92 
     93 void SetActiveExtensions(const std::set<std::string>& extension_ids) {
     94   static MainSetNumberOfExtensions set_number_of_extensions = NULL;
     95   if (!set_number_of_extensions) {
     96     HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
     97     if (!exe_module)
     98       return;
     99     set_number_of_extensions = reinterpret_cast<MainSetNumberOfExtensions>(
    100         GetProcAddress(exe_module, "SetNumberOfExtensions"));
    101     if (!set_number_of_extensions)
    102       return;
    103   }
    104 
    105   static MainSetExtensionID set_extension_id = NULL;
    106   if (!set_extension_id) {
    107     HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    108     if (!exe_module)
    109       return;
    110     set_extension_id = reinterpret_cast<MainSetExtensionID>(
    111         GetProcAddress(exe_module, "SetExtensionID"));
    112     if (!set_extension_id)
    113       return;
    114   }
    115 
    116   (set_number_of_extensions)(static_cast<int>(extension_ids.size()));
    117 
    118   std::set<std::string>::const_iterator iter = extension_ids.begin();
    119   for (size_t i = 0; i < kMaxReportedActiveExtensions; ++i) {
    120     if (iter != extension_ids.end()) {
    121       (set_extension_id)(i, ASCIIToWide(iter->c_str()).c_str());
    122       ++iter;
    123     } else {
    124       (set_extension_id)(i, L"");
    125     }
    126   }
    127 }
    128 
    129 void SetGpuInfo(const GPUInfo& gpu_info) {
    130   static MainSetGpuInfo set_gpu_info = NULL;
    131   if (!set_gpu_info) {
    132     HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    133     if (!exe_module)
    134       return;
    135     set_gpu_info = reinterpret_cast<MainSetGpuInfo>(
    136         GetProcAddress(exe_module, "SetGpuInfo"));
    137     if (!set_gpu_info)
    138       return;
    139   }
    140   (set_gpu_info)(
    141       base::StringPrintf(L"0x%04x", gpu_info.vendor_id).c_str(),
    142       base::StringPrintf(L"0x%04x", gpu_info.device_id).c_str(),
    143       UTF8ToUTF16(gpu_info.driver_version).c_str(),
    144       UTF8ToUTF16(gpu_info.pixel_shader_version).c_str(),
    145       UTF8ToUTF16(gpu_info.vertex_shader_version).c_str());
    146 }
    147 
    148 void SetNumberOfViews(int number_of_views) {
    149   static MainSetNumberOfViews set_number_of_views = NULL;
    150   if (!set_number_of_views) {
    151     HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
    152     if (!exe_module)
    153       return;
    154     set_number_of_views = reinterpret_cast<MainSetNumberOfViews>(
    155         GetProcAddress(exe_module, "SetNumberOfViews"));
    156     if (!set_number_of_views)
    157       return;
    158   }
    159   (set_number_of_views)(number_of_views);
    160 }
    161 
    162 }  // namespace child_process_logging
    163