Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 17 | #include "apex_manifest.h" |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 18 | #include "string_log.h" |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 20 | |
| 21 | #include <google/protobuf/util/json_util.h> |
| 22 | #include <google/protobuf/util/type_resolver_util.h> |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <string> |
| 25 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 26 | using google::protobuf::DescriptorPool; |
| 27 | using google::protobuf::scoped_ptr; |
| 28 | using google::protobuf::util::NewTypeResolverForDescriptorPool; |
| 29 | using google::protobuf::util::TypeResolver; |
| 30 | |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace apex { |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | const char kTypeUrlPrefix[] = "type.googleapis.com"; |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 35 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 36 | std::string GetTypeUrl(const ApexManifest& apex_manifest) { |
| 37 | const google::protobuf::Descriptor* message = apex_manifest.GetDescriptor(); |
| 38 | return std::string(kTypeUrlPrefix) + "/" + message->full_name(); |
| 39 | } |
Andreas Gampe | 0bc545a | 2018-11-19 15:12:18 -0800 | [diff] [blame] | 40 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 41 | // TODO: JsonStringToMessage is a newly added function in protobuf |
| 42 | // and is not yet available in the android tree. Replace this function with |
| 43 | // https://developers.google.com/protocol-buffers/docs/reference/cpp/ |
| 44 | // google.protobuf.util.json_util#JsonStringToMessage.details |
| 45 | // as and when the android tree gets updated |
| 46 | StatusOr<ApexManifest> JsonToApexManifestMessage(const std::string& content, |
| 47 | ApexManifest& apex_manifest) { |
| 48 | scoped_ptr<TypeResolver> resolver(NewTypeResolverForDescriptorPool( |
| 49 | kTypeUrlPrefix, DescriptorPool::generated_pool())); |
| 50 | std::string binary; |
| 51 | auto parse_status = JsonToBinaryString( |
| 52 | resolver.get(), GetTypeUrl(apex_manifest), content, &binary); |
| 53 | if (!parse_status.ok()) { |
| 54 | return StatusOr<ApexManifest>::MakeError( |
| 55 | StringLog() << "Failed to parse APEX Manifest JSON config: " |
| 56 | << parse_status.error_message().as_string()); |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 59 | if (!apex_manifest.ParseFromString(binary)) { |
| 60 | return StatusOr<ApexManifest>::MakeError( |
| 61 | StringLog() << "Unexpected fields in APEX Manifest JSON config"); |
| 62 | } |
| 63 | return StatusOr<ApexManifest>(apex_manifest); |
| 64 | } |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 65 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 66 | } // namespace |
Andreas Gampe | 0bc545a | 2018-11-19 15:12:18 -0800 | [diff] [blame] | 67 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 68 | StatusOr<ApexManifest> ParseManifest(const std::string& content) { |
| 69 | ApexManifest apex_manifest; |
| 70 | std::string err; |
| 71 | StatusOr<ApexManifest> parse_manifest_status = |
| 72 | JsonToApexManifestMessage(content, apex_manifest); |
| 73 | if (!parse_manifest_status.Ok()) { |
| 74 | return parse_manifest_status; |
| 75 | } |
| 76 | |
| 77 | // Verifying required fields. |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 78 | // name |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 79 | if (apex_manifest.name().empty()) { |
| 80 | err = StringLog() << "Missing required field \"name\" from APEX manifest."; |
| 81 | return StatusOr<ApexManifest>::MakeError(err); |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 82 | } |
Andreas Gampe | 0bc545a | 2018-11-19 15:12:18 -0800 | [diff] [blame] | 83 | |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 84 | // version |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 85 | if (apex_manifest.version() == 0) { |
| 86 | err = |
| 87 | StringLog() << "Missing required field \"version\" from APEX manifest."; |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 88 | return StatusOr<ApexManifest>::MakeError(err); |
Andreas Gampe | 0bc545a | 2018-11-19 15:12:18 -0800 | [diff] [blame] | 89 | } |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 90 | return parse_manifest_status; |
| 91 | } |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 92 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 93 | std::string GetPackageId(const ApexManifest& apexManifest) { |
| 94 | return apexManifest.name() + "@" + std::to_string(apexManifest.version()); |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace apex |
| 98 | } // namespace android |