Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 | #include "print_tools/ipp_in_json.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame^] | 10 | #include <base/check.h> |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 11 | #include <base/json/json_writer.h> |
| 12 | #include <base/macros.h> |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 13 | #include <base/optional.h> |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 14 | #include <base/values.h> |
| 15 | |
| 16 | namespace { |
| 17 | |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 18 | base::Value SaveAsJson(const ipp::Collection* coll); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 19 | |
| 20 | // It saves a single value (at given index) from the attribute as JSON |
| 21 | // structure. The parameter "attr" cannot be nullptr, "index" must be correct. |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 22 | base::Value SaveAsJson(const ipp::Attribute* attr, unsigned index) { |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 23 | CHECK(attr != nullptr); |
| 24 | CHECK(index < attr->GetSize()); |
| 25 | using AttrType = ipp::AttrType; |
| 26 | switch (attr->GetType()) { |
| 27 | case AttrType::integer: { |
| 28 | int vi; |
| 29 | attr->GetValue(&vi, index); |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 30 | return base::Value(vi); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 31 | } |
| 32 | case AttrType::boolean: { |
| 33 | int vb; |
| 34 | attr->GetValue(&vb, index); |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 35 | return base::Value(static_cast<bool>(vb)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 36 | } |
| 37 | case AttrType::enum_: { |
| 38 | std::string vs; |
| 39 | attr->GetValue(&vs, index); |
| 40 | if (vs.empty()) { |
| 41 | int vi; |
| 42 | attr->GetValue(&vi, index); |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 43 | return base::Value(vi); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 44 | } |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 45 | return base::Value(vs); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 46 | } |
| 47 | case AttrType::collection: |
| 48 | return SaveAsJson(attr->GetCollection(index)); |
| 49 | case AttrType::text: |
| 50 | case AttrType::name: { |
| 51 | ipp::StringWithLanguage vs; |
| 52 | attr->GetValue(&vs, index); |
| 53 | if (vs.language.empty()) |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 54 | return base::Value(vs.value); |
| 55 | base::Value obj(base::Value::Type::DICTIONARY); |
| 56 | obj.SetStringKey("value", vs.value); |
| 57 | obj.SetStringKey("language", vs.language); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 58 | return obj; |
| 59 | } |
| 60 | case AttrType::dateTime: |
| 61 | case AttrType::resolution: |
| 62 | case AttrType::rangeOfInteger: |
| 63 | case AttrType::octetString: |
| 64 | case AttrType::keyword: |
| 65 | case AttrType::uri: |
| 66 | case AttrType::uriScheme: |
| 67 | case AttrType::charset: |
| 68 | case AttrType::naturalLanguage: |
| 69 | case AttrType::mimeMediaType: { |
| 70 | std::string vs; |
| 71 | attr->GetValue(&vs, index); |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 72 | return base::Value(vs); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 75 | return base::Value(); // not reachable |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // It saves all attribute's values as JSON structure. |
| 79 | // The parameter "attr" cannot be nullptr. |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 80 | base::Value SaveAsJson(const ipp::Attribute* attr) { |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 81 | CHECK(attr != nullptr); |
| 82 | if (attr->IsASet()) { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 83 | base::Value arr(base::Value::Type::LIST); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 84 | const unsigned size = attr->GetSize(); |
| 85 | for (unsigned i = 0; i < size; ++i) |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 86 | arr.Append(SaveAsJson(attr, i)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 87 | return arr; |
| 88 | } else { |
| 89 | return SaveAsJson(attr, 0); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // It saves a given Collection as JSON object. |
| 94 | // The parameter "coll" cannot be nullptr. |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 95 | base::Value SaveAsJson(const ipp::Collection* coll) { |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 96 | CHECK(coll != nullptr); |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 97 | base::Value obj(base::Value::Type::DICTIONARY); |
Piotr Pawliczek | 70ef9a3 | 2019-12-03 12:16:16 -0800 | [diff] [blame] | 98 | std::vector<const ipp::Attribute*> attrs = coll->GetAllAttributes(); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 99 | |
| 100 | for (auto a : attrs) { |
| 101 | auto state = a->GetState(); |
| 102 | if (state == ipp::AttrState::unset) |
| 103 | continue; |
| 104 | |
| 105 | if (state == ipp::AttrState::set) { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 106 | base::Value obj2(base::Value::Type::DICTIONARY); |
| 107 | obj2.SetStringKey("type", ToString(a->GetType())); |
| 108 | obj2.SetKey("value", SaveAsJson(a)); |
| 109 | obj.SetKey(a->GetName(), std::move(obj2)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 110 | } else { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 111 | obj.SetStringKey(a->GetName(), ToString(state)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | return obj; |
| 116 | } |
| 117 | |
| 118 | // It saves all groups from given Package as JSON object. |
| 119 | // The parameter "pkg" cannot be nullptr. |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 120 | base::Value SaveAsJson(const ipp::Package* pkg) { |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 121 | CHECK(pkg != nullptr); |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 122 | base::Value obj(base::Value::Type::DICTIONARY); |
Piotr Pawliczek | 70ef9a3 | 2019-12-03 12:16:16 -0800 | [diff] [blame] | 123 | std::vector<const ipp::Group*> groups = pkg->GetAllGroups(); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 124 | |
| 125 | for (auto g : groups) { |
Piotr Pawliczek | 5a6380a | 2020-02-03 13:04:25 -0800 | [diff] [blame] | 126 | const size_t size = g->GetSize(); |
| 127 | if (size == 0) |
| 128 | continue; |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 129 | if (g->IsASet()) { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 130 | base::Value arr(base::Value::Type::LIST); |
Piotr Pawliczek | 5a6380a | 2020-02-03 13:04:25 -0800 | [diff] [blame] | 131 | for (size_t i = 0; i < size; ++i) |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 132 | arr.Append(SaveAsJson(g->GetCollection(i))); |
| 133 | obj.SetKey(ToString(g->GetName()), std::move(arr)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 134 | } else { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 135 | obj.SetKey(ToString(g->GetName()), SaveAsJson(g->GetCollection())); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | return obj; |
| 140 | } |
| 141 | |
| 142 | // Saves given logs as JSON array. |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 143 | base::Value SaveAsJson(const std::vector<ipp::Log>& log) { |
| 144 | base::Value arr(base::Value::Type::LIST); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 145 | for (const auto& l : log) { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 146 | base::Value obj(base::Value::Type::DICTIONARY); |
| 147 | obj.SetStringKey("message", l.message); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 148 | if (!l.frame_context.empty()) |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 149 | obj.SetStringKey("frame_context", l.frame_context); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 150 | if (!l.parser_context.empty()) |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 151 | obj.SetStringKey("parser_context", l.parser_context); |
| 152 | arr.Append(std::move(obj)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 153 | } |
| 154 | return arr; |
| 155 | } |
| 156 | |
| 157 | } // namespace |
| 158 | |
Piotr Pawliczek | 70ef9a3 | 2019-12-03 12:16:16 -0800 | [diff] [blame] | 159 | bool ConvertToJson(const ipp::Response& response, |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 160 | const std::vector<ipp::Log>& log, |
| 161 | bool compressed_json, |
| 162 | std::string* json) { |
| 163 | // Build structure. |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 164 | base::Value doc(base::Value::Type::DICTIONARY); |
| 165 | doc.SetStringKey("status", ipp::ToString(response.StatusCode())); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 166 | if (!log.empty()) { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 167 | doc.SetKey("parsing_logs", SaveAsJson(log)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 168 | } |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 169 | doc.SetKey("response", SaveAsJson(&response)); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 170 | // Convert to JSON. |
| 171 | bool result; |
| 172 | if (compressed_json) { |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 173 | result = base::JSONWriter::Write(doc, json); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 174 | } else { |
| 175 | const int options = base::JSONWriter::OPTIONS_PRETTY_PRINT; |
hscham | 2e0b8ef | 2020-11-05 11:45:57 +0900 | [diff] [blame] | 176 | result = base::JSONWriter::WriteWithOptions(doc, options, json); |
Piotr Pawliczek | e8650ea | 2019-08-10 20:14:07 -0700 | [diff] [blame] | 177 | } |
| 178 | return result; |
| 179 | } |