blob: ae2dfa43f2ca6df523131164a3639f7ece29facb [file] [log] [blame]
Dario Freniaeb233c2018-08-28 12:48:42 +01001/*
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 Freniaeb233c2018-08-28 12:48:42 +010017#include "apex_manifest.h"
Andreas Gampe8f89b762018-10-19 11:56:01 -070018#include "string_log.h"
Jiyong Park69c0f112018-11-22 20:38:05 +090019#include <android-base/logging.h>
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000020
21#include <google/protobuf/util/json_util.h>
22#include <google/protobuf/util/type_resolver_util.h>
Jiyong Park69c0f112018-11-22 20:38:05 +090023#include <memory>
24#include <string>
25
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000026using google::protobuf::DescriptorPool;
27using google::protobuf::scoped_ptr;
28using google::protobuf::util::NewTypeResolverForDescriptorPool;
29using google::protobuf::util::TypeResolver;
30
Dario Freniaeb233c2018-08-28 12:48:42 +010031namespace android {
32namespace apex {
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000033namespace {
34const char kTypeUrlPrefix[] = "type.googleapis.com";
Dario Freniaeb233c2018-08-28 12:48:42 +010035
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000036std::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 Gampe0bc545a2018-11-19 15:12:18 -080040
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000041// 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
46StatusOr<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 Freniaeb233c2018-08-28 12:48:42 +010057 }
58
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000059 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 Freniaeb233c2018-08-28 12:48:42 +010065
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000066} // namespace
Andreas Gampe0bc545a2018-11-19 15:12:18 -080067
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000068StatusOr<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 Park69c0f112018-11-22 20:38:05 +090078 // name
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000079 if (apex_manifest.name().empty()) {
80 err = StringLog() << "Missing required field \"name\" from APEX manifest.";
81 return StatusOr<ApexManifest>::MakeError(err);
Dario Freniaeb233c2018-08-28 12:48:42 +010082 }
Andreas Gampe0bc545a2018-11-19 15:12:18 -080083
Jiyong Park69c0f112018-11-22 20:38:05 +090084 // version
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000085 if (apex_manifest.version() == 0) {
86 err =
87 StringLog() << "Missing required field \"version\" from APEX manifest.";
Jiyong Park69c0f112018-11-22 20:38:05 +090088 return StatusOr<ApexManifest>::MakeError(err);
Andreas Gampe0bc545a2018-11-19 15:12:18 -080089 }
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000090 return parse_manifest_status;
91}
Dario Freniaeb233c2018-08-28 12:48:42 +010092
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000093std::string GetPackageId(const ApexManifest& apexManifest) {
94 return apexManifest.name() + "@" + std::to_string(apexManifest.version());
Dario Freniaeb233c2018-08-28 12:48:42 +010095}
96
97} // namespace apex
98} // namespace android