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