Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2009 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 // Forked from content/common/json_value_serializer.h | |
|
dmac
2011/08/10 21:39:00
worth just grabbing json_value_serializer directly
awong
2011/08/11 01:23:29
:-/ That's so ugly...I...fine. did it.
| |
| 6 | |
| 7 #ifndef REMOTING_HOST_PLUGIN_POLICY_HACK_JSON_VALUE_SERIALIZER_H_ | |
| 8 #define REMOTING_HOST_PLUGIN_POLICY_HACK_JSON_VALUE_SERIALIZER_H_ | |
| 9 #pragma once | |
| 10 | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/file_path.h" | |
| 15 #include "base/values.h" | |
| 16 | |
| 17 namespace remoting { | |
| 18 namespace policy_hack { | |
| 19 | |
| 20 class JSONStringValueSerializer : public base::ValueSerializer { | |
| 21 public: | |
| 22 // json_string is the string that will be source of the deserialization | |
| 23 // or the destination of the serialization. The caller of the constructor | |
| 24 // retains ownership of the string. | |
| 25 explicit JSONStringValueSerializer(std::string* json_string) | |
| 26 : json_string_(json_string), | |
| 27 initialized_with_const_string_(false), | |
| 28 pretty_print_(false), | |
| 29 allow_trailing_comma_(false) { | |
| 30 } | |
| 31 | |
| 32 // This version allows initialization with a const string reference for | |
| 33 // deserialization only. | |
| 34 explicit JSONStringValueSerializer(const std::string& json_string) | |
| 35 : json_string_(&const_cast<std::string&>(json_string)), | |
| 36 initialized_with_const_string_(true), | |
| 37 pretty_print_(false), | |
| 38 allow_trailing_comma_(false) { | |
| 39 } | |
| 40 | |
| 41 virtual ~JSONStringValueSerializer(); | |
| 42 | |
| 43 // Attempt to serialize the data structure represented by Value into | |
| 44 // JSON. If the return value is true, the result will have been written | |
| 45 // into the string passed into the constructor. | |
| 46 virtual bool Serialize(const Value& root); | |
| 47 | |
| 48 // Attempt to deserialize the data structure encoded in the string passed | |
| 49 // in to the constructor into a structure of Value objects. If the return | |
| 50 // value is NULL, and if |error_code| is non-null, |error_code| will | |
| 51 // contain an integer error code (either JsonFileError or JsonParseError). | |
| 52 // If |error_message| is non-null, it will be filled in with a formatted | |
| 53 // error message including the location of the error if appropriate. | |
| 54 // The caller takes ownership of the returned value. | |
| 55 virtual Value* Deserialize(int* error_code, std::string* error_message); | |
| 56 | |
| 57 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | |
| 58 bool pretty_print() { return pretty_print_; } | |
| 59 | |
| 60 void set_allow_trailing_comma(bool new_value) { | |
| 61 allow_trailing_comma_ = new_value; | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 std::string* json_string_; | |
| 66 bool initialized_with_const_string_; | |
| 67 bool pretty_print_; // If true, serialization will span multiple lines. | |
| 68 // If true, deserialization will allow trailing commas. | |
| 69 bool allow_trailing_comma_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | |
| 72 }; | |
| 73 | |
| 74 class JSONFileValueSerializer : public base::ValueSerializer { | |
| 75 public: | |
| 76 // json_file_patch is the path of a file that will be source of the | |
| 77 // deserialization or the destination of the serialization. | |
| 78 // When deserializing, the file should exist, but when serializing, the | |
| 79 // serializer will attempt to create the file at the specified location. | |
| 80 explicit JSONFileValueSerializer(const FilePath& json_file_path) | |
| 81 : json_file_path_(json_file_path) {} | |
| 82 | |
| 83 virtual ~JSONFileValueSerializer() {} | |
| 84 | |
| 85 // DO NOT USE except in unit tests to verify the file was written properly. | |
| 86 // We should never serialize directly to a file since this will block the | |
| 87 // thread. Instead, serialize to a string and write to the file you want on | |
| 88 // the file thread. | |
| 89 // | |
| 90 // Attempt to serialize the data structure represented by Value into | |
| 91 // JSON. If the return value is true, the result will have been written | |
| 92 // into the file whose name was passed into the constructor. | |
| 93 virtual bool Serialize(const Value& root); | |
| 94 | |
| 95 // Attempt to deserialize the data structure encoded in the file passed | |
| 96 // in to the constructor into a structure of Value objects. If the return | |
| 97 // value is NULL, and if |error_code| is non-null, |error_code| will | |
| 98 // contain an integer error code (either JsonFileError or JsonParseError). | |
| 99 // If |error_message| is non-null, it will be filled in with a formatted | |
| 100 // error message including the location of the error if appropriate. | |
| 101 // The caller takes ownership of the returned value. | |
| 102 virtual Value* Deserialize(int* error_code, std::string* error_message); | |
| 103 | |
| 104 // This enum is designed to safely overlap with JSONReader::JsonParseError. | |
| 105 enum JsonFileError { | |
| 106 JSON_NO_ERROR = 0, | |
| 107 JSON_ACCESS_DENIED = 1000, | |
| 108 JSON_CANNOT_READ_FILE, | |
| 109 JSON_FILE_LOCKED, | |
| 110 JSON_NO_SUCH_FILE | |
| 111 }; | |
| 112 | |
| 113 // File-specific error messages that can be returned. | |
| 114 static const char* kAccessDenied; | |
| 115 static const char* kCannotReadFile; | |
| 116 static const char* kFileLocked; | |
| 117 static const char* kNoSuchFile; | |
| 118 | |
| 119 // Convert an error code into an error message. |error_code| is assumed to | |
| 120 // be a JsonFileError. | |
| 121 static const char* GetErrorMessageForCode(int error_code); | |
| 122 | |
| 123 private: | |
| 124 FilePath json_file_path_; | |
| 125 | |
| 126 // A wrapper for file_util::ReadFileToString which returns a non-zero | |
| 127 // JsonFileError if there were file errors. | |
| 128 int ReadFileToString(std::string* json_string); | |
| 129 | |
| 130 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | |
| 131 }; | |
| 132 | |
| 133 } // namespace policy_hack | |
| 134 } // namespace remoting | |
| 135 | |
| 136 #endif // REMOTING_HOST_PLUGIN_POLICY_HACK_JSON_VALUE_SERIALIZER_H_ | |
| OLD | NEW |