Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / installer / util / uninstall_metrics.cc
blobda8ca03518c5e5e753bd07b917158587537f9771
1 // Copyright 2013 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.
5 #include "chrome/installer/util/uninstall_metrics.h"
7 #include <string>
9 #include "base/files/file_path.h"
10 #include "base/json/json_file_value_serializer.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/installer/util/util_constants.h"
17 namespace installer {
19 namespace {
21 // Given a DictionaryValue containing a set of uninstall metrics,
22 // this builds a URL parameter list of all the contained metrics.
23 // Returns true if at least one uninstall metric was found in
24 // uninstall_metrics_dict, false otherwise.
25 bool BuildUninstallMetricsString(
26 const base::DictionaryValue* uninstall_metrics_dict,
27 base::string16* metrics) {
28 DCHECK(NULL != metrics);
29 bool has_values = false;
31 for (base::DictionaryValue::Iterator iter(*uninstall_metrics_dict);
32 !iter.IsAtEnd();
33 iter.Advance()) {
34 has_values = true;
35 metrics->append(L"&");
36 metrics->append(base::UTF8ToWide(iter.key()));
37 metrics->append(L"=");
39 std::string value;
40 iter.value().GetAsString(&value);
41 metrics->append(base::UTF8ToWide(value));
44 return has_values;
47 } // namespace
49 bool ExtractUninstallMetrics(const base::DictionaryValue& root,
50 base::string16* uninstall_metrics_string) {
51 // Make sure that the user wants us reporting metrics. If not, don't
52 // add our uninstall metrics.
53 bool metrics_reporting_enabled = false;
54 if (!root.GetBoolean(prefs::kMetricsReportingEnabled,
55 &metrics_reporting_enabled) ||
56 !metrics_reporting_enabled) {
57 return false;
60 const base::DictionaryValue* uninstall_metrics_dict = NULL;
61 if (!root.HasKey(installer::kUninstallMetricsName) ||
62 !root.GetDictionary(installer::kUninstallMetricsName,
63 &uninstall_metrics_dict)) {
64 return false;
67 if (!BuildUninstallMetricsString(uninstall_metrics_dict,
68 uninstall_metrics_string)) {
69 return false;
72 return true;
75 bool ExtractUninstallMetricsFromFile(const base::FilePath& file_path,
76 base::string16* uninstall_metrics_string) {
77 JSONFileValueSerializer json_serializer(file_path);
79 std::string json_error_string;
80 scoped_ptr<base::Value> root(json_serializer.Deserialize(NULL, NULL));
81 if (!root.get())
82 return false;
84 // Preferences should always have a dictionary root.
85 if (!root->IsType(base::Value::TYPE_DICTIONARY))
86 return false;
88 return ExtractUninstallMetrics(
89 *static_cast<base::DictionaryValue*>(root.get()),
90 uninstall_metrics_string);
93 } // namespace installer