David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 6 | #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/base_export.h" |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
| 12 | #include "base/macros.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 13 | #include "base/values.h" |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 14 | #include "starboard/types.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 15 | |
| 16 | class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
| 17 | public: |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 18 | // |json_file_path_| is the path of a file that will be destination of the |
| 19 | // serialization. The serializer will attempt to create the file at the |
| 20 | // specified location. |
| 21 | explicit JSONFileValueSerializer(const base::FilePath& json_file_path); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 22 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 23 | ~JSONFileValueSerializer() override; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 24 | |
| 25 | // DO NOT USE except in unit tests to verify the file was written properly. |
| 26 | // We should never serialize directly to a file since this will block the |
| 27 | // thread. Instead, serialize to a string and write to the file you want on |
| 28 | // the file thread. |
| 29 | // |
| 30 | // Attempt to serialize the data structure represented by Value into |
| 31 | // JSON. If the return value is true, the result will have been written |
| 32 | // into the file whose name was passed into the constructor. |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 33 | bool Serialize(const base::Value& root) override; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 34 | |
| 35 | // Equivalent to Serialize(root) except binary values are omitted from the |
| 36 | // output. |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 37 | bool SerializeAndOmitBinaryValues(const base::Value& root); |
| 38 | |
| 39 | private: |
| 40 | bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
| 41 | |
| 42 | const base::FilePath json_file_path_; |
| 43 | |
| 44 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 45 | }; |
| 46 | |
| 47 | class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer { |
| 48 | public: |
| 49 | // |json_file_path_| is the path of a file that will be source of the |
| 50 | // deserialization. |options| is a bitmask of JSONParserOptions. |
| 51 | explicit JSONFileValueDeserializer(const base::FilePath& json_file_path, |
| 52 | int options = 0); |
| 53 | |
| 54 | ~JSONFileValueDeserializer() override; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 55 | |
| 56 | // Attempt to deserialize the data structure encoded in the file passed |
| 57 | // in to the constructor into a structure of Value objects. If the return |
| 58 | // value is NULL, and if |error_code| is non-null, |error_code| will |
| 59 | // contain an integer error code (either JsonFileError or JsonParseError). |
| 60 | // If |error_message| is non-null, it will be filled in with a formatted |
| 61 | // error message including the location of the error if appropriate. |
| 62 | // The caller takes ownership of the returned value. |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 63 | std::unique_ptr<base::Value> Deserialize(int* error_code, |
| 64 | std::string* error_message) override; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 65 | |
| 66 | // This enum is designed to safely overlap with JSONReader::JsonParseError. |
| 67 | enum JsonFileError { |
| 68 | JSON_NO_ERROR = 0, |
| 69 | JSON_ACCESS_DENIED = 1000, |
| 70 | JSON_CANNOT_READ_FILE, |
| 71 | JSON_FILE_LOCKED, |
| 72 | JSON_NO_SUCH_FILE |
| 73 | }; |
| 74 | |
| 75 | // File-specific error messages that can be returned. |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 76 | static const char kAccessDenied[]; |
| 77 | static const char kCannotReadFile[]; |
| 78 | static const char kFileLocked[]; |
| 79 | static const char kNoSuchFile[]; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 80 | |
| 81 | // Convert an error code into an error message. |error_code| is assumed to |
| 82 | // be a JsonFileError. |
| 83 | static const char* GetErrorMessageForCode(int error_code); |
| 84 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 85 | // Returns the size (in bytes) of JSON string read from disk in the last |
| 86 | // successful |Deserialize()| call. |
| 87 | size_t get_last_read_size() const { return last_read_size_; } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 90 | // A wrapper for ReadFileToString which returns a non-zero JsonFileError if |
| 91 | // there were file errors. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 92 | int ReadFileToString(std::string* json_string); |
| 93 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 94 | const base::FilePath json_file_path_; |
| 95 | const int options_; |
| 96 | size_t last_read_size_; |
| 97 | |
| 98 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 102 | |