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