OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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 "components/browser_watcher/watcher_metrics_provider_win.h" |
| 6 |
| 7 #include "base/metrics/sparse_histogram.h" |
| 8 #include "base/process/process_handle.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/win/registry.h" |
| 11 |
| 12 namespace browser_watcher { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // This function does soft matching on the PID recorded in the key only. |
| 17 // Due to PID reuse, the possibility exists that the process that's now live |
| 18 // is not the same process the data was recorded for. This doesn't matter for |
| 19 // this purpose, as eventually the data will be scavenged and reported. |
| 20 bool IsLiveProcess(const base::string16& key_name) { |
| 21 int pid = 0; |
| 22 base::StringToInt(key_name, &pid); |
| 23 |
| 24 if (pid == 0) |
| 25 return false; |
| 26 |
| 27 // This is a very inexpensive check for the common case of our own PID. |
| 28 if (static_cast<base::ProcessId>(pid) == base::GetCurrentProcId()) |
| 29 return true; |
| 30 |
| 31 // The process is not our own - see whether a process with this PID exists. |
| 32 base::ProcessHandle process = base::kNullProcessHandle; |
| 33 if (base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { |
| 34 base::CloseProcessHandle(process); |
| 35 |
| 36 return true; |
| 37 } |
| 38 |
| 39 return false; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] = |
| 45 "Stability.BrowserExitCodes"; |
| 46 |
| 47 |
| 48 WatcherMetricsProviderWin::WatcherMetricsProviderWin( |
| 49 const base::string16& registry_path) : registry_path_(registry_path) { |
| 50 } |
| 51 |
| 52 void WatcherMetricsProviderWin::ProvideStabilityMetrics( |
| 53 metrics::SystemProfileProto* system_profile_proto) { |
| 54 base::win::RegKey regkey(HKEY_CURRENT_USER, |
| 55 registry_path_.c_str(), |
| 56 KEY_QUERY_VALUE | KEY_SET_VALUE); |
| 57 |
| 58 std::vector<base::string16> to_delete; |
| 59 size_t num = regkey.GetValueCount(); |
| 60 |
| 61 // Record the exit codes in a sparse histogram, as the range of values |
| 62 // used to report failures is large. |
| 63 base::HistogramBase* exit_code_histogram = |
| 64 // TODO(siggi): is this an acceptable name? |
| 65 base::SparseHistogram::FactoryGet(kBrowserExitCodeHistogramName, |
| 66 base::HistogramBase::kUmaStabilityHistogramFlag); |
| 67 |
| 68 for (size_t i = 0; i < num; ++i) { |
| 69 base::string16 name; |
| 70 if (regkey.GetValueNameAt(i, &name) == ERROR_SUCCESS) { |
| 71 DWORD exit_code = 0; |
| 72 if (regkey.ReadValueDW(name.c_str(), &exit_code) == ERROR_SUCCESS) { |
| 73 // Do not report exit codes for processes that are still live, notably |
| 74 // not for our own process. |
| 75 if (exit_code != STILL_ACTIVE || !IsLiveProcess(name)) { |
| 76 to_delete.push_back(name); |
| 77 exit_code_histogram->Add(exit_code); |
| 78 } |
| 79 } |
| 80 } |
| 81 } |
| 82 |
| 83 // Delete the values reported above. |
| 84 for (size_t i = 0; i < to_delete.size(); ++i) |
| 85 regkey.DeleteValue(to_delete[i].c_str()); |
| 86 } |
| 87 |
| 88 } // namespace browser_watcher |
OLD | NEW |