1// Copyright 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 "chrome/browser/metrics/google_update_metrics_provider_win.h"
6
7#include "base/message_loop/message_loop.h"
8#include "base/task_runner_util.h"
9#include "components/metrics/proto/system_profile.pb.h"
10#include "content/public/browser/browser_thread.h"
11
12typedef metrics::SystemProfileProto::GoogleUpdate::ProductInfo ProductInfo;
13
14namespace {
15
16// Helper function for checking if this is an official build. Used instead of
17// the macro to allow checking for successful code compilation on non-official
18// builds.
19bool IsOfficialBuild() {
20#if defined(GOOGLE_CHROME_BUILD)
21  return true;
22#else
23  return false;
24#endif  // defined(GOOGLE_CHROME_BUILD)
25}
26
27void ProductDataToProto(const GoogleUpdateSettings::ProductData& product_data,
28                        ProductInfo* product_info) {
29  product_info->set_version(product_data.version);
30  product_info->set_last_update_success_timestamp(
31      product_data.last_success.ToTimeT());
32  product_info->set_last_error(product_data.last_error_code);
33  product_info->set_last_extra_error(product_data.last_extra_code);
34  if (ProductInfo::InstallResult_IsValid(product_data.last_result)) {
35    product_info->set_last_result(
36        static_cast<ProductInfo::InstallResult>(product_data.last_result));
37  }
38}
39
40}  // namespace
41
42GoogleUpdateMetricsProviderWin::GoogleUpdateMetricsProviderWin()
43    : weak_ptr_factory_(this) {
44}
45
46GoogleUpdateMetricsProviderWin::~GoogleUpdateMetricsProviderWin() {
47}
48
49void GoogleUpdateMetricsProviderWin::GetGoogleUpdateData(
50    const base::Closure& done_callback) {
51  if (!IsOfficialBuild()) {
52    base::MessageLoop::current()->PostTask(FROM_HERE, done_callback);
53    return;
54  }
55
56  // Schedules a task on a blocking pool thread to gather Google Update
57  // statistics (requires Registry reads).
58  base::PostTaskAndReplyWithResult(
59      content::BrowserThread::GetBlockingPool(),
60      FROM_HERE,
61      base::Bind(
62          &GoogleUpdateMetricsProviderWin::GetGoogleUpdateDataOnBlockingPool),
63      base::Bind(
64          &GoogleUpdateMetricsProviderWin::ReceiveGoogleUpdateData,
65          weak_ptr_factory_.GetWeakPtr(), done_callback));
66}
67
68void GoogleUpdateMetricsProviderWin::ProvideSystemProfileMetrics(
69    metrics::SystemProfileProto* system_profile_proto) {
70  if (!IsOfficialBuild())
71    return;
72
73  metrics::SystemProfileProto::GoogleUpdate* google_update =
74      system_profile_proto->mutable_google_update();
75
76  google_update->set_is_system_install(
77      google_update_metrics_.is_system_install);
78
79  if (!google_update_metrics_.last_started_automatic_update_check.is_null()) {
80    google_update->set_last_automatic_start_timestamp(
81        google_update_metrics_.last_started_automatic_update_check.ToTimeT());
82  }
83
84  if (!google_update_metrics_.last_checked.is_null()) {
85    google_update->set_last_update_check_timestamp(
86        google_update_metrics_.last_checked.ToTimeT());
87  }
88
89  if (!google_update_metrics_.google_update_data.version.empty()) {
90    ProductDataToProto(google_update_metrics_.google_update_data,
91                       google_update->mutable_google_update_status());
92  }
93
94  if (!google_update_metrics_.product_data.version.empty()) {
95    ProductDataToProto(google_update_metrics_.product_data,
96                       google_update->mutable_client_status());
97  }
98}
99
100GoogleUpdateMetricsProviderWin::GoogleUpdateMetrics::GoogleUpdateMetrics()
101    : is_system_install(false) {
102}
103
104GoogleUpdateMetricsProviderWin::GoogleUpdateMetrics::~GoogleUpdateMetrics() {
105}
106
107// static
108GoogleUpdateMetricsProviderWin::GoogleUpdateMetrics
109GoogleUpdateMetricsProviderWin::GetGoogleUpdateDataOnBlockingPool() {
110  GoogleUpdateMetrics google_update_metrics;
111
112  if (!IsOfficialBuild())
113    return google_update_metrics;
114
115  const bool is_system_install = GoogleUpdateSettings::IsSystemInstall();
116  google_update_metrics.is_system_install = is_system_install;
117  google_update_metrics.last_started_automatic_update_check =
118      GoogleUpdateSettings::GetGoogleUpdateLastStartedAU(is_system_install);
119  google_update_metrics.last_checked =
120      GoogleUpdateSettings::GetGoogleUpdateLastChecked(is_system_install);
121  GoogleUpdateSettings::GetUpdateDetailForGoogleUpdate(
122      is_system_install,
123      &google_update_metrics.google_update_data);
124  GoogleUpdateSettings::GetUpdateDetail(
125      is_system_install,
126      &google_update_metrics.product_data);
127  return google_update_metrics;
128}
129
130void GoogleUpdateMetricsProviderWin::ReceiveGoogleUpdateData(
131    const base::Closure& done_callback,
132    const GoogleUpdateMetrics& google_update_metrics) {
133  google_update_metrics_ = google_update_metrics;
134  done_callback.Run();
135}
136