Home | History | Annotate | Download | only in util
      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 DictionaryValue* uninstall_metrics_dict, string16* metrics) {
     27   DCHECK(NULL != metrics);
     28   bool has_values = false;
     29 
     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"=");
     36 
     37     std::string value;
     38     iter.value().GetAsString(&value);
     39     metrics->append(UTF8ToWide(value));
     40   }
     41 
     42   return has_values;
     43 }
     44 
     45 }  // namespace
     46 
     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;
     56   }
     57 
     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;
     63   }
     64 
     65   if (!BuildUninstallMetricsString(uninstall_metrics_dict,
     66                                    uninstall_metrics_string)) {
     67     return false;
     68   }
     69 
     70   return true;
     71 }
     72 
     73 bool ExtractUninstallMetricsFromFile(const base::FilePath& file_path,
     74                                      string16* uninstall_metrics_string) {
     75   JSONFileValueSerializer json_serializer(file_path);
     76 
     77   std::string json_error_string;
     78   scoped_ptr<Value> root(json_serializer.Deserialize(NULL, NULL));
     79   if (!root.get())
     80     return false;
     81 
     82   // Preferences should always have a dictionary root.
     83   if (!root->IsType(Value::TYPE_DICTIONARY))
     84     return false;
     85 
     86   return ExtractUninstallMetrics(*static_cast<DictionaryValue*>(root.get()),
     87                                  uninstall_metrics_string);
     88 }
     89 
     90 }  // namespace installer
     91