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