OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/util/uninstall_metrics.h" | 5 #include "chrome/installer/util/uninstall_metrics.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "chrome/installer/util/util_constants.h" | 15 #include "chrome/installer/util/util_constants.h" |
16 | 16 |
17 namespace installer { | 17 namespace installer { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Given a DictionaryValue containing a set of uninstall metrics, | 21 // Given a DictionaryValue containing a set of uninstall metrics, |
22 // this builds a URL parameter list of all the contained 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 | 23 // Returns true if at least one uninstall metric was found in |
24 // uninstall_metrics_dict, false otherwise. | 24 // uninstall_metrics_dict, false otherwise. |
25 bool BuildUninstallMetricsString( | 25 bool BuildUninstallMetricsString( |
26 const DictionaryValue* uninstall_metrics_dict, string16* metrics) { | 26 const DictionaryValue* uninstall_metrics_dict, base::string16* metrics) { |
27 DCHECK(NULL != metrics); | 27 DCHECK(NULL != metrics); |
28 bool has_values = false; | 28 bool has_values = false; |
29 | 29 |
30 for (DictionaryValue::Iterator iter(*uninstall_metrics_dict); !iter.IsAtEnd(); | 30 for (DictionaryValue::Iterator iter(*uninstall_metrics_dict); !iter.IsAtEnd(); |
31 iter.Advance()) { | 31 iter.Advance()) { |
32 has_values = true; | 32 has_values = true; |
33 metrics->append(L"&"); | 33 metrics->append(L"&"); |
34 metrics->append(UTF8ToWide(iter.key())); | 34 metrics->append(UTF8ToWide(iter.key())); |
35 metrics->append(L"="); | 35 metrics->append(L"="); |
36 | 36 |
37 std::string value; | 37 std::string value; |
38 iter.value().GetAsString(&value); | 38 iter.value().GetAsString(&value); |
39 metrics->append(UTF8ToWide(value)); | 39 metrics->append(UTF8ToWide(value)); |
40 } | 40 } |
41 | 41 |
42 return has_values; | 42 return has_values; |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 bool ExtractUninstallMetrics(const DictionaryValue& root, | 47 bool ExtractUninstallMetrics(const DictionaryValue& root, |
48 string16* uninstall_metrics_string) { | 48 base::string16* uninstall_metrics_string) { |
49 // Make sure that the user wants us reporting metrics. If not, don't | 49 // Make sure that the user wants us reporting metrics. If not, don't |
50 // add our uninstall metrics. | 50 // add our uninstall metrics. |
51 bool metrics_reporting_enabled = false; | 51 bool metrics_reporting_enabled = false; |
52 if (!root.GetBoolean(prefs::kMetricsReportingEnabled, | 52 if (!root.GetBoolean(prefs::kMetricsReportingEnabled, |
53 &metrics_reporting_enabled) || | 53 &metrics_reporting_enabled) || |
54 !metrics_reporting_enabled) { | 54 !metrics_reporting_enabled) { |
55 return false; | 55 return false; |
56 } | 56 } |
57 | 57 |
58 const DictionaryValue* uninstall_metrics_dict = NULL; | 58 const DictionaryValue* uninstall_metrics_dict = NULL; |
59 if (!root.HasKey(installer::kUninstallMetricsName) || | 59 if (!root.HasKey(installer::kUninstallMetricsName) || |
60 !root.GetDictionary(installer::kUninstallMetricsName, | 60 !root.GetDictionary(installer::kUninstallMetricsName, |
61 &uninstall_metrics_dict)) { | 61 &uninstall_metrics_dict)) { |
62 return false; | 62 return false; |
63 } | 63 } |
64 | 64 |
65 if (!BuildUninstallMetricsString(uninstall_metrics_dict, | 65 if (!BuildUninstallMetricsString(uninstall_metrics_dict, |
66 uninstall_metrics_string)) { | 66 uninstall_metrics_string)) { |
67 return false; | 67 return false; |
68 } | 68 } |
69 | 69 |
70 return true; | 70 return true; |
71 } | 71 } |
72 | 72 |
73 bool ExtractUninstallMetricsFromFile(const base::FilePath& file_path, | 73 bool ExtractUninstallMetricsFromFile(const base::FilePath& file_path, |
74 string16* uninstall_metrics_string) { | 74 base::string16* uninstall_metrics_string) { |
75 JSONFileValueSerializer json_serializer(file_path); | 75 JSONFileValueSerializer json_serializer(file_path); |
76 | 76 |
77 std::string json_error_string; | 77 std::string json_error_string; |
78 scoped_ptr<Value> root(json_serializer.Deserialize(NULL, NULL)); | 78 scoped_ptr<Value> root(json_serializer.Deserialize(NULL, NULL)); |
79 if (!root.get()) | 79 if (!root.get()) |
80 return false; | 80 return false; |
81 | 81 |
82 // Preferences should always have a dictionary root. | 82 // Preferences should always have a dictionary root. |
83 if (!root->IsType(Value::TYPE_DICTIONARY)) | 83 if (!root->IsType(Value::TYPE_DICTIONARY)) |
84 return false; | 84 return false; |
85 | 85 |
86 return ExtractUninstallMetrics(*static_cast<DictionaryValue*>(root.get()), | 86 return ExtractUninstallMetrics(*static_cast<DictionaryValue*>(root.get()), |
87 uninstall_metrics_string); | 87 uninstall_metrics_string); |
88 } | 88 } |
89 | 89 |
90 } // namespace installer | 90 } // namespace installer |
OLD | NEW |