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"
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"
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
);
35 metrics
->append(L
"&");
36 metrics
->append(base::UTF8ToWide(iter
.key()));
37 metrics
->append(L
"=");
40 iter
.value().GetAsString(&value
);
41 metrics
->append(base::UTF8ToWide(value
));
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
) {
60 const base::DictionaryValue
* uninstall_metrics_dict
= NULL
;
61 if (!root
.HasKey(installer::kUninstallMetricsName
) ||
62 !root
.GetDictionary(installer::kUninstallMetricsName
,
63 &uninstall_metrics_dict
)) {
67 if (!BuildUninstallMetricsString(uninstall_metrics_dict
,
68 uninstall_metrics_string
)) {
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
));
84 // Preferences should always have a dictionary root.
85 if (!root
->IsType(base::Value::TYPE_DICTIONARY
))
88 return ExtractUninstallMetrics(
89 *static_cast<base::DictionaryValue
*>(root
.get()),
90 uninstall_metrics_string
);
93 } // namespace installer