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