Upstream version 6.35.131.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 <list>
9 #include <map>
10 #include <string>
11 #include <set>
12 #include <vector>
13
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/values.h"
17 #include "xwalk/application/common/install_warning.h"
18
19 namespace xwalk {
20 namespace application {
21
22 struct InstallWarning;
23
24 // Wraps the DictionaryValue form of application's manifest. Enforces access to
25 // properties of the manifest using ManifestFeatureProvider.
26 class Manifest {
27  public:
28   // Where an application was loaded from.
29   enum SourceType {
30     INVALID_TYPE,
31     INTERNAL,           // Load from internal application registry.
32     COMMAND_LINE,       // Load from an unpacked application from command line.
33     NUM_TYPES
34   };
35
36   enum Type {
37     TYPE_UNKNOWN = 0,
38     TYPE_HOSTED_APP,
39     TYPE_PACKAGED_APP
40   };
41
42   enum PackageType {
43     TYPE_WGT = 0,
44     TYPE_XPK
45   };
46
47
48   Manifest(SourceType source_type, scoped_ptr<base::DictionaryValue> value);
49   virtual ~Manifest();
50
51   const std::string& GetApplicationID() const { return application_id_; }
52   void SetApplicationID(const std::string& id) { application_id_ = id; }
53
54   SourceType GetSourceType() const { return source_type_; }
55
56   // Returns false and |error| will be non-empty if the manifest is malformed.
57   // |warnings| will be populated if there are keys in the manifest that cannot
58   // be specified by the application type.
59   bool ValidateManifest(std::string* error,
60                         std::vector<InstallWarning>* warnings) const;
61
62   // The version of this application's manifest. We increase the manifest
63   // version when making breaking changes to the application system. If the
64   // manifest contains no explicit manifest version, this returns the current
65   // system default.
66   int GetManifestVersion() const;
67
68   // Returns the manifest type.
69   Type GetType() const { return type_; }
70
71   bool IsPackaged() const { return type_ == TYPE_PACKAGED_APP; }
72   bool IsHosted() const { return type_ == TYPE_HOSTED_APP; }
73
74   PackageType GetPackageType() const { return package_type_; }
75   bool IsXPKPackaged() const { return package_type_ == TYPE_XPK; }
76   bool IsWGTPackaged() const { return package_type_ == TYPE_WGT; }
77
78   // These access the wrapped manifest value, returning false when the property
79   // does not exist or if the manifest type can't access it.
80   bool HasKey(const std::string& key) const;
81   bool HasPath(const std::string& path) const;
82   bool Get(const std::string& path, const base::Value** out_value) const;
83   bool Get(const std::string& path, base::Value** out_value) const;
84   bool GetBoolean(const std::string& path, bool* out_value) const;
85   bool GetInteger(const std::string& path, int* out_value) const;
86
87   // If the path is supported by i18n, we can get a locale string from
88   // this two GetString function. The following is locale priority:
89   // Application locale (locale get from system).                 | high
90   // Default locale (defaultlocale attribute of widget element)
91   // Unlocalized (the element without xml:lang attribute)
92   // Auto ("en-us"(tizen is "en-gb") will be considered as a default)
93   // First (the worst case we get the first element)              | low
94   bool GetString(const std::string& path, std::string* out_value) const;
95   bool GetString(const std::string& path, base::string16* out_value) const;
96
97   bool GetDictionary(const std::string& path,
98                      const base::DictionaryValue** out_value) const;
99   bool GetList(const std::string& path,
100                const base::ListValue** out_value) const;
101
102   // Returns a new Manifest equal to this one, passing ownership to
103   // the caller.
104   Manifest* DeepCopy() const;
105
106   // Returns true if this equals the |other| manifest.
107   bool Equals(const Manifest* other) const;
108
109   // Gets the underlying DictionaryValue representing the manifest.
110   // Note: only use this when you KNOW you don't need the validation.
111   const base::DictionaryValue* value() const { return data_.get(); }
112
113   const std::string& default_locale() const {
114     return default_locale_;
115   }
116
117   // Update user agent locale when system locale is changed.
118   void SetSystemLocale(const std::string& locale);
119
120  private:
121   void ParseWGTI18n();
122   void ParseWGTI18nEachPath(const std::string& path);
123   bool ParseWGTI18nEachElement(base::Value* value,
124                                const std::string& path,
125                                const std::string& locale = "");
126
127   // Returns true if the application can specify the given |path|.
128   bool CanAccessPath(const std::string& path) const;
129   bool CanAccessKey(const std::string& key) const;
130
131   // A persistent, globally unique ID. An application's ID is used in things
132   // like directory structures and URLs, and is expected to not change across
133   // versions. It is generated as a SHA-256 hash of the application's public
134   // key, or as a hash of the path in the case of unpacked applications.
135   std::string application_id_;
136
137   // The source the application was loaded from.
138   SourceType source_type_;
139
140   // The underlying dictionary representation of the manifest.
141   scoped_ptr<base::DictionaryValue> data_;
142   scoped_ptr<base::DictionaryValue> i18n_data_;
143
144   std::string default_locale_;
145   scoped_ptr<std::list<std::string> > user_agent_locales_;
146
147   Type type_;
148
149   PackageType package_type_;
150
151   DISALLOW_COPY_AND_ASSIGN(Manifest);
152 };
153
154 }  // namespace application
155 }  // namespace xwalk
156
157 #endif  // XWALK_APPLICATION_COMMON_MANIFEST_H_