f201ab2e7b6ab61d7709455528e5913ecab4649f
[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 "url/gurl.h"
24 #include "xwalk/application/common/manifest.h"
25 #include "xwalk/application/common/permission_types.h"
26 #include "xwalk/application/common/package/package.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   // Where an application was loaded from.
40   enum SourceType {
41     INTERNAL,         // From internal application registry.
42     LOCAL_DIRECTORY,  // From a persistently stored unpacked application
43     TEMP_DIRECTORY,   // From a temporary folder
44     EXTERNAL_URL      // From an arbitrary URL
45   };
46
47   struct ManifestData;
48
49   struct ApplicationIdCompare {
50     bool operator()(const std::string& s1, const std::string& s2) const {
51       return base::strcasecmp(s1.c_str(), s2.c_str()) < 0;
52     }
53   };
54
55   typedef std::map<const std::string, linked_ptr<ManifestData> >
56       ManifestDataMap;
57   typedef std::map<std::string,
58       scoped_refptr<ApplicationData>, ApplicationIdCompare>
59         ApplicationDataMap;
60   typedef ApplicationDataMap::iterator ApplicationDataMapIterator;
61
62   // A base class for parsed manifest data that APIs want to store on
63   // the application. Related to base::SupportsUserData, but with an immutable
64   // thread-safe interface to match Application.
65   struct ManifestData {
66     virtual ~ManifestData() {}
67   };
68
69   static scoped_refptr<ApplicationData> Create(const base::FilePath& app_path,
70       const std::string& explicit_id, SourceType source_type,
71           scoped_ptr<Manifest> manifest, std::string* error_message);
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   SourceType source_type() const { return source_type_; }
102   Manifest::Type manifest_type() const { return manifest_->type(); }
103   const std::string& ID() const { return application_id_; }
104 #if defined(OS_TIZEN)
105   std::string GetPackageID() const;
106 #endif
107   const base::Version* Version() const { return version_.get(); }
108   const std::string VersionString() const;
109   const std::string& Name() const { return name_; }
110   const std::string& NonLocalizedName() const { return non_localized_name_; }
111   const std::string& Description() const { return description_; }
112
113   const Manifest* GetManifest() const {
114     return manifest_.get();
115   }
116
117   // App-related.
118   bool IsHostedApp() const;
119
120   // Permission related.
121   StoredPermission GetPermission(
122       const std::string& permission_name) const;
123   bool SetPermission(const std::string& permission_name,
124                      StoredPermission perm);
125   void ClearPermissions();
126   PermissionSet GetManifestPermissions() const;
127
128   bool HasCSPDefined() const;
129
130   bool SetApplicationLocale(const std::string& locale, base::string16* error);
131
132  private:
133   friend class base::RefCountedThreadSafe<ApplicationData>;
134   friend class ApplicationStorageImpl;
135
136   ApplicationData(const base::FilePath& path,
137       SourceType source_type, scoped_ptr<Manifest> manifest);
138   virtual ~ApplicationData();
139
140   // Initialize the application from a parsed manifest.
141   bool Init(const std::string& explicit_id, base::string16* error);
142
143   // Chooses the application ID for an application based on a variety of
144   // criteria. The chosen ID will be set in |manifest|.
145   bool LoadID(const std::string& explicit_id, base::string16* error);
146   // The following are helpers for InitFromValue to load various features of the
147   // application from the manifest.
148   bool LoadName(base::string16* error);
149   bool LoadVersion(base::string16* error);
150   bool LoadDescription(base::string16* error);
151
152   // The application's human-readable name. Name is used for display purpose. It
153   // might be wrapped with unicode bidi control characters so that it is
154   // displayed correctly in RTL context.
155   // NOTE: Name is UTF-8 and may contain non-ascii characters.
156   std::string name_;
157
158   // A non-localized version of the application's name. This is useful for
159   // debug output.
160   std::string non_localized_name_;
161
162   // The version of this application's manifest. We increase the manifest
163   // version when making breaking changes to the application system.
164   // Version 1 was the first manifest version (implied by a lack of a
165   // manifest_version attribute in the application's manifest). We initialize
166   // this member variable to 0 to distinguish the "uninitialized" case from
167   // the case when we know the manifest version actually is 1.
168   int manifest_version_;
169
170   // The absolute path to the directory the application is stored in.
171   base::FilePath path_;
172
173   // A persistent, globally unique ID. An application's ID is used in things
174   // like directory structures and URLs, and is expected to not change across
175   // versions.
176   std::string application_id_;
177
178   // The base application url for the application.
179   GURL application_url_;
180
181   // The application's version.
182   scoped_ptr<base::Version> version_;
183
184   // An optional longer description of the application.
185   std::string description_;
186
187   // The manifest from which this application was created.
188   scoped_ptr<Manifest> manifest_;
189
190   // Stored parsed manifest data.
191   ManifestDataMap manifest_data_;
192
193   // Set to true at the end of InitValue when initialization is finished.
194   bool finished_parsing_manifest_;
195
196   // Ensures that any call to GetManifestData() prior to finishing
197   // initialization happens from the same thread (this can happen when certain
198   // parts of the initialization process need information from previous parts).
199   base::ThreadChecker thread_checker_;
200
201   // Application's persistent permissions.
202   StoredPermissionMap permission_map_;
203
204   // The source the application was loaded from.
205   SourceType source_type_;
206
207   DISALLOW_COPY_AND_ASSIGN(ApplicationData);
208 };
209
210 }  // namespace application
211 }  // namespace xwalk
212
213 #endif  // XWALK_APPLICATION_COMMON_APPLICATION_DATA_H_