ed6fc6216c8370d37f52267d100d07d4e2593981
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / application_data.h
1 // Copyright (c) 2013 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_APPLICATION_DATA_H_
6 #define XWALK_APPLICATION_COMMON_APPLICATION_DATA_H_
7
8 #include <algorithm>
9 #include <iosfwd>
10 #include <map>
11 #include <set>
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 #include "base/files/file_path.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/strings/string_util.h"
21 #include "base/synchronization/lock.h"
22 #include "base/threading/thread_checker.h"
23 #include "base/time/time.h"
24 #include "url/gurl.h"
25 #include "xwalk/application/common/install_warning.h"
26 #include "xwalk/application/common/manifest.h"
27 #include "xwalk/application/common/permission_types.h"
28
29 namespace base {
30 class DictionaryValue;
31 class ListValue;
32 class Version;
33 }
34
35 namespace xwalk {
36 namespace application {
37
38 class ApplicationData : public base::RefCountedThreadSafe<ApplicationData> {
39  public:
40   struct ManifestData;
41
42   struct ApplicationIdCompare {
43     bool operator()(const std::string& s1, const std::string& s2) const {
44       return base::strcasecmp(s1.c_str(), s2.c_str()) < 0;
45     }
46   };
47
48   typedef std::map<const std::string, linked_ptr<ManifestData> >
49       ManifestDataMap;
50   typedef std::map<std::string,
51       scoped_refptr<ApplicationData>, ApplicationIdCompare>
52         ApplicationDataMap;
53   typedef ApplicationDataMap::iterator ApplicationDataMapIterator;
54
55   // A base class for parsed manifest data that APIs want to store on
56   // the application. Related to base::SupportsUserData, but with an immutable
57   // thread-safe interface to match Application.
58   struct ManifestData {
59     virtual ~ManifestData() {}
60   };
61
62   static scoped_refptr<ApplicationData> Create(const base::FilePath& path,
63       Manifest::SourceType source_type,
64       const base::DictionaryValue& manifest_data,
65       const std::string& explicit_id,
66       std::string* error_message);
67
68   // Checks to see if the application has a valid ID.
69   static bool IsIDValid(const std::string& id);
70
71   Manifest::Type GetType() const;
72
73   // Returns an absolute url to a resource inside of an application. The
74   // |application_url| argument should be the url() from an Application object.
75   // The |relative_path| can be untrusted user input. The returned URL will
76   // either be invalid() or a child of |application_url|.
77   // NOTE: Static so that it can be used from multiple threads.
78   static GURL GetResourceURL(const GURL& application_url,
79                              const std::string& relative_path);
80   GURL GetResourceURL(const std::string& relative_path) const {
81     return GetResourceURL(URL(), relative_path);
82   }
83
84   // Returns the base application url for a given |application_id|.
85   static GURL GetBaseURLFromApplicationId(const std::string& application_id);
86
87   // Get the manifest data associated with the key, or NULL if there is none.
88   // Can only be called after InitValue is finished.
89   ManifestData* GetManifestData(const std::string& key) const;
90
91   // Sets |data| to be associated with the key. Takes ownership of |data|.
92   // Can only be called before InitValue is finished. Not thread-safe;
93   // all SetManifestData calls should be on only one thread.
94   void SetManifestData(const std::string& key, ManifestData* data);
95
96   // Accessors:
97
98   const base::FilePath& Path() const { return path_; }
99   void SetPath(const base::FilePath& path) { path_ = path; }
100   const GURL& URL() const { return application_url_; }
101   Manifest::SourceType GetSourceType() const;
102   const std::string& ID() const;
103   const base::Version* Version() const { return version_.get(); }
104   const std::string VersionString() const;
105   const std::string& Name() const { return name_; }
106   const std::string& NonLocalizedName() const { return non_localized_name_; }
107   const std::string& Description() const { return description_; }
108   int ManifestVersion() const { return manifest_version_; }
109
110   const Manifest* GetManifest() const {
111     return manifest_.get();
112   }
113
114   bool IsDirty() const { return is_dirty_; }
115
116   const base::Time& install_time() const { return install_time_; }
117
118   // App-related.
119   bool IsPlatformApp() const;
120   bool IsHostedApp() const;
121
122   // Permission related.
123   StoredPermission GetPermission(
124       const std::string& permission_name) const;
125   bool SetPermission(const std::string& permission_name,
126                      StoredPermission perm);
127   void ClearPermissions();
128   PermissionSet GetManifestPermissions() const;
129
130   Package::Type GetPackageType() const { return package_type_; }
131
132 #if defined(OS_TIZEN)
133   bool HasCSPDefined() const;
134 #endif
135
136   bool SetApplicationLocale(const std::string& locale, base::string16* error);
137
138  private:
139   friend class base::RefCountedThreadSafe<ApplicationData>;
140   friend class ApplicationStorageImpl;
141
142   // Chooses the application ID for an application based on a variety of
143   // criteria. The chosen ID will be set in |manifest|.
144   static bool InitApplicationID(Manifest* manifest,
145                               const base::FilePath& path,
146                               const std::string& explicit_id,
147                               base::string16* error);
148
149   ApplicationData(const base::FilePath& path,
150             scoped_ptr<Manifest> manifest);
151   virtual ~ApplicationData();
152
153   // Initialize the application from a parsed manifest.
154   bool Init(base::string16* error);
155
156   // The following are helpers for InitFromValue to load various features of the
157   // application from the manifest.
158   bool LoadName(base::string16* error);
159   bool LoadVersion(base::string16* error);
160   bool LoadDescription(base::string16* error);
161   bool LoadManifestVersion(base::string16* error);
162
163   // The application's human-readable name. Name is used for display purpose. It
164   // might be wrapped with unicode bidi control characters so that it is
165   // displayed correctly in RTL context.
166   // NOTE: Name is UTF-8 and may contain non-ascii characters.
167   std::string name_;
168
169   // A non-localized version of the application's name. This is useful for
170   // debug output.
171   std::string non_localized_name_;
172
173   // The version of this application's manifest. We increase the manifest
174   // version when making breaking changes to the application system.
175   // Version 1 was the first manifest version (implied by a lack of a
176   // manifest_version attribute in the application's manifest). We initialize
177   // this member variable to 0 to distinguish the "uninitialized" case from
178   // the case when we know the manifest version actually is 1.
179   int manifest_version_;
180
181   // The absolute path to the directory the application is stored in.
182   base::FilePath path_;
183
184   // Any warnings that occurred when trying to create/parse the application.
185   std::vector<InstallWarning> install_warnings_;
186
187   // System events
188   std::set<std::string> events_;
189
190   // If it's true, means the data have been changed,
191   // and need to save in database.
192   bool is_dirty_;
193
194   // The base application url for the application.
195   GURL application_url_;
196
197   // The application's version.
198   scoped_ptr<base::Version> version_;
199
200   // An optional longer description of the application.
201   std::string description_;
202
203   // The manifest from which this application was created.
204   scoped_ptr<Manifest> manifest_;
205
206   // Stored parsed manifest data.
207   ManifestDataMap manifest_data_;
208
209   // Set to true at the end of InitValue when initialization is finished.
210   bool finished_parsing_manifest_;
211
212   base::Time install_time_;
213
214   // Ensures that any call to GetManifestData() prior to finishing
215   // initialization happens from the same thread (this can happen when certain
216   // parts of the initialization process need information from previous parts).
217   base::ThreadChecker thread_checker_;
218
219   // Application's persistent permissions.
220   StoredPermissionMap permission_map_;
221
222   // The package type, wgt or xpk.
223   Package::Type package_type_;
224
225   DISALLOW_COPY_AND_ASSIGN(ApplicationData);
226 };
227
228 }  // namespace application
229 }  // namespace xwalk
230
231 #endif  // XWALK_APPLICATION_COMMON_APPLICATION_DATA_H_