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. 4 5 #include "chrome/installer/util/uninstall_metrics.h" 6 7 #include <string> 8 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" 16 17 namespace installer { 18 19 namespace { 20 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; 30 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"="); 38 39 std::string value; 40 iter.value().GetAsString(&value); 41 metrics->append(base::UTF8ToWide(value)); 42 } 43 44 return has_values; 45 } 46 47 } // namespace 48 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; 58 } 59 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; 65 } 66 67 if (!BuildUninstallMetricsString(uninstall_metrics_dict, 68 uninstall_metrics_string)) { 69 return false; 70 } 71 72 return true; 73 } 74 75 bool ExtractUninstallMetricsFromFile(const base::FilePath& file_path, 76 base::string16* uninstall_metrics_string) { 77 JSONFileValueSerializer json_serializer(file_path); 78 79 std::string json_error_string; 80 scoped_ptr<base::Value> root(json_serializer.Deserialize(NULL, NULL)); 81 if (!root.get()) 82 return false; 83 84 // Preferences should always have a dictionary root. 85 if (!root->IsType(base::Value::TYPE_DICTIONARY)) 86 return false; 87 88 return ExtractUninstallMetrics( 89 *static_cast<base::DictionaryValue*>(root.get()), 90 uninstall_metrics_string); 91 } 92 93 } // namespace installer 94