• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 "base/json/json_file_value_serializer.h"
6 
7 #include "base/check.h"
8 #include "base/files/file_util.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/notreached.h"
11 #include "build/build_config.h"
12 
13 using base::FilePath;
14 
15 const char JSONFileValueDeserializer::kAccessDenied[] = "Access denied.";
16 const char JSONFileValueDeserializer::kCannotReadFile[] = "Can't read file.";
17 const char JSONFileValueDeserializer::kFileLocked[] = "File locked.";
18 const char JSONFileValueDeserializer::kNoSuchFile[] = "File doesn't exist.";
19 
JSONFileValueSerializer(const base::FilePath & json_file_path)20 JSONFileValueSerializer::JSONFileValueSerializer(
21     const base::FilePath& json_file_path)
22     : json_file_path_(json_file_path) {
23 }
24 
25 JSONFileValueSerializer::~JSONFileValueSerializer() = default;
26 
Serialize(base::ValueView root)27 bool JSONFileValueSerializer::Serialize(base::ValueView root) {
28   return SerializeInternal(root, false);
29 }
30 
SerializeAndOmitBinaryValues(base::ValueView root)31 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(
32     base::ValueView root) {
33   return SerializeInternal(root, true);
34 }
35 
SerializeInternal(base::ValueView root,bool omit_binary_values)36 bool JSONFileValueSerializer::SerializeInternal(base::ValueView root,
37                                                 bool omit_binary_values) {
38   std::string json_string;
39   JSONStringValueSerializer serializer(&json_string);
40   serializer.set_pretty_print(true);
41   bool result = omit_binary_values
42                     ? serializer.SerializeAndOmitBinaryValues(root)
43                     : serializer.Serialize(root);
44   if (!result)
45     return false;
46 
47   return base::WriteFile(json_file_path_, json_string);
48 }
49 
JSONFileValueDeserializer(const base::FilePath & json_file_path,int options)50 JSONFileValueDeserializer::JSONFileValueDeserializer(
51     const base::FilePath& json_file_path,
52     int options)
53     : json_file_path_(json_file_path), options_(options) {}
54 
55 JSONFileValueDeserializer::~JSONFileValueDeserializer() = default;
56 
ReadFileToString(std::string * json_string)57 int JSONFileValueDeserializer::ReadFileToString(std::string* json_string) {
58   DCHECK(json_string);
59   last_read_size_ = 0u;
60   if (!base::ReadFileToString(json_file_path_, json_string)) {
61 #if BUILDFLAG(IS_WIN)
62     DWORD error = ::GetLastError();
63     if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
64       return JSON_FILE_LOCKED;
65     } else if (error == ERROR_ACCESS_DENIED) {
66       return JSON_ACCESS_DENIED;
67     }
68 #endif
69     return base::PathExists(json_file_path_) ? JSON_CANNOT_READ_FILE
70                                              : JSON_NO_SUCH_FILE;
71   }
72 
73   last_read_size_ = json_string->size();
74   return JSON_NO_ERROR;
75 }
76 
GetErrorMessageForCode(int error_code)77 const char* JSONFileValueDeserializer::GetErrorMessageForCode(int error_code) {
78   switch (error_code) {
79     case JSON_NO_ERROR:
80       return "";
81     case JSON_ACCESS_DENIED:
82       return kAccessDenied;
83     case JSON_CANNOT_READ_FILE:
84       return kCannotReadFile;
85     case JSON_FILE_LOCKED:
86       return kFileLocked;
87     case JSON_NO_SUCH_FILE:
88       return kNoSuchFile;
89     default:
90       NOTREACHED();
91   }
92 }
93 
Deserialize(int * error_code,std::string * error_str)94 std::unique_ptr<base::Value> JSONFileValueDeserializer::Deserialize(
95     int* error_code,
96     std::string* error_str) {
97   std::string json_string;
98   int error = ReadFileToString(&json_string);
99   if (error != JSON_NO_ERROR) {
100     if (error_code)
101       *error_code = error;
102     if (error_str)
103       *error_str = GetErrorMessageForCode(error);
104     return nullptr;
105   }
106 
107   JSONStringValueDeserializer deserializer(json_string, options_);
108   return deserializer.Deserialize(error_code, error_str);
109 }
110