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