| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/json/json_file_value_serializer.h" | 5 #include "base/json/json_file_value_serializer.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" |
| 10 | 11 |
| 11 using base::FilePath; | 12 using base::FilePath; |
| 12 | 13 |
| 13 const char JSONFileValueSerializer::kAccessDenied[] = "Access denied."; | 14 const char JSONFileValueSerializer::kAccessDenied[] = "Access denied."; |
| 14 const char JSONFileValueSerializer::kCannotReadFile[] = "Can't read file."; | 15 const char JSONFileValueSerializer::kCannotReadFile[] = "Can't read file."; |
| 15 const char JSONFileValueSerializer::kFileLocked[] = "File locked."; | 16 const char JSONFileValueSerializer::kFileLocked[] = "File locked."; |
| 16 const char JSONFileValueSerializer::kNoSuchFile[] = "File doesn't exist."; | 17 const char JSONFileValueSerializer::kNoSuchFile[] = "File doesn't exist."; |
| 17 | 18 |
| 19 JSONFileValueSerializer::JSONFileValueSerializer( |
| 20 const base::FilePath& json_file_path, |
| 21 const std::string& histogram_suffix) |
| 22 : json_file_path_(json_file_path), |
| 23 allow_trailing_comma_(false), |
| 24 histogram_suffix_(histogram_suffix) {} |
| 25 |
| 26 JSONFileValueSerializer::JSONFileValueSerializer( |
| 27 const base::FilePath& json_file_path) |
| 28 : JSONFileValueSerializer(json_file_path, std::string()) {} |
| 29 |
| 30 JSONFileValueSerializer::~JSONFileValueSerializer() {} |
| 31 |
| 18 bool JSONFileValueSerializer::Serialize(const base::Value& root) { | 32 bool JSONFileValueSerializer::Serialize(const base::Value& root) { |
| 19 return SerializeInternal(root, false); | 33 return SerializeInternal(root, false); |
| 20 } | 34 } |
| 21 | 35 |
| 22 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues( | 36 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues( |
| 23 const base::Value& root) { | 37 const base::Value& root) { |
| 24 return SerializeInternal(root, true); | 38 return SerializeInternal(root, true); |
| 25 } | 39 } |
| 26 | 40 |
| 27 bool JSONFileValueSerializer::SerializeInternal(const base::Value& root, | 41 bool JSONFileValueSerializer::SerializeInternal(const base::Value& root, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 52 return JSON_FILE_LOCKED; | 66 return JSON_FILE_LOCKED; |
| 53 } else if (error == ERROR_ACCESS_DENIED) { | 67 } else if (error == ERROR_ACCESS_DENIED) { |
| 54 return JSON_ACCESS_DENIED; | 68 return JSON_ACCESS_DENIED; |
| 55 } | 69 } |
| 56 #endif | 70 #endif |
| 57 if (!base::PathExists(json_file_path_)) | 71 if (!base::PathExists(json_file_path_)) |
| 58 return JSON_NO_SUCH_FILE; | 72 return JSON_NO_SUCH_FILE; |
| 59 else | 73 else |
| 60 return JSON_CANNOT_READ_FILE; | 74 return JSON_CANNOT_READ_FILE; |
| 61 } | 75 } |
| 76 |
| 77 if (!histogram_suffix_.empty()) { |
| 78 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 79 // macro adapted to allow for a dynamically suffixed histogram name. |
| 80 // Note: The factory creates and owns the histogram. |
| 81 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 82 "Settings.JsonDataReadSizeKilobytes." + histogram_suffix_, 1, 10000, 50, |
| 83 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 84 histogram->Add(static_cast<int>(json_string->size()) / 1024); |
| 85 } |
| 86 |
| 62 return JSON_NO_ERROR; | 87 return JSON_NO_ERROR; |
| 63 } | 88 } |
| 64 | 89 |
| 65 const char* JSONFileValueSerializer::GetErrorMessageForCode(int error_code) { | 90 const char* JSONFileValueSerializer::GetErrorMessageForCode(int error_code) { |
| 66 switch (error_code) { | 91 switch (error_code) { |
| 67 case JSON_NO_ERROR: | 92 case JSON_NO_ERROR: |
| 68 return ""; | 93 return ""; |
| 69 case JSON_ACCESS_DENIED: | 94 case JSON_ACCESS_DENIED: |
| 70 return kAccessDenied; | 95 return kAccessDenied; |
| 71 case JSON_CANNOT_READ_FILE: | 96 case JSON_CANNOT_READ_FILE: |
| (...skipping 17 matching lines...) Expand all Loading... |
| 89 *error_code = error; | 114 *error_code = error; |
| 90 if (error_str) | 115 if (error_str) |
| 91 *error_str = GetErrorMessageForCode(error); | 116 *error_str = GetErrorMessageForCode(error); |
| 92 return NULL; | 117 return NULL; |
| 93 } | 118 } |
| 94 | 119 |
| 95 JSONStringValueSerializer serializer(json_string); | 120 JSONStringValueSerializer serializer(json_string); |
| 96 serializer.set_allow_trailing_comma(allow_trailing_comma_); | 121 serializer.set_allow_trailing_comma(allow_trailing_comma_); |
| 97 return serializer.Deserialize(error_code, error_str); | 122 return serializer.Deserialize(error_code, error_str); |
| 98 } | 123 } |
| OLD | NEW |