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/install_warning.h"
32 #include "xwalk/application/common/manifest.h"
33 #include "xwalk/application/common/manifest_handler.h"
35 namespace errors = xwalk::application_manifest_errors;
36 namespace keys = xwalk::application_manifest_keys;
37 namespace widget_keys = xwalk::application_widget_keys;
40 const char kAttributePrefix[] = "@";
41 const char kNamespaceKey[] = "@namespace";
42 const char kTextKey[] = "#text";
44 const xmlChar kWidgetNodeKey[] = "widget";
45 const xmlChar kNameNodeKey[] = "name";
46 const xmlChar kDescriptionNodeKey[] = "description";
47 const xmlChar kAuthorNodeKey[] = "author";
48 const xmlChar kLicenseNodeKey[] = "license";
49 const xmlChar kVersionAttributeKey[] = "version";
50 const xmlChar kShortAttributeKey[] = "short";
51 const xmlChar kDirAttributeKey[] = "dir";
53 const char kDirLTRKey[] = "ltr";
54 const char kDirRTLKey[] = "rtl";
55 const char kDirLROKey[] = "lro";
56 const char kDirRLOKey[] = "rlo";
58 inline char* ToCharPointer(void* ptr) {
59 return reinterpret_cast<char *>(ptr);
62 inline const char* ToConstCharPointer(const void* ptr) {
63 return reinterpret_cast<const char*>(ptr);
66 base::string16 ToSting16(const xmlChar* string_ptr) {
67 return base::UTF8ToUTF16(std::string(ToConstCharPointer(string_ptr)));
70 base::string16 GetDirText(const base::string16& text, const std::string& dir) {
71 if (dir == kDirLTRKey)
72 return base::i18n::kLeftToRightEmbeddingMark
74 + base::i18n::kPopDirectionalFormatting;
76 if (dir == kDirRTLKey)
77 return base::i18n::kRightToLeftEmbeddingMark
79 + base::i18n::kPopDirectionalFormatting;
81 if (dir == kDirLROKey)
82 return base::i18n::kLeftToRightOverride
84 + base::i18n::kPopDirectionalFormatting;
86 if (dir == kDirRLOKey)
87 return base::i18n::kRightToLeftOverride
89 + base::i18n::kPopDirectionalFormatting;
94 std::string GetNodeDir(xmlNode* node, const std::string& inherit_dir) {
96 std::string dir(inherit_dir);
99 for (prop = node->properties; prop; prop = prop->next) {
100 if (xmlStrEqual(prop->name, kDirAttributeKey)) {
101 char* prop_value = ToCharPointer(xmlNodeListGetString(
102 node->doc, prop->children, 1));
112 base::string16 GetNodeText(xmlNode* root, const std::string& inherit_dir) {
114 if (root->type != XML_ELEMENT_NODE)
115 return base::string16();
117 std::string current_dir(GetNodeDir(root, inherit_dir));
119 for (xmlNode* node = root->children; node; node = node->next) {
120 if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE) {
121 text = text + base::i18n::StripWrappingBidiControlCharacters(
122 ToSting16(node->content));
124 text = text + GetNodeText(node, current_dir);
127 return GetDirText(text, current_dir);
130 // According to widget specification, this two prop need to support dir.
131 // see detail on http://www.w3.org/TR/widgets/#the-dir-attribute
132 inline bool IsPropSupportDir(xmlNode* root, xmlAttr* prop) {
133 if (xmlStrEqual(root->name, kWidgetNodeKey)
134 && xmlStrEqual(prop->name, kVersionAttributeKey))
136 if (xmlStrEqual(root->name, kNameNodeKey)
137 && xmlStrEqual(prop->name, kShortAttributeKey))
142 // Only this four items need to support span and ignore other element.
143 // Besides xmlNodeListGetString can not support dir prop of span.
144 // See http://www.w3.org/TR/widgets/#the-span-element-and-its-attributes
145 inline bool IsElementSupportSpanAndDir(xmlNode* root) {
146 if (xmlStrEqual(root->name, kNameNodeKey)
147 || xmlStrEqual(root->name, kDescriptionNodeKey)
148 || xmlStrEqual(root->name, kAuthorNodeKey)
149 || xmlStrEqual(root->name, kLicenseNodeKey))
157 namespace application {
159 // Load XML node into Dictionary structure.
160 // The keys for the XML node to Dictionary mapping are described below:
162 // <e></e> "e":{"#text": ""}
163 // <e>textA</e> "e":{"#text":"textA"}
164 // <e attr="val">textA</e> "e":{ "@attr":"val", "#text": "textA"}
165 // <e> <a>textA</a> <b>textB</b> </e> "e":{
166 // "a":{"#text":"textA"}
167 // "b":{"#text":"textB"}
169 // <e> <a>textX</a> <a>textY</a> </e> "e":{
170 // "a":[ {"#text":"textX"},
171 // {"#text":"textY"}]
173 // <e> textX <a>textY</a> </e> "e":{ "#text":"textX",
174 // "a":{"#text":"textY"}
177 // For elements that are specified under a namespace, the dictionary
178 // will add '@namespace' key for them, e.g.,
180 // <e xmln="linkA" xmlns:N="LinkB">
181 // <sub-e1> text1 </sub-e>
182 // <N:sub-e2 text2 />
184 // will be saved in Dictionary as,
187 // "@namespace": "linkA"
190 // "@namespace": "linkA"
194 // "@namespace": "linkB"
197 base::DictionaryValue* LoadXMLNode(
198 xmlNode* root, const std::string& inherit_dir = "") {
199 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
200 if (root->type != XML_ELEMENT_NODE)
203 std::string current_dir(GetNodeDir(root, inherit_dir));
205 xmlAttr* prop = NULL;
206 for (prop = root->properties; prop; prop = prop->next) {
207 xmlChar* value_ptr = xmlNodeListGetString(root->doc, prop->children, 1);
208 base::string16 prop_value(ToSting16(value_ptr));
211 if (IsPropSupportDir(root, prop))
212 prop_value = GetDirText(prop_value, current_dir);
215 std::string(kAttributePrefix) + ToConstCharPointer(prop->name),
220 value->SetString(kNamespaceKey, ToConstCharPointer(root->ns->href));
222 for (xmlNode* node = root->children; node; node = node->next) {
223 std::string sub_node_name(ToConstCharPointer(node->name));
224 base::DictionaryValue* sub_value = LoadXMLNode(node, current_dir);
228 if (!value->HasKey(sub_node_name)) {
229 value->Set(sub_node_name, sub_value);
234 value->Get(sub_node_name, &temp);
237 if (temp->IsType(base::Value::TYPE_LIST)) {
238 base::ListValue* list;
239 temp->GetAsList(&list);
240 list->Append(sub_value);
242 DCHECK(temp->IsType(base::Value::TYPE_DICTIONARY));
243 base::DictionaryValue* dict;
244 temp->GetAsDictionary(&dict);
245 base::DictionaryValue* prev_value(new base::DictionaryValue());
246 prev_value = dict->DeepCopy();
248 base::ListValue* list = new base::ListValue();
249 list->Append(prev_value);
250 list->Append(sub_value);
251 value->Set(sub_node_name, list);
256 if (IsElementSupportSpanAndDir(root)) {
257 text = GetNodeText(root, current_dir);
259 xmlChar* text_ptr = xmlNodeListGetString(root->doc, root->children, 1);
261 text = ToSting16(text_ptr);
267 value->SetString(kTextKey, text);
269 return value.release();
272 scoped_refptr<ApplicationData> LoadApplication(
273 const base::FilePath& application_path,
274 Manifest::SourceType source_type,
275 std::string* error) {
276 return LoadApplication(application_path, std::string(),
280 scoped_refptr<ApplicationData> LoadApplication(
281 const base::FilePath& application_path,
282 const std::string& application_id,
283 Manifest::SourceType source_type,
284 std::string* error) {
285 scoped_ptr<base::DictionaryValue> manifest(
286 LoadManifest(application_path, error));
290 scoped_refptr<ApplicationData> application = ApplicationData::Create(
296 if (!application.get())
299 std::vector<InstallWarning> warnings;
300 ManifestHandlerRegistry* registry =
301 manifest->HasKey(widget_keys::kWidgetKey)
302 ? ManifestHandlerRegistry::GetInstance(Manifest::TYPE_WGT)
303 : ManifestHandlerRegistry::GetInstance(Manifest::TYPE_XPK);
305 if (!registry->ValidateAppManifest(application, error, &warnings))
308 if (!warnings.empty()) {
309 LOG(WARNING) << "There are some warnings when validating the application "
310 << application->ID();
316 static base::DictionaryValue* LoadManifestXpk(
317 const base::FilePath& manifest_path,
318 std::string* error) {
319 JSONFileValueSerializer serializer(manifest_path);
320 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, error));
322 if (error->empty()) {
323 // If |error| is empty, than the file could not be read.
324 // It would be cleaner to have the JSON reader give a specific error
325 // in this case, but other code tests for a file error with
326 // error->empty(). For now, be consistent.
327 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
329 *error = base::StringPrintf("%s %s",
330 errors::kManifestParseError,
336 if (!root->IsType(base::Value::TYPE_DICTIONARY)) {
337 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
341 base::DictionaryValue* dv =
342 static_cast<base::DictionaryValue*>(root.release());
343 #if defined(OS_TIZEN)
344 // Ignore any Tizen application ID, as this is automatically generated.
345 dv->Remove(keys::kTizenAppIdKey, NULL);
351 static base::DictionaryValue* LoadManifestWgt(
352 const base::FilePath& manifest_path,
353 std::string* error) {
355 xmlNode* root_node = NULL;
356 doc = xmlReadFile(manifest_path.MaybeAsASCII().c_str(), NULL, 0);
358 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
361 root_node = xmlDocGetRootElement(doc);
362 base::DictionaryValue* dv = LoadXMLNode(root_node);
363 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
365 result->Set(ToConstCharPointer(root_node->name), dv);
367 return result.release();
370 base::DictionaryValue* LoadManifest(const base::FilePath& application_path,
371 std::string* error) {
372 base::FilePath manifest_path;
374 manifest_path = application_path.Append(kManifestXpkFilename);
375 if (base::PathExists(manifest_path))
376 return LoadManifestXpk(manifest_path, error);
378 manifest_path = application_path.Append(kManifestWgtFilename);
379 if (base::PathExists(manifest_path))
380 return LoadManifestWgt(manifest_path, error);
382 *error = base::StringPrintf("%s", errors::kManifestUnreadable);
386 base::FilePath ApplicationURLToRelativeFilePath(const GURL& url) {
387 std::string url_path = url.path();
388 if (url_path.empty() || url_path[0] != '/')
389 return base::FilePath();
391 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8.
392 std::string file_path = net::UnescapeURLComponent(url_path,
393 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
394 size_t skip = file_path.find_first_not_of("/\\");
395 if (skip != file_path.npos)
396 file_path = file_path.substr(skip);
398 base::FilePath path =
399 #if defined(OS_POSIX)
400 base::FilePath(file_path);
401 #elif defined(OS_WIN)
402 base::FilePath(base::UTF8ToWide(file_path));
408 // It's still possible for someone to construct an annoying URL whose path
409 // would still wind up not being considered relative at this point.
410 // For example: app://id/c:////foo.html
411 if (path.IsAbsolute())
412 return base::FilePath();
417 } // namespace application