1// Copyright 2014 The Chromium 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#ifndef COMPONENTS_NACL_RENDERER_JSON_MANIFEST_H
6#define COMPONENTS_NACL_RENDERER_JSON_MANIFEST_H
7
8#include <set>
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "ppapi/c/pp_array_output.h"
13#include "ppapi/c/pp_instance.h"
14#include "ppapi/c/private/ppb_nacl_private.h"
15#include "third_party/jsoncpp/source/include/json/value.h"
16
17namespace nacl {
18class JsonManifest;
19class NexeLoadManager;
20
21// There is at most one JsonManifest per PP_Instance. This adds a one-to-one
22// mapping.
23void AddJsonManifest(PP_Instance instance, scoped_ptr<JsonManifest> manifest);
24
25// Returns a non-owning pointer to the JsonManifest for the given instance.
26// Returns NULL if no such JsonManifest exists.
27JsonManifest* GetJsonManifest(PP_Instance instance);
28void DeleteJsonManifest(PP_Instance instance);
29
30class JsonManifest {
31 public:
32  struct ErrorInfo {
33    PP_NaClError error;
34    std::string string;
35  };
36
37  JsonManifest(const std::string& manifest_base_url,
38               const std::string& sandbox_isa,
39               bool nonsfi_enabled,
40               bool pnacl_debug);
41
42  // Initialize the manifest object for use by later lookups. Returns
43  // true if the manifest parses correctly and matches the schema.
44  bool Init(const std::string& json_manifest, ErrorInfo* error_info);
45
46  // Gets the full program URL for the current sandbox ISA from the
47  // manifest file.
48  bool GetProgramURL(std::string* full_url,
49                     PP_PNaClOptions* pnacl_options,
50                     bool* uses_nonsfi_mode,
51                     ErrorInfo* error_info) const;
52
53  // Resolves a key from the "files" section to a fully resolved URL,
54  // i.e., relative URL values are fully expanded relative to the
55  // manifest's URL (via ResolveURL).
56  // If there was an error, details are reported via error_info.
57  bool ResolveKey(const std::string& key,
58                  std::string* full_url,
59                  PP_PNaClOptions* pnacl_options) const;
60
61 private:
62  bool MatchesSchema(ErrorInfo* error_info);
63  bool GetKeyUrl(const Json::Value& dictionary,
64                 const std::string& key,
65                 std::string* full_url,
66                 PP_PNaClOptions* pnacl_options) const;
67  bool GetURLFromISADictionary(const Json::Value& dictionary,
68                               const std::string& parent_key,
69                               std::string* url,
70                               PP_PNaClOptions* pnacl_options,
71                               bool* uses_nonsfi_mode,
72                               ErrorInfo* error_info) const;
73
74  std::string manifest_base_url_;
75  std::string sandbox_isa_;
76  bool nonsfi_enabled_;
77  bool pnacl_debug_;
78
79  // The dictionary of manifest information parsed in Init().
80  Json::Value dictionary_;
81};
82
83}  // namespace nacl
84
85#endif  // COMPONENTS_NACL_RENDERER_JSON_MANIFEST_H
86