Upstream version 8.37.186.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / installer / wgt_package.cc
1 // Copyright (c) 2013 Intel Corporation. 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 #include "xwalk/application/common/installer/wgt_package.h"
6
7 #include "base/file_util.h"
8 #include "base/files/scoped_file.h"
9 #include "third_party/libxml/chromium/libxml_utils.h"
10 #include "xwalk/application/common/id_util.h"
11
12 namespace xwalk {
13 namespace application {
14 namespace {
15 #if defined(OS_TIZEN)
16 const char kIdNodeName[] = "application";
17 #else
18 const char kIdNodeName[] = "widget";
19 #endif
20 }
21
22 WGTPackage::~WGTPackage() {
23 }
24
25 WGTPackage::WGTPackage(const base::FilePath& path)
26   : Package(path) {
27   if (!base::PathExists(path))
28     return;
29   type_ = WGT;
30   base::FilePath extracted_path;
31   if (!Extract(&extracted_path))
32     return;
33
34   XmlReader xml;
35   if (!xml.LoadFile(extracted_path.Append(FILE_PATH_LITERAL("config.xml"))
36                     .MaybeAsASCII())) {
37     LOG(ERROR) << "Unable to load WGT package config.xml file.";
38     return;
39   }
40
41   while (!xml.SkipToElement()) {
42     if (!xml.Read()) {
43       LOG(ERROR) << "Unable to read WGT package config.xml file.";
44       return;
45     }
46   }
47
48   std::string value;
49   while (xml.Read()) {
50     std::string node_name = xml.NodeName();
51     if (node_name == kIdNodeName) {
52       xml.NodeAttribute("id", &value);
53       break;
54     }
55   }
56
57   if (!value.empty())
58 #if defined(OS_TIZEN)
59     id_ = value;
60 #else
61     id_ = GenerateId(value);
62 #endif
63
64   is_valid_ = true;
65
66   scoped_ptr<base::ScopedFILE> file(
67         new base::ScopedFILE(base::OpenFile(path, "rb")));
68
69   file_ = file.Pass();
70 }
71
72 }  // namespace application
73 }  // namespace xwalk