Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / json / json_file_value_serializer.h
blobaab47eec2dc269f64126207bc42030bfd3650c3a
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 #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
6 #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
8 #include <string>
10 #include "base/base_export.h"
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/values.h"
15 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
16 public:
17 // |json_file_path_| is the path of a file that will be destination of the
18 // serialization. The serializer will attempt to create the file at the
19 // specified location.
20 explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
22 ~JSONFileValueSerializer() override;
24 // DO NOT USE except in unit tests to verify the file was written properly.
25 // We should never serialize directly to a file since this will block the
26 // thread. Instead, serialize to a string and write to the file you want on
27 // the file thread.
29 // Attempt to serialize the data structure represented by Value into
30 // JSON. If the return value is true, the result will have been written
31 // into the file whose name was passed into the constructor.
32 bool Serialize(const base::Value& root) override;
34 // Equivalent to Serialize(root) except binary values are omitted from the
35 // output.
36 bool SerializeAndOmitBinaryValues(const base::Value& root);
38 private:
39 bool SerializeInternal(const base::Value& root, bool omit_binary_values);
41 const base::FilePath json_file_path_;
43 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
46 class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer {
47 public:
48 // |json_file_path_| is the path of a file that will be source of the
49 // deserialization.
50 explicit JSONFileValueDeserializer(const base::FilePath& json_file_path);
52 ~JSONFileValueDeserializer() override;
54 // Attempt to deserialize the data structure encoded in the file passed
55 // in to the constructor into a structure of Value objects. If the return
56 // value is NULL, and if |error_code| is non-null, |error_code| will
57 // contain an integer error code (either JsonFileError or JsonParseError).
58 // If |error_message| is non-null, it will be filled in with a formatted
59 // error message including the location of the error if appropriate.
60 // The caller takes ownership of the returned value.
61 base::Value* Deserialize(int* error_code,
62 std::string* error_message) override;
64 // This enum is designed to safely overlap with JSONReader::JsonParseError.
65 enum JsonFileError {
66 JSON_NO_ERROR = 0,
67 JSON_ACCESS_DENIED = 1000,
68 JSON_CANNOT_READ_FILE,
69 JSON_FILE_LOCKED,
70 JSON_NO_SUCH_FILE
73 // File-specific error messages that can be returned.
74 static const char kAccessDenied[];
75 static const char kCannotReadFile[];
76 static const char kFileLocked[];
77 static const char kNoSuchFile[];
79 // Convert an error code into an error message. |error_code| is assumed to
80 // be a JsonFileError.
81 static const char* GetErrorMessageForCode(int error_code);
83 void set_allow_trailing_comma(bool new_value) {
84 allow_trailing_comma_ = new_value;
87 // Returns the size (in bytes) of JSON string read from disk in the last
88 // successful |Deserialize()| call.
89 size_t get_last_read_size() const { return last_read_size_; }
91 private:
92 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
93 // there were file errors.
94 int ReadFileToString(std::string* json_string);
96 const base::FilePath json_file_path_;
97 bool allow_trailing_comma_;
98 size_t last_read_size_;
100 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
103 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_