• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "apex_manifest.h"
18 #include "string_log.h"
19 #include <android-base/logging.h>
20 
21 #include <google/protobuf/util/json_util.h>
22 #include <google/protobuf/util/type_resolver_util.h>
23 #include <memory>
24 #include <string>
25 
26 using google::protobuf::DescriptorPool;
27 using google::protobuf::scoped_ptr;
28 using google::protobuf::util::NewTypeResolverForDescriptorPool;
29 using google::protobuf::util::TypeResolver;
30 
31 namespace android {
32 namespace apex {
33 namespace {
34 const char kTypeUrlPrefix[] = "type.googleapis.com";
35 
GetTypeUrl(const ApexManifest & apex_manifest)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 }
40 
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
JsonToApexManifestMessage(const std::string & content,ApexManifest & apex_manifest)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());
57   }
58 
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 }
65 
66 }  // namespace
67 
ParseManifest(const std::string & content)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.
78   // name
79   if (apex_manifest.name().empty()) {
80     err = StringLog() << "Missing required field \"name\" from APEX manifest.";
81     return StatusOr<ApexManifest>::MakeError(err);
82   }
83 
84   // version
85   if (apex_manifest.version() == 0) {
86     err =
87         StringLog() << "Missing required field \"version\" from APEX manifest.";
88     return StatusOr<ApexManifest>::MakeError(err);
89   }
90   return parse_manifest_status;
91 }
92 
GetPackageId(const ApexManifest & apexManifest)93 std::string GetPackageId(const ApexManifest& apexManifest) {
94   return apexManifest.name() + "@" + std::to_string(apexManifest.version());
95 }
96 
97 }  // namespace apex
98 }  // namespace android
99