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/application_data.h"
29 #include "xwalk/application/common/application_manifest_constants.h"
30 #include "xwalk/application/common/constants.h"
31 #include "xwalk/application/common/manifest.h"
32 #include "xwalk/application/common/manifest_handler.h"
35 #include "xwalk/application/common/id_util.h"
38 namespace errors = xwalk::application_manifest_errors;
39 namespace keys = xwalk::application_manifest_keys;
40 namespace widget_keys = xwalk::application_widget_keys;
43 const char kAttributePrefix[] = "@";
44 const char kNamespaceKey[] = "@namespace";
45 const char kTextKey[] = "#text";
47 const char kContentKey[] = "content";
49 const xmlChar kWidgetNodeKey[] = "widget";
50 const xmlChar kNameNodeKey[] = "name";
51 const xmlChar kDescriptionNodeKey[] = "description";
52 const xmlChar kAuthorNodeKey[] = "author";
53 const xmlChar kLicenseNodeKey[] = "license";
54 const xmlChar kVersionAttributeKey[] = "version";
55 const xmlChar kShortAttributeKey[] = "short";
56 const xmlChar kDirAttributeKey[] = "dir";
58 const char kDirLTRKey[] = "ltr";
59 const char kDirRTLKey[] = "rtl";
60 const char kDirLROKey[] = "lro";
61 const char kDirRLOKey[] = "rlo";
63 const char* kSingletonElements[] = {
65 "content-security-policy-report-only",
66 "content-security-policy",
70 inline char* ToCharPointer(void* ptr) {
71 return reinterpret_cast<char *>(ptr);
74 inline const char* ToConstCharPointer(const void* ptr) {
75 return reinterpret_cast<const char*>(ptr);
78 base::string16 ToSting16(const xmlChar* string_ptr) {
79 return base::UTF8ToUTF16(std::string(ToConstCharPointer(string_ptr)));
82 base::string16 GetDirText(const base::string16& text, const std::string& dir) {
83 if (dir == kDirLTRKey)
84 return base::i18n::kLeftToRightEmbeddingMark
86 + base::i18n::kPopDirectionalFormatting;
88 if (dir == kDirRTLKey)
89 return base::i18n::kRightToLeftEmbeddingMark
91 + base::i18n::kPopDirectionalFormatting;
93 if (dir == kDirLROKey)
94 return base::i18n::kLeftToRightOverride
96 + base::i18n::kPopDirectionalFormatting;
98 if (dir == kDirRLOKey)
99 return base::i18n::kRightToLeftOverride
101 + base::i18n::kPopDirectionalFormatting;
106 std::string GetNodeDir(xmlNode* node, const std::string& inherit_dir) {
108 std::string dir(inherit_dir);
110 xmlAttr* prop = NULL;
111 for (prop = node->properties; prop; prop = prop->next) {
112 if (xmlStrEqual(prop->name, kDirAttributeKey)) {
113 char* prop_value = ToCharPointer(xmlNodeListGetString(
114 node->doc, prop->children, 1));
124 base::string16 GetNodeText(xmlNode* root, const std::string& inherit_dir) {
126 if (root->type != XML_ELEMENT_NODE)
127 return base::string16();
129 std::string current_dir(GetNodeDir(root, inherit_dir));
131 for (xmlNode* node = root->children; node; node = node->next) {
132 if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE) {
133 text = text + base::i18n::StripWrappingBidiControlCharacters(
134 ToSting16(node->content));
136 text = text + GetNodeText(node, current_dir);
139 return GetDirText(text, current_dir);
142 // According to widget specification, this two prop need to support dir.
143 // see detail on http://www.w3.org/TR/widgets/#the-dir-attribute
144 inline bool IsPropSupportDir(xmlNode* root, xmlAttr* prop) {
145 if (xmlStrEqual(root->name, kWidgetNodeKey)
146 && xmlStrEqual(prop->name, kVersionAttributeKey))
148 if (xmlStrEqual(root->name, kNameNodeKey)
149 && xmlStrEqual(prop->name, kShortAttributeKey))
154 // Only this four items need to support span and ignore other element.
155 // Besides xmlNodeListGetString can not support dir prop of span.
156 // See http://www.w3.org/TR/widgets/#the-span-element-and-its-attributes
157 inline bool IsElementSupportSpanAndDir(xmlNode* root) {
158 if (xmlStrEqual(root->name, kNameNodeKey)
159 || xmlStrEqual(root->name, kDescriptionNodeKey)
160 || xmlStrEqual(root->name, kAuthorNodeKey)
161 || xmlStrEqual(root->name, kLicenseNodeKey))
166 // FIXME: This function is wrong and has to be re-implemented
167 // further (see XWALK-2230)
168 bool GetPackageType(const base::FilePath& path,
169 xwalk::application::Package::Type* package_type,
170 std::string* error) {
171 base::FilePath manifest_path;
173 manifest_path = path.Append(xwalk::application::kManifestXpkFilename);
174 if (base::PathExists(manifest_path)) {
175 *package_type = xwalk::application::Package::XPK;
179 manifest_path = path.Append(xwalk::application::kManifestWgtFilename);
180 if (base::PathExists(manifest_path)) {
181 *package_type = xwalk::application::Package::WGT;
185 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
189 #if defined(OS_TIZEN)
190 bool GetPackageType(const std::string& application_id,
191 xwalk::application::Package::Type* package_type,
192 std::string* error) {
193 if (xwalk::application::IsValidWGTID(application_id)) {
194 *package_type = xwalk::application::Package::WGT;
196 } else if (xwalk::application::IsValidXPKID(application_id)) {
197 *package_type = xwalk::application::Package::XPK;
201 *error = base::StringPrintf("Invalid application id: %s",
202 application_id.c_str());
207 bool IsSingletonElement(const std::string& name) {
208 for (int i = 0; i < arraysize(kSingletonElements); ++i)
209 if (kSingletonElements[i] == name)
210 #if defined(OS_TIZEN)
211 // On Tizen platform, need to check namespace of 'content'
212 // element further, a content element with tizen namespace
213 // will replace the one with widget namespace.
214 return name != kContentKey;
224 namespace application {
226 FileDeleter::FileDeleter(const base::FilePath& path, bool recursive)
228 recursive_(recursive) {}
230 FileDeleter::~FileDeleter() {
231 base::DeleteFile(path_, recursive_);
234 // Load XML node into Dictionary structure.
235 // The keys for the XML node to Dictionary mapping are described below:
237 // <e></e> "e":{"#text": ""}
238 // <e>textA</e> "e":{"#text":"textA"}
239 // <e attr="val">textA</e> "e":{ "@attr":"val", "#text": "textA"}
240 // <e> <a>textA</a> <b>textB</b> </e> "e":{
241 // "a":{"#text":"textA"}
242 // "b":{"#text":"textB"}
244 // <e> <a>textX</a> <a>textY</a> </e> "e":{
245 // "a":[ {"#text":"textX"},
246 // {"#text":"textY"}]
248 // <e> textX <a>textY</a> </e> "e":{ "#text":"textX",
249 // "a":{"#text":"textY"}
252 // For elements that are specified under a namespace, the dictionary
253 // will add '@namespace' key for them, e.g.,
255 // <e xmln="linkA" xmlns:N="LinkB">
256 // <sub-e1> text1 </sub-e>
257 // <N:sub-e2 text2 />
259 // will be saved in Dictionary as,
262 // "@namespace": "linkA"
265 // "@namespace": "linkA"
269 // "@namespace": "linkB"
272 base::DictionaryValue* LoadXMLNode(
273 xmlNode* root, const std::string& inherit_dir = "") {
274 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
275 if (root->type != XML_ELEMENT_NODE)
278 std::string current_dir(GetNodeDir(root, inherit_dir));
280 xmlAttr* prop = NULL;
281 for (prop = root->properties; prop; prop = prop->next) {
282 xmlChar* value_ptr = xmlNodeListGetString(root->doc, prop->children, 1);
283 base::string16 prop_value(ToSting16(value_ptr));
286 if (IsPropSupportDir(root, prop))
287 prop_value = GetDirText(prop_value, current_dir);
290 std::string(kAttributePrefix) + ToConstCharPointer(prop->name),
295 value->SetString(kNamespaceKey, ToConstCharPointer(root->ns->href));
297 for (xmlNode* node = root->children; node; node = node->next) {
298 std::string sub_node_name(ToConstCharPointer(node->name));
299 base::DictionaryValue* sub_value = LoadXMLNode(node, current_dir);
303 if (!value->HasKey(sub_node_name)) {
304 value->Set(sub_node_name, sub_value);
306 } else if (IsSingletonElement(sub_node_name)) {
308 #if defined(OS_TIZEN)
309 } else if (sub_node_name == kContentKey) {
310 std::string current_namespace, new_namespace;
311 base::DictionaryValue* current_value;
312 value->GetDictionary(sub_node_name, ¤t_value);
314 current_value->GetString(kNamespaceKey, ¤t_namespace);
315 sub_value->GetString(kNamespaceKey, &new_namespace);
316 if (current_namespace != new_namespace &&
317 new_namespace == kTizenNamespacePrefix)
318 value->Set(sub_node_name, sub_value);
324 value->Get(sub_node_name, &temp);
327 if (temp->IsType(base::Value::TYPE_LIST)) {
328 base::ListValue* list;
329 temp->GetAsList(&list);
330 list->Append(sub_value);
332 DCHECK(temp->IsType(base::Value::TYPE_DICTIONARY));
333 base::DictionaryValue* dict;
334 temp->GetAsDictionary(&dict);
335 base::DictionaryValue* prev_value(new base::DictionaryValue());
336 prev_value = dict->DeepCopy();
338 base::ListValue* list = new base::ListValue();
339 list->Append(prev_value);
340 list->Append(sub_value);
341 value->Set(sub_node_name, list);
346 if (IsElementSupportSpanAndDir(root)) {
347 text = GetNodeText(root, current_dir);
349 xmlChar* text_ptr = xmlNodeListGetString(root->doc, root->children, 1);
351 text = ToSting16(text_ptr);
357 value->SetString(kTextKey, text);
359 return value.release();
362 scoped_refptr<ApplicationData> LoadApplication(
363 const base::FilePath& application_path,
364 ApplicationData::SourceType source_type,
365 std::string* error) {
366 Package::Type package_type;
367 if (!GetPackageType(application_path, &package_type, error))
370 return LoadApplication(application_path, std::string(),
371 source_type, package_type, error);
374 scoped_refptr<ApplicationData> LoadApplication(
375 const base::FilePath& application_path,
376 const std::string& application_id,
377 ApplicationData::SourceType source_type,
378 std::string* error) {
379 Package::Type package_type;
380 #if defined(OS_TIZEN)
381 if (!GetPackageType(application_id, &package_type, error))
383 if (!GetPackageType(application_path, &package_type, error))
387 return LoadApplication(application_path, application_id,
388 source_type, package_type, error);
391 scoped_refptr<ApplicationData> LoadApplication(
392 const base::FilePath& application_path,
393 const std::string& application_id,
394 ApplicationData::SourceType source_type,
395 Package::Type package_type,
396 std::string* error) {
397 scoped_ptr<base::DictionaryValue> manifest(
398 LoadManifest(application_path, package_type, error));
402 scoped_refptr<ApplicationData> application = ApplicationData::Create(
411 ManifestHandlerRegistry* registry =
412 manifest->HasKey(widget_keys::kWidgetKey)
413 ? ManifestHandlerRegistry::GetInstance(Package::WGT)
414 : ManifestHandlerRegistry::GetInstance(Package::XPK);
416 if (!registry->ValidateAppManifest(application, error))
422 static base::DictionaryValue* LoadManifestXpk(
423 const base::FilePath& manifest_path,
424 std::string* error) {
425 JSONFileValueSerializer serializer(manifest_path);
426 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, error));
428 if (error->empty()) {
429 // If |error| is empty, than the file could not be read.
430 // It would be cleaner to have the JSON reader give a specific error
431 // in this case, but other code tests for a file error with
432 // error->empty(). For now, be consistent.
433 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
435 *error = base::StringPrintf("%s %s",
436 errors::kManifestParseError,
442 if (!root->IsType(base::Value::TYPE_DICTIONARY)) {
443 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
447 base::DictionaryValue* dv =
448 static_cast<base::DictionaryValue*>(root.release());
449 #if defined(OS_TIZEN)
450 // Ignore any Tizen application ID, as this is automatically generated.
451 dv->Remove(keys::kTizenAppIdKey, NULL);
457 static base::DictionaryValue* LoadManifestWgt(
458 const base::FilePath& manifest_path,
459 std::string* error) {
461 xmlNode* root_node = NULL;
462 doc = xmlReadFile(manifest_path.MaybeAsASCII().c_str(), NULL, 0);
464 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
467 root_node = xmlDocGetRootElement(doc);
468 base::DictionaryValue* dv = LoadXMLNode(root_node);
469 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
471 result->Set(ToConstCharPointer(root_node->name), dv);
473 return result.release();
476 base::DictionaryValue* LoadManifest(const base::FilePath& application_path,
477 Package::Type package_type,
478 std::string* error) {
479 base::FilePath manifest_path;
481 manifest_path = application_path.Append(kManifestXpkFilename);
482 if (package_type == Package::XPK)
483 return LoadManifestXpk(manifest_path, error);
485 manifest_path = application_path.Append(kManifestWgtFilename);
486 if (package_type == Package::WGT)
487 return LoadManifestWgt(manifest_path, error);
489 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
493 base::FilePath ApplicationURLToRelativeFilePath(const GURL& url) {
494 std::string url_path = url.path();
495 if (url_path.empty() || url_path[0] != '/')
496 return base::FilePath();
498 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8.
499 std::string file_path = net::UnescapeURLComponent(url_path,
500 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
501 size_t skip = file_path.find_first_not_of("/\\");
502 if (skip != file_path.npos)
503 file_path = file_path.substr(skip);
505 base::FilePath path =
506 #if defined(OS_POSIX)
507 base::FilePath(file_path);
508 #elif defined(OS_WIN)
509 base::FilePath(base::UTF8ToWide(file_path));
515 // It's still possible for someone to construct an annoying URL whose path
516 // would still wind up not being considered relative at this point.
517 // For example: app://id/c:////foo.html
518 if (path.IsAbsolute())
519 return base::FilePath();
524 } // namespace application