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