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