b5fa144d4da5e9dd2bdba98f9896daf1487bcf52
[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
75   // These access the wrapped manifest value, returning false when the property
76   // does not exist or if the manifest type can't access it.
77   bool HasKey(const std::string& key) const;
78   bool HasPath(const std::string& path) const;
79   bool Get(const std::string& path, const base::Value** out_value) const;
80   bool GetBoolean(const std::string& path, bool* out_value) const;
81   bool GetInteger(const std::string& path, int* out_value) const;
82   bool GetString(const std::string& path, std::string* out_value) const;
83   bool GetString(const std::string& path, base::string16* out_value) const;
84   bool GetDictionary(const std::string& path,
85                      const base::DictionaryValue** out_value) const;
86   bool GetList(const std::string& path,
87                const base::ListValue** out_value) const;
88
89   // Returns a new Manifest equal to this one, passing ownership to
90   // the caller.
91   Manifest* DeepCopy() const;
92
93   // Returns true if this equals the |other| manifest.
94   bool Equals(const Manifest* other) const;
95
96   // Gets the underlying DictionaryValue representing the manifest.
97   // Note: only use this when you KNOW you don't need the validation.
98   const base::DictionaryValue* value() const { return data_.get(); }
99
100  private:
101   // Returns true if the application can specify the given |path|.
102   bool CanAccessPath(const std::string& path) const;
103   bool CanAccessKey(const std::string& key) const;
104
105   // A persistent, globally unique ID. An application's ID is used in things
106   // like directory structures and URLs, and is expected to not change across
107   // versions. It is generated as a SHA-256 hash of the application's public
108   // key, or as a hash of the path in the case of unpacked applications.
109   std::string application_id_;
110
111   // The source the application was loaded from.
112   SourceType source_type_;
113
114   // The underlying dictionary representation of the manifest.
115   scoped_ptr<base::DictionaryValue> data_;
116
117   Type type_;
118
119   PackageType package_type_;
120
121   DISALLOW_COPY_AND_ASSIGN(Manifest);
122 };
123
124 }  // namespace application
125 }  // namespace xwalk
126
127 #endif  // XWALK_APPLICATION_COMMON_MANIFEST_H_