Report Windows Firewall state even for ServiceDiscoverySharedClient::GetInstance
[chromium-blink-merge.git] / base / json / json_file_value_serializer.cc
blob72a09700161776760a00c73e99f57b613f26df11
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
3 // found in the LICENSE file.
5 #include "base/json/json_file_value_serializer.h"
7 #include "base/files/file_util.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/logging.h"
11 using base::FilePath;
13 const char JSONFileValueDeserializer::kAccessDenied[] = "Access denied.";
14 const char JSONFileValueDeserializer::kCannotReadFile[] = "Can't read file.";
15 const char JSONFileValueDeserializer::kFileLocked[] = "File locked.";
16 const char JSONFileValueDeserializer::kNoSuchFile[] = "File doesn't exist.";
18 JSONFileValueSerializer::JSONFileValueSerializer(
19 const base::FilePath& json_file_path)
20 : json_file_path_(json_file_path) {
23 JSONFileValueSerializer::~JSONFileValueSerializer() {
26 bool JSONFileValueSerializer::Serialize(const base::Value& root) {
27 return SerializeInternal(root, false);
30 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(
31 const base::Value& root) {
32 return SerializeInternal(root, true);
35 bool JSONFileValueSerializer::SerializeInternal(const base::Value& root,
36 bool omit_binary_values) {
37 std::string json_string;
38 JSONStringValueSerializer serializer(&json_string);
39 serializer.set_pretty_print(true);
40 bool result = omit_binary_values ?
41 serializer.SerializeAndOmitBinaryValues(root) :
42 serializer.Serialize(root);
43 if (!result)
44 return false;
46 int data_size = static_cast<int>(json_string.size());
47 if (base::WriteFile(json_file_path_, json_string.data(), data_size) !=
48 data_size)
49 return false;
51 return true;
54 JSONFileValueDeserializer::JSONFileValueDeserializer(
55 const base::FilePath& json_file_path)
56 : json_file_path_(json_file_path),
57 allow_trailing_comma_(false),
58 last_read_size_(0U) {
61 JSONFileValueDeserializer::~JSONFileValueDeserializer() {
64 int JSONFileValueDeserializer::ReadFileToString(std::string* json_string) {
65 DCHECK(json_string);
66 if (!base::ReadFileToString(json_file_path_, json_string)) {
67 #if defined(OS_WIN)
68 int error = ::GetLastError();
69 if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
70 return JSON_FILE_LOCKED;
71 } else if (error == ERROR_ACCESS_DENIED) {
72 return JSON_ACCESS_DENIED;
74 #endif
75 if (!base::PathExists(json_file_path_))
76 return JSON_NO_SUCH_FILE;
77 else
78 return JSON_CANNOT_READ_FILE;
81 last_read_size_ = json_string->size();
82 return JSON_NO_ERROR;
85 const char* JSONFileValueDeserializer::GetErrorMessageForCode(int error_code) {
86 switch (error_code) {
87 case JSON_NO_ERROR:
88 return "";
89 case JSON_ACCESS_DENIED:
90 return kAccessDenied;
91 case JSON_CANNOT_READ_FILE:
92 return kCannotReadFile;
93 case JSON_FILE_LOCKED:
94 return kFileLocked;
95 case JSON_NO_SUCH_FILE:
96 return kNoSuchFile;
97 default:
98 NOTREACHED();
99 return "";
103 base::Value* JSONFileValueDeserializer::Deserialize(int* error_code,
104 std::string* error_str) {
105 std::string json_string;
106 int error = ReadFileToString(&json_string);
107 if (error != JSON_NO_ERROR) {
108 if (error_code)
109 *error_code = error;
110 if (error_str)
111 *error_str = GetErrorMessageForCode(error);
112 return NULL;
115 JSONStringValueDeserializer deserializer(json_string);
116 deserializer.set_allow_trailing_comma(allow_trailing_comma_);
117 return deserializer.Deserialize(error_code, error_str);