Upstream version 5.34.97.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest.h
1 // Copyright (c) 2012 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 XWALK_APPLICATION_COMMON_MANIFEST_H_
6 #define XWALK_APPLICATION_COMMON_MANIFEST_H_
7
8 #include <map>
9 #include <string>
10 #include <set>
11 #include <vector>
12
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "base/values.h"
16 #include "xwalk/application/common/install_warning.h"
17
18 namespace xwalk {
19 namespace application {
20
21 struct InstallWarning;
22
23 // Wraps the DictionaryValue form of application's manifest. Enforces access to
24 // properties of the manifest using ManifestFeatureProvider.
25 class Manifest {
26  public:
27   // Where an application was loaded from.
28   enum SourceType {
29     INVALID_TYPE,
30     INTERNAL,           // Load from internal application registry.
31     COMMAND_LINE,       // Load from an unpacked application from command line.
32     NUM_TYPES
33   };
34
35   enum Type {
36     TYPE_UNKNOWN = 0,
37     TYPE_HOSTED_APP,
38     TYPE_PACKAGED_APP
39   };
40
41   enum PackageType {
42     TYPE_WGT = 0,
43     TYPE_XPK
44   };
45
46
47   Manifest(SourceType source_type, scoped_ptr<base::DictionaryValue> value);
48   virtual ~Manifest();
49
50   const std::string& GetApplicationID() const { return application_id_; }
51   void SetApplicationID(const std::string& id) { application_id_ = id; }
52
53   SourceType GetSourceType() const { return source_type_; }
54
55   // Returns false and |error| will be non-empty if the manifest is malformed.
56   // |warnings| will be populated if there are keys in the manifest that cannot
57   // be specified by the application type.
58   bool ValidateManifest(std::string* error,
59                         std::vector<InstallWarning>* warnings) const;
60
61   // The version of this application's manifest. We increase the manifest
62   // version when making breaking changes to the application system. If the
63   // manifest contains no explicit manifest version, this returns the current
64   // system default.
65   int GetManifestVersion() const;
66
67   // Returns the manifest type.
68   Type GetType() const { return type_; }
69
70   bool IsPackaged() const { return type_ == TYPE_PACKAGED_APP; }
71   bool IsHosted() const { return type_ == TYPE_HOSTED_APP; }
72
73   PackageType GetPackageType() const { return package_type_; }
74   bool IsXPKPackaged() const { return package_type_ == TYPE_XPK; }
75
76   // These access the wrapped manifest value, returning false when the property
77   // does not exist or if the manifest type can't access it.
78   bool HasKey(const std::string& key) const;
79   bool HasPath(const std::string& path) const;
80   bool Get(const std::string& path, const base::Value** out_value) const;
81   bool GetBoolean(const std::string& path, bool* out_value) const;
82   bool GetInteger(const std::string& path, int* out_value) const;
83   bool GetString(const std::string& path, std::string* out_value) const;
84   bool GetString(const std::string& path, base::string16* out_value) const;
85   bool GetDictionary(const std::string& path,
86                      const base::DictionaryValue** out_value) const;
87   bool GetList(const std::string& path,
88                const base::ListValue** out_value) const;
89
90   // Returns a new Manifest equal to this one, passing ownership to
91   // the caller.
92   Manifest* DeepCopy() const;
93
94   // Returns true if this equals the |other| manifest.
95   bool Equals(const Manifest* other) const;
96
97   // Gets the underlying DictionaryValue representing the manifest.
98   // Note: only use this when you KNOW you don't need the validation.
99   const base::DictionaryValue* value() const { return data_.get(); }
100
101  private:
102   // Returns true if the application can specify the given |path|.
103   bool CanAccessPath(const std::string& path) const;
104   bool CanAccessKey(const std::string& key) const;
105
106   // A persistent, globally unique ID. An application's ID is used in things
107   // like directory structures and URLs, and is expected to not change across
108   // versions. It is generated as a SHA-256 hash of the application's public
109   // key, or as a hash of the path in the case of unpacked applications.
110   std::string application_id_;
111
112   // The source the application was loaded from.
113   SourceType source_type_;
114
115   // The underlying dictionary representation of the manifest.
116   scoped_ptr<base::DictionaryValue> data_;
117
118   Type type_;
119
120   PackageType package_type_;
121
122   DISALLOW_COPY_AND_ASSIGN(Manifest);
123 };
124
125 }  // namespace application
126 }  // namespace xwalk
127
128 #endif  // XWALK_APPLICATION_COMMON_MANIFEST_H_