[Android] Added UMA for search by image context menu.
[chromium-blink-merge.git] / chrome / installer / util / uninstall_metrics.cc
blob94219f6886f48a295a7a803da462ae338ba2c8b3
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 DictionaryValue* uninstall_metrics_dict, string16* metrics) {
27 DCHECK(NULL != metrics);
28 bool has_values = false;
30 for (DictionaryValue::Iterator iter(*uninstall_metrics_dict); !iter.IsAtEnd();
31 iter.Advance()) {
32 has_values = true;
33 metrics->append(L"&");
34 metrics->append(UTF8ToWide(iter.key()));
35 metrics->append(L"=");
37 std::string value;
38 iter.value().GetAsString(&value);
39 metrics->append(UTF8ToWide(value));
42 return has_values;
45 } // namespace
47 bool ExtractUninstallMetrics(const DictionaryValue& root,
48 string16* uninstall_metrics_string) {
49 // Make sure that the user wants us reporting metrics. If not, don't
50 // add our uninstall metrics.
51 bool metrics_reporting_enabled = false;
52 if (!root.GetBoolean(prefs::kMetricsReportingEnabled,
53 &metrics_reporting_enabled) ||
54 !metrics_reporting_enabled) {
55 return false;
58 const DictionaryValue* uninstall_metrics_dict = NULL;
59 if (!root.HasKey(installer::kUninstallMetricsName) ||
60 !root.GetDictionary(installer::kUninstallMetricsName,
61 &uninstall_metrics_dict)) {
62 return false;
65 if (!BuildUninstallMetricsString(uninstall_metrics_dict,
66 uninstall_metrics_string)) {
67 return false;
70 return true;
73 bool ExtractUninstallMetricsFromFile(const base::FilePath& file_path,
74 string16* uninstall_metrics_string) {
75 JSONFileValueSerializer json_serializer(file_path);
77 std::string json_error_string;
78 scoped_ptr<Value> root(json_serializer.Deserialize(NULL, NULL));
79 if (!root.get())
80 return false;
82 // Preferences should always have a dictionary root.
83 if (!root->IsType(Value::TYPE_DICTIONARY))
84 return false;
86 return ExtractUninstallMetrics(*static_cast<DictionaryValue*>(root.get()),
87 uninstall_metrics_string);
90 } // namespace installer