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 #include "xwalk/application/common/application_file_util.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/file_util.h"
15 #include "base/i18n/rtl.h"
16 #include "base/json/json_file_value_serializer.h"
17 #include "base/logging.h"
18 #include "base/metrics/histogram.h"
19 #include "base/path_service.h"
20 #include "base/strings/string16.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/threading/thread_restrictions.h"
24 #include "net/base/escape.h"
25 #include "net/base/file_stream.h"
26 #include "third_party/libxml/src/include/libxml/tree.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "xwalk/application/common/installer/package.h"
29 #include "xwalk/application/common/application_data.h"
30 #include "xwalk/application/common/application_manifest_constants.h"
31 #include "xwalk/application/common/constants.h"
32 #include "xwalk/application/common/install_warning.h"
33 #include "xwalk/application/common/manifest.h"
34 #include "xwalk/application/common/manifest_handler.h"
36 namespace errors = xwalk::application_manifest_errors;
37 namespace keys = xwalk::application_manifest_keys;
38 namespace widget_keys = xwalk::application_widget_keys;
41 const char kAttributePrefix[] = "@";
42 const char kNamespaceKey[] = "@namespace";
43 const char kTextKey[] = "#text";
45 const xmlChar kWidgetNodeKey[] = "widget";
46 const xmlChar kNameNodeKey[] = "name";
47 const xmlChar kDescriptionNodeKey[] = "description";
48 const xmlChar kAuthorNodeKey[] = "author";
49 const xmlChar kLicenseNodeKey[] = "license";
50 const xmlChar kVersionAttributeKey[] = "version";
51 const xmlChar kShortAttributeKey[] = "short";
52 const xmlChar kDirAttributeKey[] = "dir";
54 const char kDirLTRKey[] = "ltr";
55 const char kDirRTLKey[] = "rtl";
56 const char kDirLROKey[] = "lro";
57 const char kDirRLOKey[] = "rlo";
59 const char* kSingletonElements[] = {
61 "content-security-policy-report-only",
62 "content-security-policy"
65 inline char* ToCharPointer(void* ptr) {
66 return reinterpret_cast<char *>(ptr);
69 inline const char* ToConstCharPointer(const void* ptr) {
70 return reinterpret_cast<const char*>(ptr);
73 base::string16 ToSting16(const xmlChar* string_ptr) {
74 return base::UTF8ToUTF16(std::string(ToConstCharPointer(string_ptr)));
77 base::string16 GetDirText(const base::string16& text, const std::string& dir) {
78 if (dir == kDirLTRKey)
79 return base::i18n::kLeftToRightEmbeddingMark
81 + base::i18n::kPopDirectionalFormatting;
83 if (dir == kDirRTLKey)
84 return base::i18n::kRightToLeftEmbeddingMark
86 + base::i18n::kPopDirectionalFormatting;
88 if (dir == kDirLROKey)
89 return base::i18n::kLeftToRightOverride
91 + base::i18n::kPopDirectionalFormatting;
93 if (dir == kDirRLOKey)
94 return base::i18n::kRightToLeftOverride
96 + base::i18n::kPopDirectionalFormatting;
101 std::string GetNodeDir(xmlNode* node, const std::string& inherit_dir) {
103 std::string dir(inherit_dir);
105 xmlAttr* prop = NULL;
106 for (prop = node->properties; prop; prop = prop->next) {
107 if (xmlStrEqual(prop->name, kDirAttributeKey)) {
108 char* prop_value = ToCharPointer(xmlNodeListGetString(
109 node->doc, prop->children, 1));
119 base::string16 GetNodeText(xmlNode* root, const std::string& inherit_dir) {
121 if (root->type != XML_ELEMENT_NODE)
122 return base::string16();
124 std::string current_dir(GetNodeDir(root, inherit_dir));
126 for (xmlNode* node = root->children; node; node = node->next) {
127 if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE) {
128 text = text + base::i18n::StripWrappingBidiControlCharacters(
129 ToSting16(node->content));
131 text = text + GetNodeText(node, current_dir);
134 return GetDirText(text, current_dir);
137 // According to widget specification, this two prop need to support dir.
138 // see detail on http://www.w3.org/TR/widgets/#the-dir-attribute
139 inline bool IsPropSupportDir(xmlNode* root, xmlAttr* prop) {
140 if (xmlStrEqual(root->name, kWidgetNodeKey)
141 && xmlStrEqual(prop->name, kVersionAttributeKey))
143 if (xmlStrEqual(root->name, kNameNodeKey)
144 && xmlStrEqual(prop->name, kShortAttributeKey))
149 // Only this four items need to support span and ignore other element.
150 // Besides xmlNodeListGetString can not support dir prop of span.
151 // See http://www.w3.org/TR/widgets/#the-span-element-and-its-attributes
152 inline bool IsElementSupportSpanAndDir(xmlNode* root) {
153 if (xmlStrEqual(root->name, kNameNodeKey)
154 || xmlStrEqual(root->name, kDescriptionNodeKey)
155 || xmlStrEqual(root->name, kAuthorNodeKey)
156 || xmlStrEqual(root->name, kLicenseNodeKey))
161 bool GetPackageType(const base::FilePath& path,
162 xwalk::application::Package::Type* package_type,
163 std::string* error) {
164 base::FilePath manifest_path;
166 manifest_path = path.Append(xwalk::application::kManifestXpkFilename);
167 if (base::PathExists(manifest_path)) {
168 *package_type = xwalk::application::Package::XPK;
172 manifest_path = path.Append(xwalk::application::kManifestWgtFilename);
173 if (base::PathExists(manifest_path)) {
174 *package_type = xwalk::application::Package::WGT;
178 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
182 bool IsSingletonElement(const std::string& name) {
183 for (int i = 0; i < arraysize(kSingletonElements); ++i)
184 if (kSingletonElements[i] == name)
192 namespace application {
194 // Load XML node into Dictionary structure.
195 // The keys for the XML node to Dictionary mapping are described below:
197 // <e></e> "e":{"#text": ""}
198 // <e>textA</e> "e":{"#text":"textA"}
199 // <e attr="val">textA</e> "e":{ "@attr":"val", "#text": "textA"}
200 // <e> <a>textA</a> <b>textB</b> </e> "e":{
201 // "a":{"#text":"textA"}
202 // "b":{"#text":"textB"}
204 // <e> <a>textX</a> <a>textY</a> </e> "e":{
205 // "a":[ {"#text":"textX"},
206 // {"#text":"textY"}]
208 // <e> textX <a>textY</a> </e> "e":{ "#text":"textX",
209 // "a":{"#text":"textY"}
212 // For elements that are specified under a namespace, the dictionary
213 // will add '@namespace' key for them, e.g.,
215 // <e xmln="linkA" xmlns:N="LinkB">
216 // <sub-e1> text1 </sub-e>
217 // <N:sub-e2 text2 />
219 // will be saved in Dictionary as,
222 // "@namespace": "linkA"
225 // "@namespace": "linkA"
229 // "@namespace": "linkB"
232 base::DictionaryValue* LoadXMLNode(
233 xmlNode* root, const std::string& inherit_dir = "") {
234 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
235 if (root->type != XML_ELEMENT_NODE)
238 std::string current_dir(GetNodeDir(root, inherit_dir));
240 xmlAttr* prop = NULL;
241 for (prop = root->properties; prop; prop = prop->next) {
242 xmlChar* value_ptr = xmlNodeListGetString(root->doc, prop->children, 1);
243 base::string16 prop_value(ToSting16(value_ptr));
246 if (IsPropSupportDir(root, prop))
247 prop_value = GetDirText(prop_value, current_dir);
250 std::string(kAttributePrefix) + ToConstCharPointer(prop->name),
255 value->SetString(kNamespaceKey, ToConstCharPointer(root->ns->href));
257 for (xmlNode* node = root->children; node; node = node->next) {
258 std::string sub_node_name(ToConstCharPointer(node->name));
259 base::DictionaryValue* sub_value = LoadXMLNode(node, current_dir);
263 if (!value->HasKey(sub_node_name)) {
264 value->Set(sub_node_name, sub_value);
266 } else if (IsSingletonElement(sub_node_name)) {
271 value->Get(sub_node_name, &temp);
274 if (temp->IsType(base::Value::TYPE_LIST)) {
275 base::ListValue* list;
276 temp->GetAsList(&list);
277 list->Append(sub_value);
279 DCHECK(temp->IsType(base::Value::TYPE_DICTIONARY));
280 base::DictionaryValue* dict;
281 temp->GetAsDictionary(&dict);
282 base::DictionaryValue* prev_value(new base::DictionaryValue());
283 prev_value = dict->DeepCopy();
285 base::ListValue* list = new base::ListValue();
286 list->Append(prev_value);
287 list->Append(sub_value);
288 value->Set(sub_node_name, list);
293 if (IsElementSupportSpanAndDir(root)) {
294 text = GetNodeText(root, current_dir);
296 xmlChar* text_ptr = xmlNodeListGetString(root->doc, root->children, 1);
298 text = ToSting16(text_ptr);
304 value->SetString(kTextKey, text);
306 return value.release();
309 scoped_refptr<ApplicationData> LoadApplication(
310 const base::FilePath& application_path,
311 Manifest::SourceType source_type,
312 std::string* error) {
313 Package::Type package_type;
314 if (!GetPackageType(application_path, &package_type, error))
317 return LoadApplication(application_path, std::string(),
318 source_type, package_type, error);
321 scoped_refptr<ApplicationData> LoadApplication(
322 const base::FilePath& application_path,
323 const std::string& application_id,
324 Manifest::SourceType source_type,
325 Package::Type package_type,
326 std::string* error) {
327 scoped_ptr<base::DictionaryValue> manifest(
328 LoadManifest(application_path, package_type, error));
332 scoped_refptr<ApplicationData> application = ApplicationData::Create(
338 if (!application.get())
341 std::vector<InstallWarning> warnings;
342 ManifestHandlerRegistry* registry =
343 manifest->HasKey(widget_keys::kWidgetKey)
344 ? ManifestHandlerRegistry::GetInstance(Package::WGT)
345 : ManifestHandlerRegistry::GetInstance(Package::XPK);
347 if (!registry->ValidateAppManifest(application, error, &warnings))
350 if (!warnings.empty()) {
351 LOG(WARNING) << "There are some warnings when validating the application "
352 << application->ID();
358 static base::DictionaryValue* LoadManifestXpk(
359 const base::FilePath& manifest_path,
360 std::string* error) {
361 JSONFileValueSerializer serializer(manifest_path);
362 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, error));
364 if (error->empty()) {
365 // If |error| is empty, than the file could not be read.
366 // It would be cleaner to have the JSON reader give a specific error
367 // in this case, but other code tests for a file error with
368 // error->empty(). For now, be consistent.
369 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
371 *error = base::StringPrintf("%s %s",
372 errors::kManifestParseError,
378 if (!root->IsType(base::Value::TYPE_DICTIONARY)) {
379 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
383 base::DictionaryValue* dv =
384 static_cast<base::DictionaryValue*>(root.release());
385 #if defined(OS_TIZEN)
386 // Ignore any Tizen application ID, as this is automatically generated.
387 dv->Remove(keys::kTizenAppIdKey, NULL);
393 static base::DictionaryValue* LoadManifestWgt(
394 const base::FilePath& manifest_path,
395 std::string* error) {
397 xmlNode* root_node = NULL;
398 doc = xmlReadFile(manifest_path.MaybeAsASCII().c_str(), NULL, 0);
400 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
403 root_node = xmlDocGetRootElement(doc);
404 base::DictionaryValue* dv = LoadXMLNode(root_node);
405 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
407 result->Set(ToConstCharPointer(root_node->name), dv);
409 return result.release();
412 base::DictionaryValue* LoadManifest(const base::FilePath& application_path,
413 Package::Type package_type,
414 std::string* error) {
415 base::FilePath manifest_path;
417 manifest_path = application_path.Append(kManifestXpkFilename);
418 if (package_type == Package::XPK)
419 return LoadManifestXpk(manifest_path, error);
421 manifest_path = application_path.Append(kManifestWgtFilename);
422 if (package_type == Package::WGT)
423 return LoadManifestWgt(manifest_path, error);
425 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
429 base::FilePath ApplicationURLToRelativeFilePath(const GURL& url) {
430 std::string url_path = url.path();
431 if (url_path.empty() || url_path[0] != '/')
432 return base::FilePath();
434 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8.
435 std::string file_path = net::UnescapeURLComponent(url_path,
436 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
437 size_t skip = file_path.find_first_not_of("/\\");
438 if (skip != file_path.npos)
439 file_path = file_path.substr(skip);
441 base::FilePath path =
442 #if defined(OS_POSIX)
443 base::FilePath(file_path);
444 #elif defined(OS_WIN)
445 base::FilePath(base::UTF8ToWide(file_path));
451 // It's still possible for someone to construct an annoying URL whose path
452 // would still wind up not being considered relative at this point.
453 // For example: app://id/c:////foo.html
454 if (path.IsAbsolute())
455 return base::FilePath();
460 } // namespace application